home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / clobss.pak / DBLLIST.CPO < prev    next >
Text File  |  1997-07-23  |  5KB  |  184 lines

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