-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayListUtils.c
289 lines (262 loc) · 6.42 KB
/
ArrayListUtils.c
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
/**
* Arraylist implementation
*
*
* Uses dynamic extensible arrays.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "ArrayListUtils.h"
/*
* Interface section used for `makeheaders`.
*/
#if INTERFACE
struct arraylist {
unsigned int size; // Count of items currently in list
unsigned int capacity; // Allocated memory size, in items
void** body; // Pointer to allocated memory for items (of size capacity * sizeof(void*))
};
/**
* Iterates over a list, using the provided `unsigned int index` and `void* item`
* variables as containers.
*/
#define arraylist_iterate(l, index, item) \
for (index = 0, item = l->body[0]; index < l->size; item = l->body[++index])
#endif
// Initial capacity of the arraylist
#define ARRAYLIST_INITIAL_CAPACITY 4
/**
* Macro to shift a section of memory by an offset, used when inserting or removing items.
*/
#define arraylist_memshift(s, offset, length) memmove((s) + (offset), (s), (length)* sizeof(s));
/**
* Create a new, empty arraylist.
*/
arraylist* arraylist_create()
{
arraylist* new_list = malloc(sizeof(arraylist));
new_list->size = 0;
// Allocate the array
new_list->body = malloc(sizeof(void*) * ARRAYLIST_INITIAL_CAPACITY);
assert(new_list->body);
new_list->capacity = ARRAYLIST_INITIAL_CAPACITY;
return new_list;
}
/**
* Allocate sufficient array capacity for at least `size` elements.
*/
void arraylist_allocate(arraylist* l, unsigned int size)
{
assert(size > 0);
if (size > l->capacity) {
unsigned int new_capacity = l->capacity;
while (new_capacity < size) {
new_capacity *= 2;
}
void** temp = realloc(l->body, (sizeof(void*) * new_capacity));
if (!temp) return;
l->body = temp;
assert(l->body);
l->capacity = new_capacity;
}
}
/**
* Return the number of items contained in the list.
*/
extern inline unsigned int arraylist_size(arraylist* l) {
return l->size;
}
/**
* Add item at the end of the list.
*/
void arraylist_add(arraylist* l, void* item)
{
arraylist_allocate(l, l->size + 1);
l->body[l->size++] = item;
}
/**
* Pop (remove and return) an item off the end of the list.
*/
void* arraylist_pop(arraylist* l)
{
assert(l->size > 0);
return l->body[--l->size];
}
/**
* Return item located at index.
*/
void* arraylist_get(arraylist* l, unsigned int index)
{
assert(index < l->size);
return l->body[index];
}
/**
* Replace item at index with given value.
*/
void arraylist_set(arraylist* l, unsigned int index, void* value)
{
assert(index < l->size);
l->body[index] = value;
}
/**
* Insert item at index, shifting the following items by one spot.
*/
void arraylist_insert(arraylist* l, unsigned int index, void* value)
{
// Reallocate, if needed
arraylist_allocate(l, l->size + 1);
// Move data to create a spot for the new value
arraylist_memshift(l->body + index, 1, l->size - index);
l->body[index] = value;
l->size++;
}
/**
* Remove the item at index, shifting the following items back by one spot.
*/
void* arraylist_remove(arraylist* l, unsigned int index)
{
void* value = l->body[index];
arraylist_memshift(l->body + index + 1, -1, l->size - index);
l->size--;
return value;
}
/**
* Clear list of all items.
*/
void arraylist_clear(arraylist* l)
{
l->size = 0;
}
/**
* Return a slice of the list (of given length starting at index) as a new arraylist.
*/
arraylist* arraylist_slice(arraylist* l, unsigned int index, unsigned int length)
{
assert(index + length <= l->size);
arraylist* new_list = arraylist_create();
arraylist_allocate(new_list, length);
memmove(new_list->body, l->body + index, length * sizeof(void*));
new_list->size = length;
return new_list;
}
/**
* Return a slice of the list (from index to the end) as a new arraylist.
*/
arraylist* arraylist_slice_end(arraylist* l, unsigned int index)
{
return arraylist_slice(l, index, l->size - index);
}
/**
* Return a copy of the arraylist.
*/
arraylist* arraylist_copy(arraylist* l)
{
return arraylist_slice_end(l, 0);
}
/**
* Append a list onto another, in-place.
*/
void arraylist_join(arraylist* l, arraylist* source)
{
arraylist_splice(l, source, l->size);
}
/**
* Insert a list into another at the given index, in-place.
*/
void arraylist_splice(arraylist* l, arraylist* source, unsigned int index)
{
// Reallocate, if needed
arraylist_allocate(l, l->size + source->size);
// Move data to the right
arraylist_memshift(l->body + index, source->size, l->size - index);
// Copy the data over
memmove(l->body + index, source->body, source->size * sizeof(void*));
l->size += source->size;
}
void arraylist_destroy(arraylist* l)
{
free(l->body);
free(l);
}
//____________________________________________________________________________________ ALL TESTS
//
// /*
// * Make a few dummy pointers as values for testing.
// * These are never dereferenced.
// */
//
//void* a = (void*)0x500;
//void* b = (void*)0x501;
//void* c = (void*)0x502;
//void* d = (void*)0x503;
//void* e = (void*)0x504;
//void* f = (void*)0x505;
//
///*
// * Arraylist tests
// */
//printf("Running arraylist.c tests.\n");
//
//arraylist* l = arraylist_create();
//
//assert(l->size == 0);
//
//arraylist_add(l, a);
//arraylist_add(l, b);
//
//assert(l->size == 2);
//assert(arraylist_get(l, 0) == a);
//assert(arraylist_get(l, 1) == b);
//
//arraylist_add(l, c);
//arraylist_add(l, d);
//
//assert(l->size == 4);
//assert(arraylist_get(l, 3) == d);
//assert(arraylist_get(l, 2) == c);
//assert(arraylist_pop(l) == d);
//assert(arraylist_pop(l) == c);
//assert(l->size == 2);
//
//arraylist_add(l, e);
//arraylist_add(l, f);
//arraylist_insert(l, 2, c);
//
//assert(l->size == 5);
//assert(arraylist_get(l, 0) == a);
//assert(arraylist_get(l, 1) == b);
//assert(arraylist_get(l, 2) == c);
//assert(arraylist_get(l, 3) == e);
//assert(arraylist_get(l, 4) == f);
//
//arraylist_remove(l, 1);
//assert(l->size == 4);
//assert(arraylist_get(l, 0) == a);
//assert(arraylist_get(l, 1) == c);
//assert(arraylist_get(l, 2) == e);
//
//arraylist* slice = arraylist_slice(l, 1, 2);
//assert(slice->size == 2);
//assert(arraylist_get(slice, 0) == c);
//assert(arraylist_get(slice, 1) == e);
//
//arraylist* copy = arraylist_copy(l);
//assert(copy->size == l->size);
//int i;
//void* v;
//arraylist_iterate(copy, i, v) {
// assert(v == arraylist_get(l, i));
//}
//
//arraylist_debug(copy);
//
//arraylist_clear(l);
//assert(l->size == 0);
//arraylist_iterate(l, i, v) {
// assert(0); // Iterating over an empty list should never run.
//}
//
//arraylist_destroy(l);
//arraylist_destroy(slice);
//arraylist_destroy(copy);