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);
43 return (
int) ((d > 0) ? (d + 0.5) : (d - 0.5));
48 return ((c >=
'A' && c <=
'Z') ? c + 0x20 : c);
53 return ((c >=
'a' && c <=
'z') ? c - 0x20 : c);
84 this->array = (T*) malloc (
sizeof (T));
86 if (this->numAlloc < this->
num) {
87 this->numAlloc = (this->num < 100) ?
88 this->num : this->num + this->num/10;
90 (T*) realloc(this->array, (this->numAlloc *
sizeof (T)));
98 this->numAlloc = initAlloc;
107 memcpy (this->array, o.
array, sizeof (T) *
num);
144 assert (newSize >= 0);
155 int oldSize = this->
num;
157 for (
int i = oldSize; i < newSize; i++)
167 assert (i >= 0 && this->num - i > 0);
177 inline T
get (
int i)
const {
178 assert (i >= 0 && this->num - i > 0);
179 return this->array[i];
186 assert (this->num > 0);
194 assert (this->num > 0);
195 return this->array[0];
202 assert (this->num > 0);
203 return this->array + this->num - 1;
210 assert (this->num > 0);
211 return this->array[this->num - 1];
222 inline void set (
int i, T t) {
223 assert (i >= 0 && this->num - i > 0);
274 this->numAllocMain = 1;
275 this->arrayMain = (T*) malloc (
sizeof (T));
277 if (this->numAllocMain < this->
numMain) {
278 this->numAllocMain = (this->numMain < 100) ?
279 this->numMain : this->numMain + this->numMain/10;
281 (T*) realloc(this->arrayMain, (this->numAllocMain *
sizeof (T)));
291 this->numAllocExtra = 1;
292 this->arrayExtra1 = (T*) malloc (
sizeof (T));
293 this->arrayExtra2 = (T*) malloc (
sizeof (T));
295 if (this->numAllocExtra < this->
numExtra) {
296 this->numAllocExtra = (this->numExtra < 100) ?
297 this->numExtra : this->numExtra + this->numExtra/10;
299 (T*) realloc(this->arrayExtra1, (this->numAllocExtra *
sizeof (T)));
301 (T*) realloc(this->arrayExtra2, (this->numAllocExtra *
sizeof (T)));
321 this->numMain = this->numExtra = 0;
322 this->numAllocMain = initAlloc;
323 this->numAllocExtra = initAlloc;
324 this->arrayMain = this->arrayExtra1 = this->arrayExtra2 = NULL;
325 this->startExtra = -1;
330 this->arrayMain = NULL;
336 this->arrayExtra = NULL;
340 memcpy (this->arrayExtra, o.arrayExtra, sizeof (T) *
numExtra);
348 free (this->arrayMain);
349 if (this->arrayExtra1)
350 free (this->arrayExtra1);
351 if (this->arrayExtra2)
352 free (this->arrayExtra2);
361 assert (newSize >= 0);
368 assert (numInsert >= 0);
376 if (this->startExtra == -1) {
378 this->numExtra = numInsert;
379 this->startExtra = index;
384 insert (index, numInsert);
390 int toMove =
startExtra + oldNumExtra - index;
393 toMove *
sizeof (T));
401 int diff = index - this->startExtra - oldNumExtra;
403 for (
int i = diff + oldNumExtra - 1; i >= 0; i--) {
404 T *src = i < oldNumExtra ?
405 this->arrayExtra1 + i : arrayMainI + (i - oldNumExtra);
424 if (this->startExtra == -1)
425 return this->arrayMain + i;
428 return this->arrayMain + i;
429 else if (i >= this->startExtra + this->numExtra)
430 return this->arrayMain + i - this->
numExtra;
432 return this->arrayExtra1 + i - this->
startExtra;
442 inline T
get (
int i)
const 444 return *(this->
getRef(i));
451 assert (
size () > 0);
466 assert (
size () > 0);
485 inline void set (
int i, T t) {
539 bool get(
int i)
const;
540 void set(
int i,
bool val);
595 for (
int i = 0; i <
bulk->
size (); i++)
601 inline const char *
strndup (
const char *str,
size_t t) {
602 char *new_str = (
char *)
zoneAlloc (t + 1);
603 memcpy (new_str, str, t);
608 inline const char *
strdup (
const char *str) {
609 return strndup (str, strlen (str));
617 #endif // __LOUT_MISC_HH__