Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
cxx_base.cxx
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 //-*-c++-*-
00037 // ====================================================================
00038 // ====================================================================
00039 //
00040 //
00041 // Revision history:
00042 //  18-SEP-94 shin - Original Version
00043 //  30-SEP-94 dkchen - Added doubly linked list
00044 //
00045 // Description:
00046 //
00047 // ====================================================================
00048 // ====================================================================
00049 //
00050 
00051 #include "defs.h"
00052 #include "errors.h"
00053 #include "erglob.h"     // for error messages
00054 
00055 #include "cxx_base.h"
00056 
00057 SLIST_NODE*
00058 SLIST_NODE::Remove(SLIST_NODE *prev)
00059 {
00060   Is_True(this, ("SLIST_NODE::Remove(): this is NULL"));
00061 
00062   if (prev != NULL) 
00063     prev->_next = _next;
00064 
00065   _next = NULL;
00066   return this;
00067 }
00068 
00069 INT32
00070 SLIST_NODE::Len(void) const
00071 {
00072   INT i;
00073   const SLIST_NODE *p;
00074   for (i = 0, p = this; p != NULL; i++, p = p->Next());
00075   return i;
00076 }
00077 
00078 INT
00079 SLIST_NODE::Pos(SLIST_NODE *nd) const
00080 {
00081   const SLIST_NODE *cur = this;
00082   INT pos = 0;
00083   while (cur) {
00084     if (nd == cur) 
00085       return pos;
00086     cur = cur->Next();
00087     pos++;
00088   }
00089   return -1;
00090 }
00091 
00092 SLIST::SLIST( SLIST_NODE *list)
00093 {
00094   Is_True(this, ("SLIST::SLIST(): this is NULL"));
00095 
00096   _head = _tail = list;
00097   while (_tail && _tail->_next != NULL)
00098     _tail = _tail->_next;
00099 }  
00100 
00101 void
00102 SLIST::Init( SLIST_NODE *list)
00103 {
00104   Is_True(this, ("SLIST::Init(): this is NULL"));
00105 
00106   _head = _tail = list;
00107   while (_tail && _tail->_next != NULL)
00108     _tail = _tail->_next;
00109 }  
00110 
00111 BOOL
00112 SLIST::Append( SLIST_NODE *nd, SLIST_NODE *od)
00113 {
00114   Is_True(this, ("SLIST::Append(): this is NULL"));
00115 
00116   if (nd == NULL) return FALSE;
00117 
00118   if ( od == NULL && _head == NULL) {
00119     _head = _tail = nd;
00120     return TRUE;
00121   }
00122 
00123   // append nd after od
00124   if ( _tail == od ) {
00125     od->Insert_After(nd);
00126     _tail = nd;
00127     return TRUE;
00128   }
00129   // make sure od is in the list
00130   for (SLIST_NODE* tmp = _head; tmp != NULL; tmp = tmp->_next ) {
00131     if (tmp == od) {
00132       od->Insert_After(nd);
00133       return TRUE;
00134     }
00135   }
00136 
00137   return FALSE;
00138 }
00139 
00140 BOOL
00141 SLIST::Prepend( SLIST_NODE *nd, SLIST_NODE *od )
00142 {
00143   Is_True(this, ("SLIST::Prepend(): this is NULL"));
00144 
00145   if (nd == NULL) return FALSE;
00146 
00147   if (od == NULL && _head == NULL)
00148     _head = _tail = nd;
00149 
00150   // insert nd before od
00151   if ( _head == od ) {
00152     _head = od->Insert_Before(nd);
00153     return TRUE;
00154   }
00155   // make sure od is in the list
00156   for (SLIST_NODE* tmp = _head; tmp->_next != NULL; tmp = tmp->_next ) {
00157     if (tmp->_next == od) {
00158       tmp->_next = od->Insert_Before(nd);
00159       return TRUE;
00160     }
00161   }
00162   return FALSE;
00163 }
00164 
00165 void
00166 SLIST::Append_List(SLIST *new_list)
00167 {
00168   Is_True(this, ("SLIST::Append_List(): this is NULL"));
00169 
00170   // merge new_list into this
00171   SLIST_NODE* tmp = new_list->Head();
00172   SLIST_NODE* tmp2 = new_list->Tail();
00173 
00174   if (_head == NULL) {
00175     _head = tmp;
00176     _tail = tmp2;
00177   }
00178   else if (tmp2 != NULL) {
00179     _tail->_next = tmp;
00180     _tail = tmp2;
00181   }
00182 }
00183 
00184 void
00185 SLIST::Prepend_List(SLIST *new_list)
00186 {
00187   Is_True(this, ("SLIST::Prepend_List(): this is NULL"));
00188 
00189   // Prepend new_list into at the "_head" of the list
00190   SLIST_NODE* tmp = new_list->Head();
00191   SLIST_NODE* tmp2 = new_list->Tail();
00192 
00193   if (_head == NULL) {
00194     _head = tmp;
00195     _tail = tmp2;
00196   }
00197   else if (tmp2 != NULL) {
00198     tmp2->_next = _head;
00199     _head = tmp;
00200   }
00201 }
00202 
00203 SLIST_NODE*
00204 SLIST::Remove_Headnode(void)
00205 {
00206   Is_True(this, ("SLIST::Remove_Headnode() called with NULL this"));
00207 
00208   if (_head == NULL)
00209     return NULL;
00210 
00211   SLIST_NODE *old_head = _head;
00212   _head = _head->_next;
00213   if (_head == NULL)
00214     _tail = NULL;
00215 
00216   old_head->_next = NULL;
00217   return old_head;
00218 }
00219 
00220 SLIST_NODE*
00221 SLIST::Remove(SLIST_NODE *prev, SLIST_NODE *cur)
00222 {
00223   Is_True(this, ("SLIST::Remove() called with NULL this"));
00224 
00225   if (prev == NULL) {
00226     Is_True(cur == _head, ("cur is not head, but prev is null"));
00227     return Remove_Headnode();
00228   }
00229   else {
00230     Is_True(prev->_next == cur, ("prev does not point to cur"));
00231     prev->_next = cur->_next;
00232     if (prev->_next == NULL)
00233       _tail = prev;
00234   }
00235 
00236   cur->_next = NULL;
00237   return cur;
00238 }
00239 
00240 void
00241 SLIST::Remove_node(SLIST_NODE *slist_node)
00242 {
00243   Is_True(this, ("SLIST::Remove_node() called with NULL this"));
00244   SLIST_NODE *prev = NULL;
00245 
00246   // found previous node
00247   SLIST_NODE* tmp;
00248   for (tmp = Head(); tmp != NULL; tmp = tmp->_next) {
00249     if (tmp == slist_node) break;
00250     prev = tmp;
00251   }
00252   Is_True(tmp == slist_node, ("SLIST::Remove_node() cannot delete node from SLIST."));
00253 
00254   if (prev != NULL)
00255     prev->Set_Next(slist_node->Next());
00256 
00257   if (slist_node == Head())
00258     Set_Head(slist_node->Next());
00259 
00260   if (slist_node == Tail())
00261     Set_Tail(prev);
00262 
00263   slist_node->Set_Next(NULL);
00264 }
00265 
00266 INT32
00267 SLIST::Len(void) const
00268 {
00269   Is_True(this, ("SLIST::Len(): this is NULL"));
00270 
00271   SLIST_ITER lst_iter(_head);
00272   return lst_iter.Len();
00273 }
00274 
00275 
00276 SLIST_NODE *
00277 SLIST_ITER::Nth(INT n)
00278 {
00279   Is_True(this, ("SLIST_Iter::Nth(): this is NULL"));
00280 
00281   Len();
00282 
00283   if (n >= _len) return NULL;
00284   
00285   if (n < _idx) First();
00286   for (; n != _idx; Next());
00287   return _cur;
00288 }
00289 
00290 INT32
00291 SLIST_ITER::Len(void)
00292 {
00293   Is_True(this, ("SLIST_Iter::Len(): this is NULL"));
00294 
00295   if (_len == -1) { // initialize the field
00296     const SLIST_NODE *tmp;
00297     for (tmp = _head, _len = 0; tmp != NULL; tmp = tmp->Next())
00298       _len++;
00299   }
00300   return _len;
00301 }
00302 
00303 CHAIN_NODE*
00304 CHAIN_NODE::Insert_After(CHAIN_NODE *nd)
00305 {
00306   Is_True(this, ("this is NULL"));
00307 
00308   if (nd != NULL) {
00309     if (_next != NULL)
00310       _next->_prev=nd;
00311     nd->_next = _next;
00312     _next=nd;
00313     nd->_prev=this;
00314   }
00315   return nd;
00316 }
00317 
00318 CHAIN_NODE*
00319 CHAIN_NODE::Insert_Before(CHAIN_NODE *nd)
00320 {
00321   Is_True(this, ("this is NULL"));
00322 
00323   if (nd != NULL) {
00324     if (_prev != NULL)
00325       _prev->_next=nd;
00326     nd->_prev = _prev;
00327     _prev=nd;
00328     nd->_next=this;
00329   }
00330   return nd;
00331 }
00332 
00333 CHAIN_NODE*
00334 CHAIN_NODE::Remove(void)
00335 {
00336   Is_True(this, ("this is NULL"));
00337 
00338   if (_prev != NULL) 
00339     _prev->_next = _next;
00340   if (_next != NULL) 
00341     _next->_prev = _prev;
00342 
00343   _next = NULL;
00344   _prev = NULL;
00345 
00346   return this;
00347 }
00348 
00349 void
00350 CHAIN::Append( CHAIN_NODE *nd )
00351 {
00352   Is_True(this, ("this is NULL"));
00353 
00354   if (_head == NULL)
00355     _head = _tail = nd;
00356   else
00357     _tail = _tail->Insert_After(nd);
00358 }
00359 
00360 void
00361 CHAIN::Prepend( CHAIN_NODE *nd )
00362 {
00363   Is_True(this, ("this is NULL"));
00364 
00365   if (_head == NULL)
00366     _head = _tail = nd;
00367   else {
00368     _head = _head->Insert_Before(nd);
00369   }
00370 }
00371 
00372 void
00373 CHAIN::Insert_After(CHAIN_NODE *nd, CHAIN_NODE *after_nd)
00374 { 
00375   Is_True(this, ("this is NULL"));
00376 
00377   if (_head == NULL)
00378     _head = _tail = nd;
00379   else if (after_nd == _tail)
00380     _tail = _tail->Insert_After(nd);
00381   else
00382     after_nd->Insert_After(nd);
00383 }
00384 
00385 void
00386 CHAIN::Insert_Before(CHAIN_NODE *nd, CHAIN_NODE *before_nd)
00387 {
00388   Is_True(this, ("this is NULL"));
00389 
00390   if (_head == NULL)
00391     _head = _tail = nd;
00392   else if (before_nd == _head)
00393     _head = _head->Insert_Before(nd);
00394   else
00395     before_nd->Insert_Before(nd);
00396 }
00397 
00398 BOOL
00399 CHAIN::Is_Member( CHAIN_NODE *nd ) const
00400 {
00401   Is_True(this, ("this is NULL"));
00402 
00403   if (nd == NULL)
00404     return FALSE;
00405   
00406   for (CHAIN_NODE* tmp = _head; tmp != NULL; tmp = tmp->_next ) 
00407     if (tmp == nd)
00408       return TRUE;
00409 
00410   return FALSE;
00411 }
00412 
00413 void
00414 CHAIN::Append_List(CHAIN *new_list)
00415 {
00416   Is_True(this, ("this is NULL"));
00417 
00418   if (new_list == NULL)
00419     return;
00420 
00421   if (_head == NULL) {
00422     Init(new_list);
00423     return;
00424   }
00425 
00426   _tail->_next = new_list->_head;
00427   _tail->_next->_prev = _tail;
00428 
00429   _tail = new_list->_tail;
00430 }
00431 
00432 void
00433 CHAIN::Prepend_List(CHAIN *new_list)
00434 {
00435   Is_True(this, ("this is NULL"));
00436 
00437   if (new_list == NULL)
00438     return;
00439 
00440   if (_head == NULL) {
00441     Init(new_list);
00442     return;
00443   }
00444 
00445   _head->_prev = new_list->_tail;
00446   _head->_prev->_next = _head;
00447 
00448   _head = new_list->_head;
00449 
00450 }
00451 
00452 // remove the element from the chain.  No check is made to see if it
00453 // is in the chain.
00454 void
00455 CHAIN::Remove( CHAIN_NODE *nd )
00456 {
00457   Is_True(this, ("this is NULL"));
00458 
00459   if (nd == NULL)
00460     return;
00461 
00462   if (nd == _head)
00463     _head = _head->_next;
00464 
00465   if (nd == _tail)
00466     _tail = _tail->_prev;
00467 
00468   nd->Remove();
00469 }
00470 
00471 CHAIN_NODE*
00472 CHAIN::Remove_Head(void)
00473 {
00474   Is_True(this, ("this is NULL"));
00475 
00476   CHAIN_NODE* nd;
00477 
00478   if (_head == NULL)
00479     nd = NULL;
00480   else if (_head == _tail) {
00481     nd = _head->Remove();
00482     _head = _tail = NULL;
00483   }
00484   else {
00485     _head = _head->_next;
00486     nd = _head->_prev->Remove();
00487     _head->_prev = NULL;
00488   }
00489   return nd;
00490 }
00491 
00492 CHAIN_NODE*
00493 CHAIN::Remove_Tail(void)
00494 {
00495   Is_True(this, ("this is NULL"));
00496 
00497   CHAIN_NODE* nd;
00498 
00499   if (_tail == NULL)
00500     nd = NULL;
00501   else if (_head == _tail) {
00502     nd = _head->Remove();
00503     _head=_tail=NULL;
00504   }
00505   else {
00506     _tail = _tail->_prev;
00507     nd = _tail->_next->Remove();
00508     _tail->_next=NULL;
00509   }
00510   return nd;
00511 }
00512 
00513 INT32
00514 CHAIN::Len(void) const
00515 {
00516   Is_True(this, ("this is NULL"));
00517   CHAIN_ITER list_iter((CHAIN*)this);
00518   return list_iter.Len();
00519 }
00520 
00521 CHAIN_NODE *
00522 CHAIN_ITER::First(void)
00523 {
00524   Is_True(this, ("this is NULL"));
00525   _cur=this->_list->Head();
00526   _idx = 0;
00527 
00528   return _cur;
00529 }
00530 
00531 CHAIN_NODE *
00532 CHAIN_ITER::Last(void)
00533 {
00534   Is_True(this, ("this is NULL"));
00535   _idx = Len();
00536   _cur=this->_list->Tail();
00537 
00538   return _cur;
00539 }
00540 
00541 CHAIN_NODE *
00542 CHAIN_ITER::Next(void)
00543 {
00544   Is_True(this, ("this is NULL"));
00545   if (_cur != NULL) {
00546     _cur = _cur->Next();
00547     _idx++;
00548   }
00549   return _cur;
00550 }
00551 
00552 CHAIN_NODE *
00553 CHAIN_ITER::Prev(void)
00554 {
00555   Is_True(this, ("this is NULL"));
00556   if (_cur != NULL) {
00557     _cur = _cur->Prev();
00558     _idx--;
00559   }
00560   return _cur;
00561 }
00562 
00563 CHAIN_NODE *
00564 CHAIN_ITER::Nth(INT n)
00565 {
00566   Is_True(this, ("this is NULL"));
00567 
00568   Len();
00569 
00570   if (n >= _len) return NULL;
00571 
00572   _cur=_list->Head();
00573   _idx=0;
00574   while (_idx !=n){
00575     _cur=_cur->_next;
00576     _idx++;
00577   }
00578   return _cur;
00579 }
00580 
00581 CHAIN_NODE *
00582 CHAIN_ITER::Last_Nth(INT n)
00583 {
00584   Is_True(this, ("this is NULL"));
00585 
00586   Len();
00587 
00588   if (n >= _len) return NULL;
00589 
00590   _cur=_list->Tail();
00591   _idx=_len-1;
00592   while (_idx != _len-1-n){
00593     _cur=_cur->_prev;
00594     _idx--;
00595   }
00596   return _cur;
00597 }
00598 
00599 INT32
00600 CHAIN_ITER::Len(void)
00601 {
00602   Is_True(this, ("this is NULL"));
00603 
00604   if (_len == -1) { // initialize the field
00605     _len=0;
00606     _cur=_list->Head();
00607     while (_cur){
00608       _cur=_cur->_next;
00609       _len++;
00610     }
00611   }
00612   return _len;
00613 }
00614 
00615 // ==================================================================
00616 // ==================================================================
00617 //
00618 // Circularly Linked Lists
00619 //
00620 // ==================================================================
00621 // ==================================================================
00622 
00623 // ==================================================================
00624 // CLIST_NODE::Find_Next just searches the circularly-linked list for
00625 // the node whose '_next' field points to 'this'
00626 // ==================================================================
00627 
00628 CLIST_NODE *
00629 CLIST_NODE::Find_Next( void ) const
00630 {
00631   for ( const CLIST_NODE *cn = this; cn != NULL; cn = cn->Next() ) {
00632     if ( cn->Next() == this )
00633       return (CLIST_NODE *)cn;  // need to cast a const * to non-const
00634   }
00635 
00636   ErrMsg( EC_Unimplemented, "CLIST_NODE::Find_Next: invalid list" );
00637   return NULL;
00638 }
00639 
00640 // ==================================================================
00641 // CLIST_NODE::Insert_Before
00642 //
00643 // Inserts a node "nd" before "this" node.  Please NOTE that this 
00644 // method is inefficient time-wise because of the need to search for 
00645 // the node that has "this" as its "next" node.
00646 // ==================================================================
00647 
00648 CLIST_NODE *
00649 CLIST_NODE::Insert_Before( CLIST_NODE *nd )
00650 {
00651   if ( this == NULL ) {
00652     nd->Set_Next(nd);   // circ list contains this single node
00653     return nd;
00654   }
00655   else {
00656     // need to go through the list to find which one points to 'this'
00657     CLIST_NODE *next_is_this = Find_Next();
00658 
00659     nd->Set_Next(this);
00660     next_is_this->Set_Next(nd);
00661     return nd;
00662   }
00663 }
00664 
00665 // ==================================================================
00666 // CLIST_NODE::Remove
00667 //
00668 // Removes "this" node from the list, returning it
00669 // ==================================================================
00670 
00671 CLIST_NODE*
00672 CLIST_NODE::Remove( CLIST_NODE *prev )
00673 {
00674   if ( prev != NULL )
00675     prev->Set_Next(this->Next());
00676   _next = NULL;
00677   return this;
00678 }
00679 
00680 
00681 // ==================================================================
00682 // CLIST_NODE::Len
00683 //
00684 // Returns the number of elements in the list
00685 // ==================================================================
00686 
00687 INT32
00688 CLIST_NODE::Len( void ) const
00689 {
00690   if ( this == NULL ) {
00691     return 0;
00692   }
00693   else {
00694     INT32 len = 1;
00695     for ( const CLIST_NODE *cn = this->Next(); 
00696           cn != this; 
00697           cn = cn->Next() )
00698     {
00699       len++;
00700     }
00701 
00702     return len;
00703   }
00704 }
00705 
00706 // ==================================================================
00707 // CLIST::Init
00708 //
00709 // Initialize a CLIST by finding the head/tail of the given list
00710 // ==================================================================
00711 
00712 void
00713 CLIST::Init( CLIST_NODE *list )
00714 {
00715   if ( this == NULL )
00716     return;
00717 
00718   _head = list;
00719   for ( _tail = list; 
00720         _tail != NULL && _tail->Next() != _head; 
00721         _tail = _tail->Next() )
00722     ; // do nothing, except loop until we find the head
00723 
00724   FmtAssert( _tail != NULL || list == NULL,
00725     ("CLIST::Init: Invalid list") );
00726 }
00727 
00728 // ==================================================================
00729 // CLIST::Append
00730 //
00731 // Appends node "nd" to the list so it is at the "end" of the list, and
00732 // therefore precedes the head
00733 // ==================================================================
00734 
00735 void
00736 CLIST::Append( CLIST_NODE *nd )
00737 {
00738   if ( this == NULL || nd == NULL ) 
00739     return;
00740 
00741   if (_head == NULL) {
00742     Init( nd );
00743   }
00744   else {
00745     _tail->Insert_After(nd);
00746     _tail = _tail->Next();
00747   }
00748 }
00749 
00750 // ==================================================================
00751 // CLIST::Append
00752 //
00753 // Appends node "nd" after the node "od" in the list
00754 // ==================================================================
00755 
00756 BOOL
00757 CLIST::Append( CLIST_NODE *nd, CLIST_NODE *od)
00758 {
00759   if (this == NULL) return FALSE;
00760   if (nd == NULL) return FALSE;
00761 
00762   if ( od == NULL && _head == NULL) {
00763     _head = _tail = nd;
00764     return TRUE;
00765   }
00766 
00767   // append nd after od
00768   if ( _tail == od ) {
00769     od->Insert_After(nd);
00770     _tail = nd;
00771     return TRUE;
00772   }
00773 
00774   // make sure od is in the list
00775   for ( CLIST_NODE* tmp = _tail->Next(); 
00776         tmp != NULL && tmp != _tail; 
00777         tmp = tmp->Next() )
00778   {
00779     if (tmp == od) {
00780       od->Insert_After(nd);
00781       return TRUE;
00782     }
00783   }
00784 
00785   return FALSE;
00786 }
00787 
00788 // ==================================================================
00789 // CLIST::Prepend
00790 //
00791 // Inserts node "nd" at the "_head" of the list
00792 // ==================================================================
00793 
00794 void
00795 CLIST::Prepend( CLIST_NODE *nd )
00796 {
00797   if (this == NULL) return;
00798   if (nd == NULL) return;
00799 
00800   // insert nd to beginning of list
00801   if (_head == NULL)
00802     _head = _tail = nd;
00803   else {
00804     _tail->Insert_After(nd);
00805     _head = _tail->Next();
00806   }
00807 }
00808 
00809 // ==================================================================
00810 // CLIST::Prepend
00811 //
00812 // Inserts node "nd" before the node "od" in the list
00813 // ==================================================================
00814 
00815 BOOL
00816 CLIST::Prepend( CLIST_NODE *nd, CLIST_NODE *od )
00817 {
00818   if (this == NULL) return FALSE;
00819   if (nd == NULL) return FALSE;
00820 
00821   if ( od == NULL && _head == NULL) {
00822     _head = _tail = nd;
00823     return TRUE;
00824   }
00825 
00826   // prepend nd before od
00827   if ( _head == od ) {
00828     _tail->Insert_After(nd);    // can insert after tail which is
00829     _head = nd;                 //   the same as before head
00830     return TRUE;
00831   }
00832 
00833   // make sure od is in the list
00834   CLIST_NODE *precedes = _head;
00835   for ( CLIST_NODE* tmp = _head->Next(); 
00836         tmp != NULL && tmp != _head; 
00837         tmp = tmp->Next(), precedes = precedes->Next() )
00838   {
00839     if (tmp == od) {
00840       precedes->Insert_After(nd);       // insert after one that
00841       return TRUE;                      // precedes it
00842     }
00843   }
00844 
00845   return FALSE;
00846 }
00847 
00848 // ==================================================================
00849 // CLIST::Append_List
00850 //
00851 // Appends the "new_list" at the "_tail" of the list
00852 // ==================================================================
00853 
00854 void
00855 CLIST::Append_List(CLIST *new_list)
00856 {
00857   if (this == NULL) return;
00858 
00859   // merge new_list into this
00860   CLIST_NODE* nlh = new_list->Head();
00861   CLIST_NODE* nlt = new_list->Tail();
00862 
00863   if ( nlh == NULL ) {
00864     return;             // nothing to append
00865   }
00866   else if (_head == NULL) {
00867     _head = nlh;        // the new list is the only list
00868     _tail = nlt;
00869   }
00870   else {
00871     _tail->Set_Next(nlh);       // need to append to existing list
00872     nlt->Set_Next(_head);
00873     _tail = nlt;
00874   }
00875 }
00876 
00877 // ==================================================================
00878 // CLIST::Prepend_List
00879 //
00880 // Prepends the "new_list" at the "_head" of the list
00881 // ==================================================================
00882 
00883 void
00884 CLIST::Prepend_List(CLIST *new_list)
00885 {
00886   if (this == NULL) return;
00887 
00888   // Prepend new_list into at the "_head" of the list
00889   CLIST_NODE* nlh = new_list->Head();
00890   CLIST_NODE* nlt = new_list->Tail();
00891 
00892   if ( nlh == NULL ) {
00893     return;             // nothing to prepend
00894   }
00895   else if (_head == NULL) {
00896     _head = nlh;        // the new list is the only list
00897     _tail = nlt;
00898   }
00899   else {
00900     nlt->Set_Next(_head);       // need to prepend to existing list
00901     _tail->Set_Next(nlh);
00902     _head = nlh;
00903   }
00904 }
00905 
00906 // ==================================================================
00907 // CLIST::Remove_Headnode
00908 //
00909 // Removes the head of the list, if any, returning it
00910 // ==================================================================
00911 
00912 CLIST_NODE*
00913 CLIST::Remove_Headnode(void)
00914 {
00915   CLIST_NODE* rv;
00916 
00917   if (this == NULL)
00918     return NULL;
00919 
00920   rv = _head;
00921   if (_head != NULL) {
00922     if ( _head == _tail ) {
00923       _head = _tail = NULL;
00924     }
00925     else {
00926       _head = _head->Next();
00927       _tail->Set_Next(_head);
00928     }
00929   }
00930   rv->_next = NULL;
00931   return rv;
00932 }
00933 
00934 // ==================================================================
00935 // CLIST::Len
00936 //
00937 // Returns the number of items in the list
00938 // ==================================================================
00939 
00940 INT32
00941 CLIST::Len(void) const
00942 {
00943   if (this == NULL) {
00944     return 0;
00945   }
00946   else {
00947     return _head->Len();
00948   }
00949 }
00950 
00951 // ==================================================================
00952 // CLIST_ITER::First
00953 //
00954 // Gets the first element "_head" and set "_cur" pointer
00955 // ==================================================================
00956 
00957 CLIST_NODE * 
00958 CLIST_ITER::First(void)
00959 {
00960   if (this == NULL) return NULL;
00961 
00962   _cur = _head;
00963   _saw_head = FALSE;
00964 
00965   return _cur;
00966 }
00967 
00968 // ==================================================================
00969 // CLIST_ITER::Next
00970 //
00971 // Gets the next element and bumps the current pointer
00972 // ==================================================================
00973 
00974 CLIST_NODE * 
00975 CLIST_ITER::Next(void)
00976 {
00977   if (this == NULL) return NULL;
00978 
00979   if ( _cur != NULL )
00980     _cur = _cur->Next();
00981 
00982   _saw_head = TRUE;
00983 
00984   return _cur;
00985 }
00986 
00987 // ==================================================================
00988 // CLIST_ITER::Len
00989 //
00990 // Gets the number of elements in the list
00991 // ==================================================================
00992 
00993 INT32
00994 CLIST_ITER::Len(void) const
00995 {
00996   if (this == NULL || _head == NULL) 
00997     return 0;
00998 
00999   return _head->Len();
01000 }
01001 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines