-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathslist.c
More file actions
500 lines (388 loc) · 10.4 KB
/
slist.c
File metadata and controls
500 lines (388 loc) · 10.4 KB
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* slist.c
* This file is part of slist and dutils library collection
* Copyright (C) 2014 Darcy Bras da Silva
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
#include "slist.h"
/****************************************************************************
* library interface implementation
****************************************************************************/
struct slist_list *slist_init(struct slist_list *list,
void *(*node_alloc)(size_t),
void (*node_dalloc)(void *))
{
list->count = 0;
list->node_alloc = (node_alloc ? node_alloc : SLIST_DEF_ALLOC);
list->node_dalloc = (node_dalloc ? node_dalloc : SLIST_DEF_DALLOC);
list->head = NULL;
return list;
}/* slist_init */
struct slist_list *slist_list_new(void *(*node_alloc)(size_t),
void (*node_dalloc)(void *))
{
node_alloc = (node_alloc ? node_alloc : SLIST_DEF_ALLOC);
node_dalloc = (node_dalloc ? node_dalloc : SLIST_DEF_DALLOC);
struct slist_list *list = NULL;
if ( NULL == (list = node_alloc( sizeof( struct slist_list))) ) {
//FIXME: add support for custom error logging and msg
fprintf(stderr,"%s[%d]:%s alloc failed\n", __FILE__,
__LINE__,__func__);
return NULL;
}
list->node_alloc = node_alloc;
list->node_dalloc = node_dalloc;
list->count = 0;
list->head = NULL;
return list;
}/* slist_list_new */
struct slist_node *slist_node_new(struct slist_list *list,
void *data, void (*dalloc)(void *))
{
struct slist_node *node = NULL;
if ( NULL == (node = list->node_alloc( sizeof( struct slist_node))) ) {
//FIXME: add support for custom error loggin and msg
fprintf(stderr,"%s[%d]:%s alloc failed\n", __FILE__,
__LINE__,__func__);
return NULL;
}
node->data = data;
node->data_dalloc = dalloc;
node->next = NULL;
return node;
}/* slist_node_new */
inline void slist_node_print(const struct slist_node *node,
void(*print)(void *))
{
if ( !node || !print )
return;
print(node->data);
}/* slist_node_print */
void slist_node_delete(struct slist_list *list,
struct slist_node *node)
{
if (!list || !node )
return;
//FIXME: add debug warning for existing 'node->data
//------ without _node_dalloc
if ( node->data && node->data_dalloc )
node->data_dalloc( node->data );
list->node_dalloc(node);
node = NULL;
}/* slist_node_delete */
void slist_list_delete(struct slist_list *list)
{
if (!list)
return;
void (*node_dalloc)(void *) = list->node_dalloc;
node_dalloc(list);
}/* slist_list_delete */
struct slist_node *slist_node_push(struct slist_list *list,
struct slist_node *node)
{
if ( !list || !node )
return NULL;
node->next = list->head;
list->head = node;
++list->count;
return node;
}/* slist_node_push */
struct slist_node *slist_node_append(struct slist_list *list,
struct slist_node *node)
{
if ( !list || !node )
return NULL;
struct slist_node *pnode = NULL;
//check add @ head
if ( !list->head ) {
list->head = node;
++list->count;
return node;
}
pnode = list->head;
while( NULL != pnode->next )
pnode = pnode->next;
pnode->next = node;
//ensure a node is a working tail
node->next = NULL;
++list->count;
return pnode;
}/* slist_node_append */
struct slist_node *slist_node_pop(struct slist_list *list)
{
if ( !list || !list->head )
return NULL;
struct slist_node *node = list->head;
list->head = list->head->next;
--list->count;
return node;
}/* slist_node_pop */
struct slist_node *slist_node_find(struct slist_list *list, void *key,
int (*cmp)(void *a, void *b))
{
if ( !list || !list->head || !key || !cmp )
return NULL;
struct slist_node *iter;
//check if found @ head
if ( 0 == cmp(list->head->data, key) )
return list->head;
for(iter = list->head; NULL != iter->next; iter = iter->next)
{
if ( 0 == cmp(iter->next->data, key) )
break;
}
return iter->next;
}/* slist_node_find */
size_t slist_find_index_of(struct slist_list *list, void *key,
int (*cmp)(void *a, void *b))
{
if ( !list || !list->head || !key || !cmp )
return 0;
struct slist_node *iter;
size_t idx = 1;
for(iter = list->head; NULL != iter; iter = iter->next, ++idx)
{
if ( 0 == cmp(iter->data, key) )
break;
}
if( !iter )
return 0;
return idx;
}/* slist_find_index_of */
struct slist_node *slist_node_remove(struct slist_list *list, void *key,
int (*cmp)(void *a, void *b))
{
if ( !list || !list->head || !key || !cmp )
return NULL;
struct slist_node *iter;
struct slist_node *pnode;
//check found @ head
if ( 0 == cmp(list->head->data, key) ) {
pnode = list->head;
list->head = list->head->next;
--list->count;
return pnode;
}
for(iter = list->head; NULL != iter->next; iter = iter->next)
{
if ( 0 == cmp(iter->next->data, key) )
break;
}
//see if found a node
if ( !iter->next )
return NULL;
pnode = iter->next;
iter->next = pnode->next;
--list->count;
return pnode;
}/* slist_node_remove */
struct slist_node *slist_node_remove_at(struct slist_list *list,
const size_t index)
{
if ( !list || !list->head || index == 0 || index > list->count )
return NULL;
struct slist_node *head = list->head;
struct slist_node *prev = NULL;
size_t idx = 1;
if ( 1 == index )
return slist_node_pop(list);
do{
prev = head;
head = head->next;
}while( index > ++idx );
prev->next = head->next;
--list->count;
return head;
}/* slist_node_remove_at */
void slist_node_foreach(struct slist_list *list,
void *(*action)(void *carry, void *data, void *param),
void *param)
{
if ( !list || !list->head || !action )
return;
struct slist_node *iter = NULL;
void *carry = NULL;
for(iter = list->head; NULL != iter; iter = iter->next)
{
carry = action(carry, iter->data, param);
}
}/* slist_node_foreach */
struct slist_list *slist_list_delete_all_nodes(struct slist_list *list)
{
if ( !list || !list->head )
return NULL;
while( NULL != list->head )
{
slist_node_delete(list, slist_node_pop(list));
}
return list;
}/* slist_list_delete_all_nodes */
struct slist_list *slist_list_reverse(struct slist_list *list)
{
if ( !list || !list->head )
return NULL;
struct slist_node *head = list->head;
struct slist_node *prev = NULL;
struct slist_node *next = NULL;
while ( head )
{
//set the head as last node
list->head = head;
//save the heads next in next
next = head->next;
//change the next to previous
head->next = prev;
//set the current as previous
prev = head;
//set current as next
head = next;
}
//set new head and return
next = list->head;
list->head = next;
return list;
}/* slist_list_reverse */
inline size_t slist_get_size(struct slist_list *list)
{
return list->count;
}/* slist_get_size */
struct slist_list *slist_list_push(struct slist_list *list,
struct slist_list *s_list)
{
if ( !list || !s_list || !s_list->head )
return NULL;
struct slist_node *iter = s_list->head;
struct slist_node *head = list->head;
if ( !list->head ) {
list->head = s_list->head;
list->count = s_list->count;
//empty s_list
s_list->head = NULL;
s_list->count = 0;
return list;
}
//skip to end of s_list
while( NULL != iter->next )
iter = iter->next;
list->head = s_list->head;
iter->next = head;
list->count += s_list->count;
//empty s_list
s_list->head = NULL;
s_list->count = 0;
return list;
}/* slist_list_push */
struct slist_list *slist_list_append(struct slist_list *list,
struct slist_list *s_list)
{
if ( !list || !s_list || !s_list->head )
return NULL;
struct slist_node *iter = list->head;
if ( !list->head ) {
list->head = s_list->head;
list->count = s_list->count;
//empty s_list
s_list->head = NULL;
s_list->count = 0;
return list;
}
//slip to end of list
while( NULL != iter->next )
iter = iter->next;
iter->next = s_list->head;
list->count += s_list->count;
//empty s_list
s_list->head = NULL;
s_list->count = 0;
return list;
}/* slist_list_append */
struct slist_list *slist_list_split(struct slist_list *list, void *key,
int (*cmp)(void *a, void *b))
{
if ( !list || !list->head || !key || !cmp )
return NULL;
struct slist_list *n_list = NULL;
struct slist_node *iter = NULL;
//check key @ head
if ( 0 == cmp(list->head->data, key) ) {
n_list = slist_list_new(list->node_alloc, list->node_dalloc);
if ( NULL == n_list )
return NULL;
n_list->head = list->head;
n_list->count = list->count;
//make list empty
list->head = NULL;
list->count = 0;
return n_list;
}
//help us update list sizes without transversing
size_t idx = 0;
//search for key
for(iter = list->head; NULL != iter->next; iter = iter->next, ++idx)
{
if ( 0 == cmp(iter->next->data, key) )
break;
}
//check if node was found
if ( NULL == iter->next )
return NULL;
n_list = slist_list_new(list->node_alloc, list->node_dalloc);
if ( NULL == n_list )
return NULL;
//keep in mind we are one node behind so we can remove/trim
n_list->head = iter->next;
n_list->count = list->count - idx - 1;
list->count = idx + 1;
iter->next = NULL;
return n_list;
}/* slist_list_split */
struct slist_list *slist_list_split_at(struct slist_list *list,
const size_t index)
{
if ( !list || !list->head || 0 == index || index > list->count )
return NULL;
struct slist_list *n_list = NULL;
struct slist_node *head = NULL;
struct slist_node *prev = NULL;
size_t idx = 1;
//create our slist
n_list = slist_list_new(list->node_alloc, list->node_dalloc);
if ( !n_list )
return NULL;
//remove at head
if ( 1 == index ) {
n_list->head = list->head;
n_list->count = list->count;
list->head = NULL;
list->count = 0;
return n_list;
}
prev = head = list->head;
for(idx = index - 1; idx; --idx)
{
prev = head;
head = head->next;
}
prev->next = NULL;
n_list->head = head;
//remove at tail ?
if (list->count == index ) {
n_list->count = list->count - index + 1;
} else {
n_list->count = index;
}
list->count = index - 1;
return n_list;
}/* slist_list_split_at */