| 1 | // -*- C++ -*- |
| 2 | |
| 3 | /* |
| 4 | |
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
| 6 | |
| 7 | Copyright (C) 2000-2020 by Emery Berger |
| 8 | http://www.emeryberger.com |
| 9 | [email protected] |
| 10 | |
| 11 | Heap Layers is distributed under the terms of the Apache 2.0 license. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | #ifndef HL_DYNARRAY_H |
| 19 | #define HL_DYNARRAY_H |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | /** |
| 25 | * @class DynamicArray |
| 26 | * @brief A dynamic array that grows to fit any index for assignment. |
| 27 | * |
| 28 | * This array also features a clear() method, |
| 29 | * to free the entire array, and a trim(n) method, |
| 30 | * which tells the array it is no bigger than n elements. |
| 31 | */ |
| 32 | |
| 33 | namespace HL { |
| 34 | |
| 35 | template <class ObjType> |
| 36 | class DynamicArray { |
| 37 | public: |
| 38 | DynamicArray() |
| 39 | : internalArray (NULL), |
| 40 | internalArrayLength (0) |
| 41 | {} |
| 42 | |
| 43 | ~DynamicArray() |
| 44 | { |
| 45 | clear(); |
| 46 | } |
| 47 | |
| 48 | /// Clear deletes everything in the array. |
| 49 | inline void clear() { |
| 50 | if (internalArray != NULL) { |
| 51 | delete internalArray; |
| 52 | internalArray = NULL; |
| 53 | internalArrayLength = 0; |
| 54 | //printf ("\ninternalArrayLength %x = %d\n", this, internalArrayLength); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// Read-only access to an array element; asserts that index is in range. |
| 59 | inline const ObjType& operator[] (int index) const { |
| 60 | assert (index < internalArrayLength); |
| 61 | assert (index >= 0); |
| 62 | return internalArray[index]; |
| 63 | } |
| 64 | |
| 65 | /// Access an array index by reference, growing the array if necessary. |
| 66 | inline ObjType& operator[] (int index) { |
| 67 | assert (index >= 0); |
| 68 | if (index >= internalArrayLength) { |
| 69 | |
| 70 | // This index is beyond the current size of the array. |
| 71 | // Grow the array by doubling and copying the old array into the new. |
| 72 | |
| 73 | const int newSize = index * 2 + 1; |
| 74 | ObjType * arr = new ObjType[newSize]; |
| 75 | #if MALLOC_TRACE |
| 76 | printf ("m %x %d\n", arr, newSize * sizeof(ObjType)); |
| 77 | #endif |
| 78 | if (internalArray != NULL) { |
| 79 | memcpy (arr, internalArray, internalArrayLength * sizeof(ObjType)); |
| 80 | delete internalArray; |
| 81 | #if MALLOC_TRACE |
| 82 | printf ("f %x\n", internalArray); |
| 83 | #endif |
| 84 | } |
| 85 | internalArray = arr; |
| 86 | internalArrayLength = newSize; |
| 87 | //printf ("\ninternalArrayLength %x = %d\n", this, internalArrayLength); |
| 88 | } |
| 89 | return internalArray[index]; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Trim informs the array that it is now only nelts long |
| 94 | * as far as the client is concerned. This may trigger |
| 95 | * shrinking of the array. |
| 96 | */ |
| 97 | inline void trim (int nelts) { |
| 98 | |
| 99 | // Halve the array if the number of elements |
| 100 | // drops below one-fourth of the array size. |
| 101 | |
| 102 | if (internalArray != NULL) { |
| 103 | if (nelts * 4 < internalArrayLength) { |
| 104 | const int newSize = nelts * 2; |
| 105 | ObjType * arr = new ObjType[newSize]; |
| 106 | #if MALLOC_TRACE |
| 107 | printf ("m %x %d\n", arr, newSize * sizeof(ObjType)); |
| 108 | #endif |
| 109 | memcpy (arr, internalArray, sizeof(ObjType) * nelts); |
| 110 | delete internalArray; |
| 111 | #if MALLOC_TRACE |
| 112 | printf ("f %x\n", internalArray); |
| 113 | #endif |
| 114 | internalArray = arr; |
| 115 | internalArrayLength = newSize; |
| 116 | } |
| 117 | assert (nelts <= internalArrayLength); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | private: |
| 123 | |
| 124 | /// The pointer to the current array. |
| 125 | ObjType * internalArray; |
| 126 | |
| 127 | /// The length of the internal array, in elements. |
| 128 | int internalArrayLength; |
| 129 | }; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | #endif |