| 1 | // -*- C++ -*-cxx . . $ `<file>/vendor/Heap-Layers/utility/dynarray.h`/ |
| 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_Hcxx . . $ `vendor/Heap-Layers/utility/dynarray.h:19:9`! |
| 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 HLcxx . . $ HL/ { |
| 34 | |
| 35 | template <class ObjType> |
| 36 | class DynamicArraycxx . . $ HL/DynamicArray# { |
| 37 | public: |
| 38 | DynamicArraycxx . . $ HL/DynamicArray#`DynamicArray<ObjType>`(49f6e7a06ebc5aa8).() |
| 39 | : internalArraycxx . . $ HL/DynamicArray#internalArray. (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!), |
| 40 | internalArrayLengthcxx . . $ HL/DynamicArray#internalArrayLength. (0) |
| 41 | {} |
| 42 | |
| 43 | ~cxx . . $ HL/DynamicArray#`~DynamicArray<ObjType>`(49f6e7a06ebc5aa8).DynamicArray() |
| 44 | { |
| 45 | clearcxx . . $ HL/DynamicArray#clear(49f6e7a06ebc5aa8).(); |
| 46 | } |
| 47 | |
| 48 | /// Clear deletes everything in the array. |
| 49 | inline void clearcxx . . $ HL/DynamicArray#clear(49f6e7a06ebc5aa8).() { |
| 50 | if (internalArraycxx . . $ HL/DynamicArray#internalArray. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 51 | delete internalArraycxx . . $ HL/DynamicArray#internalArray.; |
| 52 | internalArraycxx . . $ HL/DynamicArray#internalArray. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 53 | internalArrayLengthcxx . . $ HL/DynamicArray#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& operatorcxx . . $ HL/DynamicArray#`operator[]`(e2681f9bb6b2f357).[] (int index) const { |
| 60 | assertcxx . . $ `/usr/include/assert.h:50:10`! (index < internalArrayLength); |
| 61 | assertcxx . . $ `/usr/include/assert.h:50:10`! (index >= 0); |
| 62 | return internalArraycxx . . $ HL/DynamicArray#internalArray.[index]; |
| 63 | } |
| 64 | |
| 65 | /// Access an array index by reference, growing the array if necessary. |
| 66 | inline ObjType& operatorcxx . . $ HL/DynamicArray#`operator[]`(7a0ef1c7079e8533).[] (int index) { |
| 67 | assertcxx . . $ `/usr/include/assert.h:50:10`! (index >= 0); |
| 68 | if (index >= internalArrayLengthcxx . . $ HL/DynamicArray#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 (internalArraycxx . . $ HL/DynamicArray#internalArray. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 79 | memcpy (arr, internalArraycxx . . $ HL/DynamicArray#internalArray., internalArrayLengthcxx . . $ HL/DynamicArray#internalArrayLength. * sizeof(ObjType)); |
| 80 | delete internalArraycxx . . $ HL/DynamicArray#internalArray.; |
| 81 | #if MALLOC_TRACE |
| 82 | printf ("f %x\n", internalArray); |
| 83 | #endif |
| 84 | } |
| 85 | internalArraycxx . . $ HL/DynamicArray#internalArray. = arr; |
| 86 | internalArrayLengthcxx . . $ HL/DynamicArray#internalArrayLength. = newSize; |
| 87 | //printf ("\ninternalArrayLength %x = %d\n", this, internalArrayLength); |
| 88 | } |
| 89 | return internalArraycxx . . $ HL/DynamicArray#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 trimcxx . . $ HL/DynamicArray#trim(d4f767463ce0a6b3). (int nelts) { |
| 98 | |
| 99 | // Halve the array if the number of elements |
| 100 | // drops below one-fourth of the array size. |
| 101 | |
| 102 | if (internalArraycxx . . $ HL/DynamicArray#internalArray. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 103 | if (nelts * 4 < internalArrayLengthcxx . . $ HL/DynamicArray#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, internalArraycxx . . $ HL/DynamicArray#internalArray., sizeof(ObjType) * nelts); |
| 110 | delete internalArraycxx . . $ HL/DynamicArray#internalArray.; |
| 111 | #if MALLOC_TRACE |
| 112 | printf ("f %x\n", internalArray); |
| 113 | #endif |
| 114 | internalArraycxx . . $ HL/DynamicArray#internalArray. = arr; |
| 115 | internalArrayLengthcxx . . $ HL/DynamicArray#internalArrayLength. = newSize; |
| 116 | } |
| 117 | assertcxx . . $ `/usr/include/assert.h:50:10`! (nelts <= internalArrayLength); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | private: |
| 123 | |
| 124 | /// The pointer to the current array. |
| 125 | ObjType * internalArraycxx . . $ HL/DynamicArray#internalArray.; |
| 126 | |
| 127 | /// The length of the internal array, in elements. |
| 128 | int internalArrayLengthcxx . . $ HL/DynamicArray#internalArrayLength.; |
| 129 | }; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | #endif |