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
00169 #ifndef cxx_template_INCLUDED
00170 #define cxx_template_INCLUDED "cxx_template.h"
00171 #ifdef _KEEP_RCS_ID
00172 static char *cxx_templatercs_id = cxx_template_INCLUDED"$Revision: 1.2 $";
00173 #endif
00174
00175 #include "mempool.h"
00176 #if 0 // SC
00177 #include "erglob.h"
00178 #include "errors.h"
00179 #include "opt_sys.h"
00180 #endif // SC
00181
00182 template <class T>
00183 class DYN_ARRAY {
00184 private:
00185 MEM_POOL *_mpool;
00186 mUINT32 _size;
00187 mINT32 _lastidx;
00188 T *_array;
00189
00190 DYN_ARRAY(const DYN_ARRAY&);
00191 public:
00192
00193 DYN_ARRAY(void);
00194 DYN_ARRAY(MEM_POOL *pool);
00195 ~DYN_ARRAY(void);
00196
00197 MEM_POOL* Get_Mem_Pool() { return _mpool; }
00198 void Set_Mem_Pool(MEM_POOL* mpool) { _mpool = mpool; }
00199
00200 void Alloc_array(mUINT32 arr_size);
00201 void Force_Alloc_array(mUINT32 arr_size);
00202 void Realloc_array(mUINT32 new_size);
00203 void Free_array(void);
00204 void Bzero_array(void);
00205
00206 DYN_ARRAY<T>& operator = (const DYN_ARRAY<T>& a);
00207 T& Get(mUINT32 idx) const;
00208 void Set(mUINT32 idx, const T& val);
00209 T& operator[] (mUINT32 idx) const
00210 { Is_True(idx <= _lastidx, ("DYN_ARRAY::[]:Subscript out of range"));
00211 return (_array[idx]); }
00212 T& operator[] (mUINT32 idx)
00213 { Is_True(idx <= _lastidx, ("DYN_ARRAY::[]:Subscript out of range"));
00214 return (_array[idx]); }
00215
00216 void AddElement (const T& val)
00217 {
00218 #ifdef KEY
00219 mUINT32 idx = Newidx();
00220 _array[idx] = val;
00221 #else
00222 _array[Newidx()] = val;
00223 #endif
00224 }
00225 mUINT32 Elements () const { return (_lastidx+1); }
00226
00227 mUINT32 Newidx(void);
00228 void Decidx(void) { _lastidx--; }
00229 void Initidx(UINT32 idx);
00230 void Setidx(UINT32 idx);
00231 void Resetidx(void) { _lastidx = -1; }
00232 mUINT32 Sizeof(void) const { return _size; }
00233 mINT32 Lastidx(void) const { return _lastidx; }
00234 mUINT32 Idx(T *t) const { return t - _array; }
00235 };
00236
00300 template <class T>
00301 class STACK {
00302 private:
00303 DYN_ARRAY<T> _stack;
00304
00305 STACK(const STACK&);
00306 STACK& operator = (const STACK&);
00307
00308 public:
00309 STACK(MEM_POOL *pool):_stack(pool) {}
00310 ~STACK(void) {}
00311 void Push(const T& val) { _stack[_stack.Newidx()]=val;}
00312 void Settop(const T& val);
00313 INT32 Topidx(void) { return _stack.Lastidx(); }
00314 T Pop(void) {
00315 T t;
00316 INT32 idx = _stack.Lastidx();
00317 FmtAssert(idx >= 0, ("STACK::pop(): Stack Empty"));
00318 t = _stack[idx];
00319 _stack.Decidx();
00320 return t;
00321 }
00322
00323 T& Top_nth(const INT32 n) const;
00324 T& Bottom_nth(const INT32 n) const;
00325 T& Top(void) const;
00326 BOOL Is_Empty(void) const;
00327 void Clear(void) { _stack.Resetidx(); }
00328 void Free() { _stack.Free_array(); }
00329 void Alloc(const INT32 n) { _stack.Alloc_array(n); }
00330 mINT32 Elements() const { return(_stack.Lastidx() +1);}
00331 };
00332
00333
00348 template <class CONTAINER, class PREDICATE>
00349 void Remove_if(CONTAINER& container, PREDICATE pred);
00350
00351
00352
00353
00354
00355
00356 #define MIN_ARRAY_SIZE 16
00357
00358 template <class T >
00359 DYN_ARRAY<T>::DYN_ARRAY(void)
00360 {
00361 _lastidx = -1;
00362 _size = 0;
00363 _array = NULL;
00364 _mpool = NULL;
00365 }
00366
00367 template <class T >
00368 DYN_ARRAY<T>::DYN_ARRAY(MEM_POOL *pool)
00369 {
00370 _lastidx = -1;
00371 _size = 0;
00372 _array = NULL;
00373 _mpool = pool;
00374 }
00375
00376 template <class T >
00377 DYN_ARRAY<T>::~DYN_ARRAY()
00378 {
00379 Free_array();
00380 }
00381
00382
00383 template <class T>
00384 void
00385 DYN_ARRAY<T>::Alloc_array(mUINT32 arr_size)
00386 {
00387 _size = arr_size > MIN_ARRAY_SIZE ? arr_size : MIN_ARRAY_SIZE;
00388 _array = (T*)MEM_POOL_Alloc(_mpool, _size * sizeof(T));
00389 if ( _array == NULL ) ErrMsg ( EC_No_Mem, "DYN_ARRAY::Alloc_array" );
00390 }
00391
00392
00393 template <class T>
00394 void
00395 DYN_ARRAY<T>::Force_Alloc_array (mUINT32 arr_size)
00396 {
00397 _size = arr_size > 1 ? arr_size : 1;
00398 _array = (T*)MEM_POOL_Alloc(_mpool, _size * sizeof(T));
00399 if ( _array == NULL ) ErrMsg ( EC_No_Mem, "DYN_ARRAY::Alloc_array" );
00400 }
00401
00402 template <class T>
00403 void
00404 DYN_ARRAY<T>::Realloc_array(mUINT32 new_size)
00405 {
00406 _array = (T*)MEM_POOL_Realloc(_mpool,
00407 _array,
00408 sizeof(T) * _size,
00409 sizeof(T) * new_size);
00410 if ( _array == NULL ) ErrMsg ( EC_No_Mem, "DYN_ARRAY::Realloc_array" );
00411 _size = new_size;
00412 }
00413
00414 template <class T>
00415 void
00416 DYN_ARRAY<T>::Free_array()
00417 {
00418 if (_array != NULL) {
00419 MEM_POOL_FREE(_mpool,_array);
00420 _array = NULL;
00421 _size = 0;
00422 }
00423 }
00424
00425 template <class T>
00426 void
00427 DYN_ARRAY<T>::Bzero_array()
00428 {
00429 if (_array != NULL) BZERO(_array,sizeof(T) * _size);
00430 }
00431
00432 template <class T>
00433 DYN_ARRAY<T>&
00434 DYN_ARRAY<T>::operator = (const DYN_ARRAY<T>& a)
00435 {
00436 if (_size != a._size) Realloc_array(a._size);
00437 _lastidx = a._lastidx;
00438 memcpy (_array, a._array, a._size * sizeof(T));
00439 return *this;
00440 }
00441
00442 template <class T >
00443 mUINT32
00444 DYN_ARRAY<T>::Newidx()
00445 {
00446 _lastidx++;
00447 if (_lastidx >= _size) {
00448
00449 if (_array == NULL) {
00450 Alloc_array (MIN_ARRAY_SIZE);
00451 } else {
00452 Realloc_array (_size * 2);
00453 }
00454 }
00455 return _lastidx;
00456 }
00457
00458 template <class T >
00459 void
00460 DYN_ARRAY<T>::Initidx(UINT32 idx)
00461 {
00462 _lastidx=idx;
00463 if (_lastidx >= _size) {
00464
00465 if (_array != NULL) {
00466 Free_array();
00467 }
00468 Alloc_array(_lastidx + 1);
00469 }
00470 }
00471
00472 template <class T >
00473 void
00474 DYN_ARRAY<T>::Setidx(UINT32 idx)
00475 {
00476 _lastidx=idx;
00477 if (_lastidx >= _size) {
00478
00479 if (_array == 0)
00480 Alloc_array(_lastidx + 1);
00481 else {
00482 INT32 new_size = _size * 2;
00483 while (new_size < _lastidx + 1) new_size *= 2;
00484 Realloc_array(new_size);
00485 }
00486 }
00487 }
00488
00489 template <class T>
00490 void STACK<T>::Settop(const T& val)
00491 {
00492 INT32 idx = _stack.Lastidx();
00493
00494 Is_True(idx >= 0, ("STACK::Settop(): Stack Empty"));
00495 _stack[idx] = val;
00496 }
00497
00498
00499 template <class T>
00500 T& STACK<T>::Top_nth(const INT32 n) const
00501 {
00502 INT32 idx = _stack.Lastidx();
00503
00504 Is_True(idx >= n, ("STACK::Top_nth(): Access beyond stack bottom"));
00505 return _stack[idx - n];
00506 }
00507
00508
00509 template <class T>
00510 T& STACK<T>::Bottom_nth(const INT32 n) const
00511 {
00512 INT32 idx = _stack.Lastidx();
00513
00514 Is_True(n <= idx, ("STACK::Bottom_nth(): Access beyond stack top"));
00515 return _stack[n];
00516 }
00517
00518
00519 template <class T>
00520 T& STACK<T>::Top(void) const
00521 {
00522 INT32 idx = _stack.Lastidx();
00523
00524 Is_True(idx >= 0, ("STACK::Top(): Stack Empty"));
00525 return _stack[idx];
00526 }
00527
00528 template <class T>
00529 BOOL STACK<T>::Is_Empty(void) const
00530 {
00531 return _stack.Lastidx() < 0;
00532 }
00533
00534
00535 template <class CONTAINER, class PREDICATE>
00536 void Remove_if(CONTAINER& container, PREDICATE predicate)
00537 {
00538 typename CONTAINER::CONTAINER_NODE *prev = NULL, *curr, *next;
00539 for (curr = container.Head(); curr != NULL; curr = next) {
00540 next = curr->Next();
00541 if (predicate(curr)) {
00542 if (prev == NULL)
00543 container.Set_Head(next);
00544 else
00545 prev->Set_Next(next);
00546 } else {
00547 prev = curr;
00548 }
00549 }
00550 if (prev == NULL)
00551 container.Set_Tail(container.Head());
00552 else
00553 container.Set_Tail(prev);
00554 }
00555
00556 #endif // cxx_template_INCLUDED