21#ifndef __LOUT_MISC_HH__
22#define __LOUT_MISC_HH__
40template <
class T>
inline T
min (T a, T b) {
return a < b ? a : b; }
41template <
class T>
inline T
max (T a, T b) {
return a > b ? a : b; }
43template <
class T>
inline T
min (T a, T b, T c)
45 return (
min (a,
min (b, c)));
47template <
class T>
inline T
max (T a, T b, T c)
49 return (
max (a,
max (b, c)));
54void init (
int argc,
char *argv[]);
58 fprintf (stderr,
"*** [%s] This should not happen! ***\n",
prgName);
67 fprintf (stderr,
"*** [%s] This should not happen: ",
prgName);
68 vfprintf(stderr, fmt, argp);
69 fprintf (stderr,
"! ***\n");
78 fprintf (stderr,
"*** [%s] Not implemented: %s ***\n",
prgName, name);
84 return (
int) ((d > 0) ? (d + 0.5) : (d - 0.5));
89 return ((c >=
'A' && c <=
'Z') ? c + 0x20 : c);
94 return ((c >=
'a' && c <=
'z') ? c - 0x20 : c);
108inline const char *
boolToStr (
bool b) {
return b ?
"true" :
"false"; }
127 this->array = (T*) malloc (
sizeof (T));
129 if (this->numAlloc < this->
num) {
130 this->numAlloc = (this->num < 100) ?
131 this->num : this->num + this->num/10;
133 (T*) realloc(this->array, (this->numAlloc *
sizeof (T)));
141 this->numAlloc = initAlloc;
150 memcpy (this->array, o.
array, sizeof (T) *
num);
189 assert (newSize >= 0);
200 int oldSize = this->
num;
202 for (
int i = oldSize; i < newSize; i++)
212 assert (i >= 0 && this->num - i > 0);
222 inline T
get (
int i)
const {
223 assert (i >= 0 && this->num - i > 0);
224 return this->array[i];
231 assert (this->num > 0);
239 assert (this->num > 0);
240 return this->array[0];
247 assert (this->num > 0);
248 return this->array + this->num - 1;
255 assert (this->num > 0);
256 return this->array[this->num - 1];
267 inline void set (
int i, T t) {
268 assert (i >= 0 && this->num - i > 0);
276 assert (this->num > 0);
277 this->array[this->num - 1] = t;
288 int thisLast = -1,
int destStart = 0) {
289 assert (dest !=
this);
291 thisLast = this->
size () - 1;
292 for (
int i = thisStart; i <= thisLast; i++)
293 dest->
set (i - thisStart + destStart,
get (i));
343 this->numAllocMain = 1;
344 this->arrayMain = (T*) malloc (
sizeof (T));
346 if (this->numAllocMain < this->
numMain) {
347 this->numAllocMain = (this->numMain < 100) ?
348 this->numMain : this->numMain + this->numMain/10;
350 (T*) realloc(this->arrayMain, (this->numAllocMain *
sizeof (T)));
360 this->numAllocExtra = 1;
361 this->arrayExtra1 = (T*) malloc (
sizeof (T));
362 this->arrayExtra2 = (T*) malloc (
sizeof (T));
364 if (this->numAllocExtra < this->
numExtra) {
365 this->numAllocExtra = (this->numExtra < 100) ?
366 this->numExtra : this->numExtra + this->numExtra/10;
368 (T*) realloc(this->arrayExtra1, (this->numAllocExtra *
sizeof (T)));
370 (T*) realloc(this->arrayExtra2, (this->numAllocExtra *
sizeof (T)));
390 this->numMain = this->numExtra = 0;
391 this->numAllocMain = initAlloc;
392 this->numAllocExtra = initAlloc;
393 this->arrayMain = this->arrayExtra1 = this->arrayExtra2 = NULL;
394 this->startExtra = -1;
400 free (this->arrayMain);
401 if (this->arrayExtra1)
402 free (this->arrayExtra1);
403 if (this->arrayExtra2)
404 free (this->arrayExtra2);
415 assert (newSize >= 0);
422 assert (numInsert >= 0);
430 if (this->startExtra == -1) {
432 this->numExtra = numInsert;
433 this->startExtra = index;
438 insert (index, numInsert);
444 int toMove =
startExtra + oldNumExtra - index;
447 toMove *
sizeof (T));
455 int diff = index - this->startExtra - oldNumExtra;
457 for (
int i = diff + oldNumExtra - 1; i >= 0; i--) {
458 T *src = i < oldNumExtra ?
459 this->arrayExtra1 + i : arrayMainI + (i - oldNumExtra);
478 if (this->startExtra == -1) {
479 assert (i >= 0 && i < this->
numMain);
480 return this->arrayMain + i;
484 return this->arrayMain + i;
485 }
else if (i >= this->startExtra + this->numExtra) {
498 assert (i - this->numExtra < this->
numMain);
499 return this->arrayMain + i - this->
numExtra;
501 return this->arrayExtra1 + i - this->
startExtra;
511 inline T
get (
int i)
const
513 return *(this->
getRef(i));
520 assert (
size () > 0);
535 assert (
size () > 0);
554 inline void set (
int i, T t) {
595 {
char buf[32]; sprintf (buf,
"%d", n);
append (buf); }
597 {
char buf[32]; sprintf (buf,
"%p", p);
append (buf); }
620 bool get(
int i)
const;
621 void set(
int i,
bool val);
642 this->pools =
new SimpleVector <char*> (1);
643 this->bulk =
new SimpleVector <char*> (1);
676 for (
int i = 0; i <
bulk->
size (); i++)
682 inline const char *
strndup (
const char *str,
size_t t) {
683 char *new_str = (
char *)
zoneAlloc (t + 1);
684 memcpy (new_str, str, t);
689 inline const char *
strdup (
const char *str) {
690 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.
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)