-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
297 lines (241 loc) · 7.63 KB
/
list.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
290
291
292
293
294
295
296
297
#include <stdlib.h>
#include <string.h>
#include "list.h"
typedef struct list_node_s list_node_t;
struct list_node_s {
void *element;
list_node_t *next;
list_node_t *prev;
};
typedef struct {
list_node_t **data;
size_t size;
size_t headdirt;
size_t taildirt;
} list_atcache_t;
struct list_s {
list_node_t *head;
list_node_t *tail;
list_atcache_t atcache;
size_t length;
};
/* List node */
static list_node_t *list_node_create(void *element) {
list_node_t *node = malloc(sizeof(*node));
node->element = element;
node->next = NULL;
node->prev = NULL;
return node;
}
static void list_node_destroy(list_node_t *node) {
free(node);
}
static void list_node_scrub(list_node_t **node) {
list_node_destroy(*node);
*node = NULL;
}
static void list_atcache_create(list_t *list) {
list->atcache.data = calloc(16, sizeof(list_node_t));
list->atcache.size = 16;
list->atcache.taildirt = 0;
list->atcache.headdirt = 0;
}
static void list_atcache_destroy(list_t *list) {
free(list->atcache.data);
}
static void list_atcache_resize(list_t *list) {
size_t last = list->atcache.size;
list->atcache.size *= 2;
list->atcache.data = realloc(list->atcache.data, sizeof(list_node_t*) * list->atcache.size);
memset(&list->atcache.data[last], 0, sizeof(list_node_t*) * (list->atcache.size - last));
}
static void list_atcache_thrash(list_t *list) {
memset(list->atcache.data, 0, sizeof(list_node_t*) * list->atcache.size);
list->atcache.headdirt = 0;
list->atcache.taildirt = 0;
}
static void list_atcache_check(list_t *list) {
if (list->atcache.taildirt > 0 && list->atcache.headdirt == 0) {
size_t index = list->length - (list->atcache.taildirt + 1);
if (index >= list->atcache.size) {
list_atcache_thrash(list);
return;
}
for (size_t i = index; i < list->atcache.taildirt; i++)
list->atcache.data[index] = NULL;
return;
}
/* thrash it all */
if (list->atcache.headdirt > 0)
list_atcache_thrash(list);
}
static void list_atcache_cache_index(list_t *list, list_node_t *node, size_t index) {
list_atcache_check(list);
if (list->length >= list->atcache.size)
list_atcache_resize(list);
list->atcache.data[index] = node;
}
static void list_atcache_cache(list_t *list, list_node_t *node) {
return list_atcache_cache_index(list, node, list->length);
}
/* List */
list_t *list_create(void) {
list_t *list = malloc(sizeof(*list));
list->head = NULL;
list->tail = NULL;
list->length = 0;
list_atcache_create(list);
return list;
}
void list_destroy(list_t *list) {
list_node_t *temp;
for (list_node_t *node = list->head; node; ) {
temp = node->next;
list_node_destroy(node);
node = temp;
}
list_atcache_destroy(list);
free(list);
}
void list_push(list_t *list, void *element) {
list_node_t *node = list_node_create(element);
if (!list->head)
list->head = node;
else {
list->tail->next = node;
node->prev = list->tail;
}
list->tail = node;
list_atcache_cache(list, node);
list->length++;
}
void list_prepend(list_t *list, void *element) {
list_node_t *node = list_node_create(element);
node->next = list->head;
node->prev = list->tail;
list->head = node;
list->atcache.headdirt++;
list->length++;
}
void *list_pop(list_t *list) {
if (!list->head)
return NULL;
void *element = list->tail->element;
list->tail = list->tail->prev;
list_node_scrub((list->tail) ? &list->tail->next : &list->head);
list->length--;
list->atcache.taildirt++;
return element;
}
void *list_shift(list_t *list) {
if (!list->head)
return NULL;
void *element = list->head->element;
list->head = list->head->next;
list_node_scrub((list->head) ? &list->head->prev : &list->tail);
list->length--;
list->atcache.headdirt++;
return element;
}
void *list_at(list_t *list, size_t index) {
list_atcache_check(list);
if (list->atcache.data[index])
return list->atcache.data[index]->element;
if (index > list->length)
return NULL;
list_node_t *node = NULL;
if (index > list->length / 2) {
node = list->tail;
for (size_t i = list->length / 2; i < index; i++)
node = node->prev;
} else {
node = list->head;
for (size_t i = 0; i < index; i++)
node = node->next;
}
if (node) {
list_atcache_cache_index(list, node, index);
return node->element;
}
return NULL;
}
list_t *list_copy(list_t *list) {
list_t *copy = list_create();
for (list_node_t *curr = list->head; curr; curr = curr->next)
list_push(copy, curr->element);
return copy;
}
bool list_erase(list_t *list, void *element) {
for (list_node_t *curr = list->head; curr; curr = curr->next) {
if (curr->element != element)
continue;
if (curr == list->head)
list->head = list->head->next;
if (curr == list->tail)
list->tail = list->tail->prev;
if (curr->next)
curr->next->prev = curr->prev;
if (curr->prev)
curr->prev->next = curr->next;
list_node_destroy(curr);
list->length--;
list_atcache_thrash(list);
return true;
}
return false;
}
bool list_find(list_t *list, const void *element) {
list_node_t *node = list->head;
list_atcache_thrash(list);
for(size_t index = 0; node && node->element != element; node = node->next, index++)
list_atcache_cache_index(list, node, index);
return !!node;
}
void *list_search_impl(list_t *list, const void *pass, bool (*predicate)(const void *, const void *)) {
list_node_t *node = list->head;
list_atcache_thrash(list);
for(size_t index = 0; node && !predicate(node->element, pass); node = node->next, index++)
list_atcache_cache_index(list, node, index);
return (node) ? node->element : NULL;
}
size_t list_length(list_t *list) {
return (list) ? list->length : 0;
}
void list_clear(list_t *list) {
while (list->length != 0)
list_pop(list);
list_atcache_thrash(list);
}
void list_foreach_impl(list_t *list, void *pass, void (*callback)(void *, void *)) {
for (list_node_t *curr = list->head; curr; curr = curr->next)
callback(curr->element, pass);
}
static list_node_t *list_sort_split(list_node_t *node) {
if (!node || !node->next) return NULL;
list_node_t *split = node->next;
node->next = split->next;
split->next = list_sort_split(split->next);
return split;
}
static list_node_t *list_sort_merge(list_node_t *a, list_node_t *b, bool (*predicate)(const void *, const void *)) {
if (!a) return b;
if (!b) return a;
if (predicate(a->element, b->element)) {
b->next = list_sort_merge(a, b->next, predicate);
b->next->prev = b;
return b;
}
a->next = list_sort_merge(a->next, b, predicate);
a->next->prev = a;
return a;
}
static list_node_t *list_sort_dispatch(list_node_t *begin, bool (*predicate)(const void *, const void *)) {
if (!begin) return NULL;
if (!begin->next) return begin;
list_node_t *split = list_sort_split(begin);
return list_sort_merge(list_sort_dispatch(begin, predicate), list_sort_dispatch(split, predicate), predicate);
}
void list_sort_impl(list_t *list, bool (*predicate)(const void *, const void *)) {
list->head = list_sort_dispatch(list->head, predicate);
list_atcache_thrash(list);
}