home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 11.ddi / CLASSSRC.ZIP / LIST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  7.8 KB  |  353 lines

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