-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
vector.spice
346 lines (309 loc) · 8.12 KB
/
vector.spice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import "std/type/error";
import "std/iterator/iterable";
import "std/iterator/iterator";
import "std/data/pair";
// Constants
const unsigned long INITIAL_CAPACITY = 5l;
const unsigned int RESIZE_FACTOR = 2;
// Add generic type definitions
type T dyn;
type UIntOrULong unsigned int|unsigned long;
type Numeric int|long|short;
/**
* A vector in Spice is a commonly used data structure, which can be used to represent a list of items.
*
* Time complexity:
* Insert: O(1)
* Delete: O(n * m); n = deleted elements, m = moved elements
* Search: O(n)
*
* Vectors pre-allocate space using an initial size and a resize factor to not have to re-allocate
* with every item pushed.
*/
public type Vector<T> struct : IIterable<T> {
heap T* contents // Pointer to the first data element
unsigned long capacity // Allocated number of items
unsigned long size // Current number of items
}
public p Vector.ctor(unsigned long initialCapacity = INITIAL_CAPACITY) {
// Allocate space for the initial number of elements
const long itemSize = sizeof(type T) / 8l;
assert itemSize != 0l;
unsafe {
Result<heap byte*> allocResult = sAlloc(itemSize * initialCapacity);
this.contents = (heap T*) allocResult.unwrap();
}
this.size = 0l;
this.capacity = initialCapacity;
}
public p Vector.ctor(unsigned int initialCapacity) {
this.ctor((long) initialCapacity);
}
public p Vector.ctor(unsigned long initAllocItems, const T& defaultValue) {
// Allocate space for the initial number of elements
this.ctor(initAllocItems);
// Fill in the default values
for int index = 0; index < initAllocItems; index++ {
unsafe {
this.contents[index] = defaultValue;
}
}
this.size = initAllocItems;
}
/**
* Checks if the vector contains any items at the moment
*
* @return Empty or not empty
*/
public f<bool> Vector.isEmpty() {
return this.size == 0;
}
/**
* Checks if the vector exhausts its capacity and needs to resize at the next call of push
*
* @return Full or not full
*/
public f<bool> Vector.isFull() {
return this.size == this.capacity;
}
/**
* Add an item at the end of the vector
*/
public p Vector.pushBack<T>(const T& item) {
// Check if we need to re-allocate memory
if this.isFull() {
this.resize(this.capacity * RESIZE_FACTOR);
}
// Insert the element at the back
unsafe {
this.contents[(int) this.size++] = item;
}
}
/**
* Get an item at a certain index
*
* @return item at index
*/
public f<T&> Vector.get(unsigned long index) {
if index >= this.size { panic(Error("Access index out of bounds")); }
unsafe {
return this.contents[index];
}
}
/**
* Get an item at a certain index
*
* @return item at index
*/
public f<T&> Vector.get(unsigned int index) {
return this.get((unsigned long) index);
}
/**
* Remove an item at a certain index
*
* @param index Index of the item to remove
*/
public p Vector.removeAt(unsigned long index) {
if index >= this.size { panic(Error("Access index out of bounds")); }
// Move all elements after the index one to the front
for unsigned long i = index; i < this.size - 1; i++ {
unsafe {
this.contents[i] = this.contents[i + 1];
}
}
// Decrement the size
this.size--;
}
/**
* Remove an item at a certain index
*
* @param index Index of the item to remove
*/
public p Vector.removeAt(unsigned int index) {
this.removeAt((unsigned long) index);
}
/**
* Get the first item in the vector
*
* @return item at index 0
*/
public f<T&> Vector.front() {
return this.get(0);
}
/**
* Get the last item in the vector
*
* @return item at index size - 1
*/
public f<T&> Vector.back() {
return this.get(this.size - 1);
}
/**
* Removes all items from the vector
*/
public p Vector.clear() {
this.size = 0l;
}
/**
* Reserves `itemCount` items
*/
public p Vector.reserve(unsigned long itemCount) {
if itemCount > this.capacity {
this.resize(itemCount);
}
}
/**
* Reserves `itemCount` items
*/
public p Vector.reserve(unsigned int itemCount) {
if itemCount > this.capacity {
this.resize((long) itemCount);
}
}
/**
* Retrieve the current size of the vector
*
* @return Current size of the vector
*/
public f<long> Vector.getSize() {
return this.size;
}
/**
* Retrieve the current capacity of the vector
*
* @return Current capacity of the vector
*/
public f<long> Vector.getCapacity() {
return this.capacity;
}
/**
* Retrieve a pointer to the data of the vector
*/
public f<T*> Vector.getDataPtr() {
unsafe {
return (T*) this.contents;
}
}
/**
* Frees allocated memory that is not used by the queue
*/
public p Vector.pack() {
// Return if no packing is required
if this.isFull() { return; }
// Pack the array
this.resize(this.size);
}
public f<bool> operator==<T>(const Vector<T>& lhs, const Vector<T>& rhs) {
// Compare the sizes
if lhs.size != rhs.size { return false; }
// Compare the contents
for unsigned long index = 0l; index < lhs.size; index++ {
if lhs.contents[index] != rhs.contents[index] { return false; }
}
return true;
}
public f<bool> operator!=<T>(const Vector<T>& lhs, const Vector<T>& rhs) {
return !(lhs == rhs);
}
/**
* Re-allocates heap space for the queue contents
*/
p Vector.resize(unsigned long itemCount) {
// Allocate the new memory
const long itemSize = sizeof(type T) / 8l;
assert itemSize != 0l;
unsafe {
heap byte* oldAddress = (heap byte*) this.contents;
unsigned long newSize = (unsigned long) (itemSize * itemCount);
Result<heap byte*> allocResult = sRealloc(oldAddress, newSize);
this.contents = (heap T*) allocResult.unwrap();
}
// Set new capacity
this.capacity = itemCount;
}
/**
* Iterator to iterate over a vector data structure
*/
public type VectorIterator<T> struct : IIterator<T> {
Vector<T>& vector
unsigned long cursor
}
public p VectorIterator.ctor<T>(Vector<T>& vector) {
this.vector = vector;
this.cursor = 0l;
}
/**
* Returns the current item of the vector
*
* @return Reference to the current item
*/
public inline f<T&> VectorIterator.get() {
return this.vector.get(this.cursor);
}
/**
* Returns the current index and the current item of the vector
*
* @return Pair of current index and reference to current item
*/
public inline f<Pair<unsigned long, T&>> VectorIterator.getIdx() {
return Pair<unsigned long, T&>(this.cursor, this.vector.get(this.cursor));
}
/**
* Check if the iterator is valid
*
* @return true or false
*/
public inline f<bool> VectorIterator.isValid() {
return this.cursor < this.vector.getSize();
}
/**
* Returns the current item of the vector iterator and moves the cursor to the next item
*/
public inline p VectorIterator.next() {
if !this.isValid() { panic(Error("Calling next() on invalid iterator")); }
this.cursor++;
}
/**
* Advances the cursor by one
*
* @param it VectorIterator
*/
public inline p operator++<T>(VectorIterator<T>& it) {
if it.cursor >= it.vector.getSize() { panic(Error("Iterator out of bounds")); }
it.cursor++;
}
/**
* Move the cursor back by one
*
* @param it VectorIterator
*/
public inline p operator--<T>(VectorIterator<T>& it) {
if it.cursor <= 0 { panic(Error("Iterator out of bounds")); }
it.cursor--;
}
/**
* Advances the cursor by the given offset
*
* @param it VectorIterator
* @param offset Offset
*/
public inline p operator+=<T, Numeric>(VectorIterator<T>& it, Numeric offset) {
if it.cursor + offset >= it.vector.getSize() || it.cursor + offset < 0 { panic(Error("Iterator out of bounds")); }
it.cursor += offset;
}
/**
* Move the cursor back by the given offset
*
* @param it VectorIterator
* @param offset Offset
*/
public inline p operator-=<T, Numeric>(VectorIterator<T>& it, Numeric offset) {
if it.cursor - offset >= it.vector.getSize() || it.cursor - offset < 0 { panic(Error("Iterator out of bounds")); }
it.cursor -= offset;
}
/**
* Retrieve an forward iterator for the vector
*/
public f<VectorIterator<T>> Vector.getIterator() {
return VectorIterator<T>(*this);
}