-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
dn-list.c
447 lines (371 loc) · 9.58 KB
/
dn-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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
/* (C) 2006 Novell, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "dn-list.h"
static dn_list_node_t *
list_new_node (
dn_allocator_t *allocator,
dn_list_node_t *prev,
dn_list_node_t *next,
void *data)
{
dn_list_node_t *node = (dn_list_node_t *)dn_allocator_alloc (allocator, sizeof (dn_list_node_t));
if (DN_UNLIKELY (!node))
return NULL;
node->data = data;
node->prev = prev;
node->next = next;
if (prev)
prev->next = node;
if (next)
next->prev = node;
return node;
}
static dn_list_node_t*
list_unlink_node (dn_list_node_t *node)
{
if (node->next)
node->next->prev = node->prev;
if (node->prev)
node->prev->next = node->next;
return node;
}
static dn_list_node_t *
list_insert_node_before (
dn_allocator_t *allocator,
dn_list_node_t *node,
void *data)
{
return list_new_node (allocator, node ? node->prev : NULL, node, data);
}
static dn_list_node_t *
list_insert_node_after (
dn_allocator_t *allocator,
dn_list_node_t *node,
void *data)
{
return list_new_node (allocator, node, node ? node->next : NULL, data);
}
static void
list_free_node (
dn_allocator_t *allocator,
dn_list_node_t *node)
{
dn_allocator_free (allocator, node);
}
static void
list_dispose_node (
dn_allocator_t *allocator,
dn_list_dispose_func_t dispose_func,
dn_list_node_t *node)
{
if (node && dispose_func)
dispose_func (node->data);
list_free_node (allocator, node);
}
static void
list_remove_node (
dn_list_t *list,
const void * data,
dn_list_equal_func_t equal_func,
dn_list_dispose_func_t dispose_func)
{
dn_list_node_t *current = list->head;
dn_list_node_t *next;
while (current) {
next = current->next;
if ((equal_func && equal_func (current->data, data)) || (!equal_func && current->data == data)) {
if (current == list->head)
list->head = next;
if (current == list->tail)
list->tail = current->prev;
list_dispose_node (list->_internal._allocator, dispose_func, list_unlink_node (current));
}
current = next;
}
}
dn_list_t *
dn_list_custom_alloc (dn_allocator_t *allocator)
{
dn_list_t *list = (dn_list_t *)dn_allocator_alloc (allocator, sizeof (dn_list_t));
if (!dn_list_custom_init (list, allocator)) {
dn_allocator_free (allocator, list);
return NULL;
}
return list;
}
bool
dn_list_custom_init (
dn_list_t *list,
dn_allocator_t *allocator)
{
if (DN_UNLIKELY (!list))
return false;
memset (list, 0, sizeof(dn_list_t));
list->_internal._allocator = allocator;
return true;
}
void
dn_list_custom_free (
dn_list_t *list,
dn_list_dispose_func_t dispose_func)
{
if (list) {
dn_list_custom_dispose (list, dispose_func);
dn_allocator_free (list->_internal._allocator, list);
}
}
void
dn_list_custom_dispose (
dn_list_t *list,
dn_list_dispose_func_t dispose_func)
{
if (DN_UNLIKELY(!list))
return;
dn_list_node_t *current = list->head;
while (current) {
dn_list_node_t *next = current->next;
list_dispose_node (list->_internal._allocator, dispose_func, current);
current = next;
}
}
uint32_t
dn_list_size (const dn_list_t *list)
{
DN_ASSERT (list);
uint32_t size = 0;
dn_list_node_t *nodes = list->head;
while (nodes) {
size ++;
nodes = nodes->next;
}
return size;
}
void
dn_list_custom_clear (
dn_list_t *list,
dn_list_dispose_func_t dispose_func)
{
DN_ASSERT (list);
dn_list_custom_dispose (list, dispose_func);
list->head = NULL;
list->tail = NULL;
}
dn_list_result_t
dn_list_insert (
dn_list_it_t position,
void *data)
{
dn_list_t *list = position._internal._list;
DN_ASSERT (list);
if (!list->head)
position.it = list_insert_node_before (list->_internal._allocator, list->head, data);
else if (!position.it)
position.it = list_insert_node_after (list->_internal._allocator, list->tail, data);
else
position.it = list_insert_node_before (list->_internal._allocator, position.it, data);
if (position.it) {
if (!position.it->prev)
list->head = position.it;
if (!position.it->next)
list->tail = position.it;
}
dn_list_result_t result = { { position.it, {position._internal._list } }, position.it != NULL };
return result;
}
dn_list_result_t
dn_list_insert_range (
dn_list_it_t position,
dn_list_it_t first,
dn_list_it_t last)
{
dn_list_result_t first_inserted = { { position.it, {position._internal._list } }, true };
if (first.it == last.it)
return first_inserted;
DN_ASSERT (first.it);
first_inserted = dn_list_insert (position, first.it->data);
for (first.it = first.it->next; first.it && first.it != last.it; first.it = first.it->next)
dn_list_insert (position, first.it->data);
if (last.it)
dn_list_insert (position, last.it->data);
return first_inserted;
}
dn_list_it_t
dn_list_custom_erase (
dn_list_it_t position,
dn_list_dispose_func_t dispose_func)
{
if (DN_UNLIKELY(!position.it))
return position;
dn_list_t *list = position._internal._list;
DN_ASSERT (list && !dn_list_it_end (position));
if (position.it == list->head) {
if (dispose_func)
dispose_func (*dn_list_front (list));
dn_list_pop_front (list);
position.it = list->head;
} else if (position.it == list->tail) {
if (dispose_func)
dispose_func (*dn_list_back (list));
dn_list_pop_back (list);
position.it = NULL;
} else if (position.it) {
dn_list_node_t *to_remove = position.it;
position.it = position.it->next;
list_dispose_node (list->_internal._allocator, dispose_func, list_unlink_node (to_remove));
}
return position;
}
void
dn_list_custom_pop_back (
dn_list_t *list,
dn_list_dispose_func_t dispose_func)
{
DN_ASSERT (list && list->tail);
dn_list_node_t *prev = list->tail->prev;
list_dispose_node (list->_internal._allocator, dispose_func, list_unlink_node (list->tail));
list->tail = prev;
if (!list->tail)
list->head = NULL;
}
void
dn_list_custom_pop_front (
dn_list_t *list,
dn_list_dispose_func_t dispose_func)
{
DN_ASSERT (list && list->head);
dn_list_node_t *next = list->head->next;
list_dispose_node (list->_internal._allocator, dispose_func, list_unlink_node (list->head));
list->head = next;
if (!list->head)
list->tail = NULL;
}
bool
dn_list_custom_resize (
dn_list_t *list,
uint32_t count,
dn_list_dispose_func_t dispose_func)
{
DN_ASSERT (list);
if (count == 0) {
dn_list_custom_clear (list, dispose_func);
return true;
}
dn_list_node_t *current = list->head;
uint32_t i = 0;
while (current) {
i++;
if (i == count) {
dn_list_node_t *to_dispose = current->next;
while (to_dispose) {
dn_list_node_t *next = to_dispose->next;
list_dispose_node (list->_internal._allocator, dispose_func, list_unlink_node (to_dispose));
to_dispose = next;
}
list->tail = current;
break;
}
current = current->next;
}
while (i++ < count)
dn_list_insert (dn_list_end (list), NULL);
return true;
}
void
dn_list_custom_remove (
dn_list_t *list,
const void *data,
dn_list_dispose_func_t dispose_func)
{
DN_ASSERT (list);
list_remove_node (list, data, NULL, dispose_func);
}
void
dn_list_custom_remove_if (
dn_list_t *list,
const void * data,
dn_list_equal_func_t equal_func,
dn_list_dispose_func_t dispose_func)
{
DN_ASSERT (list && equal_func);
list_remove_node (list, data, equal_func, dispose_func);
}
void
dn_list_reverse (dn_list_t *list)
{
DN_ASSERT (list);
dn_list_node_t *node = list->head;
dn_list_node_t *reverse;
list->head = list->tail;
list->tail = node;
while (node) {
reverse = node;
node = reverse->next;
reverse->next = reverse->prev;
reverse->prev = node;
}
}
void
dn_list_for_each (
dn_list_t *list,
dn_list_for_each_func_t for_each_func,
void *user_data)
{
DN_ASSERT (list && for_each_func);
for (dn_list_node_t *it = list->head; it; it = it->next)
for_each_func (it->data, user_data);
}
typedef dn_list_node_t list_node;
typedef dn_list_compare_func_t compare_func_t;
#include "dn-sort-frag.inc"
void
dn_list_sort (
dn_list_t *list,
dn_list_compare_func_t compare_func)
{
DN_ASSERT (list && compare_func);
if (DN_UNLIKELY (!list->head || !list->head->next))
return;
list->head = do_sort (list->head, compare_func);
list->head->prev = NULL;
dn_list_node_t *current;
for (current = list->head; current->next; current = current->next)
current->next->prev = current;
list->tail = current;
}
dn_list_it_t
dn_list_custom_find (
dn_list_t *list,
const void *data,
dn_list_equal_func_t equal_func)
{
DN_ASSERT (list);
dn_list_it_t found = { NULL, { list } };
for (dn_list_node_t *it = list->head; it; it = it->next) {
if ((equal_func && equal_func (it->data, data)) || (!equal_func && it->data == data)) {
found.it = it;
break;
}
}
return found;
}