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 / LIST.CPP < prev    next >
C/C++ Source or Header  |  1990-09-26  |  8KB  |  361 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. //      List::add
  15. //      List::destroy
  16. //      List::detach
  17. //      List::initIterator
  18. //         List::hashValue
  19. //
  20. //      ListIterator::operator int
  21. //      ListIterator::operator Object&
  22. //      ListIterator::operator ++
  23. //      ListIterator::restart
  24. //
  25. // Description
  26. //
  27. //      Implementation of class List member functions.
  28. //
  29. // End ---------------------------------------------------------------------
  30.  
  31. // Interface Dependencies ---------------------------------------------------
  32.  
  33. #ifndef __IOSTREAM_H
  34. #include <iostream.h>
  35. #define __IOSTREAM_H
  36. #endif
  37.  
  38. #ifndef __CLSTYPES_H
  39. #include <clstypes.h>
  40. #endif
  41.  
  42. #ifndef __OBJECT_H
  43. #include <object.h>
  44. #endif
  45.  
  46. #ifndef __CONTAIN_H
  47. #include <contain.h>
  48. #endif
  49.  
  50. #ifndef __LIST_H
  51. #include <list.h>
  52. #endif
  53.  
  54. // End Interface Dependencies ------------------------------------------------
  55.  
  56.  
  57. // Implementation Dependencies ----------------------------------------------
  58.  
  59. #ifndef __LSTELEM_H
  60. #include <lstelem.h>
  61. #endif
  62.  
  63. // End Implementation Dependencies -------------------------------------------
  64.  
  65. // Member Function //
  66.  
  67. List::~List()
  68.  
  69. // Summary -----------------------------------------------------------------
  70. //
  71. //      Destructor for a List object.
  72. //
  73. // End ---------------------------------------------------------------------
  74. {
  75.     while( head != 0 )
  76.         {
  77.         ListElement *temp = head;
  78.         head = head->next;
  79.         delete temp;
  80.         }
  81. }
  82. // End Destructor //
  83.  
  84.  
  85. // Member Function //
  86.  
  87. void List::add( Object& toAdd )
  88.  
  89. // Summary -----------------------------------------------------------------
  90. //
  91. //      Adds the given object on the list.
  92. //
  93. // Parameters
  94. //
  95. //      toAdd
  96. //
  97. //      The object we are to add to the list.  Once the object is
  98. //      added, it is owned by the list.
  99. //
  100. // End ---------------------------------------------------------------------
  101. {
  102.     ListElement *newElement = new ListElement( &toAdd );
  103.  
  104.     newElement->next = head;
  105.     head = newElement;
  106.     itemsInContainer++;
  107. }
  108. // End Member Function List::add //
  109.  
  110.  
  111. // Member Function //
  112.  
  113. void    List::detach( const Object& toDetach, int deleteObjectToo )
  114.  
  115. // Summary -----------------------------------------------------------------
  116. //
  117. //      Detaches an object from the list.
  118. //
  119. // Parameter
  120. //
  121. //      toDetach
  122. //
  123. //      The object we are to search for and detach from the List.
  124. //
  125. //      deleteObjectToo
  126. //
  127. //      Specifies whether we are to delete the object.
  128. //
  129. // Functional Description                     
  130. //
  131. //      If the object specified is at the head of the list, we remove
  132. //      the reference right away.  Otherwise, we iterate through the list until
  133. //      we find it, then remove the reference.
  134. //
  135. // Remarks
  136. //
  137. //  warnings:
  138. //      No error condition is generated if the object which was specified
  139. //      isn't in the list.
  140. //
  141. // End ---------------------------------------------------------------------
  142. {
  143.     ListElement *cursor = head;
  144.  
  145.     if ( *(head->data) == toDetach )
  146.     {
  147.         head = head->next;
  148.     }
  149.     else  // the object isn't at the head of the list.
  150.     {
  151.  
  152. // Body Comment
  153. //
  154. //     Normally we would do this iteration with a list iterator.
  155. //     Since we need to keep track of not only the objects in the
  156. //     list but also the list elements, i.e. the pointer nodes,
  157. //     we don't use the iterator.
  158. //
  159. // End
  160.  
  161.         ListElement *trailer = head;
  162.  
  163.         while ( cursor != 0 )
  164.         {
  165.             cursor = cursor->next;
  166.             if ( *(trailer->data) == toDetach )
  167.             {
  168.                 trailer->next = cursor->next;
  169.                 break;
  170.             }
  171.             else // the object isn't the one we want.
  172.             {
  173.                 trailer = trailer->next;
  174.             }
  175.         } // end while
  176.  
  177.     } // end else the object wasn't at the head of the list.
  178.  
  179. // Body Comment
  180. //
  181. //  Now cursor points to the object that we've found
  182. //
  183. // End
  184.  
  185.     if( cursor != 0 )
  186.     {
  187.         itemsInContainer--;
  188.         if ( deleteObjectToo )
  189.         {
  190.             delete cursor->data;
  191.         }
  192.         else
  193.         {
  194.             cursor->data = 0;       // insure that we don't delete the data
  195.         }
  196.  
  197.         delete cursor;
  198.     }
  199. }
  200. // End Member Function List::detach //
  201.  
  202.  
  203. // Member Function //
  204.  
  205. classType List::isA() const
  206.  
  207. // Summary -----------------------------------------------------------------
  208. //
  209. //      Returns a predefined value for the class List.
  210. //
  211. // Parameters
  212. //
  213. //      none
  214. //
  215. // End ---------------------------------------------------------------------
  216. {
  217.     return listClass;
  218. }
  219. // End Member Function List::isA //
  220.  
  221. // Member Function //
  222.  
  223. char *List::nameOf() const
  224.  
  225. // Summary -----------------------------------------------------------------
  226. //
  227. //      Returns the string "List".
  228. //
  229. // Parameters
  230. //
  231. //      none
  232. //
  233. // End ---------------------------------------------------------------------
  234. {
  235.     return "List";
  236. }
  237. // End Member Function List::nameOf //
  238.  
  239.  
  240. // Member Function //
  241.  
  242. hashValueType List::hashValue() const
  243.  
  244. // Summary -----------------------------------------------------------------
  245. //
  246. //      Returns the hash value of a list.
  247. //
  248. // End ---------------------------------------------------------------------
  249. {
  250.     return hashValueType(0);
  251. }
  252. // End Member Function List::hashValue //
  253.  
  254. // Member Function //
  255.  
  256. ContainerIterator& List::initIterator() const
  257.  
  258. // Summary -----------------------------------------------------------------
  259. //
  260. //      Initializes an iterator for a list.
  261. //
  262. // End ---------------------------------------------------------------------
  263. {
  264.     return *( (ContainerIterator *)new ListIterator( *this ) );
  265. }
  266. // End Member Function List::initIterator //
  267.  
  268.  
  269. // Member Function //
  270.  
  271. ListIterator::~ListIterator()
  272.  
  273. // Summary -----------------------------------------------------------------
  274. //
  275. //
  276. //
  277. // End ---------------------------------------------------------------------
  278. {
  279. }
  280. // End Destructor //
  281.  
  282.  
  283. // Member Function //
  284.  
  285. ListIterator::operator int()
  286.  
  287. // Summary -----------------------------------------------------------------
  288. //
  289. //  Returns an integer value indicating whether the iteration is complete.
  290. //
  291. //    1 indicates not complete, 0 indicates complete.
  292. //
  293. // End ---------------------------------------------------------------------
  294. {
  295.     return currentElement != 0;
  296. }
  297. // End Member Function ListIterator::operator int //
  298.  
  299.  
  300. // Member Function //
  301.  
  302. ListIterator::operator Object&()
  303.  
  304. // Summary -----------------------------------------------------------------
  305. //
  306. //      Object reference conversion operator.
  307. //
  308. // End ---------------------------------------------------------------------
  309. {
  310.     if ( currentElement == 0 )
  311.     {
  312.         return NOOBJECT;
  313.     }
  314.     else
  315.     {
  316.         return ( (Object&)(*(currentElement->data)) );
  317.     }
  318. }
  319. // End Member Function ListIterator::operator Object& //
  320.  
  321.  
  322. // Member Function //
  323.  
  324. Object& ListIterator::operator ++()
  325.  
  326. // Summary -----------------------------------------------------------------
  327. //
  328. //     Increments the list iterator and returns the next object.
  329. //
  330. // End ---------------------------------------------------------------------
  331. {
  332.     ListElement *trailer = currentElement;
  333.  
  334.     if ( currentElement != 0 )
  335.     {
  336.         currentElement = currentElement->next;
  337.         return ( (Object&)(*(trailer->data)) );
  338.     }
  339.     else // no more elements in the list.
  340.     {
  341.         return NOOBJECT;
  342.     }
  343.  
  344. }
  345. // End Member Function ListIterator::operator ++ //
  346.  
  347.  
  348. // Member Function //
  349.  
  350. void ListIterator::restart()
  351.  
  352. // Summary -----------------------------------------------------------------
  353. //
  354. //      Restart function for a list iterator object.
  355. //
  356. // End ---------------------------------------------------------------------
  357. {
  358.     currentElement = startingElement;
  359. }
  360. // End Member Function ListIterator::restart //
  361.