home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / BCPPOWL1.ZIP / CLSRC.ZIP / DBLLIST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  14.9 KB  |  626 lines

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. // Contents ----------------------------------------------------------------
  4. //
  5. //      DoubleList::isA
  6. //      DoubleList::nameOf
  7. //      DoubleList::add
  8. //      DoubleList::addAtHead
  9. //      DoubleList::addAtTail
  10. //      DoubleList::detach
  11. //      DoubleList::detachFromHead
  12. //      DoubleList::detachFromTail
  13. //      DoubleList::initIterator
  14. //      DoubleList::initReverseIterator
  15. //         DoubleList::hashValue
  16. //
  17. //      DoubleListIterator::operator int
  18. //      DoubleListIterator::operator Object&
  19. //      DoubleListIterator::operator ++
  20. //      DoubleListIterator::restart
  21. //      DoubleListIterator::operator --
  22. //
  23. // Description
  24. //
  25. //      Implementation of class DoubleList member functions.
  26. //
  27. // End ---------------------------------------------------------------------
  28.  
  29. // Interface Dependencies ---------------------------------------------------
  30.  
  31. #ifndef __IOSTREAM_H
  32. #include <iostream.h>
  33. #define __IOSTREAM_H
  34. #endif
  35.  
  36. #ifndef __CLSTYPES_H
  37. #include <clstypes.h>
  38. #endif
  39.  
  40. #ifndef __OBJECT_H
  41. #include <object.h>
  42. #endif
  43.  
  44. #ifndef __CONTAIN_H
  45. #include <contain.h>
  46. #endif
  47.  
  48. #ifndef __DLSTELEM_H
  49. #include <dlstelem.h>
  50. #endif
  51.  
  52. #ifndef __DBLLIST_H
  53. #include <dbllist.h>
  54. #endif
  55.  
  56. // End Interface Dependencies ------------------------------------------------
  57.  
  58. // Implementation Dependencies ----------------------------------------------
  59. // End Implementation Dependencies -------------------------------------------
  60.  
  61.  
  62. // Member Function //
  63.  
  64. DoubleList::~DoubleList()
  65.  
  66. // Summary -----------------------------------------------------------------
  67. //
  68. //      Destructor for a DoubleList object.
  69. //
  70. // End ---------------------------------------------------------------------
  71. {
  72.     while( head != 0 )
  73.         {
  74.         DoubleListElement *temp = head;
  75.         head = head->next;
  76.         delete temp;
  77.         }
  78. }
  79. // End Destructor //
  80.  
  81. RObject DoubleList::peekAtHead() const
  82.       if(head)
  83.          return *(head->data);
  84.       else
  85.          return NOOBJECT;
  86. }
  87.  
  88. RObject DoubleList::peekAtTail() const
  89. {
  90.       if(tail)
  91.          return *(tail->data); 
  92.       else
  93.          return NOOBJECT;
  94. }
  95.  
  96. // Member Function //
  97.  
  98. classType DoubleList::isA() const
  99.  
  100. // Summary -----------------------------------------------------------------
  101. //
  102. //         Returns the class type of a double list.
  103. //
  104. // End ---------------------------------------------------------------------
  105. {
  106.     return doubleListClass; 
  107. }
  108. // End Member Function DoubleList::isA //
  109.  
  110.  
  111. // Member Function //
  112.  
  113. char *DoubleList::nameOf() const
  114.  
  115. // Summary -----------------------------------------------------------------
  116. //
  117. //         Returns a pointer to the character string "DoubleList."
  118. //
  119. // End ---------------------------------------------------------------------
  120. {
  121.     return "DoubleList";
  122. }
  123. // End Member Function DoubleList::nameOf //
  124.  
  125.  
  126. // Member Function //
  127.  
  128. void DoubleList::add( Object& toAdd )
  129.  
  130. // Summary -----------------------------------------------------------------
  131. //
  132. //      Adds the given object on the double list at the head of the list.
  133. //
  134. // Parameters
  135. //
  136. //      toAdd
  137. //
  138. //      The object we are to add to the head of the list.  Once the object is
  139. //      added, it is owned by the double list.
  140. //
  141. // Functional Description
  142. //
  143. //      Wrap of addAtHead.
  144. //
  145. // End ---------------------------------------------------------------------
  146. {
  147.     addAtHead( toAdd );
  148. }
  149. // End Member Function DoubleList::add //
  150.  
  151.  
  152. // Member Function //
  153.  
  154. void DoubleList::addAtHead( Object& toAdd )
  155.  
  156. // Summary -----------------------------------------------------------------
  157. //
  158. //      Adds the given object on the double list at the head of the list.
  159. //
  160. // Parameters
  161. //
  162. //      toAdd
  163. //
  164. //      The object we are to add to the head of the list.  Once the object is
  165. //      added, it is owned by the double list.
  166. //
  167. // End ---------------------------------------------------------------------
  168. {
  169.     DoubleListElement *newElement = new DoubleListElement( &toAdd );
  170.  
  171.     if ( head )
  172.     {
  173.         head->previous = newElement;
  174.         newElement->next = head;
  175.         head = newElement;
  176.     }
  177.     else
  178.     {
  179.         tail = head = newElement;
  180.     }
  181.     itemsInContainer++;
  182. }
  183. // End Member Function DoubleList::addAtHead //
  184.  
  185.  
  186. // Member Function //
  187.  
  188. void DoubleList::addAtTail( Object& toAdd )
  189.  
  190. // Summary -----------------------------------------------------------------
  191. //
  192. //      Adds the given object on the double list at the tail of the list.
  193. //
  194. // Parameters
  195. //
  196. //      toAdd
  197. //
  198. //      The object we are to add to the tail of the list.  Once the object is
  199. //      added, it is owned by the double list.
  200. //
  201. // End ---------------------------------------------------------------------
  202. {
  203.     DoubleListElement *newElement = new DoubleListElement( &toAdd );
  204.  
  205.     if ( tail )
  206.     {
  207.         tail->next = newElement;
  208.         newElement->previous = tail;
  209.         tail = newElement;
  210.     }
  211.     else
  212.     {
  213.         head = tail = newElement;
  214.     }
  215.     itemsInContainer++;
  216. }
  217. // End Member Function DoubleList::addAtTail //
  218.  
  219.  
  220. // Member Function //
  221.  
  222. void DoubleList::detach( const Object& toDetach, int destroyToo )
  223.  
  224. // Summary -----------------------------------------------------------------
  225. //
  226. //      Detaches an object from a double list.  By default the object
  227. //         is searched for starting at the head of the list.
  228. //
  229. // Parameter
  230. //
  231. //      toDetach
  232. //
  233. //      The object we are to search for and destroy from the DoubleList.
  234. //
  235. //         destroyToo
  236. //
  237. //         Indicates whether we are also to destroy the object.
  238. //
  239. // Functional Description                     
  240. //
  241. //      Wrap of detachFromHead.
  242. //
  243. // Remarks
  244. //
  245. //  warnings:
  246. //      No error condition is generated if the object which was specified
  247. //      isn't on the double list.
  248. //
  249. // End ---------------------------------------------------------------------
  250. {
  251.     detachFromHead( toDetach, destroyToo );
  252. }
  253. // End Member Function DoubleList::detach //
  254.  
  255.  
  256. // Member Function //
  257.  
  258. void DoubleList::detachFromHead( const Object& toDetach, int deleteToo )
  259.  
  260. // Summary -----------------------------------------------------------------
  261. //
  262. //      Detaches an object from the head of a double list.
  263. //
  264. // Parameter
  265. //
  266. //      toDetach
  267. //
  268. //      The object we are to search for and detach from the DoubleList.
  269. //
  270. //      deleteToo
  271. //
  272. //      Specifies whether we are to delete the object.
  273. //
  274. // Functional Description                     
  275. //
  276. //      If the object specified is at the head of the double list, we remove
  277. //      the reference right away.  Otherwise, we iterate through the double list until
  278. //      we find it, then remove the reference.
  279. //
  280. // Remarks
  281. //
  282. //  warnings:
  283. //      No error condition is generated if the object which was specified
  284. //      isn't on the double list.
  285. //
  286. // End ---------------------------------------------------------------------
  287. {
  288.    if(head == 0)
  289.       return;
  290.  
  291.     DoubleListElement *cursor = head;
  292.  
  293.     if ( *(head->data) == toDetach )
  294.     {
  295.         if( head->next == 0 )
  296.             tail = 0;
  297.         head = head->next;
  298.         head->previous = 0;
  299.     }
  300.     else  // the object isn't at the head of the list.
  301.     {
  302.  
  303. // Body Comment
  304. //
  305. //         Normally we would do this iteration with a list iterator.
  306. //         Since we need to keep track of not only the objects in the
  307. //         list but also the list elements, i.e. the pointer nodes,
  308. //         we don't use the iterator.
  309. //
  310. // End
  311.  
  312.  
  313.        while ( (cursor = cursor->next) != 0 )
  314.         {
  315.             if ( *(cursor->data) == toDetach )
  316.             {
  317.                 cursor->previous->next = cursor->next;
  318.                 if( cursor->next )
  319.                     cursor->next->previous = cursor->previous;
  320.                 else
  321.                     tail = cursor->previous;                  
  322.                 break;
  323.             }
  324.         } // end while
  325.  
  326.     } // end else the object wasn't at the head of the list.
  327.  
  328. // Body Comment
  329. //
  330. //  Now cursor points to the object that we've found
  331. //
  332. // End
  333.  
  334.    if( cursor != 0 )
  335.    {
  336.       itemsInContainer--;
  337.  
  338.       if ( !deleteToo )
  339.          cursor->data = 0;       // insure that we don't delete the data
  340.  
  341.       delete cursor;
  342.    }
  343. }
  344.  
  345. // End Member Function DoubleList::detachFromHead //
  346.  
  347.  
  348. // Member Function //
  349.  
  350. void DoubleList::detachFromTail( const Object& toDetach, int deleteToo )
  351.  
  352. // Summary -----------------------------------------------------------------
  353. //
  354. //      Detaches an object from the tail of a double list.
  355. //
  356. // Parameter
  357. //
  358. //      toDetach
  359. //
  360. //      The object we are to search for and detach from the DoubleList.
  361. //
  362. //      deleteToo
  363. //
  364. //      Specifies whether we are to delete the object.
  365. //
  366. // Functional Description                     
  367. //
  368. //      If the object specified is at the tail of the double list, we remove
  369. //      the reference right away.  Otherwise, we iterate backwards through 
  370. //      the double list until we find it, then remove the reference.
  371. //
  372. // Remarks
  373. //
  374. //  warnings:
  375. //      No error condition is generated if the object which was specified
  376. //      isn't on the double list.
  377. //
  378. // End ---------------------------------------------------------------------
  379. {
  380.    if(tail == 0)
  381.       return;
  382.  
  383.     DoubleListElement *cursor = tail;
  384.  
  385.     if ( *(tail->data) == toDetach )
  386.     {
  387.         if( tail->previous == 0 )
  388.             head = 0;
  389.         tail = tail->previous;
  390.         tail->next = 0;
  391.        }
  392.     else  // the object isn't at the tail of the list.
  393.     {
  394.  
  395. // Body Comment
  396. //
  397. //         Normally we would do this iteration with a list iterator.
  398. //         Since we need to keep track of not only the objects in the
  399. //         list but also the list elements, i.e. the pointer nodes,
  400. //         we don't use the iterator.
  401. //
  402. // End
  403.  
  404.  
  405.         while ( (cursor = cursor->previous) != 0 )
  406.         {
  407.             if ( *(cursor->data) == toDetach )
  408.             {
  409.                 if (cursor->previous)
  410.                     cursor->previous->next = cursor->next;
  411.                 else
  412.                     head = cursor->next;
  413.                 cursor->next->previous = cursor->previous;
  414.                 break;
  415.             }
  416.         } // end while
  417.  
  418.     } // end else the object wasn't at the tail of the list.
  419.  
  420. // Body Comment
  421. //
  422. //  Now cursor points to the object that we've found
  423. //
  424. // End
  425.  
  426.    if( cursor != 0 )
  427.       {
  428.       itemsInContainer--;
  429.       if ( !deleteToo )
  430.          {
  431.          cursor->data = 0;       // insure that we don't delete the data
  432.          }
  433.       delete cursor;
  434.       }
  435.  
  436. //    if( cursor != 0 )
  437. //    {
  438. //        itemsInContainer--;
  439. //        if ( deleteToo )
  440. //        {
  441. //            delete cursor->data;
  442. //        }
  443. //        else
  444. //        {
  445. //            cursor->data = 0;       // insure that we don't delete the data
  446. //        }
  447. //
  448. //        delete cursor;
  449. //    }
  450. }
  451. // End Member Function DoubleList::detachFromTail //
  452.  
  453.  
  454. // Member Function //
  455.  
  456. ContainerIterator& DoubleList::initIterator() const
  457.  
  458. // Summary -----------------------------------------------------------------
  459. //
  460. //      Initializes an iterator for a double list.
  461. //
  462. // End ---------------------------------------------------------------------
  463. {
  464.     return *( (ContainerIterator *)new DoubleListIterator( *this ) );
  465. }
  466. // End Member Function DoubleList::initIterator //
  467.  
  468.  
  469. // Member Function //
  470.  
  471. ContainerIterator& DoubleList::initReverseIterator() const
  472.  
  473. // Summary -----------------------------------------------------------------
  474. //
  475. //      Initializes an iterator for a double list.
  476. //
  477. // End ---------------------------------------------------------------------
  478. {
  479.     return *( (ContainerIterator *)new DoubleListIterator( *this, 0 ) );
  480. }
  481. // End Member Function DoubleList::initReverseIterator //
  482.  
  483.  
  484. // Member Function //
  485.  
  486. hashValueType DoubleList::hashValue() const
  487.  
  488. // Summary -----------------------------------------------------------------
  489. //
  490. //      Returns the hash value of a double list.
  491. //
  492. // End ---------------------------------------------------------------------
  493. {
  494.     return hashValueType(0);
  495. }
  496. // End Member Function DoubleList::hashValue //
  497.  
  498.  
  499. // Member Function //
  500.  
  501. DoubleListIterator::operator int()
  502.  
  503. // Summary -----------------------------------------------------------------
  504. //
  505. //      Integer conversion operator for a Double List iterator.
  506. //
  507. // End ---------------------------------------------------------------------
  508. {
  509.     return ( currentElement != 0 );
  510. }
  511. // End Member Function DoubleListIterator::operator int //
  512.  
  513.  
  514. // Member Function //
  515.  
  516. DoubleListIterator::operator Object&()
  517.  
  518. // Summary -----------------------------------------------------------------
  519. //
  520. //      Object conversion operator for a Double List iterator.
  521. //
  522. // End ---------------------------------------------------------------------
  523. {
  524.    if(currentElement)
  525.       return ( (Object&)(*(currentElement->data)) );
  526.    else
  527.       return NOOBJECT;
  528.  
  529. //    return ( (Object&)(*(currentElement->data)) );
  530. }
  531. // End Member Function DoubleListIterator::operator Object& //
  532.  
  533.  
  534. // Member Function //
  535.  
  536. Object& DoubleListIterator::operator ++()
  537.  
  538. // Summary -----------------------------------------------------------------
  539. //
  540. //         Increments the list iterator and returns the next object.
  541. //
  542. // Return Value
  543. //
  544. //         listObject
  545. //
  546. //      A reference to the object which is after the current object
  547. //         in the iterator sequence.
  548. //
  549. // End ---------------------------------------------------------------------
  550. {
  551.     DoubleListElement *trailer = currentElement;
  552.  
  553.     if ( currentElement != 0 )
  554.     {
  555.         currentElement = currentElement->next;
  556.         return ( (Object&)(*(trailer->data)) );
  557.     }
  558.     else // no more elements in the list.
  559.     {
  560.         return NOOBJECT;
  561.     }
  562. }
  563. // End Member Function DoubleListIterator::operator ++ //
  564.  
  565.  
  566. // Member Function //
  567.  
  568. void DoubleListIterator::restart()
  569.  
  570. // Summary -----------------------------------------------------------------
  571. //
  572. //      Restart function for a list iterator object.
  573. //
  574. // End ---------------------------------------------------------------------
  575. {
  576.     currentElement = startingElement;
  577. }
  578. // End Member Function DoubleListIterator::restart //
  579.  
  580.  
  581. // Member Function //
  582.  
  583. Object& DoubleListIterator::operator --()
  584.  
  585. // Summary -----------------------------------------------------------------
  586. //
  587. //         Decrements the list iterator and returns the previous object.
  588. //
  589. // Return Value
  590. //
  591. //         listObject
  592. //
  593. //      A reference to the object which is before the current object
  594. //         in the iterator sequence.
  595. //
  596. // End ---------------------------------------------------------------------
  597. {
  598.     DoubleListElement *trailer = currentElement;
  599.  
  600.     if ( currentElement != 0 )
  601.     {
  602.         currentElement = currentElement->previous;
  603.         return ( (Object&)(*(trailer->data)) );
  604.     }
  605.     else // no more elements in the list.
  606.     {
  607.         return NOOBJECT;
  608.     }
  609. }
  610. // End Member Function DoubleListIterator::operator -- //
  611.  
  612.  
  613. // Destructor //
  614.  
  615. DoubleListIterator::~DoubleListIterator()
  616.  
  617. // Summary -----------------------------------------------------------------
  618. //
  619. //      Destructor for a DoubleListIterator object.
  620. //
  621. // End ---------------------------------------------------------------------
  622. {
  623. }
  624. // End Destructor //
  625.