4 * This module contains a simple dynamic array implementation for use in the
5 * Naive Garbage Collector. Standard D dynamic arrays can't be used because
6 * they rely on the GC itself.
9 * Copyright: Public Domain
10 * License: Public Domain
11 * Authors: Leandro Lucarella <llucax@gmail.com>
14 module rt.gc.cdgc.dynarray;
16 import tango.stdc.stdlib: realloc;
17 import tango.stdc.string: memmove;
20 private void Invariant(T)(DynArray!(T)* a)
22 assert ((a._data && a._capacity)
23 || ((a._data is null) && (a._capacity == 0)));
24 assert (a._capacity >= a._size);
33 * This is a simple dynamic array implementation. D dynamic arrays can't be
34 * used because they rely on the GC, and we are implementing the GC.
41 /// Memory block to hold the array.
44 /// Total array capacity, in number of elements.
47 /// Current array size, in number of elements.
59 * Check the structure invariant.
75 * Get the array capacity.
79 return this._capacity;
83 * Get the total ammount of bytes the elements consumes (capacity included).
85 size_t elements_sizeof()
87 return this._capacity * (T.sizeof + (T*).sizeof);
91 * Get the pointer to the array's data.
93 * Use with care, the data belongs to the array and you should not
94 * realloc() or free() it, you should only use it a as a read-only chunk of
103 * Access an element by index.
105 * A pointer is returned so the real element can be modified in place.
109 assert (i < this._size);
110 return this._data + i;
114 * Append a copy of the element x at the end of the array.
116 * This can trigger an allocation if the array is not big enough.
118 * Returns true if the append was successful, false otherwise (i.e. an
119 * allocation was triggered but the allocation failed) in which case the
120 * internal state is not changed.
124 if (this._size == this._capacity)
127 this._data[this._size] = x;
129 return this._data + this._size - 1;
133 * Append a copy of the element x at the end of the array.
135 * This can trigger an allocation if the array is not big enough.
137 * Returns true if the append was successful, false otherwise (i.e. an
138 * allocation was triggered but the allocation failed) in which case the
139 * internal state is not changed.
141 T* insert_sorted(in T x)
144 for (; i < this._size; i++) {
145 if (this._data[i] < x)
149 if (this._size == this._capacity)
152 memmove(this._data + i + 1, this._data + i,
153 (this._size - i) * T.sizeof);
156 return this._data + i;
160 * Remove the element at position pos.
162 void remove_at(size_t pos)
165 // move the rest of the items one place to the front
166 memmove(this._data + pos, this._data + pos + 1,
167 (this._size - pos) * T.sizeof);
171 * Remove the first occurrence of the element x from the array.
175 for (size_t i = 0; i < this._size; i++) {
176 if (this._data[i] == x) {
185 * Change the current capacity of the array to new_capacity.
187 * This can enlarge or shrink the array, depending on the current capacity.
188 * If new_capacity is 0, the array is enlarged to hold double the current
189 * size. If new_capacity is less than the current size, the current size is
190 * truncated, and the (size - new_capacity) elements at the end are lost.
192 * Returns true if the resize was successful, false otherwise (and the
193 * internal state is not changed).
195 bool resize(in size_t new_capacity=0)
197 // adjust new_capacity if necessary
198 if (new_capacity == 0)
199 new_capacity = this._size * 2;
200 if (new_capacity == 0)
202 // reallocate the memory with the new_capacity
203 T* new_data = cast(T*) realloc(this._data, new_capacity * T.sizeof);
204 if (new_data is null)
206 this._data = new_data;
207 this._capacity = new_capacity;
208 // truncate the size if necessary
209 if (this._size > this._capacity)
210 this._size = this._capacity;
215 * Remove all the elements of the array and set the capacity to 0.
219 this._data = cast(T*) realloc(this._data, 0);
220 assert (this._data is null);
230 DynArray!(int) array;
231 assert (array.length == 0);
232 assert (array.capacity == 0);
233 assert (array.ptr is null);
234 assert (array.append(5));
235 assert (array.length == 1);
236 assert (array.capacity >= 1);
237 assert (array.ptr !is null);
238 for (auto i = 0; i < array.length; i++)
239 assert (*array[i] == 5);
240 assert (array.append(6));
241 assert (array.length == 2);
242 assert (array.capacity >= 2);
243 assert (array.ptr !is null);
245 while (j < array.length)
246 assert (*array[j] == (5 + j++));
249 assert (array.length == 1);
250 assert (array.capacity >= 1);
251 assert (array.ptr !is null);
252 for (auto i = 0; i < array.length; i++)
253 assert (*array[i] == 6);
254 assert (array.resize(100));
255 assert (array.length == 1);
256 assert (array.capacity >= 100);
257 assert (array.ptr !is null);
259 assert (array.length == 0);
260 assert (array.capacity == 0);
261 assert (array.ptr is null);
265 // vim: set et sw=4 sts=4 :