home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP10-8.ZIP / CLASSSRC.ZIP / SORTARRY.CPP < prev    next >
C/C++ Source or Header  |  1990-09-26  |  5KB  |  218 lines

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