1 | /* |
---|
2 | |
---|
3 | Heap Layers: An Extensible Memory Allocation Infrastructure |
---|
4 | |
---|
5 | Copyright (C) 2000-2003 by Emery Berger |
---|
6 | http://www.cs.umass.edu/~emery |
---|
7 | emery@cs.umass.edu |
---|
8 | |
---|
9 | This program is free software; you can redistribute it and/or modify |
---|
10 | it under the terms of the GNU General Public License as published by |
---|
11 | the Free Software Foundation; either version 2 of the License, or |
---|
12 | (at your option) any later version. |
---|
13 | |
---|
14 | This program is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | GNU General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
22 | |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef _DYNARRAY_H_ |
---|
26 | #define _DYNARRAY_H_ |
---|
27 | |
---|
28 | #include <assert.h> |
---|
29 | #include <stdlib.h> |
---|
30 | |
---|
31 | /** |
---|
32 | * @class DynamicArray |
---|
33 | * @brief A dynamic array that grows to fit any index for assignment. |
---|
34 | * |
---|
35 | * This array also features a clear() method, |
---|
36 | * to free the entire array, and a trim(n) method, |
---|
37 | * which tells the array it is no bigger than n elements. |
---|
38 | */ |
---|
39 | |
---|
40 | namespace HL { |
---|
41 | |
---|
42 | template <class ObjType> |
---|
43 | class DynamicArray { |
---|
44 | public: |
---|
45 | DynamicArray (void) |
---|
46 | : internalArray (NULL), |
---|
47 | internalArrayLength (0) |
---|
48 | {} |
---|
49 | |
---|
50 | ~DynamicArray (void) |
---|
51 | { |
---|
52 | clear(); |
---|
53 | } |
---|
54 | |
---|
55 | /// Clear deletes everything in the array. |
---|
56 | inline void clear (void) { |
---|
57 | if (internalArray != NULL) { |
---|
58 | delete internalArray; |
---|
59 | internalArray = NULL; |
---|
60 | internalArrayLength = 0; |
---|
61 | //printf ("\ninternalArrayLength %x = %d\n", this, internalArrayLength); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | /// Read-only access to an array element; asserts that index is in range. |
---|
66 | inline const ObjType& operator[] (int index) const { |
---|
67 | assert (index < internalArrayLength); |
---|
68 | assert (index >= 0); |
---|
69 | return internalArray[index]; |
---|
70 | } |
---|
71 | |
---|
72 | /// Access an array index by reference, growing the array if necessary. |
---|
73 | inline ObjType& operator[] (int index) { |
---|
74 | assert (index >= 0); |
---|
75 | if (index >= internalArrayLength) { |
---|
76 | |
---|
77 | // This index is beyond the current size of the array. |
---|
78 | // Grow the array by doubling and copying the old array into the new. |
---|
79 | |
---|
80 | const int newSize = index * 2 + 1; |
---|
81 | ObjType * arr = new ObjType[newSize]; |
---|
82 | #if MALLOC_TRACE |
---|
83 | printf ("m %x %d\n", arr, newSize * sizeof(ObjType)); |
---|
84 | #endif |
---|
85 | if (internalArray != NULL) { |
---|
86 | memcpy (arr, internalArray, internalArrayLength * sizeof(ObjType)); |
---|
87 | delete internalArray; |
---|
88 | #if MALLOC_TRACE |
---|
89 | printf ("f %x\n", internalArray); |
---|
90 | #endif |
---|
91 | } |
---|
92 | internalArray = arr; |
---|
93 | internalArrayLength = newSize; |
---|
94 | //printf ("\ninternalArrayLength %x = %d\n", this, internalArrayLength); |
---|
95 | } |
---|
96 | return internalArray[index]; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Trim informs the array that it is now only nelts long |
---|
101 | * as far as the client is concerned. This may trigger |
---|
102 | * shrinking of the array. |
---|
103 | */ |
---|
104 | inline void trim (int nelts) { |
---|
105 | |
---|
106 | // Halve the array if the number of elements |
---|
107 | // drops below one-fourth of the array size. |
---|
108 | |
---|
109 | if (internalArray != NULL) { |
---|
110 | if (nelts * 4 < internalArrayLength) { |
---|
111 | const int newSize = nelts * 2; |
---|
112 | ObjType * arr = new ObjType[newSize]; |
---|
113 | #if MALLOC_TRACE |
---|
114 | printf ("m %x %d\n", arr, newSize * sizeof(ObjType)); |
---|
115 | #endif |
---|
116 | memcpy (arr, internalArray, sizeof(ObjType) * nelts); |
---|
117 | delete internalArray; |
---|
118 | #if MALLOC_TRACE |
---|
119 | printf ("f %x\n", internalArray); |
---|
120 | #endif |
---|
121 | internalArray = arr; |
---|
122 | internalArrayLength = newSize; |
---|
123 | } |
---|
124 | assert (nelts <= internalArrayLength); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | private: |
---|
130 | |
---|
131 | /// The pointer to the current array. |
---|
132 | ObjType * internalArray; |
---|
133 | |
---|
134 | /// The length of the internal array, in elements. |
---|
135 | int internalArrayLength; |
---|
136 | }; |
---|
137 | |
---|
138 | }; |
---|
139 | |
---|
140 | #endif |
---|