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

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. // Contents ----------------------------------------------------------------
  4. //
  5. //      Array::isA
  6. //      Array::nameOf
  7. //      Array::add
  8. //      Array::addAt
  9. //
  10. // Description
  11. //
  12. //      Implementation of class Array member functions.
  13. //
  14. // End ---------------------------------------------------------------------
  15.  
  16. // Interface Dependencies ---------------------------------------------------
  17.  
  18. #ifndef __CLSTYPES_H
  19. #include <clstypes.h>
  20. #endif
  21.  
  22. #ifndef __OBJECT_H
  23. #include <object.h>
  24. #endif
  25.  
  26. #ifndef __CONTAIN_H
  27. #include <contain.h>
  28. #endif
  29.  
  30. #ifndef __ARRAY_H
  31. #include <array.h>
  32. #endif
  33.  
  34. // End Interface Dependencies ------------------------------------------------
  35.  
  36. // Implementation Dependencies ----------------------------------------------
  37. // End Implementation Dependencies -------------------------------------------
  38.  
  39.  
  40. // Member Function //
  41.  
  42. Array::~Array()
  43.  
  44. // Summary -----------------------------------------------------------------
  45. //
  46. //      Destructor for an Array object.
  47. //
  48. //        We don't do anything here, because the destructor for AbstractArray
  49. //        will take care of destroying the contained objects.
  50. //
  51. // End ---------------------------------------------------------------------
  52. {
  53. }
  54. // End Destructor //
  55.  
  56.  
  57. // Member Function //
  58.  
  59. classType Array::isA() const
  60.  
  61. // Summary -----------------------------------------------------------------
  62. //
  63. //         Returns the class type of an array.
  64. //
  65. // End ---------------------------------------------------------------------
  66. {
  67.     return arrayClass; 
  68. }
  69. // End Member Function Array::isA //
  70.  
  71.  
  72. // Member Function //
  73.  
  74. char *Array::nameOf() const
  75.  
  76. // Summary -----------------------------------------------------------------
  77. //
  78. //         Returns a pointer to the character string "Array."
  79. //
  80. // End ---------------------------------------------------------------------
  81. {
  82.     return "Array";
  83. }
  84. // End Member Function Array::nameOf //
  85.  
  86.  
  87. // Member Function //
  88.  
  89. void Array::add( Object& toAdd )
  90.  
  91. // Summary -----------------------------------------------------------------
  92. //
  93. //      Adds the given object to the array.
  94. //
  95. // Parameters
  96. //
  97. //      toAdd
  98. //
  99. //      The object we are to add to the array.  Once the object is
  100. //      added, it is owned by the array.
  101. //
  102. // End ---------------------------------------------------------------------
  103. {
  104.  
  105. // Body Comment
  106. //
  107. //      We search for the first available space for an array element.
  108. //      Since the user may have inserted an element with addAt or
  109. //      with the subscript operator, we check first before overwriting
  110. //      anything.
  111. //
  112. // End
  113.  
  114.     while( theArray[ whereToAdd ] != ZERO && whereToAdd <= upperbound )
  115.     {
  116.         whereToAdd++;
  117.     } // end while an array index already has an element.
  118.  
  119.     if( whereToAdd > upperbound )
  120.     {
  121.         reallocate( whereToAdd - lowerbound + 1 );
  122.     }
  123.     theArray[ whereToAdd++ ] = &toAdd;
  124.     itemsInContainer++;
  125.  
  126. }
  127. // End Member Function Array::add //
  128.  
  129.  
  130. // Member Function //
  131.  
  132. void Array::addAt( Object& toAdd, int atIndex )
  133.  
  134. // Summary -----------------------------------------------------------------
  135. //
  136. //      Adds the given object to the array at the given index.  If there
  137. //      is an object already at that index, destroys the object.
  138. //
  139. // Parameters
  140. //
  141. //      toAdd
  142. //
  143. //      The object we are to add to the array.  Once the object is
  144. //      added, it is owned by the array.
  145. //
  146. //      atIndex
  147. //
  148. //      The index at which to add the object.
  149. //
  150. // End ---------------------------------------------------------------------
  151. {
  152.  
  153.     if( atIndex > upperbound )
  154.     {
  155.         reallocate( atIndex - lowerbound + 1 );
  156.     }
  157.  
  158.     if ( theArray[ atIndex ] != ZERO )
  159.     {
  160.         delete theArray[ atIndex ];
  161.         itemsInContainer--;
  162.     }
  163.     theArray[ atIndex ] = &toAdd;
  164.     itemsInContainer++;
  165. }
  166. // End Member Function Array::addAt //
  167.  
  168.  
  169.