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

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. // Contents ----------------------------------------------------------------
  4. //
  5. //      Container::Container                        copy constructor
  6. //      Container::forEach
  7. //      Container::firstThat
  8. //      Container::lastThat
  9. //      Container::isEqual
  10. //      Container::printOn
  11. //      Container::printHeader
  12. //      Container::printSeparator
  13. //      Container::printTrailer
  14. //
  15. // Description
  16. //
  17. //      Implementation of class Container member functions.
  18. //
  19. // End ---------------------------------------------------------------------
  20.  
  21. // Interface Dependencies ---------------------------------------------------
  22.  
  23. #ifndef __IOSTREAM_H
  24. #include <iostream.h>
  25. #define __IOSTREAM_H
  26. #endif
  27.  
  28. #ifndef __CLSTYPES_H
  29. #include <clstypes.h>
  30. #endif
  31.  
  32. #ifndef __CONTAIN_H
  33. #include <contain.h>
  34. #endif
  35.  
  36. // End Interface Dependencies ------------------------------------------------
  37.  
  38. // Implementation Dependencies ----------------------------------------------
  39. // End Implementation Dependencies -------------------------------------------
  40.  
  41.  
  42. // Constructor //
  43.  
  44. Container::Container( const Container& toCopy )
  45.  
  46. // Summary -----------------------------------------------------------------
  47. //
  48. //      Constructors a container and copies the given container into it.
  49. //
  50. // Parameters
  51. //
  52. //      toCopy
  53. //
  54. //      The container which we are to copy.
  55. //
  56. // Functional Description
  57. //
  58. //      We initialize an iterator, then iterate through each object in the
  59. //      container we are copying, constructing and copying as we go.
  60. //
  61. // Remarks
  62. //
  63. //  warnings:
  64. //      We must be sure to delete the container iterator, since it was
  65. //      allocated on the heap.
  66. //
  67. // End ---------------------------------------------------------------------
  68. {
  69.    ContainerIterator ©Iterator = toCopy.initIterator();
  70.  
  71.    while( int(copyIterator) != 0 )
  72.     {
  73.         copyIterator++;
  74.     } // end while //
  75.  
  76.     delete ©Iterator;
  77. }
  78. // End Constructor Container::Container //
  79.  
  80.  
  81. // Member Function //
  82.  
  83. Container::~Container()
  84.  
  85. // Summary -----------------------------------------------------------------
  86. //
  87. //      Destructor for a Container object.
  88. //
  89. //      We don't do anything here, because the derived class will have
  90. //      taken care of deleting all the contained objects.
  91. //
  92. // End ---------------------------------------------------------------------
  93. {
  94. }
  95. // End Destructor //
  96.  
  97.  
  98. // Member Function //
  99.  
  100. void Container::forEach( iterFuncType actionPtr, void *paramListPtr )
  101.  
  102. // Summary -----------------------------------------------------------------
  103. //
  104. //      Calls the given iterator function on each object in this container.
  105. //
  106. // Parameters
  107. //
  108. //      actionPtr
  109. //
  110. //      Pointer to the action routine which is to be called for each object.
  111. //
  112. //      paramListPtr
  113. //
  114. //      Pointer to the list of parameters which will be passed along to
  115. //      the action routine.
  116. //
  117. // Functional Description
  118. //
  119. //      We initialize an iterator, then iterator through each object,
  120. //      asking the objects to perform the forEach function on themselves.
  121. //
  122. // Remarks
  123. //
  124. //  warnings:
  125. //      The action routine must have a prototype of the form:
  126. //          void action( Object&, void * );
  127. //
  128. //      We must be sure to delete the container iterator, since it was
  129. //      allocated on the heap.
  130. //
  131. // End ---------------------------------------------------------------------
  132. {
  133.     ContainerIterator& containerIterator = initIterator();
  134.  
  135.     while( int(containerIterator) != 0 )
  136.     {
  137.         containerIterator++.forEach( actionPtr, paramListPtr );
  138.     }
  139.     delete &containerIterator;
  140. }
  141. // End Member Function Container::forEach //
  142.  
  143.  
  144. // Member Function //
  145.  
  146. const Object& Container::firstThat( condFuncType testFuncPtr, void *paramListPtr ) const
  147.  
  148. // Summary -----------------------------------------------------------------
  149. //
  150. //      Finds the first object in the container which satisfies the
  151. //      given condition function.
  152. //
  153. // Parameters
  154. //
  155. //      testFuncPtr
  156. //
  157. //      Pointer to the conditional test routine which is to be called 
  158. //      for this container.
  159. //
  160. //      paramListPtr
  161. //
  162. //      Pointer to the list of parameters which will be passed along to
  163. //      the conditional test routine.
  164. //
  165. // Return Value
  166. //
  167. //      Returns the first object in the container which satisfies the 
  168. //      condition.  Returns NOOBJECT otherwise.
  169. //
  170. // Functional Description
  171. //
  172. //      We initialize an iterator, then iterator through each object,
  173. //      asking the objects to perform the firstThat function on themselves.
  174. //
  175. // Remarks
  176. //
  177. //  warnings:
  178. //      The conditional test routine must have a prototype of the form:
  179. //          int test( Object&, void * );
  180. //
  181. //      The conditional test routine must return 1 if the given object
  182. //      satisfies the condition.
  183. //
  184. //      We must be sure to delete the container iterator, since it was
  185. //      allocated on the heap.
  186. //
  187. // End ---------------------------------------------------------------------
  188. {
  189.     ContainerIterator &containerIterator = initIterator();
  190.  
  191.     while( int(containerIterator) != 0 )
  192.     {
  193.         const Object& testResult = 
  194.                     containerIterator++.firstThat( testFuncPtr, paramListPtr );
  195.         if ( testResult != NOOBJECT )
  196.         {
  197.             delete &containerIterator;
  198.             return testResult;
  199.         }
  200.     } // end while //
  201.     delete &containerIterator;
  202.     return NOOBJECT;
  203. }
  204. // End Member Function Container::firstThat //
  205.  
  206.  
  207. // Member Function //
  208.  
  209. const Object& Container::lastThat( condFuncType testFuncPtr, void *paramListPtr ) const
  210.  
  211. // Summary -----------------------------------------------------------------
  212. //
  213. //      Finds the last object in the container which satisfies the
  214. //      given condition function.
  215. //
  216. // Parameters
  217. //
  218. //      testFuncPtr
  219. //
  220. //      Pointer to the conditional test routine which is to be called 
  221. //      for this object.
  222. //
  223. //      paramListPtr
  224. //
  225. //      Pointer to the list of parameters which will be passed along to
  226. //      the conditional test routine.
  227. //
  228. // Functional Description
  229. //
  230. //      We initialize an iterator, then iterator through each object,
  231. //      asking the objects to perform the lastThat function on themselves.
  232. //      As we iterate, if we find an object which satisfies the condition,
  233. //      we make that object the last object which met the condition.  Note
  234. //      that there is no short-circuiting the search, since we must search
  235. //      through every object in the container to find the last one.
  236. //
  237. // Remarks
  238. //
  239. //  warnings:
  240. //      The conditional test routine must have a prototype of the form:
  241. //          int test( Object&, void * );
  242. //
  243. //      The conditional test routine must return 1 if the given object
  244. //      satisfies the condition.
  245. //
  246. //      We must be sure to delete the container iterator, since it was
  247. //      allocated on the heap.
  248. //
  249. // End ---------------------------------------------------------------------
  250. {
  251.     ContainerIterator& containerIterator = initIterator();
  252.  
  253.      Object *lastMet =
  254.         &(Object &)(containerIterator++.lastThat( testFuncPtr, paramListPtr ));
  255.  
  256.     while( int(containerIterator) != 0 )
  257.         {
  258.             const Object& testResult = 
  259.                     containerIterator++.lastThat( testFuncPtr, paramListPtr );
  260.             if ( testResult != NOOBJECT )
  261.             {
  262.             lastMet = &(Object &)testResult;
  263.             }
  264.         } // end while //
  265.         delete &containerIterator;
  266.     return *lastMet;
  267. }
  268. // End Member Function Container::lastThat //
  269.  
  270.  
  271. // Member Function //
  272.  
  273. int  Container::isEqual( const Object& testContainer ) const
  274.  
  275. // Summary -----------------------------------------------------------------
  276. //
  277. //      Determines whether the given container is equal to this.
  278. //
  279. // Parameters
  280. //
  281. //      testContainer
  282. //
  283. //      The container which we will be testing for equality with this.
  284. //
  285. // Return Value
  286. //
  287. //      Returns 1 if the two containers have the same objects in them
  288. //      in the same order.
  289. //
  290. // Functional Description
  291. //
  292. //      We initialize an iterator, then iterator through each object,
  293. //      asking the objects to perform an equality test.  As we iterate, 
  294. //      if we find an object which fails the test, we return 0.  If every
  295. //      object is equal and there are the same number of objects in both
  296. //      containers, we return 1.
  297. //
  298. // Remarks
  299. //
  300. //  warnings:
  301. //      We must be sure to delete the container iterator, since it was
  302. //      allocated on the heap.
  303. //
  304. // End ---------------------------------------------------------------------
  305. {
  306.     ContainerIterator& thisIterator = initIterator();
  307.     ContainerIterator& testContainerIterator =
  308.                                 ((Container &)(testContainer)).initIterator();
  309.  
  310.     while( int(thisIterator) != 0 && int(testContainerIterator) != 0 )
  311.     {
  312.         int objectsAreNotEqual = 
  313.                     (thisIterator++ != testContainerIterator++);
  314.         if ( objectsAreNotEqual )
  315.         {
  316.             delete &testContainerIterator;
  317.             delete &thisIterator;
  318.             return 0;
  319.         }
  320.             
  321.     } // end while //
  322.  
  323.     if ( int(thisIterator) !=0 || int(testContainerIterator) != 0 )
  324.     {
  325.         delete &testContainerIterator;
  326.         delete &thisIterator;
  327.         return 0;
  328.     }
  329.     else  // one of the containers has more objects than the other.
  330.     {
  331.         delete &testContainerIterator;
  332.         delete &thisIterator;
  333.         return 1;
  334.     }
  335. }
  336. // End Member Function Container::isEqual //
  337.  
  338.  
  339. // Member Function //
  340.  
  341. void Container::printOn( ostream& outputStream ) const
  342.  
  343. // Summary -----------------------------------------------------------------
  344. //
  345. //      Displays the contents of a container on the given stream.
  346. //
  347. // Parameters
  348. //
  349. //      outputStream
  350. //
  351. //      The stream on which we will be writing the container contents.
  352. //
  353. // Functional Description
  354. //
  355. //      We initialize an iterator, then iterator through each object,
  356. //      asking the objects to print themselves.
  357. //
  358. // Remarks
  359. //
  360. //  warnings:
  361. //      We must be sure to delete the container iterator, since it was
  362. //      allocated on the heap.
  363. //
  364. // End ---------------------------------------------------------------------
  365. {
  366.     ContainerIterator& printIterator = initIterator();
  367.  
  368.     printHeader( outputStream );
  369.  
  370.     while( int(printIterator) != 0 )
  371.     {
  372.         printIterator++.printOn( outputStream );
  373.         if ( int(printIterator) != 0 )
  374.         {
  375.             printSeparator( outputStream );
  376.         }
  377.         else // there are no more objects in the container.
  378.         {
  379.             break;
  380.         }
  381.     } // end while //
  382.  
  383.     printTrailer( outputStream );
  384.     delete &printIterator;
  385. }
  386. // End Member Function Container::printOn //
  387.  
  388.  
  389. // Member Function //
  390.  
  391. void Container::printHeader( ostream& outputStream ) const
  392.  
  393. // Summary -----------------------------------------------------------------
  394. //
  395. //      Displays a standard header for a container on the given stream.
  396. //
  397. // Parameters
  398. //
  399. //      outputStream
  400. //
  401. //      The stream on which we will be writing the header.
  402. //
  403. // Functional Description
  404. //
  405. //      We print the string returned by nameOf() and an opening brace.
  406. //
  407. // End ---------------------------------------------------------------------
  408. {
  409.     outputStream << nameOf() << " { ";
  410. }
  411. // End Member Function Container::printHeader //
  412.  
  413.  
  414. // Member Function //
  415.  
  416. void Container::printSeparator( ostream& outputStream ) const
  417.  
  418. // Summary -----------------------------------------------------------------
  419. //
  420. //      Displays a standard separator for a container on the given stream.
  421. //
  422. // Parameters
  423. //
  424. //      outputStream
  425. //
  426. //      The stream on which we will be writing the separator.
  427. //
  428. // End ---------------------------------------------------------------------
  429. {
  430.     outputStream << ",\n    ";
  431. }
  432. // End Member Function Container::printSeparator //
  433.  
  434.  
  435. // Member Function //
  436.  
  437. void Container::printTrailer( ostream& outputStream ) const
  438.  
  439. // Summary -----------------------------------------------------------------
  440. //
  441. //      Displays a standard trailer for a container on the given stream.
  442. //
  443. // Parameters
  444. //
  445. //      outputStream
  446. //
  447. //      The stream on which we will be writing the trailer.
  448. //
  449. // End ---------------------------------------------------------------------
  450. {
  451.     outputStream << " }\n";
  452. }
  453. // End Member Function Container::printTrailer //
  454.  
  455. // Member Function //
  456.  
  457. ContainerIterator::~ContainerIterator()
  458.  
  459. // Summary -----------------------------------------------------------------
  460. //
  461. //      Destructor for a ContainerIterator object.
  462. //
  463. // End ---------------------------------------------------------------------
  464. {
  465. }
  466. // End Destructor //
  467.  
  468.  
  469.  
  470.