home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 11.ddi / CLASSSRC.ZIP / SORTARRY.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  4.9 KB  |  209 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 __ASSERT_H
  35. #include <assert.h>
  36. #define __ASSERT_H
  37. #endif
  38.  
  39. #ifndef __CLSTYPES_H
  40. #include <clstypes.h>
  41. #endif
  42.  
  43. #ifndef __SORTABLE_H
  44. #include <sortable.h>
  45. #endif
  46.  
  47. #ifndef __CONTAIN_H
  48. #include <contain.h>
  49. #endif
  50.  
  51. // End Implementation Dependencies -------------------------------------------
  52.  
  53.  
  54. // Member Function //
  55.  
  56. SortedArray::~SortedArray()
  57.  
  58. // Summary -----------------------------------------------------------------
  59. //
  60. //      Destructor for a SortedArray object.
  61. //
  62. //        We don't do anything here, because the destructor for Array
  63. //        will take care of destroying the contained objects.
  64. //
  65. // End ---------------------------------------------------------------------
  66. {
  67. }
  68. // End Destructor //
  69.  
  70.  
  71. // Member Function //
  72.  
  73. void    SortedArray::add( Object& toAdd )
  74.  
  75. // Summary -----------------------------------------------------------------
  76. //
  77. //      Adds an object to the sorted array.
  78. //
  79. // Parameter
  80. //
  81. //      toAdd
  82. //
  83. //      The object we are to add to SortedArray.
  84. //
  85. // Functional Description
  86. //
  87. //      We check that the object we are adding is indeed sortable, then
  88. //      expand the array if needed.  We then search for the point to
  89. //      insert the object and move the succeeding objects down the array
  90. //      to make room for the new object.
  91. //  
  92. // End ---------------------------------------------------------------------
  93. {
  94.  
  95.     if ( toAdd.isSortable() )
  96.     {
  97.         if ( lastElementIndex == upperbound )
  98.         {
  99.             reallocate( arraySize() + 1 );
  100.         }
  101.  
  102.         int insertionPoint = 0;
  103.         while ( insertionPoint <= lastElementIndex - lowerbound &&
  104.                 ((Sortable&)(*(theArray[ insertionPoint ])) < (Sortable&)toAdd) )
  105.         {
  106.             insertionPoint++;
  107.         }
  108.  
  109.         for ( int i = lastElementIndex - lowerbound; i >= insertionPoint; i-- )
  110.         {
  111.             theArray[i+1] = theArray[i];
  112.         }
  113.         theArray[ insertionPoint ] = &toAdd;
  114.         itemsInContainer++;
  115.         lastElementIndex++;
  116.  
  117.     }
  118.     else // the object we are to add isn't sortable.
  119.     {
  120.         cerr << "Error:  Object must be sortable.";
  121.         exit(__ENOTSORT);
  122.     }
  123. }
  124. // End Member Function SortedArray::add //
  125.  
  126.  
  127. // Member Function //
  128.  
  129. void    SortedArray::detach( const Object& toDetach, int deleteObjectToo )
  130.  
  131. // Summary -----------------------------------------------------------------
  132. //
  133. //      Detachs an object to the sorted array.
  134. //
  135. // Parameter
  136. //
  137. //      toDetach
  138. //
  139. //      The object we are to detach to SortedArray.
  140. //
  141. // Functional Description
  142. //
  143. // End ---------------------------------------------------------------------
  144. {
  145.     if ( toDetach == NOOBJECT )
  146.         return;
  147.  
  148.     int detachPoint, moveCount;
  149.  
  150.     for ( detachPoint = lowerbound; detachPoint <= upperbound; detachPoint++ )
  151.     {
  152.         if ( *theArray[ detachPoint ] == toDetach )
  153.         {
  154.             if ( deleteObjectToo )
  155.             {
  156.                 delete theArray[ detachPoint ];
  157.             }
  158.             for ( moveCount = detachPoint; 
  159.                   moveCount < lastElementIndex;
  160.                   moveCount++ )
  161.             {
  162.                 theArray[ moveCount ] = theArray[ moveCount + 1 ];
  163.             }
  164.             theArray[ lastElementIndex-- ] = ZERO;
  165.             itemsInContainer--;
  166.             return;
  167.         }
  168.     } // end for //
  169. }
  170. // End Member Function SortedArray::detach //
  171.  
  172.  
  173. // Member Function //
  174.  
  175. classType SortedArray::isA() const
  176.  
  177. // Summary -----------------------------------------------------------------
  178. //
  179. //      Returns a predefined value for the class SortedArray.
  180. //
  181. // Parameters
  182. //
  183. //      none
  184. //
  185. // End ---------------------------------------------------------------------
  186. {
  187.     return sortedArrayClass;
  188. }
  189. // End Member Function SortedArray::isA //
  190.  
  191.  
  192. // Member Function //
  193.  
  194. char *SortedArray::nameOf() const
  195.  
  196. // Summary -----------------------------------------------------------------
  197. //
  198. //      Returns the string "SortedArray".
  199. //
  200. // Parameters
  201. //
  202. //      none
  203. //
  204. // End ---------------------------------------------------------------------
  205. {
  206.     return "SortedArray";
  207. }
  208. // End Member Function SortedArray::nameOf //
  209.