home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 1.ddi / CLASSINC.ZIP / ABSTARRY.H next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  8.0 KB  |  314 lines

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. // Contents ----------------------------------------------------------------
  4. //
  5. //      AbstractArray
  6. //         AbstractArray::arraySize
  7. //
  8. //      ArrayIterator
  9. //      ArrayIterator::ArrayIterator
  10. //
  11. // Description
  12. //
  13. //      Defines the class AbstractArray.  An array object implies 
  14. //      indexability.  Defines the class ArrayIterator.
  15. //
  16. // End ---------------------------------------------------------------------
  17.  
  18. // Interface Dependencies ---------------------------------------------------
  19.  
  20. #ifndef __ABSTARRY_H
  21. #define __ABSTARRY_H
  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 __OBJECT_H
  33. #include <object.h>
  34. #endif
  35.  
  36. #ifndef __COLLECT_H
  37. #include <collect.h>
  38. #endif
  39.  
  40. // End Interface Dependencies ------------------------------------------------
  41.  
  42.  
  43. class AbstractArray:  public Collection
  44. {
  45. public:                                                         
  46.             AbstractArray( int upper, int lower = 0, sizeType aDelta = 0 );
  47.     virtual ~AbstractArray();
  48.  
  49.             int             lowerBound() const { return lowerbound; }
  50.             int             upperBound() const { return upperbound; }
  51.             sizeType        arraySize() const;
  52.  
  53.     virtual ContainerIterator& initIterator() const;
  54.  
  55.     virtual void            add( Object& ) = 0;
  56.             void            destroy( int i ) { detach( i, 1 ); }
  57.             void            detach( int, int = 0 );
  58.  
  59.     virtual    void            detach( const Object&, int = 0 );
  60.  
  61.     virtual classType       isA() const = 0;
  62.     virtual char           *nameOf() const = 0;
  63.     virtual int             isEqual( const Object& ) const;
  64.     virtual hashValueType   hashValue() const;
  65.  
  66.     virtual    void            printContentsOn( ostream& ) const;
  67.  
  68. protected:
  69.             Object&         objectAt( int i ) const
  70.                                 { return *theArray[ i - lowerbound ]; }
  71.             void            reallocate( sizeType );
  72.             sizeType        delta;
  73.             int                lowerbound;
  74.             int                upperbound;
  75.             int                whereToAdd;
  76.             Object        **theArray;
  77.  
  78.     friend  class ArrayIterator;
  79. };
  80.  
  81. // Description -------------------------------------------------------------
  82. //
  83. //      Defines the class AbstractArray.  The AbstractArray class is 
  84. //      used as a base class for random-access and sorted arrays.
  85. //      The size of the array, i.e. the maximum number of elements 
  86. //      which may be put into the array, is calculated from the bounds 
  87. //      given at the construction of the array object.
  88. //
  89. // Constructor
  90. //
  91. //         AbstractArray
  92. //
  93. //         Constructor.  Parameter upper specifies the upper bound for the
  94. //         index of the array.    Parameter lower specifies a lower bound for
  95. //      the index of the array.  Paramter aDelta specifies the number of 
  96. //         array elements by which the array will grow if an element is added 
  97. //         to an array    which has no more space for elements.  Specify aDelta = 0 
  98. //         if the array should not be allowed to grow.
  99. //
  100. // Public Members
  101. //
  102. //         lowerBound
  103. //
  104. //         Returns the current lower bound of the array.  The lower bound is
  105. //         fixed when the array is constructed.
  106. //
  107. //         upperBound
  108. //
  109. //         Returns the upper bound of the array.  The upper bound is initially
  110. //         set when the array is constructed but may increase is more elements
  111. //         are added.    The amount by which the upper bound will increase is
  112. //         a parameter to the constructor for the array.
  113. //
  114. //         arraySize
  115. //
  116. //         Returns the size of the array, in elements, as determined by the
  117. //         lower bound and the current upper bound.
  118. //
  119. //         initIterator
  120. //
  121. //         Array iterator initializer.
  122. //
  123. //      add
  124. //
  125. //      Pure virtual function.
  126. //     
  127. //      destroy
  128. //
  129. //      Removes an object reference from the array at the given index and
  130. //      destroys the object.
  131. //
  132. //         detach
  133. //
  134. //         Removes all references to the object at the given index in the array.
  135. //      Does not delete the object.  Use this function when the array elements
  136. //         are not owned by the array.
  137. //
  138. //         detach
  139. //
  140. //      Removes a reference to the given object from the array.
  141. //
  142. //      hashValue
  143. //
  144. //         Returns a pre-defined value as the hash value of an array.
  145. //
  146. //         isEqual
  147. //
  148. //         Determines whether two arrays are equal.
  149. //
  150. //         printContentsOn
  151. //
  152. //         Displays the non-ZERO elements of the array.
  153. //
  154. // Inherited Members
  155. //
  156. //      hasMember
  157. //
  158. //      Inherited from Collection
  159. //
  160. //         isEmpty
  161. //
  162. //      Inherited from Collection.
  163. //
  164. //      forEach
  165. //
  166. //      Inherited from Container.
  167. //
  168. //      firstThat
  169. //
  170. //      Inherited from Container.
  171. //
  172. //      lastThat
  173. //
  174. //      Inherited from Container.
  175. //
  176. //         printOn
  177. //
  178. //         Inherited from Container.
  179. //
  180. //      destroy
  181. //
  182. //      Inherited from Collection.
  183. //
  184. // Protected Members
  185. //
  186. //         objectAt
  187. //
  188. //         Returns a reference to the object at the given index.
  189. //
  190. //         reallocate
  191. //
  192. //         Expands the pointer array to a new size.
  193. //
  194. //      delta
  195. //
  196. //      Defines the number of elements by which we are to expand the
  197. //      array, if needed.
  198. //
  199. //      lowerbound
  200. //
  201. //      Defines the smallest value for an index in this array.
  202. //
  203. //      upperbound
  204. //
  205. //      Defines the largest index in the array which, if referenced,
  206. //      will not cause an array expansion to take place.
  207. //
  208. //      theArray
  209. //
  210. //      Points to the area in which array element references are located.
  211. //
  212. // End ---------------------------------------------------------------------
  213.  
  214.  
  215. // Member Function //
  216.  
  217. inline sizeType AbstractArray::arraySize() const
  218.  
  219. // Summary -----------------------------------------------------------------
  220. //
  221. //      Returns the current size of the array.
  222. //
  223. // End ---------------------------------------------------------------------
  224. {
  225.     return sizeType( upperbound - lowerbound + 1 );
  226. }
  227. // End Member Function AbstractArray::arraySize //
  228.  
  229.  
  230. // Class //
  231.  
  232. class ArrayIterator:  public ContainerIterator
  233. {
  234. public:
  235.             ArrayIterator( const AbstractArray& );
  236.     virtual ~ArrayIterator();
  237.  
  238.     virtual                operator int();
  239.     virtual                operator Object&();
  240.     virtual    Object&        operator ++();
  241.     virtual    void        restart();
  242.  
  243. private:
  244.             int            currentIndex;
  245.     const   AbstractArray& beingIterated;
  246. };
  247.  
  248. // Description -------------------------------------------------------------
  249. //
  250. //         Defines the array iterator class.  Upon initialization, we set up
  251. //         an internal pointer to our current position in the array.  As
  252. //      the increment operator is called, we update this current position.
  253. //
  254. // Constructor
  255. //
  256. //      ArrayIterator( const AbstractArray& )
  257. //
  258. //         Constructor for an iterator.  Note that this isn't a copy
  259. //         constructor, since it takes an object from a different class.
  260. //
  261. // Destructor
  262. //
  263. //         ~ArrayIterator
  264. //
  265. // Public Members
  266. //
  267. //         operator int
  268. //
  269. //         We are allowed one cast operator to a predefined type.  This
  270. //         operator defines an explicit or an implicit cast from a
  271. //         ArrayIterator to an integer.
  272. //
  273. //      operator Object&
  274. //
  275. //      Conversion to Object operator.
  276. //
  277. //         operator ++
  278. //
  279. //      The increment operator.
  280. //
  281. //         restart
  282. //
  283. //         Restarts an array iterator.
  284. //
  285. // Private Members
  286. //
  287. //         currentIndex
  288. //
  289. //         Maintains the position information for this iterator.
  290. //
  291. //         beingIterated
  292. //
  293. //      Maintains a pointer to the array being iterated.
  294. //
  295. // End ---------------------------------------------------------------------
  296.  
  297.  
  298. // Constructor //
  299.  
  300. inline  ArrayIterator::ArrayIterator( const AbstractArray& toIterate ) :
  301.             beingIterated( toIterate ), currentIndex( toIterate.lowerbound )
  302.  
  303. // Summary -----------------------------------------------------------------
  304. //
  305. //      Constructor for a array iterator object.
  306. //
  307. // End ---------------------------------------------------------------------
  308. {
  309. }
  310. // End Constructor ArrayIterator::ArrayIterator //
  311.  
  312.  
  313. #endif // ifndef __ABSTARRY_H //
  314.