1#ifndef __LOUT_MISC_HH__
2#define __LOUT_MISC_HH__
20template <
class T>
inline T
min (T a, T b) {
return a < b ? a : b; }
21template <
class T>
inline T
max (T a, T b) {
return a > b ? a : b; }
23template <
class T>
inline T
min (T a, T b, T c)
25 return (
min (a,
min (b, c)));
27template <
class T>
inline T
max (T a, T b, T c)
29 return (
max (a,
max (b, c)));
34void init (
int argc,
char *argv[]);
38 fprintf (stderr,
"*** [%s] This should not happen! ***\n",
prgName);
47 fprintf (stderr,
"*** [%s] This should not happen: ",
prgName);
48 vfprintf(stderr, fmt, argp);
49 fprintf (stderr,
"! ***\n");
58 fprintf (stderr,
"*** [%s] Not implemented: %s ***\n",
prgName, name);
64 return (
int) ((d > 0) ? (d + 0.5) : (d - 0.5));
69 return ((c >=
'A' && c <=
'Z') ? c + 0x20 : c);
74 return ((c >=
'a' && c <=
'z') ? c - 0x20 : c);
88inline const char *
boolToStr (
bool b) {
return b ?
"true" :
"false"; }
107 this->array = (T*) malloc (
sizeof (T));
109 if (this->numAlloc < this->
num) {
110 this->numAlloc = (this->num < 100) ?
111 this->num : this->num + this->num/10;
113 (T*) realloc(this->array, (this->numAlloc *
sizeof (T)));
121 this->numAlloc = initAlloc;
130 memcpy (this->array, o.
array, sizeof (T) *
num);
169 assert (newSize >= 0);
180 int oldSize = this->
num;
182 for (
int i = oldSize; i < newSize; i++)
192 assert (i >= 0 && this->num - i > 0);
202 inline T
get (
int i)
const {
203 assert (i >= 0 && this->num - i > 0);
204 return this->array[i];
211 assert (this->num > 0);
219 assert (this->num > 0);
220 return this->array[0];
227 assert (this->num > 0);
228 return this->array + this->num - 1;
235 assert (this->num > 0);
236 return this->array[this->num - 1];
247 inline void set (
int i, T t) {
248 assert (i >= 0 && this->num - i > 0);
256 assert (this->num > 0);
257 this->array[this->num - 1] = t;
268 int thisLast = -1,
int destStart = 0) {
269 assert (dest !=
this);
271 thisLast = this->
size () - 1;
272 for (
int i = thisStart; i <= thisLast; i++)
273 dest->
set (i - thisStart + destStart,
get (i));
323 this->numAllocMain = 1;
324 this->arrayMain = (T*) malloc (
sizeof (T));
326 if (this->numAllocMain < this->
numMain) {
327 this->numAllocMain = (this->numMain < 100) ?
328 this->numMain : this->numMain + this->numMain/10;
330 (T*) realloc(this->arrayMain, (this->numAllocMain *
sizeof (T)));
340 this->numAllocExtra = 1;
341 this->arrayExtra1 = (T*) malloc (
sizeof (T));
342 this->arrayExtra2 = (T*) malloc (
sizeof (T));
344 if (this->numAllocExtra < this->
numExtra) {
345 this->numAllocExtra = (this->numExtra < 100) ?
346 this->numExtra : this->numExtra + this->numExtra/10;
348 (T*) realloc(this->arrayExtra1, (this->numAllocExtra *
sizeof (T)));
350 (T*) realloc(this->arrayExtra2, (this->numAllocExtra *
sizeof (T)));
370 this->numMain = this->numExtra = 0;
371 this->numAllocMain = initAlloc;
372 this->numAllocExtra = initAlloc;
373 this->arrayMain = this->arrayExtra1 = this->arrayExtra2 = NULL;
374 this->startExtra = -1;
379 this->arrayMain = NULL;
385 this->arrayExtra = NULL;
389 memcpy (this->arrayExtra, o.arrayExtra, sizeof (T) *
numExtra);
397 free (this->arrayMain);
398 if (this->arrayExtra1)
399 free (this->arrayExtra1);
400 if (this->arrayExtra2)
401 free (this->arrayExtra2);
412 assert (newSize >= 0);
419 assert (numInsert >= 0);
427 if (this->startExtra == -1) {
429 this->numExtra = numInsert;
430 this->startExtra = index;
435 insert (index, numInsert);
441 int toMove =
startExtra + oldNumExtra - index;
444 toMove *
sizeof (T));
452 int diff = index - this->startExtra - oldNumExtra;
454 for (
int i = diff + oldNumExtra - 1; i >= 0; i--) {
455 T *src = i < oldNumExtra ?
456 this->arrayExtra1 + i : arrayMainI + (i - oldNumExtra);
475 if (this->startExtra == -1) {
476 assert (i >= 0 && i < this->
numMain);
477 return this->arrayMain + i;
481 return this->arrayMain + i;
482 }
else if (i >= this->startExtra + this->numExtra) {
495 assert (i - this->numExtra < this->
numMain);
496 return this->arrayMain + i - this->
numExtra;
498 return this->arrayExtra1 + i - this->
startExtra;
508 inline T
get (
int i)
const
510 return *(this->
getRef(i));
517 assert (
size () > 0);
532 assert (
size () > 0);
551 inline void set (
int i, T t) {
592 {
char buf[32]; sprintf (buf,
"%d", n);
append (buf); }
594 {
char buf[32]; sprintf (buf,
"%p", p);
append (buf); }
617 bool get(
int i)
const;
618 void set(
int i,
bool val);
639 this->pools =
new SimpleVector <char*> (1);
640 this->bulk =
new SimpleVector <char*> (1);
673 for (
int i = 0; i <
bulk->
size (); i++)
679 inline const char *
strndup (
const char *str,
size_t t) {
680 char *new_str = (
char *)
zoneAlloc (t + 1);
681 memcpy (new_str, str, t);
686 inline const char *
strdup (
const char *str) {
687 return strndup (str, strlen (str));
A bit set, which automatically reallocates when needed.
void intoStringBuffer(misc::StringBuffer *sb)
void set(int i, bool val)
int bytesForBits(int bits)
Container similar to lout::misc::SimpleVector, but some cases of insertion optimized (used for hyphen...
NotSoSimpleVector(int initAlloc)
T getFirst() const
Return the first element, explicitly.
void set(int i, T t)
Store an object in the vector.
T * getLastRef() const
Return the reference of the last element (convenience method).
void insert(int index, int numInsert)
T * getFirstRef() const
Return the reference of the first element (convenience method).
void setSize(int newSize)
T get(int i) const
Return the one element, explicitly.
T getLast() const
Return the last element, explicitly.
T * getRef(int i) const
Return the reference of one element.
NotSoSimpleVector(const NotSoSimpleVector &o)
void setLast(T t)
Store an object at the end of the vector.
Simple (simpler than container::untyped::Vector and container::typed::Vector) template based vector.
SimpleVector(const SimpleVector &o)
void setSize(int newSize)
Set the size explicitly.
void increase()
Increase the vector size by one.
void copyTo(SimpleVector< T > *dest, int thisStart=0, int thisLast=-1, int destStart=0)
Copies some elements into another vector of the same type.
T getFirst() const
Return the first element, explicitly.
void setSize(int newSize, T t)
Set the size explicitly and initialize new values.
T * getFirstRef() const
Return the reference of the first element (convenience method).
void set(int i, T t)
Store an object in the vector.
T get(int i) const
Return the one element, explicitly.
int size() const
Return the number of elements put into this vector.
T * getRef(int i) const
Return the reference of one element.
T * getLastRef() const
Return the reference of the last element (convenience method).
void setLast(T t)
Store an object at the end of the vector.
T getLast() const
Return the last element, explicitly.
SimpleVector(int initAlloc=1)
A class for fast concatenation of a large number of strings.
void appendPointer(void *p)
void clear()
Remove all strings appended to the string buffer.
void append(const char *str)
Append a NUL-terminated string to the buffer, with copying.
void appendNoCopy(char *str)
Append a NUL-terminated string to the buffer, without copying.
const char * getChars()
Return a NUL-terminated strings containing all appended strings.
A simple allocator optimized to handle many small chunks of memory.
const char * strdup(const char *str)
const char * strndup(const char *str, size_t t)
void * zoneAlloc(size_t t)
ZoneAllocator(size_t poolSize)
SimpleVector< char * > * bulk
SimpleVector< char * > * pools
char * dStrdup(const char *s)
void notImplemented(const char *name)
int AsciiStrcasecmp(const char *s1, const char *s2)
void init(int argc, char *argv[])
const char * boolToStr(bool b)