home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / Em / expandArray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-17  |  3.5 KB  |  132 lines

  1. /*  C O P Y R I G H T   N O T I C E :                                     */
  2. /* Copyright 1986 Eric Jul and Norm Hutchinson.     May not be used for any  */
  3. /* purpose without written permission from the authors.              */
  4.  
  5. #include "Kernel/h/system.h"
  6. #include "Kernel/h/assert.h"
  7. #include "Kernel/h/expandArray.h"
  8.  
  9. void CreateArray(fArrayPtr, fSize)
  10. int           **fArrayPtr;
  11. int             fSize;
  12. /* Create an array of the specified size */
  13. {
  14.     *fArrayPtr = (int *) malloc((unsigned)fSize);
  15.     bzero((char *) *fArrayPtr, (fSize));
  16. }
  17.  
  18. void DiscardArray(fArrayPtr)
  19. int           **fArrayPtr;
  20. /* Discard the array, freeing up its storage */
  21. {
  22.     free((char *)*fArrayPtr);
  23.     *fArrayPtr = (int *) 0;
  24. }
  25.  
  26. void ExpandArray(fArrayPtr, fOldSize, fNewSize)
  27. int                       **fArrayPtr;
  28. int                         fOldSize;
  29. int                         fNewSize;
  30. /* Expand the array to fNewSize (>= fOldSize assumed).
  31.    The elements are copied. */
  32. {
  33.     register int           *newArray;
  34.  
  35.     assert(fNewSize >= fOldSize);
  36.     newArray = (int *) malloc((unsigned)fNewSize);
  37.     /* Copy over the old part */
  38.     bcopy((char *)*fArrayPtr, (char *)newArray, fOldSize);
  39.     free((char *)*fArrayPtr);
  40.     /* Zero out the new part of the new array */
  41.     bzero(((char *) newArray) + fOldSize, fNewSize - fOldSize);
  42.     *fArrayPtr = newArray;
  43. }
  44.  
  45. void EnsureArray(fArrayPtr, fOldSize, fNewSize)
  46. int                       **fArrayPtr;
  47. int                         fOldSize;
  48. int                         fNewSize;
  49. /* Ensure that the size of the array is at least fNewSize */
  50. {
  51.     if (*fArrayPtr) {
  52.     if (fOldSize < fNewSize) ExpandArray(fArrayPtr, fOldSize, fNewSize);
  53.     } else {
  54.     CreateArray(fArrayPtr, fNewSize);
  55.     }
  56. }
  57.  
  58. /**********************************************************************/
  59. /* Dynamic array */
  60.  
  61. void DynAddUpper(fArr, fElem)
  62. DynArray            fArr;
  63. int                 fElem;
  64. /* Add another element at the upper bound, expand if necessary */
  65. {
  66.     if (fArr->count == fArr->maxCount) {
  67.     int oldSize = fArr->allocSize;
  68.     fArr->allocSize += fArr->allocSize;
  69.     fArr->maxCount += fArr->maxCount;
  70.     ExpandArray(&fArr->array, oldSize, fArr->allocSize);
  71.     }
  72.     fArr->array[fArr->count++] = fElem;
  73. }
  74.  
  75. void DynPut(fArr, fIndex, fElem)
  76. DynArray            fArr;
  77. int                 fIndex;
  78. int                 fElem;
  79. /* Put this into an array */
  80. {
  81.     assert ( 0 <= fIndex && fIndex < fArr->count );
  82.     fArr->array[fIndex] = fElem;
  83. }
  84.  
  85. void DynRemove(fArr, fIndex)
  86. DynArray            fArr;
  87. int                 fIndex;
  88. /* Remove an entry, compacting the arrry */
  89. {
  90.     assert ( 0 <= fIndex && fIndex < fArr->count );
  91.     if (fIndex < --fArr->count) {
  92.     fArr->array[fIndex] = fArr->array[fArr->count];
  93.     }
  94. }
  95.  
  96. void DynClear(fArr)
  97. DynArray            fArr;
  98. /* Reset the array */
  99. {
  100.     fArr->count         = 0;
  101. }
  102.  
  103. int DynCount(fArr)
  104. DynArray            fArr;
  105. /* Return the current count */
  106. {
  107.     return fArr->count;
  108. }
  109.  
  110. int DynGet(fArr, fIndex)
  111. DynArray            fArr;
  112. int                 fIndex;
  113. /* Get the element with the specified index */
  114. {
  115.  
  116.     return fArr->array[fIndex];
  117. }
  118.  
  119. DynArray DynCreate(fInitialCount)
  120. int                 fInitialCount;
  121. /* Create a DynArray with initail room for the specified element count */
  122. {
  123.     DynArray        arr = (DynArray) malloc(sizeof(DynArrayRec));
  124.     arr->allocSize      = fInitialCount * sizeof(int);
  125.     arr->count          = 0;
  126.     arr->maxCount       = fInitialCount;
  127.     CreateArray(&arr->array, arr->allocSize);
  128.     return arr;
  129. }
  130.  
  131. /**********************************************************************/
  132.