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

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. // Contents ----------------------------------------------------------------
  4. //
  5. //      SortedArray::~SortedArray
  6. //      SortedArray::add
  7. //      SortedArray::detach
  8. //
  9. // Description
  10. //
  11. //      Implementation of class SortedArray.
  12. //
  13. // End ---------------------------------------------------------------------
  14.  
  15. // Interface Dependencies ---------------------------------------------------
  16.  
  17. #ifndef __STDLIB_H
  18. #include <stdlib.h>
  19. #define __STDLIB_H
  20. #endif
  21.  
  22. #ifndef __OBJECT_H
  23. #include <object.h>
  24. #endif
  25.  
  26. #ifndef __SORTARRY_H
  27. #include <sortarry.h>
  28. #endif
  29.  
  30. // End Interface Dependencies ------------------------------------------------
  31.  
  32. // Implementation Dependencies ----------------------------------------------
  33.  
  34. #ifndef __CLSTYPES_H
  35. #include <clstypes.h>
  36. #endif
  37.  
  38. #ifndef __SORTABLE_H
  39. #include <sortable.h>
  40. #endif
  41.  
  42. #ifndef __CONTAIN_H
  43. #include <contain.h>
  44. #endif
  45.  
  46. #if !defined( __ERREXIT_H )
  47. #include <errexit.h>
  48. #endif
  49.  
  50. // End Implementation Dependencies -------------------------------------------
  51.  
  52.  
  53. // Member Function //
  54.  
  55. SortedArray::~SortedArray()
  56.  
  57. // Summary -----------------------------------------------------------------
  58. //
  59. //      Destructor for a SortedArray object.
  60. //
  61. //        We don't do anything here, because the destructor for Array
  62. //        will take care of destroying the contained objects.
  63. //
  64. // End ---------------------------------------------------------------------
  65. {
  66. }
  67. // End Destructor //
  68.  
  69.  
  70. // Member Function //
  71.  
  72. void    SortedArray::add( Object& toAdd )
  73.  
  74. // Summary -----------------------------------------------------------------
  75. //
  76. //      Adds an object to the sorted array.
  77. //
  78. // Parameter
  79. //
  80. //      toAdd
  81. //
  82. //      The object we are to add to SortedArray.
  83. //
  84. // Functional Description
  85. //
  86. //      We check that the object we are adding is indeed sortable, then
  87. //      expand the array if needed.  We then search for the point to
  88. //      insert the object and move the succeeding objects down the array
  89. //      to make room for the new object.
  90. //  
  91. // End ---------------------------------------------------------------------
  92. {
  93.  
  94.     if ( toAdd.isSortable() )
  95.     {
  96.         if ( lastElementIndex == upperbound )
  97.         {
  98.             reallocate( arraySize() + 1 );
  99.         }
  100.  
  101.         int insertionPoint = 0;
  102.         while ( insertionPoint <= lastElementIndex - lowerbound &&
  103.                 ((Sortable&)(*(theArray[ insertionPoint ])) < (Sortable&)toAdd) )
  104.         {
  105.             insertionPoint++;
  106.         }
  107.  
  108.         for ( int i = lastElementIndex - lowerbound; i >= insertionPoint; i-- )
  109.         {
  110.             theArray[i+1] = theArray[i];
  111.         }
  112.         theArray[ insertionPoint ] = &toAdd;
  113.         itemsInContainer++;
  114.         lastElementIndex++;
  115.  
  116.     }
  117.     else // the object we are to add isn't sortable.
  118.         ClasslibErrorExit("Error:  Object must be sortable.",
  119.                           __ENOTSORT);
  120. }
  121. // End Member Function SortedArray::add //
  122.  
  123.  
  124. // Member Function //
  125.  
  126. void    SortedArray::detach( const Object& toDetach, int deleteObjectToo )
  127.  
  128. // Summary -----------------------------------------------------------------
  129. //
  130. //      Detachs an object to the sorted array.
  131. //
  132. // Parameter
  133. //
  134. //      toDetach
  135. //
  136. //      The object we are to detach to SortedArray.
  137. //
  138. // Functional Description
  139. //
  140. // End ---------------------------------------------------------------------
  141. {
  142.     if ( toDetach == NOOBJECT )
  143.         return;
  144.  
  145.     int detachPoint, moveCount;
  146.  
  147.     for ( detachPoint = lowerbound; detachPoint <= upperbound; detachPoint++ )
  148.     {
  149.         if ( *theArray[ detachPoint ] == toDetach )
  150.         {
  151.             if ( deleteObjectToo )
  152.             {
  153.                 delete theArray[ detachPoint ];
  154.             }
  155.             for ( moveCount = detachPoint; 
  156.                   moveCount < lastElementIndex;
  157.                   moveCount++ )
  158.             {
  159.                 theArray[ moveCount ] = theArray[ moveCount + 1 ];
  160.             }
  161.             theArray[ lastElementIndex-- ] = ZERO;
  162.             itemsInContainer--;
  163.             return;
  164.         }
  165.     } // end for //
  166. }
  167. // End Member Function SortedArray::detach //
  168.  
  169.  
  170. // Member Function //
  171.  
  172. classType SortedArray::isA() const
  173.  
  174. // Summary -----------------------------------------------------------------
  175. //
  176. //      Returns a predefined value for the class SortedArray.
  177. //
  178. // Parameters
  179. //
  180. //      none
  181. //
  182. // End ---------------------------------------------------------------------
  183. {
  184.     return sortedArrayClass;
  185. }
  186. // End Member Function SortedArray::isA //
  187.  
  188.  
  189. // Member Function //
  190.  
  191. char *SortedArray::nameOf() const
  192.  
  193. // Summary -----------------------------------------------------------------
  194. //
  195. //      Returns the string "SortedArray".
  196. //
  197. // Parameters
  198. //
  199. //      none
  200. //
  201. // End ---------------------------------------------------------------------
  202. {
  203.     return "SortedArray";
  204. }
  205. // End Member Function SortedArray::nameOf //
  206.