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

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