home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_7 / 1.ddi / CLASSINC.ZIP / DBLLIST.H < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  9.1 KB  |  370 lines

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. // Contents ----------------------------------------------------------------
  4. //
  5. //      DoubleList
  6. //      DoubleList::destroyFromHead
  7. //      DoubleList::destroyFromTail
  8. //
  9. //      DoubleListIterator
  10. //      DoubleListIterator::DoubleListIterator          constructor
  11. //
  12. // Description
  13. //
  14. //      Defines the class DoubleList.
  15. //      Defines the class DoubleListIterator.  A list iterator visits each
  16. //      of the items in a list.
  17. //
  18. // End ---------------------------------------------------------------------
  19.  
  20. // Interface Dependencies ---------------------------------------------------
  21.  
  22. #ifndef __DBLLIST_H
  23. #define __DBLLIST_H
  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 __DLSTELEM_H
  39. #include <dlstelem.h>
  40. #endif
  41.  
  42. #ifndef __COLLECT_H
  43. #include <collect.h>
  44. #endif
  45.  
  46. // End Interface Dependencies ------------------------------------------------
  47.  
  48.  
  49. // Class //
  50.  
  51. class DoubleList:  public Collection
  52. {
  53. public:
  54.             DoubleList() { head = tail = 0; }
  55.     virtual ~DoubleList();
  56.  
  57.             Object&         peekAtHead() const { return *(head->data); }
  58.             Object&         peekAtTail() const { return *(tail->data); }
  59.             
  60.     virtual void            add( Object& );
  61.     virtual void            detach( const Object&, int = 0 );
  62.             void            addAtHead( Object& );
  63.             void            addAtTail( Object& );
  64.             void            destroyFromHead( const Object& );
  65.             void            destroyFromTail( const Object& );
  66.             void            detachFromHead( const Object&, int = 0 );
  67.             void            detachFromTail( const Object&, int = 0 );
  68.  
  69.     virtual ContainerIterator& initIterator() const;
  70.             ContainerIterator& initReverseIterator() const;
  71.  
  72.     virtual classType       isA() const;
  73.     virtual char           *nameOf() const;
  74.     virtual hashValueType   hashValue() const;
  75.  
  76. private:
  77.             DoubleListElement    *head;
  78.             DoubleListElement    *tail;
  79.     friend  class DoubleListIterator;
  80. };
  81.  
  82. // Description -------------------------------------------------------------
  83. //
  84. //      Defines the container class DoubleList. 
  85. //
  86. // Constructor
  87. //
  88. //      DoubleList
  89. //
  90. //      Default constructor
  91. //
  92. // Destructor
  93. //
  94. //      ~DoubleList
  95. //
  96. // Public Members
  97. //
  98. //      peekAtHead
  99. //
  100. //      Returns a reference to the object at the head of the list.
  101. //
  102. //      peekAtTail
  103. //
  104. //      Returns a reference to the object at the tail of the list.
  105. //
  106. //      add
  107. //
  108. //      Adds an element to the list.  By default, the element is
  109. //      added to the head of the list.
  110. //
  111. //      addAtHead
  112. //
  113. //      Adds an element at the head of the list.
  114. //
  115. //      addAtTail
  116. //
  117. //      Adds an element to the tail of the list.
  118. //
  119. //      destroyFromHead
  120. //
  121. //      Destroys the given element, searching forward from the head of the
  122. //      list.
  123. //
  124. //      destroyFromTail
  125. //
  126. //      Destroys the given element, searching forward from the head of the
  127. //      list.
  128. //
  129. //      detach
  130. //
  131. //      Detaches the given element.  By default, the element is searched
  132. //      for from the head of the list.
  133. //
  134. //      detachFromHead
  135. //
  136. //      Detachs the given element, searching forward from the head of the
  137. //      list.
  138. //
  139. //      detachFromTail
  140. //
  141. //      Detachs the given element, searching forward from the head of the
  142. //      list.
  143. //
  144. //      isA
  145. //
  146. //      Returns the class type of class DoubleList.
  147. //
  148. //      nameOf
  149. //
  150. //      Returns a pointer to the character string "DoubleList."
  151. //     
  152. //      hashValue
  153. //
  154. //      Inherited from Object.
  155. //
  156. //      initIterator
  157. //
  158. //      Initializes a double list iterator for a forward traversal of the
  159. //      list.
  160. //
  161. //      initReverseIterator
  162. //
  163. //      Initializes a double list iterator for a reverse traversal of the list.
  164. //
  165. // Inherited Members
  166. //
  167. //      printOn
  168. //
  169. //      Inherited from Container.
  170. //
  171. //      isEqual
  172. //
  173. //      Inherited from Container.
  174. //
  175. //      forEach
  176. //
  177. //      Inherited from Container.
  178. //
  179. //      firstThat
  180. //
  181. //      Inherited from Container.
  182. //
  183. //      lastThat
  184. //
  185. //      Inherited from Container.
  186. //
  187. //      isSortable
  188. //
  189. //      Inherited from Object.
  190. //
  191. //      isAssociation
  192. //
  193. //      Inherited from Object.
  194. //
  195. // End ---------------------------------------------------------------------
  196.  
  197.  
  198. // Member Function //
  199.  
  200. inline void DoubleList::destroyFromHead( const Object& toDestroy )
  201.  
  202. // Summary -----------------------------------------------------------------
  203. //
  204. //      Destroys an object from the head of a double list.  We remove
  205. //      the object from the list and call that object's destructor.
  206. //
  207. // Parameter
  208. //
  209. //      toDestroy
  210. //
  211. //      The object we are to search for and destroy from the DoubleList.
  212. //
  213. // Functional Description                     
  214. //
  215. //      Inline wrap of detach which deleteObjectToo parameter set to 1.
  216. //
  217. // Remarks
  218. //
  219. //  warnings:
  220. //      No error condition is generated if the object which was specified
  221. //      isn't on the double list.
  222. //
  223. // End ---------------------------------------------------------------------
  224. {
  225.     detachFromHead( toDestroy, 1 );
  226. }
  227. // End Member Function DoubleList::destroyFromHead //
  228.  
  229.  
  230. // Member Function //
  231.  
  232. inline void DoubleList::destroyFromTail( const Object& toDestroy )
  233.  
  234. // Summary -----------------------------------------------------------------
  235. //
  236. //      Destroys an object from the tail of a double list.  We remove
  237. //      the object from the list and call that object's destructor.
  238. //
  239. // Parameter
  240. //
  241. //      toDestroy
  242. //
  243. //      The object we are to search for and destroy from the DoubleList.
  244. //
  245. // Functional Description                     
  246. //
  247. //      Inline wrap of detach which deleteObjectToo parameter set to 1.
  248. //
  249. // Remarks
  250. //
  251. //  warnings:
  252. //      No error condition is generated if the object which was specified
  253. //      isn't on the double list.
  254. //
  255. // End ---------------------------------------------------------------------
  256. {
  257.     detachFromTail( toDestroy, 1 );
  258. }
  259. // End Member Function DoubleList::destroyFromTail //
  260.  
  261.  
  262. // Class //
  263.  
  264. class DoubleListIterator:  public ContainerIterator
  265. {
  266. public:
  267.             DoubleListIterator( const DoubleList&, int = 1 );
  268.     virtual ~DoubleListIterator();
  269.  
  270.     virtual                 operator int();
  271.     virtual                 operator Object&();
  272.     virtual Object&         operator ++();
  273.             Object&         operator --();
  274.     virtual void            restart();
  275.  
  276. private:
  277.             DoubleListElement *currentElement;
  278.             DoubleListElement *startingElement;
  279. };
  280.  
  281. // Description -------------------------------------------------------------
  282. //
  283. //      Defines the double list iterator class.  Upon initialization, we 
  284. //      set up an internal pointer to our current position in the list.  When
  285. //      an increment or decrement operator is called, we update this 
  286. //      current position.
  287. //
  288. // Constructor
  289. //
  290. //      DoubleListIterator( const DoubleList&, int = 1 )
  291. //
  292. //      Constructor for an iterator.  Note that this isn't a copy
  293. //      constructor, since it takes an object from a different class.
  294. //
  295. // Destructor
  296. //
  297. //      ~DoubleListIterator
  298. //
  299. // Public Members
  300. //
  301. //      operator int
  302. //
  303. //      We are allowed one cast operator to a predefined type.  This
  304. //      operator defines an explicit or an implicit cast from a
  305. //      DoubleListIterator to an integer.
  306. //
  307. //      operator Object&
  308. //
  309. //      Converts a double list iterator to an Object reference.
  310. //
  311. //      operator ++
  312. //
  313. //      The increment operator.
  314. //
  315. //      operator --
  316. //
  317. //      The decrement operator.  This makes sense for double lists.
  318. //
  319. //      restart
  320. //
  321. //      DoubleList iterator restart mechanism.
  322. //
  323. // Private Members
  324. //
  325. //      currentElement
  326. //
  327. //      The current position in the iteration sequence.
  328. //
  329. //      startingElement
  330. //
  331. //      The starting position in the iteration sequence.
  332. //
  333. // End ---------------------------------------------------------------------
  334.  
  335.  
  336. // Constructor //
  337.  
  338. inline DoubleListIterator::DoubleListIterator( const DoubleList& toIterate, int atHead )
  339.  
  340. // Summary -----------------------------------------------------------------
  341. //
  342. //      Constructor for a list iterator object.
  343. //
  344. // Parameters
  345. //
  346. //      toIterate
  347. //
  348. //      The double list we are to iterate.
  349. //
  350. //      atHead
  351. //
  352. //      Indicates whether we are to start at the head of the list.  If this is
  353. //      not 1, we will start at the tail.
  354. //
  355. // End ---------------------------------------------------------------------
  356. {
  357.     if ( atHead == 1 )
  358.     {
  359.         startingElement = currentElement = toIterate.head;
  360.     }
  361.     else
  362.     {
  363.         startingElement = currentElement = toIterate.tail;
  364.     }
  365. }
  366. // End Constructor DoubleListIterator::DoubleListIterator //
  367.  
  368.  
  369. #endif // ifndef __DBLLIST_H //
  370.