/
zlist.c
621 lines (510 loc) · 16.5 KB
/
zlist.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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/* =========================================================================
zlist - simple generic list container
Copyright (c) the Contributors as noted in the AUTHORS file.
This file is part of CZMQ, the high-level C binding for 0MQ:
http://czmq.zeromq.org.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
=========================================================================
*/
/*
@header
Provides a generic container implementing a fast singly-linked list. You
can use this to construct multi-dimensional lists, and other structures
together with other generic containers like zhash. This is a simple
class. For demanding applications we recommend using zlistx.
@discuss
To iterate through a list, use zlist_first to get the first item, then
loop while not null, and do zlist_next at the end of each iteration.
@end
*/
#include "../include/czmq.h"
// List node, used internally only
typedef struct _node_t {
struct _node_t *next;
void *item;
zlist_free_fn *free_fn;
} node_t;
// ---------------------------------------------------------------------
// Structure of our class
struct _zlist_t {
node_t *head; // First item in list, if any
node_t *tail; // Last item in list, if any
node_t *cursor; // Current cursors for iteration
size_t size; // Number of items in list
bool autofree; // If true, free items in destructor
zlist_compare_fn *compare_fn; // Function to compare two list item for
// less than, equals or greater than
};
// --------------------------------------------------------------------------
// List constructor
zlist_t *
zlist_new (void)
{
zlist_t *self = (zlist_t *) zmalloc (sizeof (zlist_t));
return self;
}
// --------------------------------------------------------------------------
// List destructor
void
zlist_destroy (zlist_t **self_p)
{
assert (self_p);
if (*self_p) {
zlist_t *self = *self_p;
zlist_purge (self);
free (self);
*self_p = NULL;
}
}
// --------------------------------------------------------------------------
// Return the item at the head of list. If the list is empty, returns NULL.
// Leaves cursor pointing at the head item, or NULL if the list is empty.
void *
zlist_first (zlist_t *self)
{
assert (self);
self->cursor = self->head;
if (self->cursor)
return self->cursor->item;
else
return NULL;
}
// --------------------------------------------------------------------------
// Return the next item. If the list is empty, returns NULL. To move to
// the start of the list call zlist_first (). Advances the cursor.
void *
zlist_next (zlist_t *self)
{
assert (self);
if (self->cursor)
self->cursor = self->cursor->next;
else
self->cursor = self->head;
if (self->cursor)
return self->cursor->item;
else
return NULL;
}
// --------------------------------------------------------------------------
// Return the item at the tail of list. If the list is empty, returns NULL.
// Leaves cursor pointing at the tail item, or NULL if the list is empty.
void *
zlist_last (zlist_t *self)
{
assert (self);
self->cursor = self->tail;
if (self->cursor)
return self->cursor->item;
else
return NULL;
}
// --------------------------------------------------------------------------
// Return the item at the head of list. If the list is empty, returns NULL.
// Leaves cursor as-is.
void *
zlist_head (zlist_t *self)
{
assert (self);
return self->head ? self->head->item : NULL;
}
// --------------------------------------------------------------------------
// Return the item at the tail of list. If the list is empty, returns NULL.
// Leaves cursor as-is.
void *
zlist_tail (zlist_t *self)
{
assert (self);
return self->tail ? self->tail->item : NULL;
}
// --------------------------------------------------------------------------
// Return current item in the list. If the list is empty, or the cursor
// passed the end of the list, returns NULL. Does not change the cursor.
void *
zlist_item (zlist_t *self)
{
assert (self);
if (self->cursor)
return self->cursor->item;
else
return NULL;
}
// --------------------------------------------------------------------------
// Append an item to the end of the list, return 0 if OK or -1 if this
// failed for some reason (out of memory).
int
zlist_append (zlist_t *self, void *item)
{
if (!item)
return -1;
node_t *node;
node = (node_t *) zmalloc (sizeof (node_t));
if (!node)
return -1;
// If necessary, take duplicate of (string) item
if (self->autofree)
item = strdup ((char *) item);
node->item = item;
if (self->tail)
self->tail->next = node;
else
self->head = node;
self->tail = node;
node->next = NULL;
self->size++;
self->cursor = NULL;
return 0;
}
// --------------------------------------------------------------------------
// Push an item to the start of the list, return 0 if OK or -1 if this
// failed for some reason (out of memory).
int
zlist_push (zlist_t *self, void *item)
{
node_t *node;
node = (node_t *) zmalloc (sizeof (node_t));
if (!node)
return -1;
// If necessary, take duplicate of (string) item
if (self->autofree)
item = strdup ((char *) item);
node->item = item;
node->next = self->head;
self->head = node;
if (self->tail == NULL)
self->tail = node;
self->size++;
self->cursor = NULL;
return 0;
}
// --------------------------------------------------------------------------
// Remove item from the beginning of the list, returns NULL if none
void *
zlist_pop (zlist_t *self)
{
node_t *node = self->head;
void *item = NULL;
if (node) {
item = node->item;
self->head = node->next;
if (self->tail == node)
self->tail = NULL;
free (node);
self->size--;
}
self->cursor = NULL;
return item;
}
// --------------------------------------------------------------------------
// Checks if an item already is present. Uses compare method to determine if
// items are equal. If the compare method is NULL the check will only compare
// pointers. Returns true if item is present else false.
bool
zlist_exists (zlist_t *self, void *item)
{
assert (self);
assert (item);
node_t *node = self->head;
while (node) {
if (self->compare_fn) {
if ((*self->compare_fn)(node->item, item) == 0)
return true;
}
else {
if (node->item == item) {
return true;
}
}
node = node->next;
}
return false;
}
// --------------------------------------------------------------------------
// Remove the item from the list, if present. Safe to call on items that
// are not in the list.
void
zlist_remove (zlist_t *self, void *item)
{
node_t *node, *prev = NULL;
// First off, we need to find the list node
for (node = self->head; node != NULL; node = node->next) {
if (self->compare_fn) {
if ((*self->compare_fn)(node->item, item) == 0)
break;
}
else {
if (node->item == item)
break;
}
prev = node;
}
if (node) {
if (prev)
prev->next = node->next;
else
self->head = node->next;
if (node->next == NULL)
self->tail = prev;
if (self->cursor == node)
self->cursor = prev;
if (node->free_fn)
(node->free_fn)(node->item);
free (node);
self->size--;
}
}
// --------------------------------------------------------------------------
// Make a copy of list. The list will hold pointers back to the items in
// the original list.
zlist_t *
zlist_dup (zlist_t *self)
{
if (!self)
return NULL;
zlist_t *copy = zlist_new ();
if (copy) {
node_t *node;
for (node = self->head; node; node = node->next) {
if (zlist_append (copy, node->item) == -1) {
zlist_destroy (©);
break;
}
}
}
return copy;
}
// --------------------------------------------------------------------------
// Purge all items from list
void
zlist_purge (zlist_t *self)
{
assert (self);
node_t *node = self->head;
while (node) {
node_t *next = node->next;
if (self->autofree)
free (node->item);
else
if (node->free_fn)
(node->free_fn)(node->item);
free (node);
node = next;
}
self->head = NULL;
self->tail = NULL;
self->cursor = NULL;
self->size = 0;
}
// --------------------------------------------------------------------------
// Return the number of items in the list
size_t
zlist_size (zlist_t *self)
{
return self->size;
}
// --------------------------------------------------------------------------
// Sort the list by ascending key value using a straight ASCII comparison.
// The sort is not stable, so may reorder items with the same keys.
void
zlist_sort (zlist_t *self, zlist_compare_fn *compare)
{
// If the compare function is NULL use the lists one if present
if (!compare && self->compare_fn)
compare = self->compare_fn;
// Uses a comb sort, which is simple and reasonably fast.
// See http://en.wikipedia.org/wiki/Comb_sort
size_t gap = self->size;
bool swapped = false;
while (gap > 1 || swapped) {
if (gap > 1)
gap = (size_t) ((double) gap / 1.3);
node_t *base = self->head;
node_t *test = self->head;
size_t jump = gap;
while (jump--)
test = test->next;
swapped = false;
while (base && test) {
if ((*compare)(base->item, test->item) > 0) {
// It's trivial to swap items in a generic container
void *item = base->item;
base->item = test->item;
test->item = item;
swapped = true;
}
base = base->next;
test = test->next;
}
}
}
// --------------------------------------------------------------------------
// Sets a compare function for this list. The function compares two items.
// It returns an integer less than, equal to, or greater than zero if the
// first item is found, respectively, to be less than, to match, or be
// greater than the second item.
// This function is used for sorting, removal and exists checking.
void
zlist_comparefn (zlist_t *self, zlist_compare_fn fn)
{
assert (self);
self->compare_fn = fn;
}
// --------------------------------------------------------------------------
// Set a free function for the specified list item. When the item is
// destroyed, the free function, if any, is called on that item.
// Use this when list items are dynamically allocated, to ensure that
// you don't have memory leaks. You can pass 'free' or NULL as a free_fn.
// Returns the item, or NULL if there is no such item.
void *
zlist_freefn (zlist_t *self, void *item, zlist_free_fn *fn, bool at_tail)
{
node_t *node = self->head;
if (at_tail)
node = self->tail;
while (node) {
if (node->item == item) {
node->free_fn = fn;
return item;
}
node = node->next;
}
return NULL;
}
// --------------------------------------------------------------------------
// Set list for automatic item destruction; item values MUST be strings.
// By default a list item refers to a value held elsewhere. When you set
// this, each time you append or push a list item, zlist will take a copy
// of the string value. Then, when you destroy the list, it will free all
// item values automatically. If you use any other technique to allocate
// list values, you must free them explicitly before destroying the list.
// The usual technique is to pop list items and destroy them, until the
// list is empty.
void
zlist_autofree (zlist_t *self)
{
assert (self);
self->autofree = true;
}
static void
s_zlist_free (void *data)
{
zlist_t *self = (zlist_t *) data;
zlist_destroy (&self);
}
static int
s_compare (void *item1, void *item2)
{
return strcmp ((char *) item1, (char *) item2);
}
// --------------------------------------------------------------------------
// Runs selftest of class
void
zlist_test (int verbose)
{
printf (" * zlist: ");
// @selftest
zlist_t *list = zlist_new ();
assert (list);
assert (zlist_size (list) == 0);
// Three items we'll use as test data
// List items are void *, not particularly strings
char *cheese = "boursin";
char *bread = "baguette";
char *wine = "bordeaux";
zlist_append (list, cheese);
assert (zlist_size (list) == 1);
assert ( zlist_exists (list, cheese));
assert (!zlist_exists (list, bread));
assert (!zlist_exists (list, wine));
zlist_append (list, bread);
assert (zlist_size (list) == 2);
assert ( zlist_exists (list, cheese));
assert ( zlist_exists (list, bread));
assert (!zlist_exists (list, wine));
zlist_append (list, wine);
assert (zlist_size (list) == 3);
assert ( zlist_exists (list, cheese));
assert ( zlist_exists (list, bread));
assert ( zlist_exists (list, wine));
assert (zlist_head (list) == cheese);
assert (zlist_next (list) == cheese);
assert (zlist_first (list) == cheese);
assert (zlist_tail (list) == wine);
assert (zlist_next (list) == bread);
assert (zlist_first (list) == cheese);
assert (zlist_next (list) == bread);
assert (zlist_next (list) == wine);
assert (zlist_next (list) == NULL);
// After we reach end of list, next wraps around
assert (zlist_next (list) == cheese);
assert (zlist_size (list) == 3);
zlist_remove (list, wine);
assert (zlist_size (list) == 2);
assert (zlist_first (list) == cheese);
zlist_remove (list, cheese);
assert (zlist_size (list) == 1);
assert (zlist_first (list) == bread);
zlist_remove (list, bread);
assert (zlist_size (list) == 0);
zlist_append (list, cheese);
zlist_append (list, bread);
assert (zlist_last (list) == bread);
zlist_remove (list, bread);
assert (zlist_last (list) == cheese);
zlist_remove (list, cheese);
assert (zlist_last (list) == NULL);
zlist_push (list, cheese);
assert (zlist_size (list) == 1);
assert (zlist_first (list) == cheese);
zlist_push (list, bread);
assert (zlist_size (list) == 2);
assert (zlist_first (list) == bread);
assert (zlist_item (list) == bread);
zlist_append (list, wine);
assert (zlist_size (list) == 3);
assert (zlist_first (list) == bread);
zlist_t *sub_list = zlist_dup (list);
assert (sub_list);
assert (zlist_size (sub_list) == 3);
zlist_sort (list, s_compare);
char *item;
item = (char *) zlist_pop (list);
assert (item == bread);
item = (char *) zlist_pop (list);
assert (item == wine);
item = (char *) zlist_pop (list);
assert (item == cheese);
assert (zlist_size (list) == 0);
assert (zlist_size (sub_list) == 3);
zlist_push (list, sub_list);
zlist_t *sub_list_2 = zlist_dup (sub_list);
zlist_append (list, sub_list_2);
assert (zlist_freefn (list, sub_list, &s_zlist_free, false) == sub_list);
assert (zlist_freefn (list, sub_list_2, &s_zlist_free, true) == sub_list_2);
zlist_destroy (&list);
// Test autofree functionality
list = zlist_new ();
assert (list);
zlist_autofree (list);
// Set equals function otherwise equals will not work as autofree copies strings
zlist_comparefn (list, s_compare);
zlist_push (list, bread);
zlist_append (list, cheese);
assert (zlist_size (list) == 2);
zlist_append (list, wine);
assert (zlist_exists (list, wine));
zlist_remove (list, wine);
assert (!zlist_exists (list, wine));
assert (streq ((const char *) zlist_first (list), bread));
item = (char *) zlist_pop (list);
assert (streq (item, bread));
free (item);
item = (char *) zlist_pop (list);
assert (streq (item, cheese));
free (item);
zlist_destroy (&list);
assert (list == NULL);
// @end
printf ("OK\n");
}