src/server/ucx/dlist.c

changeset 14
b8bf95b39952
parent 13
1fdbf4170ef4
child 15
cff9c4101dd7
equal deleted inserted replaced
13:1fdbf4170ef4 14:b8bf95b39952
1 #include "dlist.h"
2
3 void ucx_dlist_free(UcxDlist *l) {
4 UcxDlist *e = l, *f;
5 while (e != NULL) {
6 f = e;
7 e = e->next;
8 free(f);
9 }
10 }
11
12 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) {
13 UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist));
14 if (nl == NULL) return NULL;
15
16 nl->data = data;
17 nl->next = NULL;
18 if (l == NULL) {
19 return nl;
20 } else {
21 UcxDlist *t = ucx_dlist_last(l);
22 t->next = nl;
23 nl->prev = t;
24 return l;
25 }
26 }
27
28 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) {
29 UcxDlist *nl = ucx_dlist_append(NULL, data);
30 if (nl == NULL) return NULL;
31
32 if (l != NULL) {
33 nl->next = l;
34 l->prev = nl;
35 }
36 return nl;
37 }
38
39 UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) {
40 if (l1 == NULL) {
41 return l2;
42 } else {
43 UcxDlist *last = ucx_dlist_last(l1);
44 last->next = l2;
45 l2->prev = last;
46 return l1;
47 }
48 }
49
50 UcxDlist *ucx_dlist_last(UcxDlist *l) {
51 if (l == NULL) return NULL;
52
53 UcxDlist *e = l;
54 while (e->next != NULL) {
55 e = e->next;
56 }
57 return e;
58 }
59
60 UcxDlist *ucx_dlist_get(UcxDlist *l, int index) {
61 if (l == NULL) return NULL;
62
63 UcxDlist *e = l;
64 while (e->next != NULL && index > 0) {
65 e = e->next;
66 index--;
67 }
68
69 return index == 0 ? e : NULL;
70 }
71
72 size_t ucx_dlist_size(UcxDlist *l) {
73 if (l == NULL) return 0;
74
75 UcxDlist *e = l;
76 size_t s = 1;
77 while (e->next != NULL) {
78 e = e->next;
79 s++;
80 }
81
82 return s;
83 }
84
85 void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data) {
86 UcxDlist *e = l;
87 while (e != NULL) {
88 fnc(e, data);
89 e = e->next;
90 }
91 }
92
93 /* dlist specific functions */
94 UcxDlist *ucx_dlist_first(UcxDlist *l) {
95 if (l == NULL) return NULL;
96
97 UcxDlist *e = l;
98 while (e->prev != NULL) {
99 e = e->prev;
100 }
101 return e;
102 }

mercurial