home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP10-8.ZIP / CLASSSRC.ZIP / DBLLIST.CPP < prev    next >
C/C++ Source or Header  |  1990-09-26  |  15KB  |  604 lines

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