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