-
Notifications
You must be signed in to change notification settings - Fork 14
/
arraylist.c
206 lines (179 loc) · 4.5 KB
/
arraylist.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
/**
* Arraylist implementation
* (c) 2011 @marekweb
*
* Uses dynamic extensible arrays.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "arraylist.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;
}
l->body = realloc(l->body, sizeof(void*) * new_capacity);
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);
}