-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.h
352 lines (318 loc) · 8.29 KB
/
list.h
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
347
348
349
350
351
352
#ifndef INC_LIST
#define INC_LIST
#include "buffers.h"
#include "security.h"
#include <stdbool.h>
#define single_list_foreach(list, element) for (single_list_element_t *(element) = (list).first; (element) != NULL; (element) = (element)->next)
#define single_list_ref_foreach(list, element) for (single_list_element_t *(element) = (list)->first; (element) != NULL; (element) = (element)->next)
#define single_list_foreach_free(list, element) for (single_list_element_t *(element) = (list).first; (element) != NULL; (element) = single_list_free_and_next(element))
#define single_list_ref_foreach_free(list, element) for (single_list_element_t *(element) = (list)->first; (element) != NULL; (element) = single_list_free_and_next(element))
#define double_list_foreach_free(list, element) for (double_list_element_t *(element) = (list).first; (element) != NULL; (element) = double_list_free_and_next(element))
typedef struct single_list_element
{
void *data;
struct single_list_element *next;
} single_list_element_t;
typedef struct
{
size_t count;
single_list_element_t *first;
single_list_element_t *last;
} single_list_t;
typedef struct double_list_element
{
void *data;
struct double_list_element *previous;
struct double_list_element *next;
} double_list_element_t;
typedef struct
{
size_t count;
double_list_element_t *first;
double_list_element_t *last;
} double_list_t;
size_t single_list_count(single_list_t* list)
{
return list->count;
}
bool single_list_iterate(single_list_t *list, bool (*f)(void *, void *), void *param)
{
single_list_element_t* element = list->first;
while (element != NULL)
{
single_list_element_t *next = element->next;
if(!f(element->data, param))
{
return false;
}
element = next;
}
return true;
}
bool single_list_iterate_free(single_list_t *list, bool (*f)(void *, void *), void *param)
{
single_list_element_t* element = list->first;
while (element != NULL)
{
single_list_element_t *next = element->next;
if(!f(element->data, param))
{
return false;
}
free(element);
list->first = next;
list->count--;
element = next;
}
list->last = NULL;
return true;
}
bool single_list_element_set_array_element(void *element, void *param)
{
buffer_t *buffer = param;
void **data = buffer->data;
data[buffer->len++] = element;
return true;
}
buffer_t single_list_to_array(single_list_t *list)
{
buffer_t buf;
buf.len = 0;
buf.data = safe_malloc(sizeof(buf.data) * list->count);
single_list_iterate(list, single_list_element_set_array_element, &buf);
return buf;
}
buffer_t single_list_to_array_copy(single_list_t *list, size_t element_size)
{
buffer_t buf;
buf.len = list->count;
buf.data = safe_malloc(element_size * list->count);
size_t i = 0;
single_list_ref_foreach(list, element)
{
memcpy(((uint8_t*)buf.data) + (i++) * element_size, element->data, element_size);
}
return buf;
}
single_list_t *single_list_new()
{
single_list_t *list = safe_calloc(sizeof(*list));
return list;
}
void single_list_init(single_list_t *list)
{
bzero(list, sizeof(*list));
}
void single_list_cat(single_list_t* left, single_list_t* right)
{
if(left->last != NULL)
{
left->last->next = right->first;
}
else
{
left->first = right->first;
left->last = right->last;
}
left->count += right->count;
left->last = right->last;
}
void single_list_clear(single_list_t *list)
{
single_list_element_t *element = list->first;
while (element != NULL)
{
single_list_element_t *next = element->next;
free(element);
element = next;
}
single_list_init(list);
}
void single_list_free(single_list_t *list)
{
if(list != NULL)
{
single_list_clear(list);
}
free(list);
}
void single_list_free_elements(single_list_t *list)
{
if(list == NULL)
{
return;
}
single_list_element_t *current = list->first;
while(current != NULL)
{
single_list_element_t *next = current->next;
free(current->data);
free(current);
current = next;
}
}
void single_list_free_with_elements(single_list_t *list)
{
single_list_free_elements(list);
free(list);
}
void single_list_push_front(single_list_t *list, void *data)
{
single_list_element_t *new_element = safe_malloc(sizeof(*new_element));
new_element->data = data;
new_element->next = list->first;
if (list->last == NULL)
{
list->last = new_element;
}
list->first = new_element;
list->count++;
}
single_list_element_t *single_list_push_back(single_list_t *list, void *data)
{
single_list_element_t *new_element = safe_malloc(sizeof(*new_element));
new_element->data = data;
new_element->next = NULL;
list->count++;
if (list->last)
{
list->last->next = new_element;
}
else
{
list->first = new_element;
}
list->last = new_element;
return new_element;
}
/**
* Pop the first element from a list and push it to the back.
*
* @param list The list to be wrapped.
*/
void single_list_wrap_first(single_list_t *list)
{
if(list->first == NULL)
{
return;
}
list->last->next = list->first;
list->last = list->first;
list->first = list->first->next;
list->last->next = NULL;
}
double_list_t* double_list_new()
{
double_list_t *list = safe_calloc(sizeof(*list));
return list;
}
void double_list_init(double_list_t *list)
{
bzero(list, sizeof(*list));
}
void double_list_free(double_list_t *list_holder)
{
double_list_element_t *list = list_holder->first;
while (list != NULL)
{
double_list_element_t *next = list->next;
free(list);
list = next;
}
}
void double_list_push_front(double_list_t *list, void *data)
{
double_list_element_t *new_element = safe_malloc(sizeof(*new_element));
new_element->data = data;
new_element->next = list->first;
new_element->previous = NULL;
if (list->last == NULL)
{
list->last = new_element;
}
list->first = new_element;
list->count++;
}
double_list_element_t *double_list_push_back(double_list_t *list, void *data)
{
double_list_element_t *new_element = safe_malloc(sizeof(*new_element));
new_element->data = data;
new_element->next = NULL;
new_element->previous = list->last;
list->count++;
if (list->last)
{
list->last->next = new_element;
}
else
{
list->first = new_element;
}
list->last = new_element;
return new_element;
}
void double_list_clear(double_list_t *list)
{
double_list_element_t *element = list->first;
while (element != NULL)
{
double_list_element_t *next = element->next;
free(element);
element = next;
}
double_list_init(list);
}
void double_list_iterate(double_list_t *list, void (*f)(double_list_element_t *, size_t, void *), void *param)
{
double_list_element_t* element = list->first;
size_t counter = 0;
while (element != NULL)
{
double_list_element_t *next = element->next;
f(element, counter++, param);
element = next;
}
}
double_list_element_t* double_list_free_and_next(double_list_element_t *element)
{
double_list_element_t *next = element->next;
free(element);
return next;
}
single_list_element_t* single_list_free_and_next(single_list_element_t *element)
{
single_list_element_t *next = element->next;
free(element);
return next;
}
void single_list_remove(single_list_t* list, void *value)
{
single_list_element_t *last = NULL;
single_list_element_t *element = list->first;
while (element != NULL)
{
if(element->data == value)
{
if(last == NULL)
{
list->first = list->first->next;
}
else
{
last->next = element->next;
}
if(element == list->last)
{
list->last = last;
}
list->count--;
element = single_list_free_and_next(element);
}
else
{
last = element;
element = element->next;
}
}
}
#endif