1 #ifndef __LOUT_MISC_HH__
2 #define __LOUT_MISC_HH__
19 template <
class T>
inline T
min (T a, T b) {
return a < b ? a : b; }
20 template <
class T>
inline T
max (T a, T b) {
return a > b ? a : b; }
22 template <
class T>
inline T
min (T a, T b, T c)
24 return (
min (a,
min (b, c)));
26 template <
class T>
inline T
max (T a, T b, T c)
28 return (
max (a,
max (b, c)));
33 void init (
int argc,
char *argv[]);
37 fprintf (stderr,
"*** [%s] This should not happen! ***\n", prgName);
46 fprintf (stderr,
"*** [%s] This should not happen: ", prgName);
47 vfprintf(stderr, fmt, argp);
48 fprintf (stderr,
"! ***\n");
57 fprintf (stderr,
"*** [%s] Not implemented: %s ***\n", prgName, name);
63 return (
int) ((d > 0) ? (d + 0.5) : (d - 0.5));
68 return ((c >=
'A' && c <=
'Z') ? c + 0x20 : c);
73 return ((c >=
'a' && c <=
'z') ? c - 0x20 : c);
87 inline const char *
boolToStr (
bool b) {
return b ?
"true" :
"false"; }
106 this->array = (T*) malloc (
sizeof (T));
108 if (this->numAlloc < this->num) {
109 this->numAlloc = (this->num < 100) ?
110 this->num : this->num + this->num/10;
112 (T*) realloc(this->array, (this->numAlloc *
sizeof (T)));
120 this->numAlloc = initAlloc;
129 memcpy (this->array, o.
array, sizeof (T) * num);
168 assert (newSize >= 0);
179 int oldSize = this->
num;
181 for (
int i = oldSize; i < newSize; i++)
191 assert (i >= 0 && this->num - i > 0);
201 inline T
get (
int i)
const {
202 assert (i >= 0 && this->num - i > 0);
203 return this->array[i];
210 assert (this->num > 0);
218 assert (this->num > 0);
219 return this->array[0];
226 assert (this->num > 0);
227 return this->array + this->num - 1;
234 assert (this->num > 0);
235 return this->array[this->num - 1];
246 inline void set (
int i, T t) {
247 assert (i >= 0 && this->num - i > 0);
255 assert (this->num > 0);
256 this->array[this->num - 1] = t;
267 int thisLast = -1,
int destStart = 0) {
268 assert (dest !=
this);
270 thisLast = this->
size () - 1;
271 for (
int i = thisStart; i <= thisLast; i++)
272 dest->
set (i - thisStart + destStart,
get (i));
321 if (arrayMain == NULL) {
322 this->numAllocMain = 1;
323 this->arrayMain = (T*) malloc (
sizeof (T));
325 if (this->numAllocMain < this->numMain) {
326 this->numAllocMain = (this->numMain < 100) ?
327 this->numMain : this->numMain + this->numMain/10;
329 (T*) realloc(this->arrayMain, (this->numAllocMain *
sizeof (T)));
338 if (arrayExtra1 == NULL) {
339 this->numAllocExtra = 1;
340 this->arrayExtra1 = (T*) malloc (
sizeof (T));
341 this->arrayExtra2 = (T*) malloc (
sizeof (T));
343 if (this->numAllocExtra < this->numExtra) {
344 this->numAllocExtra = (this->numExtra < 100) ?
345 this->numExtra : this->numExtra + this->numExtra/10;
347 (T*) realloc(this->arrayExtra1, (this->numAllocExtra *
sizeof (T)));
349 (T*) realloc(this->arrayExtra2, (this->numAllocExtra *
sizeof (T)));
355 if (startExtra != -1) {
358 memmove (arrayMain + startExtra + numExtra, arrayMain + startExtra,
359 (numMain - (startExtra + numExtra)) *
sizeof (T));
360 memmove (arrayMain + startExtra, arrayExtra1, numExtra *
sizeof (T));
369 this->numMain = this->numExtra = 0;
370 this->numAllocMain = initAlloc;
371 this->numAllocExtra = initAlloc;
372 this->arrayMain = this->arrayExtra1 = this->arrayExtra2 = NULL;
373 this->startExtra = -1;
378 this->arrayMain = NULL;
382 memcpy (this->arrayMain, o.
arrayMain, sizeof (T) * numMain);
384 this->arrayExtra = NULL;
388 memcpy (this->arrayExtra, o.arrayExtra, sizeof (T) * numExtra);
396 free (this->arrayMain);
397 if (this->arrayExtra1)
398 free (this->arrayExtra1);
399 if (this->arrayExtra2)
400 free (this->arrayExtra2);
411 assert (newSize >= 0);
418 assert (numInsert >= 0);
426 if (this->startExtra == -1) {
428 this->numExtra = numInsert;
429 this->startExtra = index;
432 if (index < startExtra) {
434 insert (index, numInsert);
435 }
else if (index < startExtra + numExtra) {
437 numExtra += numInsert;
440 int toMove = startExtra + oldNumExtra - index;
441 memmove (arrayExtra1 + numExtra - toMove,
442 arrayExtra1 + index - startExtra,
443 toMove *
sizeof (T));
446 numExtra += numInsert;
451 int diff = index - this->startExtra - oldNumExtra;
453 for (
int i = diff + oldNumExtra - 1; i >= 0; i--) {
454 T *src = i < oldNumExtra ?
455 this->arrayExtra1 + i : arrayMainI + (i - oldNumExtra);
457 arrayMainI + i : arrayExtra2 + (i - diff);
461 memcpy (arrayExtra1, arrayExtra2,
sizeof (T) * oldNumExtra);
462 startExtra = index - oldNumExtra;
474 if (this->startExtra == -1) {
475 assert (i >= 0 && i < this->numMain);
476 return this->arrayMain + i;
478 if (i < this->startExtra) {
480 return this->arrayMain + i;
481 }
else if (i >= this->startExtra + this->numExtra) {
494 assert (i - this->numExtra < this->numMain);
495 return this->arrayMain + i - this->
numExtra;
497 return this->arrayExtra1 + i - this->
startExtra;
507 inline T
get (
int i)
const
509 return *(this->
getRef(i));
516 assert (
size () > 0);
531 assert (
size () > 0);
550 inline void set (
int i, T t) {
591 {
char buf[32]; sprintf (buf,
"%d", n);
append (buf); }
593 {
char buf[32]; sprintf (buf,
"%p", p);
append (buf); }
610 inline int bytesForBits(
int bits) {
return bits == 0 ? 1 : (bits + 7) / 8; }
616 bool get(
int i)
const;
617 void set(
int i,
bool val);
636 this->poolLimit = poolSize / 4;
653 bulk->
set (bulk->
size () - 1, (
char*) malloc (t));
654 return bulk->
get (bulk->
size () - 1);
657 if (t > poolSize - freeIdx) {
659 pools->
set (pools->
size () - 1, (
char*) malloc (poolSize));
663 ret = pools->
get (pools->
size () - 1) + freeIdx;
669 for (
int i = 0; i < pools->
size (); i++)
670 free (pools->
get (i));
672 for (
int i = 0; i < bulk->
size (); i++)
673 free (bulk->
get (i));
678 inline const char *
strndup (
const char *str,
size_t t) {
679 char *new_str = (
char *)
zoneAlloc (t + 1);
680 memcpy (new_str, str, t);
685 inline const char *
strdup (
const char *str) {
686 return strndup (str, strlen (str));
694 #endif // __LOUT_MISC_HH__
~BitSet()
Definition: misc.cc:142
T * getRef(int i) const
Return the reference of one element.
Definition: misc.hh:190
void setSize(int newSize, T t)
Set the size explicitly and initialize new values.
Definition: misc.hh:178
int numChars
Definition: misc.hh:575
void set(int i, T t)
Store an object in the vector.
Definition: misc.hh:550
void resizeMain()
Definition: misc.hh:316
int numExtra
Definition: misc.hh:314
void set(int i, T t)
Store an object in the vector.
Definition: misc.hh:246
bool strValid
Definition: misc.hh:577
void increase()
Definition: misc.hh:407
SimpleVector(int initAlloc=1)
Definition: misc.hh:117
A class for fast concatenation of a large number of strings.
Definition: misc.hh:565
void set(int i, bool val)
Definition: misc.cc:163
T * getArray() const
Definition: misc.hh:145
SimpleVector(const SimpleVector &o)
Definition: misc.hh:124
T getFirst() const
Return the first element, explicitly.
Definition: misc.hh:523
void intoStringBuffer(misc::StringBuffer *sb)
Definition: misc.cc:147
void appendNoCopy(char *str)
Append a NUL-terminated string to the buffer, without copying.
Definition: misc.cc:68
~StringBuffer()
Definition: misc.cc:54
void resizeExtra()
Definition: misc.hh:333
void copyTo(SimpleVector< T > *dest, int thisStart=0, int thisLast=-1, int destStart=0)
Copies some elements into another vector of the same type.
Definition: misc.hh:266
void assertNotReached()
Definition: misc.hh:35
void append(const char *str)
Append a NUL-terminated string to the buffer, with copying.
Definition: misc.hh:589
int AsciiToupper(char c)
Definition: misc.hh:71
T min(T a, T b)
Definition: misc.hh:19
void setSize(int newSize)
Definition: misc.hh:409
~NotSoSimpleVector()
Definition: misc.hh:393
A simple allocator optimized to handle many small chunks of memory. The chunks can not be free'd indi...
Definition: misc.hh:626
int size() const
Return the number of elements put into this vector.
Definition: misc.hh:141
char * data
Definition: misc.hh:570
char * str
Definition: misc.hh:576
void clear()
Remove all strings appended to the string buffer.
Definition: misc.cc:116
int numBytes
Definition: misc.hh:608
T * getLastRef() const
Return the reference of the last element (convenience method).
Definition: misc.hh:530
ZoneAllocator(size_t poolSize)
Definition: misc.hh:634
int bytesForBits(int bits)
Definition: misc.hh:610
T * array
Definition: misc.hh:96
void setLast(T t)
Store an object at the end of the vector.
Definition: misc.hh:254
const char * boolToStr(bool b)
Definition: misc.hh:87
int numBits
Definition: misc.hh:608
T * arrayExtra2
Definition: misc.hh:313
T * arrayExtra1
Definition: misc.hh:313
SimpleVector< char * > * pools
Definition: misc.hh:630
const char * prgName
Definition: misc.cc:33
T getLast() const
Return the last element, explicitly.
Definition: misc.hh:233
SimpleVector< char * > * bulk
Definition: misc.hh:631
const char * strdup(const char *str)
Definition: misc.hh:685
const char * getChars()
Return a NUL-terminated strings containing all appended strings.
Definition: misc.cc:92
Node * lastNode
Definition: misc.hh:574
BitSet(int initBits)
Definition: misc.cc:134
T max(T a, T b)
Definition: misc.hh:20
Simple (simpler than container::untyped::Vector and container::typed::Vector) template based vector...
Definition: misc.hh:93
Definition: container.cc:27
size_t poolSize
Definition: misc.hh:629
~ZoneAllocator()
Definition: misc.hh:642
int numMain
Definition: misc.hh:314
int numAlloc
Definition: misc.hh:97
T * getFirstRef() const
Return the reference of the first element (convenience method).
Definition: misc.hh:209
T get(int i) const
Return the one element, explicitly.
Definition: misc.hh:201
int num
Definition: misc.hh:97
bool empty() const
Definition: misc.hh:143
void notImplemented(const char *name)
Definition: misc.hh:55
Node * next
Definition: misc.hh:571
T * arrayMain
Definition: misc.hh:313
int size() const
Definition: misc.hh:403
void setSize(int newSize)
Set the size explicitly.
Definition: misc.hh:167
void increase()
Increase the vector size by one.
Definition: misc.hh:160
T * getRef(int i) const
Return the reference of one element.
Definition: misc.hh:472
const char * strndup(const char *str, size_t t)
Definition: misc.hh:678
void zoneFree()
Definition: misc.hh:668
T * detachArray()
Definition: misc.hh:147
int numAllocExtra
Definition: misc.hh:314
StringBuffer()
Definition: misc.cc:46
size_t poolLimit
Definition: misc.hh:629
void resize()
Definition: misc.hh:99
T * getLastRef() const
Return the reference of the last element (convenience method).
Definition: misc.hh:225
void init(int argc, char *argv[])
Definition: misc.cc:35
int roundInt(double d)
Definition: misc.hh:61
Container similar to lout::misc::SimpleVector, but some cases of insertion optimized (used for hyphen...
Definition: misc.hh:310
void clear()
Definition: misc.cc:184
void appendPointer(void *p)
Definition: misc.hh:592
NotSoSimpleVector(int initAlloc)
Definition: misc.hh:367
int AsciiTolower(char c)
Definition: misc.hh:66
int startExtra
Definition: misc.hh:314
A bit set, which automatically reallocates when needed.
Definition: misc.hh:604
unsigned char * bits
Definition: misc.hh:607
Node * firstNode
Definition: misc.hh:574
int AsciiStrcasecmp(const char *s1, const char *s2)
Definition: misc.hh:76
void setLast(T t)
Store an object at the end of the vector.
Definition: misc.hh:557
size_t freeIdx
Definition: misc.hh:629
T getFirst() const
Return the first element, explicitly.
Definition: misc.hh:217
~SimpleVector()
Definition: misc.hh:132
T * getFirstRef() const
Return the reference of the first element (convenience method).
Definition: misc.hh:515
T getLast() const
Return the last element, explicitly.
Definition: misc.hh:538
void appendInt(int n)
Definition: misc.hh:590
void consolidate()
Definition: misc.hh:353
void insert(int index, int numInsert)
Definition: misc.hh:416
int numAllocMain
Definition: misc.hh:314
void * zoneAlloc(size_t t)
Definition: misc.hh:648
void appendBool(bool b)
Definition: misc.hh:594
NotSoSimpleVector(const NotSoSimpleVector &o)
Definition: misc.hh:376
bool empty() const
Definition: misc.hh:405