home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-1.ZIP / CLASSSRC.ZIP / DBLLIST.CPP < prev    next >
C/C++ Source or Header  |  1992-02-18  |  5KB  |  185 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  DBLLIST.CPP                                                           */
  4. /*                                                                        */
  5. /*  Copyright Borland International 1991                                  */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __RESOURCE_H )
  11. #include <Resource.h>
  12. #endif  // __RESOURCE_H
  13.  
  14. #if !defined( __DBLLIST_H )
  15. #include <DblList.h>
  16. #endif  // __DBLLIST_H
  17.  
  18. #ifndef __IOSTREAM_H
  19. #include <iostream.h>
  20. #define __IOSTREAM_H
  21. #endif
  22.  
  23. unsigned DoubleListBlockInitializer::count = 0;
  24.  
  25. MemBlocks *DoubleList::ListElement::mgr = 0;
  26.  
  27. void DoubleList::flush( DeleteType dt )
  28. {
  29.     ListElement *current = head->next;
  30.     while( current != tail )
  31.         {
  32.         ListElement *temp = current;
  33.         current = current->next;
  34.         if( delObj(dt) )
  35.             delete temp->data;
  36.         delete temp;
  37.         }
  38.     head->next = tail;
  39.     tail->prev = head;
  40.     itemsInContainer = 0;
  41. }
  42.  
  43. void DoubleList::addAtHead( Object& toAdd )
  44. {
  45.     ListElement *newElement =
  46.         new ListElement( &toAdd, head, head->next );
  47.     CHECK( newElement != 0 );
  48.  
  49.     head->next->prev = newElement;
  50.     head->next = newElement;
  51.  
  52.     itemsInContainer++;
  53. }
  54.  
  55. void DoubleList::addAtTail( Object& toAdd )
  56. {
  57.     ListElement *newElement =
  58.         new ListElement( &toAdd, tail->prev, tail );
  59.     CHECK( newElement != 0 );
  60.  
  61.     tail->prev->next = newElement;
  62.     tail->prev = newElement;
  63.  
  64.     itemsInContainer++;
  65. }
  66.  
  67. void DoubleList::detach( Object& toDetach, DeleteType dt )
  68. {
  69.     detachFromHead( toDetach, dt );
  70. }
  71.  
  72. void DoubleList::detachFromHead( Object& toDetach, DeleteType dt )
  73. {
  74.     tail->data = &toDetach;
  75.     ListElement *current = head->next;
  76.     while( *(current->data) != toDetach )
  77.         current = current->next;
  78.     tail->data = 0;
  79.  
  80.     if( current->data == 0 )    // not found
  81.         return;
  82.  
  83.     current->next->prev = current->prev;
  84.     current->prev->next = current->next;
  85.  
  86.     if( delObj(dt) )
  87.         delete current->data;
  88.     delete current;
  89.  
  90.     itemsInContainer--;
  91. }
  92.  
  93. void DoubleList::detachFromTail( Object& toDetach, DeleteType dt )
  94. {
  95.     head->data = &toDetach;
  96.     ListElement *current = tail->prev;
  97.     while( *(current->data) != toDetach )
  98.         current = current->prev;
  99.     head->data = 0;
  100.  
  101.     if( current->data == 0 )    // not found
  102.         return;
  103.  
  104.     current->next->prev = current->prev;
  105.     current->prev->next = current->next;
  106.  
  107.     if( delObj(dt) )
  108.         delete current->data;
  109.     delete current;
  110.  
  111.     itemsInContainer--;
  112. }
  113.  
  114. ContainerIterator& DoubleList::initIterator() const
  115. {
  116.     return *( (ContainerIterator *)new DoubleListIterator( *this ) );
  117. }
  118.  
  119. ContainerIterator& DoubleList::initReverseIterator() const
  120. {
  121.     return *( (ContainerIterator *)new DoubleListIterator( *this, 0 ) );
  122. }
  123.  
  124. DoubleListIterator::operator int()
  125. {
  126.     return currentElement != currentElement->next &&
  127.            currentElement != currentElement->prev;
  128. }
  129.  
  130. Object& DoubleListIterator::current()
  131. {
  132.     return *(currentElement->data);
  133. }
  134.  
  135. Object& DoubleListIterator::operator ++ ( int )
  136. {
  137.     if( currentElement != currentElement->next )
  138.         {
  139.         currentElement = currentElement->next;
  140.         return *(currentElement->prev->data);
  141.         }
  142.     else
  143.         return NOOBJECT;
  144. }
  145.  
  146. Object& DoubleListIterator::operator ++ ()
  147. {
  148.     currentElement = currentElement->next;
  149.  
  150.     if( currentElement != currentElement->next )
  151.         return *(currentElement->data);
  152.     else
  153.         return NOOBJECT;
  154. }
  155.  
  156. void DoubleListIterator::restart()
  157. {
  158.     currentElement = startingElement;
  159. }
  160.  
  161. Object& DoubleListIterator::operator -- ( int )
  162. {
  163.     if( currentElement != currentElement->prev )
  164.         {
  165.         currentElement = currentElement->prev;
  166.         return *(currentElement->next->data);
  167.         }
  168.     else
  169.         return NOOBJECT;
  170. }
  171.  
  172. Object& DoubleListIterator::operator -- ()
  173. {
  174.     currentElement = currentElement->prev;
  175.  
  176.     if( currentElement != currentElement->prev )
  177.         return *(currentElement->data);
  178.     else
  179.         return NOOBJECT;
  180. }
  181.  
  182. DoubleListIterator::~DoubleListIterator()
  183. {
  184. }
  185.