1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
#ifndef __LOUT_MISC_HH__
#define __LOUT_MISC_HH__
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
namespace lout {
/**
* \brief Miscellaneous stuff, which does not fit anywhere else.
*
* Actually, the other parts, beginning with ::object, depend on this.
*/
namespace misc {
template <class T> inline T min (T a, T b) { return a < b ? a : b; }
template <class T> inline T max (T a, T b) { return a > b ? a : b; }
template <class T> inline T min (T a, T b, T c)
{
return (min (a, min (b, c)));
}
template <class T> inline T max (T a, T b, T c)
{
return (max (a, max (b, c)));
}
extern const char *prgName;
void init (int argc, char *argv[]);
inline void assertNotReached ()
{
fprintf (stderr, "*** [%s] This should not happen! ***\n", prgName);
abort ();
}
inline int roundInt(double d)
{
return (int) ((d > 0) ? (d + 0.5) : (d - 0.5));
}
inline int AsciiTolower(char c)
{
return ((c >= 'A' && c <= 'Z') ? c + 0x20 : c);
}
inline int AsciiToupper(char c)
{
return ((c >= 'a' && c <= 'z') ? c - 0x20 : c);
}
inline int AsciiStrcasecmp(const char *s1, const char *s2)
{
int ret = 0;
while ((*s1 || *s2) && !(ret = AsciiTolower(*s1) - AsciiTolower(*s2))) {
s1++;
s2++;
}
return ret;
}
/**
* \brief Instances of a sub class of this interface may be compared (less,
* greater).
*
* Used for sorting etc.
*/
class Comparable
{
public:
virtual ~Comparable();
/**
* \brief Compare two objects c1 and c2.
*
* return a value < 0, when c1 is less than c2, a value > 0, when c1
* is greater than c2, or 0, when c1 and c2 are equal.
*
* If also object::Object is implemented, and if c1.equals(c2),
* c1.compareTo(c2) must be 0, but, unlike you may expect,
* the reversed is not necessarily true. This method returns 0, if,
* according to the rules for sorting, there is no difference, but there
* may still be differences (not relevant for sorting), which "equals" will
* care about.
*/
virtual int compareTo(Comparable *other) = 0;
static int compareFun(const void *p1, const void *p2);
};
/**
* \brief Simple (simpler than container::untyped::Vector and
* container::typed::Vector) template based vector.
*/
template <class T> class SimpleVector
{
private:
T *array;
int num, numAlloc;
inline void resize ()
{
/* This algorithm was tuned for memory&speed with this huge page:
* http://downloads.mysql.com/docs/refman-6.0-en.html.tar.gz
*/
if (array == NULL) {
this->numAlloc = 1;
this->array = (T*) malloc (sizeof (T));
}
if (this->numAlloc < this->num) {
this->numAlloc = (this->num < 100) ?
this->num : this->num + this->num/10;
this->array =
(T*) realloc(this->array, (this->numAlloc * sizeof (T)));
}
}
public:
inline SimpleVector (int initAlloc)
{
this->num = 0;
this->numAlloc = initAlloc;
this->array = NULL;
}
inline SimpleVector (const SimpleVector &o) {
this->array = NULL;
this->num = o.num;
this->numAlloc = o.numAlloc;
resize ();
memcpy (this->array, o.array, sizeof (T) * num);
}
inline ~SimpleVector ()
{
if (this->array)
free (this->array);
}
/**
* \brief Return the number of elements put into this vector.
*/
inline int size() { return this->num; }
inline T* getArray() { return array; }
/**
* \brief Increase the vector size by one.
*
* May be necessary before calling misc::SimpleVector::set.
*/
inline void increase() { setSize(this->num + 1); }
/**
* \brief Set the size explicitly.
*
* May be necessary before calling misc::SimpleVector::set.
*/
inline void setSize(int newSize) {
assert (newSize >= 0);
this->num = newSize;
this->resize ();
}
/**
* \brief Set the size explicitly and initialize new values.
*
* May be necessary before calling misc::SimpleVector::set.
*/
inline void setSize (int newSize, T t) {
int oldSize = this->num;
setSize (newSize);
for (int i = oldSize; i < newSize; i++)
set (i, t);
}
/**
* \brief Return the reference of one element.
*
* \sa misc::SimpleVector::get
*/
inline T* getRef (int i) {
assert (i >= 0 && this->num - i > 0);
return array + i;
}
/**
* \brief Return the one element, explicitly.
*
* The element is copied, so for complex elements, you should rather used
* misc::SimpleVector::getRef.
*/
inline T get (int i) {
assert (i >= 0 && this->num - i > 0);
return this->array[i];
}
/**
* \brief Return the reference of the first element (convenience method).
*/
inline T* getFirstRef () {
assert (this->num > 0);
return this->array;
}
/**
* \brief Return the first element, explicitly.
*/
inline T getFirst () {
assert (this->num > 0);
return this->array[0];
}
/**
* \brief Return the reference of the last element (convenience method).
*/
inline T* getLastRef () {
assert (this->num > 0);
return this->array + this->num - 1;
}
/**
* \brief Return the last element, explicitly.
*/
inline T getLast () {
assert (this->num > 0);
return this->array[this->num - 1];
}
/**
* \brief Store an object in the vector.
*
* Unlike in container::untyped::Vector and container::typed::Vector,
* you have to care about the size, so a call to
* misc::SimpleVector::increase or misc::SimpleVector::setSize may
* be necessary before.
*/
inline void set (int i, T t) {
assert (i >= 0 && this->num - i > 0);
this->array[i] = t;
}
};
/**
* \brief A class for fast concatenation of a large number of strings.
*/
class StringBuffer
{
private:
struct Node
{
char *data;
Node *next;
};
Node *firstNode, *lastNode;
int numChars;
char *str;
bool strValid;
public:
StringBuffer();
~StringBuffer();
/**
* \brief Append a NUL-terminated string to the buffer, with copying.
*
* A copy is kept in the buffer, so the caller does not have to care
* about memory management.
*/
inline void append(const char *str) { appendNoCopy(strdup(str)); }
void appendNoCopy(char *str);
const char *getChars();
void clear ();
};
/**
* \brief A bit set, which automatically reallocates when needed.
*/
class BitSet
{
private:
unsigned char *bits;
int numBytes;
inline int bytesForBits(int bits) { return bits == 0 ? 1 : (bits + 7) / 8; }
public:
BitSet(int initBits);
~BitSet();
void intoStringBuffer(misc::StringBuffer *sb);
bool get(int i);
void set(int i, bool val);
void clear();
};
/**
* \brief A simple allocator optimized to handle many small chunks of memory.
* The chunks can not be free'd individually. Instead the whole zone must be
* free'd with zoneFree().
*/
class ZoneAllocator
{
private:
size_t poolSize, poolLimit, freeIdx;
SimpleVector <char*> *pools;
SimpleVector <char*> *bulk;
public:
ZoneAllocator (size_t poolSize) {
this->poolSize = poolSize;
this->poolLimit = poolSize / 4;
this->freeIdx = poolSize;
this->pools = new SimpleVector <char*> (1);
this->bulk = new SimpleVector <char*> (1);
};
~ZoneAllocator () {
zoneFree ();
delete pools;
delete bulk;
}
inline void * zoneAlloc (size_t t) {
void *ret;
if (t > poolLimit) {
bulk->increase ();
bulk->set (bulk->size () - 1, (char*) malloc (t));
return bulk->get (bulk->size () - 1);
}
if (t > poolSize - freeIdx) {
pools->increase ();
pools->set (pools->size () - 1, (char*) malloc (poolSize));
freeIdx = 0;
}
ret = pools->get (pools->size () - 1) + freeIdx;
freeIdx += t;
return ret;
}
inline void zoneFree () {
for (int i = 0; i < pools->size (); i++)
free (pools->get (i));
pools->setSize (0);
for (int i = 0; i < bulk->size (); i++)
free (bulk->get (i));
bulk->setSize (0);
freeIdx = poolSize;
}
inline const char *strndup (const char *str, size_t t) {
char *new_str = (char *) zoneAlloc (t + 1);
memcpy (new_str, str, t);
new_str[t] = '\0';
return new_str;
}
inline const char *strdup (const char *str) {
return strndup (str, strlen (str));
}
};
} // namespace misc
} // namespace lout
#endif // __LOUT_MISC_HH__
|