Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00164 #ifndef cxx_template_INCLUDED
00165 #define cxx_template_INCLUDED "cxx_template.h"
00166 #ifdef _KEEP_RCS_ID
00167 #endif
00168
00169 #include "mempool.h"
00170 #include "erglob.h"
00171 #include "errors.h"
00172
00173
00174 template <class T>
00175 class DYN_ARRAY {
00176 private:
00177 MEM_POOL *_mpool;
00178 mUINT32 _size;
00179 mINT32 _lastidx;
00180 T *_array;
00181
00182 DYN_ARRAY(const DYN_ARRAY&);
00183 public:
00184
00185 DYN_ARRAY(void);
00186 DYN_ARRAY(MEM_POOL *pool);
00187 ~DYN_ARRAY(void);
00188
00189 MEM_POOL* Get_Mem_Pool() { return _mpool; }
00190 void Set_Mem_Pool(MEM_POOL* mpool) { _mpool = mpool; }
00191
00192 void Alloc_array(mUINT32 arr_size);
00193 void Force_Alloc_array(mUINT32 arr_size);
00194 void Realloc_array(mUINT32 new_size);
00195 void Free_array(void);
00196 void Bzero_array(void);
00197
00198 DYN_ARRAY<T>& operator = (const DYN_ARRAY<T>& a);
00199 T& Get(mUINT32 idx) const;
00200 void Set(mUINT32 idx, const T& val);
00201 T& operator[] (mUINT32 idx) const
00202 { Is_True(idx <= _lastidx, ("DYN_ARRAY::[]:Subscript out of range"));
00203 return (_array[idx]); }
00204 T& operator[] (mUINT32 idx)
00205 { Is_True(idx <= _lastidx, ("DYN_ARRAY::[]:Subscript out of range"));
00206 return (_array[idx]); }
00207
00208 void AddElement (const T& val) { _array[Newidx()] = val; }
00209 mUINT32 Elements () const { return (_lastidx+1); }
00210
00211 mUINT32 Newidx(void);
00212 void Decidx(void) { _lastidx--; }
00213 void Initidx(UINT32 idx);
00214 void Setidx(UINT32 idx);
00215 void Resetidx(void) { _lastidx = -1; }
00216 mUINT32 Sizeof(void) const { return _size; }
00217 mINT32 Lastidx(void) const { return _lastidx; }
00218 mUINT32 Idx(T *t) const { return t - _array; }
00219 };
00220
00284 template <class T>
00285 class STACK {
00286 private:
00287 DYN_ARRAY<T> _stack;
00288
00289 STACK(const STACK&);
00290 STACK& operator = (const STACK&);
00291
00292 public:
00293 STACK(MEM_POOL *pool):_stack(pool) {}
00294 ~STACK(void) {}
00295 void Push(const T& val) { _stack[_stack.Newidx()]=val;}
00296 void Settop(const T& val);
00297 INT32 Topidx(void) { return _stack.Lastidx(); }
00298 T Pop(void) {
00299 T t;
00300 INT32 idx = _stack.Lastidx();
00301 FmtAssert(idx >= 0, ("STACK::pop(): Stack Empty"));
00302 t = _stack[idx];
00303 _stack.Decidx();
00304 return t;
00305 }
00306
00307 T& Top_nth(const INT32 n) const;
00308 T& Bottom_nth(const INT32 n) const;
00309 T& Top(void) const;
00310 BOOL Is_Empty(void) const;
00311 void Clear(void) { _stack.Resetidx(); }
00312 void Free() { _stack.Free_array(); }
00313 void Alloc(const INT32 n) { _stack.Alloc_array(n); }
00314 mINT32 Elements() const { return(_stack.Lastidx() +1);}
00315 };
00316
00317
00332 template <class CONTAINER, class PREDICATE>
00333 void Remove_if(CONTAINER& container, PREDICATE pred);
00334
00335
00336
00337
00338
00339
00340 #define MIN_ARRAY_SIZE 16
00341
00342 template <class T >
00343 DYN_ARRAY<T>::DYN_ARRAY(void)
00344 {
00345 _lastidx = -1;
00346 _size = 0;
00347 _array = NULL;
00348 _mpool = NULL;
00349 }
00350
00351 template <class T >
00352 DYN_ARRAY<T>::DYN_ARRAY(MEM_POOL *pool)
00353 {
00354 _lastidx = -1;
00355 _size = 0;
00356 _array = NULL;
00357 _mpool = pool;
00358 }
00359
00360 template <class T >
00361 DYN_ARRAY<T>::~DYN_ARRAY()
00362 {
00363 Free_array();
00364 }
00365
00366
00367 template <class T>
00368 void
00369 DYN_ARRAY<T>::Alloc_array(mUINT32 arr_size)
00370 {
00371 _size = arr_size > MIN_ARRAY_SIZE ? arr_size : MIN_ARRAY_SIZE;
00372 _array = (T*)MEM_POOL_Alloc(_mpool, _size * sizeof(T));
00373 if ( _array == NULL ) ErrMsg ( EC_No_Mem, "DYN_ARRAY::Alloc_array" );
00374 }
00375
00376
00377 template <class T>
00378 void
00379 DYN_ARRAY<T>::Force_Alloc_array (mUINT32 arr_size)
00380 {
00381 _size = arr_size > 1 ? arr_size : 1;
00382 _array = (T*)MEM_POOL_Alloc(_mpool, _size * sizeof(T));
00383 if ( _array == NULL ) ErrMsg ( EC_No_Mem, "DYN_ARRAY::Alloc_array" );
00384 }
00385
00386 template <class T>
00387 void
00388 DYN_ARRAY<T>::Realloc_array(mUINT32 new_size)
00389 {
00390 _array = (T*)MEM_POOL_Realloc(_mpool,
00391 _array,
00392 sizeof(T) * _size,
00393 sizeof(T) * new_size);
00394 if ( _array == NULL ) ErrMsg ( EC_No_Mem, "DYN_ARRAY::Realloc_array" );
00395 _size = new_size;
00396 }
00397
00398 template <class T>
00399 void
00400 DYN_ARRAY<T>::Free_array()
00401 {
00402 if (_array != NULL) {
00403 MEM_POOL_FREE(_mpool,_array);
00404 _array = NULL;
00405 _size = 0;
00406 }
00407 }
00408
00409 template <class T>
00410 void
00411 DYN_ARRAY<T>::Bzero_array()
00412 {
00413 if (_array != NULL) bzero(_array,sizeof(T) * _size);
00414 }
00415
00416 template <class T>
00417 DYN_ARRAY<T>&
00418 DYN_ARRAY<T>::operator = (const DYN_ARRAY<T>& a)
00419 {
00420 if (_size != a._size) Realloc_array(a._size);
00421 _lastidx = a._lastidx;
00422 memcpy (_array, a._array, a._size * sizeof(T));
00423 return *this;
00424 }
00425
00426 template <class T >
00427 mUINT32
00428 DYN_ARRAY<T>::Newidx()
00429 {
00430 _lastidx++;
00431 if (_lastidx >= _size) {
00432
00433 if (_array == NULL) {
00434 Alloc_array (MIN_ARRAY_SIZE);
00435 } else {
00436 Realloc_array (_size * 2);
00437 }
00438 }
00439 return _lastidx;
00440 }
00441
00442 template <class T >
00443 void
00444 DYN_ARRAY<T>::Initidx(UINT32 idx)
00445 {
00446 _lastidx=idx;
00447 if (_lastidx >= _size) {
00448
00449 if (_array != NULL) {
00450 Free_array();
00451 }
00452 Alloc_array(_lastidx + 1);
00453 }
00454 }
00455
00456 template <class T >
00457 void
00458 DYN_ARRAY<T>::Setidx(UINT32 idx)
00459 {
00460 _lastidx=idx;
00461 if (_lastidx >= _size) {
00462
00463 if (_array == 0)
00464 Alloc_array(_lastidx + 1);
00465 else {
00466 INT32 new_size = _size * 2;
00467 while (new_size < _lastidx + 1) new_size *= 2;
00468 Realloc_array(new_size);
00469 }
00470 }
00471 }
00472
00473 template <class T>
00474 void STACK<T>::Settop(const T& val)
00475 {
00476 INT32 idx = _stack.Lastidx();
00477
00478 Is_True(idx >= 0, ("STACK::Settop(): Stack Empty"));
00479 _stack[idx] = val;
00480 }
00481
00482
00483 template <class T>
00484 T& STACK<T>::Top_nth(const INT32 n) const
00485 {
00486 INT32 idx = _stack.Lastidx();
00487
00488 Is_True(idx >= n, ("STACK::Top_nth(): Access beyond stack bottom"));
00489 return _stack[idx - n];
00490 }
00491
00492
00493 template <class T>
00494 T& STACK<T>::Bottom_nth(const INT32 n) const
00495 {
00496 INT32 idx = _stack.Lastidx();
00497
00498 Is_True(n <= idx, ("STACK::Bottom_nth(): Access beyond stack top"));
00499 return _stack[n];
00500 }
00501
00502
00503 template <class T>
00504 T& STACK<T>::Top(void) const
00505 {
00506 INT32 idx = _stack.Lastidx();
00507
00508 Is_True(idx >= 0, ("STACK::Top(): Stack Empty"));
00509 return _stack[idx];
00510 }
00511
00512 template <class T>
00513 BOOL STACK<T>::Is_Empty(void) const
00514 {
00515 return _stack.Lastidx() < 0;
00516 }
00517
00518
00519 template <class CONTAINER, class PREDICATE>
00520 void Remove_if(CONTAINER& container, PREDICATE predicate)
00521 {
00522 typename CONTAINER::CONTAINER_NODE *prev = NULL, *curr, *next;
00523 for (curr = container.Head(); curr != NULL; curr = next) {
00524 next = curr->Next();
00525 if (predicate(curr)) {
00526 if (prev == NULL)
00527 container.Set_Head(next);
00528 else
00529 prev->Set_Next(next);
00530 } else {
00531 prev = curr;
00532 }
00533 }
00534 if (prev == NULL)
00535 container.Set_Tail(container.Head());
00536 else
00537 container.Set_Tail(prev);
00538 }
00539
00540 #endif // cxx_template_INCLUDED