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 pointer to the array's data.
85 * Use with care, the data belongs to the array and you should not
86 * realloc() or free() it, you should only use it a as a read-only chunk of
95 * Access an element by index.
97 * A pointer is returned so the real element can be modified in place.
101 assert (i < this._size);
102 return this._data + i;
106 * Append a copy of the element x at the end of the array.
108 * This can trigger an allocation if the array is not big enough.
110 * Returns true if the append was successful, false otherwise (i.e. an
111 * allocation was triggered but the allocation failed) in which case the
112 * internal state is not changed.
116 if (this._size == this._capacity)
119 this._data[this._size] = x;
121 return this._data + this._size - 1;
125 * Append a copy of the element x at the end of the array.
127 * This can trigger an allocation if the array is not big enough.
129 * Returns true if the append was successful, false otherwise (i.e. an
130 * allocation was triggered but the allocation failed) in which case the
131 * internal state is not changed.
133 T* insert_sorted(in T x)
136 for (; i < this._size; i++) {
137 if (this._data[i] < x)
141 if (this._size == this._capacity)
144 memmove(this._data + i + 1, this._data + i,
145 (this._size - i) * T.sizeof);
148 return this._data + i;
152 * Remove the element at position pos.
154 void remove_at(size_t pos)
157 // move the rest of the items one place to the front
158 memmove(this._data + pos, this._data + pos + 1,
159 (this._size - pos) * T.sizeof);
163 * Remove the first occurrence of the element x from the array.
167 for (size_t i = 0; i < this._size; i++) {
168 if (this._data[i] == x) {
177 * Change the current capacity of the array to new_capacity.
179 * This can enlarge or shrink the array, depending on the current capacity.
180 * If new_capacity is 0, the array is enlarged to hold double the current
181 * size. If new_capacity is less than the current size, the current size is
182 * truncated, and the (size - new_capacity) elements at the end are lost.
184 * Returns true if the resize was successful, false otherwise (and the
185 * internal state is not changed).
187 bool resize(in size_t new_capacity=0)
189 // adjust new_capacity if necessary
190 if (new_capacity == 0)
191 new_capacity = this._size * 2;
192 if (new_capacity == 0)
194 // reallocate the memory with the new_capacity
195 T* new_data = cast(T*) realloc(this._data, new_capacity * T.sizeof);
196 if (new_data is null)
198 this._data = new_data;
199 this._capacity = new_capacity;
200 // truncate the size if necessary
201 if (this._size > this._capacity)
202 this._size = this._capacity;
207 * Remove all the elements of the array and set the capacity to 0.
211 this._data = cast(T*) realloc(this._data, 0);
212 assert (this._data is null);
222 DynArray!(int) array;
223 assert (array.length == 0);
224 assert (array.capacity == 0);
225 assert (array.ptr is null);
226 assert (array.append(5));
227 assert (array.length == 1);
228 assert (array.capacity >= 1);
229 assert (array.ptr !is null);
230 for (auto i = 0; i < array.length; i++)
231 assert (*array[i] == 5);
232 assert (array.append(6));
233 assert (array.length == 2);
234 assert (array.capacity >= 2);
235 assert (array.ptr !is null);
237 while (j < array.length)
238 assert (*array[j] == (5 + j++));
241 assert (array.length == 1);
242 assert (array.capacity >= 1);
243 assert (array.ptr !is null);
244 for (auto i = 0; i < array.length; i++)
245 assert (*array[i] == 6);
246 assert (array.resize(100));
247 assert (array.length == 1);
248 assert (array.capacity >= 100);
249 assert (array.ptr !is null);
251 assert (array.length == 0);
252 assert (array.capacity == 0);
253 assert (array.ptr is null);
257 // vim: set et sw=4 sts=4 :