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
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include "defs.h"
00057 #include "errors.h"
00058 #include "erglob.h"
00059
00060 #include "cxx_base.h"
00061
00062 SLIST_NODE*
00063 SLIST_NODE::Remove(SLIST_NODE *prev)
00064 {
00065 Is_True(this, ("SLIST_NODE::Remove(): this is NULL"));
00066
00067 if (prev != NULL)
00068 prev->_next = _next;
00069
00070 _next = NULL;
00071 return this;
00072 }
00073
00074 INT32
00075 SLIST_NODE::Len(void) const
00076 {
00077 INT i;
00078 const SLIST_NODE *p;
00079 for (i = 0, p = this; p != NULL; i++, p = p->Next());
00080 return i;
00081 }
00082
00083 INT
00084 SLIST_NODE::Pos(SLIST_NODE *nd) const
00085 {
00086 const SLIST_NODE *cur = this;
00087 INT pos = 0;
00088 while (cur) {
00089 if (nd == cur)
00090 return pos;
00091 cur = cur->Next();
00092 pos++;
00093 }
00094 return -1;
00095 }
00096
00097 SLIST::SLIST( SLIST_NODE *list)
00098 {
00099 Is_True(this, ("SLIST::SLIST(): this is NULL"));
00100
00101 _head = _tail = list;
00102 while (_tail && _tail->_next != NULL)
00103 _tail = _tail->_next;
00104 }
00105
00106 void
00107 SLIST::Init( SLIST_NODE *list)
00108 {
00109 Is_True(this, ("SLIST::Init(): this is NULL"));
00110
00111 _head = _tail = list;
00112 while (_tail && _tail->_next != NULL)
00113 _tail = _tail->_next;
00114 }
00115
00116 BOOL
00117 SLIST::Append( SLIST_NODE *nd, SLIST_NODE *od)
00118 {
00119 Is_True(this, ("SLIST::Append(): this is NULL"));
00120
00121 if (nd == NULL) return FALSE;
00122
00123 if ( od == NULL && _head == NULL) {
00124 _head = _tail = nd;
00125 return TRUE;
00126 }
00127
00128
00129 if ( _tail == od ) {
00130 od->Insert_After(nd);
00131 _tail = nd;
00132 return TRUE;
00133 }
00134
00135 for (SLIST_NODE* tmp = _head; tmp != NULL; tmp = tmp->_next ) {
00136 if (tmp == od) {
00137 od->Insert_After(nd);
00138 return TRUE;
00139 }
00140 }
00141
00142 return FALSE;
00143 }
00144
00145 BOOL
00146 SLIST::Prepend( SLIST_NODE *nd, SLIST_NODE *od )
00147 {
00148 Is_True(this, ("SLIST::Prepend(): this is NULL"));
00149
00150 if (nd == NULL) return FALSE;
00151
00152 if (od == NULL && _head == NULL)
00153 _head = _tail = nd;
00154
00155
00156 if ( _head == od ) {
00157 _head = od->Insert_Before(nd);
00158 return TRUE;
00159 }
00160
00161 for (SLIST_NODE* tmp = _head; tmp->_next != NULL; tmp = tmp->_next ) {
00162 if (tmp->_next == od) {
00163 tmp->_next = od->Insert_Before(nd);
00164 return TRUE;
00165 }
00166 }
00167 return FALSE;
00168 }
00169
00170 void
00171 SLIST::Append_List(SLIST *new_list)
00172 {
00173 Is_True(this, ("SLIST::Append_List(): this is NULL"));
00174
00175
00176 SLIST_NODE* tmp = new_list->Head();
00177 SLIST_NODE* tmp2 = new_list->Tail();
00178
00179 if (_head == NULL) {
00180 _head = tmp;
00181 _tail = tmp2;
00182 }
00183 else if (tmp2 != NULL) {
00184 _tail->_next = tmp;
00185 _tail = tmp2;
00186 }
00187 }
00188
00189 void
00190 SLIST::Prepend_List(SLIST *new_list)
00191 {
00192 Is_True(this, ("SLIST::Prepend_List(): this is NULL"));
00193
00194
00195 SLIST_NODE* tmp = new_list->Head();
00196 SLIST_NODE* tmp2 = new_list->Tail();
00197
00198 if (_head == NULL) {
00199 _head = tmp;
00200 _tail = tmp2;
00201 }
00202 else if (tmp2 != NULL) {
00203 tmp2->_next = _head;
00204 _head = tmp;
00205 }
00206 }
00207
00208 SLIST_NODE*
00209 SLIST::Remove_Headnode(void)
00210 {
00211 Is_True(this, ("SLIST::Remove_Headnode() called with NULL this"));
00212
00213 if (_head == NULL)
00214 return NULL;
00215
00216 SLIST_NODE *old_head = _head;
00217 _head = _head->_next;
00218 if (_head == NULL)
00219 _tail = NULL;
00220
00221 old_head->_next = NULL;
00222 return old_head;
00223 }
00224
00225 SLIST_NODE*
00226 SLIST::Remove(SLIST_NODE *prev, SLIST_NODE *cur)
00227 {
00228 Is_True(this, ("SLIST::Remove() called with NULL this"));
00229
00230 if (prev == NULL) {
00231 Is_True(cur == _head, ("cur is not head, but prev is null"));
00232 return Remove_Headnode();
00233 }
00234 else {
00235 Is_True(prev->_next == cur, ("prev does not point to cur"));
00236 prev->_next = cur->_next;
00237 if (prev->_next == NULL)
00238 _tail = prev;
00239 }
00240
00241 cur->_next = NULL;
00242 return cur;
00243 }
00244
00245 void
00246 SLIST::Remove_node(SLIST_NODE *slist_node)
00247 {
00248 Is_True(this, ("SLIST::Remove_node() called with NULL this"));
00249 SLIST_NODE *prev = NULL;
00250
00251
00252 SLIST_NODE* tmp;
00253 for (tmp = Head(); tmp != NULL; tmp = tmp->_next) {
00254 if (tmp == slist_node) break;
00255 prev = tmp;
00256 }
00257 Is_True(tmp == slist_node, ("SLIST::Remove_node() cannot delete node from SLIST."));
00258
00259 if (prev != NULL)
00260 prev->Set_Next(slist_node->Next());
00261
00262 if (slist_node == Head())
00263 Set_Head(slist_node->Next());
00264
00265 if (slist_node == Tail())
00266 Set_Tail(prev);
00267
00268 slist_node->Set_Next(NULL);
00269 }
00270
00271 INT32
00272 SLIST::Len(void) const
00273 {
00274 Is_True(this, ("SLIST::Len(): this is NULL"));
00275
00276 SLIST_ITER lst_iter(_head);
00277 return lst_iter.Len();
00278 }
00279
00280
00281 SLIST_NODE *
00282 SLIST_ITER::Nth(INT n)
00283 {
00284 Is_True(this, ("SLIST_Iter::Nth(): this is NULL"));
00285
00286 Len();
00287
00288 if (n >= _len) return NULL;
00289
00290 if (n < _idx) First();
00291 for (; n != _idx; Next());
00292 return _cur;
00293 }
00294
00295 INT32
00296 SLIST_ITER::Len(void)
00297 {
00298 Is_True(this, ("SLIST_Iter::Len(): this is NULL"));
00299
00300 if (_len == -1) {
00301 const SLIST_NODE *tmp;
00302 for (tmp = _head, _len = 0; tmp != NULL; tmp = tmp->Next())
00303 _len++;
00304 }
00305 return _len;
00306 }
00307
00308 CHAIN_NODE*
00309 CHAIN_NODE::Insert_After(CHAIN_NODE *nd)
00310 {
00311 Is_True(this, ("this is NULL"));
00312
00313 if (nd != NULL) {
00314 if (_next != NULL)
00315 _next->_prev=nd;
00316 nd->_next = _next;
00317 _next=nd;
00318 nd->_prev=this;
00319 }
00320 return nd;
00321 }
00322
00323 CHAIN_NODE*
00324 CHAIN_NODE::Insert_Before(CHAIN_NODE *nd)
00325 {
00326 Is_True(this, ("this is NULL"));
00327
00328 if (nd != NULL) {
00329 if (_prev != NULL)
00330 _prev->_next=nd;
00331 nd->_prev = _prev;
00332 _prev=nd;
00333 nd->_next=this;
00334 }
00335 return nd;
00336 }
00337
00338 CHAIN_NODE*
00339 CHAIN_NODE::Remove(void)
00340 {
00341 Is_True(this, ("this is NULL"));
00342
00343 if (_prev != NULL)
00344 _prev->_next = _next;
00345 if (_next != NULL)
00346 _next->_prev = _prev;
00347
00348 _next = NULL;
00349 _prev = NULL;
00350
00351 return this;
00352 }
00353
00354 void
00355 CHAIN::Append( CHAIN_NODE *nd )
00356 {
00357 Is_True(this, ("this is NULL"));
00358
00359 if (_head == NULL)
00360 _head = _tail = nd;
00361 else
00362 _tail = _tail->Insert_After(nd);
00363 }
00364
00365 void
00366 CHAIN::Prepend( CHAIN_NODE *nd )
00367 {
00368 Is_True(this, ("this is NULL"));
00369
00370 if (_head == NULL)
00371 _head = _tail = nd;
00372 else {
00373 _head = _head->Insert_Before(nd);
00374 }
00375 }
00376
00377 void
00378 CHAIN::Insert_After(CHAIN_NODE *nd, CHAIN_NODE *after_nd)
00379 {
00380 Is_True(this, ("this is NULL"));
00381
00382 if (_head == NULL)
00383 _head = _tail = nd;
00384 else if (after_nd == _tail)
00385 _tail = _tail->Insert_After(nd);
00386 else
00387 after_nd->Insert_After(nd);
00388 }
00389
00390 void
00391 CHAIN::Insert_Before(CHAIN_NODE *nd, CHAIN_NODE *before_nd)
00392 {
00393 Is_True(this, ("this is NULL"));
00394
00395 if (_head == NULL)
00396 _head = _tail = nd;
00397 else if (before_nd == _head)
00398 _head = _head->Insert_Before(nd);
00399 else
00400 before_nd->Insert_Before(nd);
00401 }
00402
00403 BOOL
00404 CHAIN::Is_Member( CHAIN_NODE *nd ) const
00405 {
00406 Is_True(this, ("this is NULL"));
00407
00408 if (nd == NULL)
00409 return FALSE;
00410
00411 for (CHAIN_NODE* tmp = _head; tmp != NULL; tmp = tmp->_next )
00412 if (tmp == nd)
00413 return TRUE;
00414
00415 return FALSE;
00416 }
00417
00418 void
00419 CHAIN::Append_List(CHAIN *new_list)
00420 {
00421 Is_True(this, ("this is NULL"));
00422
00423 if (new_list == NULL)
00424 return;
00425
00426 if (_head == NULL) {
00427 Init(new_list);
00428 return;
00429 }
00430
00431 _tail->_next = new_list->_head;
00432 _tail->_next->_prev = _tail;
00433
00434 _tail = new_list->_tail;
00435 }
00436
00437 void
00438 CHAIN::Prepend_List(CHAIN *new_list)
00439 {
00440 Is_True(this, ("this is NULL"));
00441
00442 if (new_list == NULL)
00443 return;
00444
00445 if (_head == NULL) {
00446 Init(new_list);
00447 return;
00448 }
00449
00450 _head->_prev = new_list->_tail;
00451 _head->_prev->_next = _head;
00452
00453 _head = new_list->_head;
00454
00455 }
00456
00457
00458
00459 void
00460 CHAIN::Remove( CHAIN_NODE *nd )
00461 {
00462 Is_True(this, ("this is NULL"));
00463
00464 if (nd == NULL)
00465 return;
00466
00467 if (nd == _head)
00468 _head = _head->_next;
00469
00470 if (nd == _tail)
00471 _tail = _tail->_prev;
00472
00473 nd->Remove();
00474 }
00475
00476 CHAIN_NODE*
00477 CHAIN::Remove_Head(void)
00478 {
00479 Is_True(this, ("this is NULL"));
00480
00481 CHAIN_NODE* nd;
00482
00483 if (_head == NULL)
00484 nd = NULL;
00485 else if (_head == _tail) {
00486 nd = _head->Remove();
00487 _head = _tail = NULL;
00488 }
00489 else {
00490 _head = _head->_next;
00491 nd = _head->_prev->Remove();
00492 _head->_prev = NULL;
00493 }
00494 return nd;
00495 }
00496
00497 CHAIN_NODE*
00498 CHAIN::Remove_Tail(void)
00499 {
00500 Is_True(this, ("this is NULL"));
00501
00502 CHAIN_NODE* nd;
00503
00504 if (_tail == NULL)
00505 nd = NULL;
00506 else if (_head == _tail) {
00507 nd = _head->Remove();
00508 _head=_tail=NULL;
00509 }
00510 else {
00511 _tail = _tail->_prev;
00512 nd = _tail->_next->Remove();
00513 _tail->_next=NULL;
00514 }
00515 return nd;
00516 }
00517
00518 INT32
00519 CHAIN::Len(void) const
00520 {
00521 Is_True(this, ("this is NULL"));
00522 CHAIN_ITER list_iter((CHAIN*)this);
00523 return list_iter.Len();
00524 }
00525
00526 CHAIN_NODE *
00527 CHAIN_ITER::First(void)
00528 {
00529 Is_True(this, ("this is NULL"));
00530 _cur=this->_list->Head();
00531 _idx = 0;
00532
00533 return _cur;
00534 }
00535
00536 CHAIN_NODE *
00537 CHAIN_ITER::Last(void)
00538 {
00539 Is_True(this, ("this is NULL"));
00540 _idx = Len();
00541 _cur=this->_list->Tail();
00542
00543 return _cur;
00544 }
00545
00546 CHAIN_NODE *
00547 CHAIN_ITER::Next(void)
00548 {
00549 Is_True(this, ("this is NULL"));
00550 if (_cur != NULL) {
00551 _cur = _cur->Next();
00552 _idx++;
00553 }
00554 return _cur;
00555 }
00556
00557 CHAIN_NODE *
00558 CHAIN_ITER::Prev(void)
00559 {
00560 Is_True(this, ("this is NULL"));
00561 if (_cur != NULL) {
00562 _cur = _cur->Prev();
00563 _idx--;
00564 }
00565 return _cur;
00566 }
00567
00568 CHAIN_NODE *
00569 CHAIN_ITER::Nth(INT n)
00570 {
00571 Is_True(this, ("this is NULL"));
00572
00573 Len();
00574
00575 if (n >= _len) return NULL;
00576
00577 _cur=_list->Head();
00578 _idx=0;
00579 while (_idx !=n){
00580 _cur=_cur->_next;
00581 _idx++;
00582 }
00583 return _cur;
00584 }
00585
00586 CHAIN_NODE *
00587 CHAIN_ITER::Last_Nth(INT n)
00588 {
00589 Is_True(this, ("this is NULL"));
00590
00591 Len();
00592
00593 if (n >= _len) return NULL;
00594
00595 _cur=_list->Tail();
00596 _idx=_len-1;
00597 while (_idx != _len-1-n){
00598 _cur=_cur->_prev;
00599 _idx--;
00600 }
00601 return _cur;
00602 }
00603
00604 INT32
00605 CHAIN_ITER::Len(void)
00606 {
00607 Is_True(this, ("this is NULL"));
00608
00609 if (_len == -1) {
00610 _len=0;
00611 _cur=_list->Head();
00612 while (_cur){
00613 _cur=_cur->_next;
00614 _len++;
00615 }
00616 }
00617 return _len;
00618 }
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633 CLIST_NODE *
00634 CLIST_NODE::Find_Next( void ) const
00635 {
00636 for ( const CLIST_NODE *cn = this; cn != NULL; cn = cn->Next() ) {
00637 if ( cn->Next() == this )
00638 return (CLIST_NODE *)cn;
00639 }
00640
00641 ErrMsg( EC_Unimplemented, "CLIST_NODE::Find_Next: invalid list" );
00642 return NULL;
00643 }
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653 CLIST_NODE *
00654 CLIST_NODE::Insert_Before( CLIST_NODE *nd )
00655 {
00656 if ( this == NULL ) {
00657 nd->Set_Next(nd);
00658 return nd;
00659 }
00660 else {
00661
00662 CLIST_NODE *next_is_this = Find_Next();
00663
00664 nd->Set_Next(this);
00665 next_is_this->Set_Next(nd);
00666 return nd;
00667 }
00668 }
00669
00670
00671
00672
00673
00674
00675
00676 CLIST_NODE*
00677 CLIST_NODE::Remove( CLIST_NODE *prev )
00678 {
00679 if ( prev != NULL )
00680 prev->Set_Next(this->Next());
00681 _next = NULL;
00682 return this;
00683 }
00684
00685
00686
00687
00688
00689
00690
00691
00692 INT32
00693 CLIST_NODE::Len( void ) const
00694 {
00695 if ( this == NULL ) {
00696 return 0;
00697 }
00698 else {
00699 INT32 len = 1;
00700 for ( const CLIST_NODE *cn = this->Next();
00701 cn != this;
00702 cn = cn->Next() )
00703 {
00704 len++;
00705 }
00706
00707 return len;
00708 }
00709 }
00710
00711
00712
00713
00714
00715
00716
00717 void
00718 CLIST::Init( CLIST_NODE *list )
00719 {
00720 if ( this == NULL )
00721 return;
00722
00723 _head = list;
00724 for ( _tail = list;
00725 _tail != NULL && _tail->Next() != _head;
00726 _tail = _tail->Next() )
00727 ;
00728
00729 FmtAssert( _tail != NULL || list == NULL,
00730 ("CLIST::Init: Invalid list") );
00731 }
00732
00733
00734
00735
00736
00737
00738
00739
00740 void
00741 CLIST::Append( CLIST_NODE *nd )
00742 {
00743 if ( this == NULL || nd == NULL )
00744 return;
00745
00746 if (_head == NULL) {
00747 Init( nd );
00748 }
00749 else {
00750 _tail->Insert_After(nd);
00751 _tail = _tail->Next();
00752 }
00753 }
00754
00755
00756
00757
00758
00759
00760
00761 BOOL
00762 CLIST::Append( CLIST_NODE *nd, CLIST_NODE *od)
00763 {
00764 if (this == NULL) return FALSE;
00765 if (nd == NULL) return FALSE;
00766
00767 if ( od == NULL && _head == NULL) {
00768 _head = _tail = nd;
00769 return TRUE;
00770 }
00771
00772
00773 if ( _tail == od ) {
00774 od->Insert_After(nd);
00775 _tail = nd;
00776 return TRUE;
00777 }
00778
00779
00780 for ( CLIST_NODE* tmp = _tail->Next();
00781 tmp != NULL && tmp != _tail;
00782 tmp = tmp->Next() )
00783 {
00784 if (tmp == od) {
00785 od->Insert_After(nd);
00786 return TRUE;
00787 }
00788 }
00789
00790 return FALSE;
00791 }
00792
00793
00794
00795
00796
00797
00798
00799 void
00800 CLIST::Prepend( CLIST_NODE *nd )
00801 {
00802 if (this == NULL) return;
00803 if (nd == NULL) return;
00804
00805
00806 if (_head == NULL)
00807 _head = _tail = nd;
00808 else {
00809 _tail->Insert_After(nd);
00810 _head = _tail->Next();
00811 }
00812 }
00813
00814
00815
00816
00817
00818
00819
00820 BOOL
00821 CLIST::Prepend( CLIST_NODE *nd, CLIST_NODE *od )
00822 {
00823 if (this == NULL) return FALSE;
00824 if (nd == NULL) return FALSE;
00825
00826 if ( od == NULL && _head == NULL) {
00827 _head = _tail = nd;
00828 return TRUE;
00829 }
00830
00831
00832 if ( _head == od ) {
00833 _tail->Insert_After(nd);
00834 _head = nd;
00835 return TRUE;
00836 }
00837
00838
00839 CLIST_NODE *precedes = _head;
00840 for ( CLIST_NODE* tmp = _head->Next();
00841 tmp != NULL && tmp != _head;
00842 tmp = tmp->Next(), precedes = precedes->Next() )
00843 {
00844 if (tmp == od) {
00845 precedes->Insert_After(nd);
00846 return TRUE;
00847 }
00848 }
00849
00850 return FALSE;
00851 }
00852
00853
00854
00855
00856
00857
00858
00859 void
00860 CLIST::Append_List(CLIST *new_list)
00861 {
00862 if (this == NULL) return;
00863
00864
00865 CLIST_NODE* nlh = new_list->Head();
00866 CLIST_NODE* nlt = new_list->Tail();
00867
00868 if ( nlh == NULL ) {
00869 return;
00870 }
00871 else if (_head == NULL) {
00872 _head = nlh;
00873 _tail = nlt;
00874 }
00875 else {
00876 _tail->Set_Next(nlh);
00877 nlt->Set_Next(_head);
00878 _tail = nlt;
00879 }
00880 }
00881
00882
00883
00884
00885
00886
00887
00888 void
00889 CLIST::Prepend_List(CLIST *new_list)
00890 {
00891 if (this == NULL) return;
00892
00893
00894 CLIST_NODE* nlh = new_list->Head();
00895 CLIST_NODE* nlt = new_list->Tail();
00896
00897 if ( nlh == NULL ) {
00898 return;
00899 }
00900 else if (_head == NULL) {
00901 _head = nlh;
00902 _tail = nlt;
00903 }
00904 else {
00905 nlt->Set_Next(_head);
00906 _tail->Set_Next(nlh);
00907 _head = nlh;
00908 }
00909 }
00910
00911
00912
00913
00914
00915
00916
00917 CLIST_NODE*
00918 CLIST::Remove_Headnode(void)
00919 {
00920 CLIST_NODE* rv;
00921
00922 if (this == NULL)
00923 return NULL;
00924
00925 rv = _head;
00926 if (_head != NULL) {
00927 if ( _head == _tail ) {
00928 _head = _tail = NULL;
00929 }
00930 else {
00931 _head = _head->Next();
00932 _tail->Set_Next(_head);
00933 }
00934 }
00935 rv->_next = NULL;
00936 return rv;
00937 }
00938
00939
00940
00941
00942
00943
00944
00945 INT32
00946 CLIST::Len(void) const
00947 {
00948 if (this == NULL) {
00949 return 0;
00950 }
00951 else {
00952 return _head->Len();
00953 }
00954 }
00955
00956
00957
00958
00959
00960
00961
00962 CLIST_NODE *
00963 CLIST_ITER::First(void)
00964 {
00965 if (this == NULL) return NULL;
00966
00967 _cur = _head;
00968 _saw_head = FALSE;
00969
00970 return _cur;
00971 }
00972
00973
00974
00975
00976
00977
00978
00979 CLIST_NODE *
00980 CLIST_ITER::Next(void)
00981 {
00982 if (this == NULL) return NULL;
00983
00984 if ( _cur != NULL )
00985 _cur = _cur->Next();
00986
00987 _saw_head = TRUE;
00988
00989 return _cur;
00990 }
00991
00992
00993
00994
00995
00996
00997
00998 INT32
00999 CLIST_ITER::Len(void) const
01000 {
01001 if (this == NULL || _head == NULL)
01002 return 0;
01003
01004 return _head->Len();
01005 }
01006