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