-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.h
403 lines (392 loc) · 14 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//
// Created by oleg on 10/25/17.
//
#ifndef CLIST_LIST_H
#define CLIST_LIST_H
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#define DEFINE_LIST(TYPE) \
\
struct _list__node_##TYPE { \
TYPE value; \
struct _list__node_##TYPE* next; \
struct _list__node_##TYPE* prev; \
}; \
\
struct _list__list_##TYPE { \
struct _list__node_##TYPE* head; \
struct _list__node_##TYPE* tail; \
size_t length; \
}; \
\
struct _list__iterator_##TYPE { \
struct _list__node_##TYPE* current; \
struct _list__list_##TYPE* list; \
}; \
\
static struct _list__list_##TYPE* \
_list__create_##TYPE() { \
struct _list__list_##TYPE* list = \
(struct _list__list_##TYPE*)malloc( \
sizeof(struct _list__list_##TYPE)); \
if (!list) { \
return NULL; \
} \
list->head = NULL; \
list->tail = NULL; \
list->length = 0; \
return list; \
} \
\
static bool _list__clear_##TYPE( \
struct _list__list_##TYPE* list); \
\
static bool _list__destroy_##TYPE( \
struct _list__list_##TYPE* list) { \
if (!list) { \
return true; \
} \
if (!_list__clear_##TYPE(list)) { \
return false; \
} \
free(list); \
return true; \
} \
\
static bool _list__push_back_##TYPE( \
struct _list__list_##TYPE* list, \
TYPE value) { \
if (!list) { \
return false; \
} \
struct _list__node_##TYPE* node = \
(struct _list__node_##TYPE*)malloc( \
sizeof(struct _list__node_##TYPE)); \
if (!node) { \
return false; \
} \
node->value = value; \
node->prev = list->tail; \
node->next = NULL; \
list->tail = node; \
if (node->prev) { \
node->prev->next = node; \
} \
else { \
list->head = node; \
} \
++list->length; \
return true; \
} \
\
static bool _list__insert_##TYPE( \
struct _list__iterator_##TYPE iter, \
TYPE value) { \
if (!iter.list) { \
return false; \
} \
struct _list__node_##TYPE* node = \
(struct _list__node_##TYPE*)malloc( \
sizeof(struct _list__node_##TYPE)); \
if (!node) { \
return false; \
} \
node->value = value; \
node->next = iter.current; \
if (node->next) { \
node->prev = node->next->prev; \
if (node->prev) { \
node->prev->next = node; \
} \
node->next->prev = node; \
} \
else { \
node->prev = iter.list->tail; \
if (node->prev) { \
node->prev->next = node; \
} \
iter.list->tail = node; \
} \
if (!node->prev) { \
iter.list->head = node; \
} \
++iter.list->length; \
return true; \
} \
\
static bool _list__clear_##TYPE( \
struct _list__list_##TYPE* list) { \
if (!list || !list->length || !list->head) { \
assert(!(list && list->tail)); \
return true; \
} \
struct _list__node_##TYPE* temp_node; \
while (list->head) { \
temp_node = list->head->next; \
free(list->head); \
list->head = temp_node; \
} \
list->head = NULL; \
list->tail = NULL; \
list->length = 0; \
return true; \
} \
\
static bool _list__copy_##TYPE( \
const struct _list__list_##TYPE* source_list, \
struct _list__list_##TYPE* destination_list) { \
if (!source_list || !destination_list) { \
return false; \
} \
for (struct _list__node_##TYPE* node = source_list->head; \
node; \
node = node->next) { \
if (!_list__push_back_##TYPE(destination_list, node->value)) { \
return false; \
} \
} \
return true; \
} \
\
static bool _list__move_##TYPE( \
struct _list__list_##TYPE* source_list, \
struct _list__list_##TYPE* destination_list) { \
if (!source_list || !destination_list) { \
return false; \
} \
if (destination_list->tail) { \
destination_list->tail->next = source_list->head; \
source_list->head->prev = destination_list->tail; \
destination_list->tail = source_list->tail; \
} \
else { \
destination_list->head = source_list->head; \
destination_list->tail = source_list->tail; \
} \
destination_list->length += source_list->length; \
source_list->head = NULL; \
source_list->tail = NULL; \
source_list->length = 0; \
return true; \
} \
\
static struct _list__iterator_##TYPE \
_list__find_by_value_cmp_##TYPE( \
struct _list__list_##TYPE* list, \
TYPE* value, \
int (*comparator)(TYPE* a, TYPE* b)) { \
struct _list__iterator_##TYPE iter = { NULL, list }; \
if (!list || !list->length) { \
return iter; \
} \
iter.current = list->head; \
while (iter.current) { \
if (comparator(&iter.current->value, value) == 0) { \
break; \
} \
iter.current = iter.current->next; \
} \
return iter; \
} \
\
static bool _list__remove_##TYPE( \
struct _list__iterator_##TYPE iter) { \
if (!iter.list || !iter.current) { \
return false; \
} \
if (iter.current->next) { \
iter.current->next->prev = iter.current->prev; \
} \
else { \
iter.list->tail = iter.current->prev; \
} \
if (iter.current->prev) { \
iter.current->prev->next = iter.current->next; \
} \
else { \
iter.list->head = iter.current->next; \
} \
free(iter.current); \
--iter.list->length; \
return true; \
} \
\
static bool _list__remove_by_value_cmp_##TYPE( \
struct _list__list_##TYPE* list, \
TYPE* value, \
int (*comparator)(TYPE* a, TYPE* b)) { \
struct _list__iterator_##TYPE iter = \
_list__find_by_value_cmp_##TYPE(list, value, comparator); \
return _list__remove_##TYPE(iter); \
} \
\
static TYPE _list__pop_front_##TYPE( \
struct _list__list_##TYPE* list) { \
assert(list->head); \
if (list->head->next) { \
list->head->next->prev = NULL; \
} \
else { \
list->tail = NULL; \
} \
struct _list__node_##TYPE* node = list->head; \
list->head = list->head->next; \
TYPE value = node->value; \
free(node); \
--list->length; \
return value; \
} \
\
static TYPE _list__pop_back_##TYPE( \
struct _list__list_##TYPE* list) { \
assert(list->tail); \
if (list->tail->prev) { \
list->tail->prev->next = NULL; \
} \
else { \
list->head = NULL; \
} \
struct _list__node_##TYPE* node = list->tail; \
list->tail = list->tail->prev; \
TYPE value = node->value; \
free(node); \
--list->length; \
return value; \
} \
\
static struct _list__iterator_##TYPE \
_list__begin_##TYPE( \
struct _list__list_##TYPE* list) { \
assert(list); \
struct _list__iterator_##TYPE iter = { list->head, list }; \
return iter; \
} \
\
static struct _list__iterator_##TYPE \
_list__end_##TYPE( \
struct _list__list_##TYPE* list) { \
assert(list); \
struct _list__iterator_##TYPE iter = { NULL, list }; \
return iter; \
} \
\
static struct _list__iterator_##TYPE \
_list__tail_##TYPE( \
struct _list__list_##TYPE* list) { \
assert(list); \
struct _list__iterator_##TYPE iter = { list->tail, list }; \
return iter; \
} \
\
static void _list__next_##TYPE( \
struct _list__iterator_##TYPE* iter) { \
if (iter && iter->current) { \
iter->current = iter->current->next; \
} \
} \
\
static void _list__prev_##TYPE( \
struct _list__iterator_##TYPE* iter) { \
if (iter && iter->current) { \
iter->current = iter->current->prev; \
} \
} \
\
static void _list__selection_sort_cmp_##TYPE( \
struct _list__list_##TYPE* list, \
bool (*is_less)(TYPE* a, TYPE* b)) { \
struct _list__node_##TYPE* i_node = list->head; \
while (i_node) { \
struct _list__node_##TYPE *j_node = i_node->next, *min_node = i_node; \
while (j_node) { \
if (is_less(&j_node->value, &min_node->value)) { \
min_node = j_node; \
} \
j_node = j_node->next; \
} \
if (min_node != i_node) { \
TYPE temp = i_node->value; \
i_node->value = min_node->value; \
min_node->value = temp; \
} \
i_node = i_node->next; \
} \
}
#define DEFINE_LIST_OPS_FOR_BUILTINS(TYPE) \
\
static struct _list__iterator_##TYPE \
_list__find_by_value_##TYPE( \
struct _list__list_##TYPE* list, \
TYPE value) { \
struct _list__iterator_##TYPE iter = { NULL, list }; \
if (!list || !list->length) { \
return iter; \
} \
iter.current = list->head; \
while (iter.current) { \
if (iter.current->value == value) { \
break; \
} \
iter.current = iter.current->next; \
} \
return iter; \
} \
\
static bool _list__remove_by_value_##TYPE( \
struct _list__list_##TYPE* list, \
TYPE value) { \
struct _list__iterator_##TYPE iter = \
_list__find_by_value_##TYPE(list, value); \
return _list__remove_##TYPE(iter); \
}
#define LIST_TYPE(TYPE) struct _list__list_##TYPE*
#define LIST_ITER_TYPE(TYPE) struct _list__iterator_##TYPE
#define list_create(TYPE) _list__create_##TYPE()
#define list_destroy(TYPE, list) _list__destroy_##TYPE(list)
#define list_push_back(TYPE, list, value) _list__push_back_##TYPE(list, value)
#define list_insert(TYPE, iter, value) _list__insert_##TYPE(iter, value)
#define list_clear(TYPE, list) _list__clear_##TYPE(list)
#define list_copy(TYPE, source_list, destination_list) \
_list__copy_##TYPE(source_list, destination_list)
#define list_move(TYPE, source_list, destination_list) \
_list__move_##TYPE(source_list, destination_list)
#define list_find_by_value(TYPE, list, value) \
_list__find_by_value_##TYPE(list, value)
#define list_find_by_value_cmp(TYPE, list, value, comparator) \
_list__find_by_value_cmp_##TYPE(list, &value, &comparator)
#define list_remove(TYPE, iter) _list__remove_##TYPE(iter)
#define list_remove_by_value(TYPE, list, value) \
_list__remove_by_value_##TYPE(list, value)
#define list_remove_by_value_cmp(TYPE, list, value, comparator) \
_list__remove_by_value_cmp_##TYPE(list, &value, &comparator)
#define list_pop_front(TYPE, list) _list__pop_front_##TYPE(list)
#define list_pop_back(TYPE, list) _list__pop_back_##TYPE(list)
#define list_len(list) list->length
#define list_empty(list) (list->length == 0)
#define list_begin(TYPE, list) _list__begin_##TYPE(list)
#define list_end(TYPE, list) _list__end_##TYPE(list)
#define list_head(TYPE, list) list_begin(TYPE, list)
#define list_tail(TYPE, list) _list__tail_##TYPE(list)
#define list_first(list) list->head->value
#define list_last(list) list->tail->value
#define list_next(TYPE, iter) _list__next_##TYPE(&iter)
#define list_prev(TYPE, iter) _list__prev_##TYPE(&iter)
#define list_iter_valid(iter) (!!iter.current)
#define list_iter_value(iter) iter.current->value
#define list_iter_eq(iter_i, iter_j) (iter_i.current == iter_j.current)
#define list_selection_sort_cmp(TYPE, list, is_less) \
_list__selection_sort_cmp_##TYPE(list, is_less)
#define list_for_each(TYPE, list, var_name) \
assert(list); \
for (struct _list__node_##TYPE* var_name = list->head; \
var_name; \
var_name = var_name->next)
#define list_for_each_reversed(TYPE, list, var_name) \
assert(list); \
for (struct _list__node_##TYPE* var_name = list->tail; \
var_name; \
var_name = var_name->prev)
#define list_var_value(var_name) var_name->value
#define list_var_eq(var_name1, var_name2) (var_name1 == var_name2)
#endif //CLIST_LIST_H