home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / CLOBSS.PAK / LIST.CPO < prev    next >
Text File  |  1995-08-29  |  3KB  |  107 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  LIST.CPP                                                              */
  4. /*                                                                        */
  5. /*  Copyright Borland International 1991, 1993                            */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __RESOURCE_H )
  11. #include "classlib\resource.h"
  12. #endif  // __RESOURCE_H
  13.  
  14. #if !defined( __LIST_H )
  15. #include "classlib\obsolete\list.h"
  16. #endif  // __LIST_H
  17.  
  18. unsigned ListBlockInitializer::count = 0;
  19.  
  20. MemBlocks *List::ListElement::mgr = 0;
  21.  
  22. void List::add( Object& toAdd )
  23. {
  24.     ListElement *newElement = new ListElement( &toAdd, head->next );
  25.     CHECK( newElement != 0 );
  26.     head->next = newElement;
  27.     itemsInContainer++;
  28. }
  29.  
  30. List::ListElement *List::findPred( const Object& o )
  31. {
  32.     tail->data = (Object *)&o;
  33.     ListElement *cursor = head;
  34.     while( o != *(cursor->next->data) )
  35.         cursor = cursor->next;
  36.     tail->data = 0;
  37.     return cursor;
  38. }
  39.  
  40. void List::detach( Object& toDetach, DeleteType dt )
  41. {
  42.     ListElement *pred = findPred( toDetach );
  43.     ListElement *item = pred->next;
  44.     if( delObj(dt) && pred->next != tail )
  45.         delete item->data;
  46.     pred->next = pred->next->next;
  47.     if( item != tail )
  48.         {
  49.         itemsInContainer--;
  50.         delete item;
  51.         }
  52. }
  53.  
  54. void List::flush( DeleteType dt )
  55. {
  56.     ListElement *current = head->next;
  57.     while( current != tail )
  58.         {
  59.         ListElement *temp = current;
  60.         current = current->next;
  61.         if( delObj(dt) )
  62.             delete temp->data;
  63.         delete temp;
  64.         }
  65.     head->next = tail;
  66.     itemsInContainer = 0;
  67. }
  68.  
  69. ContainerIterator& List::initIterator() const
  70. {
  71.     return *( (ContainerIterator *)new ListIterator( *this ) );
  72. }
  73.  
  74. ListIterator::~ListIterator()
  75. {
  76. }
  77.  
  78. ListIterator::operator int()
  79. {
  80.     return currentElement->next != currentElement;
  81. }
  82.  
  83. Object& ListIterator::current()
  84. {
  85.     return currentElement->data == 0 ? NOOBJECT : *(currentElement->data);
  86. }
  87.  
  88. Object& ListIterator::operator ++ ( int )
  89. {
  90.     Object *data = currentElement->data;
  91.     currentElement = currentElement->next;
  92.     return data == 0 ? NOOBJECT : *data;
  93. }
  94.  
  95. Object& ListIterator::operator ++ ()
  96. {
  97.     currentElement = currentElement->next;
  98.     return currentElement->data == 0 ? NOOBJECT : *(currentElement->data);
  99. }
  100.  
  101. void ListIterator::restart()
  102. {
  103.     currentElement = startingElement;
  104. }
  105.  
  106.  
  107.