14:b8bf95b39952 | 15:cff9c4101dd7 |
---|---|
1 #include "dlist.h" | 1 #include "dlist.h" |
2 | |
3 UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void *data) { | |
4 UcxDlist *ret = NULL; | |
5 while (l != NULL) { | |
6 if (fnc != NULL) { | |
7 ret = ucx_dlist_append(ret, fnc(l->data, data)); | |
8 } else { | |
9 ret = ucx_dlist_append(ret, l->data); | |
10 } | |
11 l = l->next; | |
12 } | |
13 return ret; | |
14 } | |
15 | |
16 int ucx_dlist_equals(UcxDlist *l1, UcxDlist *l2, cmp_func fnc, void* data) { | |
17 if (l1 == l2) return 1; | |
18 | |
19 while (l1 != NULL && l2 != NULL) { | |
20 if (fnc == NULL) { | |
21 if (l1->data != l2->data) return 0; | |
22 } else { | |
23 if (fnc(l1->data, l2->data, data) != 0) return 0; | |
24 } | |
25 l1 = l1->next; | |
26 l2 = l2->next; | |
27 } | |
28 | |
29 return (l1 == NULL && l2 == NULL); | |
30 } | |
2 | 31 |
3 void ucx_dlist_free(UcxDlist *l) { | 32 void ucx_dlist_free(UcxDlist *l) { |
4 UcxDlist *e = l, *f; | 33 UcxDlist *e = l, *f; |
5 while (e != NULL) { | 34 while (e != NULL) { |
6 f = e; | 35 f = e; |
10 } | 39 } |
11 | 40 |
12 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) { | 41 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) { |
13 UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist)); | 42 UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist)); |
14 if (nl == NULL) return NULL; | 43 if (nl == NULL) return NULL; |
15 | 44 |
16 nl->data = data; | 45 nl->data = data; |
17 nl->next = NULL; | 46 nl->next = NULL; |
18 if (l == NULL) { | 47 if (l == NULL) { |
19 return nl; | 48 return nl; |
20 } else { | 49 } else { |
26 } | 55 } |
27 | 56 |
28 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) { | 57 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) { |
29 UcxDlist *nl = ucx_dlist_append(NULL, data); | 58 UcxDlist *nl = ucx_dlist_append(NULL, data); |
30 if (nl == NULL) return NULL; | 59 if (nl == NULL) return NULL; |
31 | 60 |
32 if (l != NULL) { | 61 if (l != NULL) { |
33 nl->next = l; | 62 nl->next = l; |
34 l->prev = nl; | 63 l->prev = nl; |
35 } | 64 } |
36 return nl; | 65 return nl; |
47 } | 76 } |
48 } | 77 } |
49 | 78 |
50 UcxDlist *ucx_dlist_last(UcxDlist *l) { | 79 UcxDlist *ucx_dlist_last(UcxDlist *l) { |
51 if (l == NULL) return NULL; | 80 if (l == NULL) return NULL; |
52 | 81 |
53 UcxDlist *e = l; | 82 UcxDlist *e = l; |
54 while (e->next != NULL) { | 83 while (e->next != NULL) { |
55 e = e->next; | 84 e = e->next; |
56 } | 85 } |
57 return e; | 86 return e; |
63 UcxDlist *e = l; | 92 UcxDlist *e = l; |
64 while (e->next != NULL && index > 0) { | 93 while (e->next != NULL && index > 0) { |
65 e = e->next; | 94 e = e->next; |
66 index--; | 95 index--; |
67 } | 96 } |
68 | 97 |
69 return index == 0 ? e : NULL; | 98 return index == 0 ? e : NULL; |
70 } | 99 } |
71 | 100 |
72 size_t ucx_dlist_size(UcxDlist *l) { | 101 size_t ucx_dlist_size(UcxDlist *l) { |
73 if (l == NULL) return 0; | 102 if (l == NULL) return 0; |
74 | 103 |
75 UcxDlist *e = l; | 104 UcxDlist *e = l; |
76 size_t s = 1; | 105 size_t s = 1; |
77 while (e->next != NULL) { | 106 while (e->next != NULL) { |
78 e = e->next; | 107 e = e->next; |
79 s++; | 108 s++; |
91 } | 120 } |
92 | 121 |
93 /* dlist specific functions */ | 122 /* dlist specific functions */ |
94 UcxDlist *ucx_dlist_first(UcxDlist *l) { | 123 UcxDlist *ucx_dlist_first(UcxDlist *l) { |
95 if (l == NULL) return NULL; | 124 if (l == NULL) return NULL; |
96 | 125 |
97 UcxDlist *e = l; | 126 UcxDlist *e = l; |
98 while (e->prev != NULL) { | 127 while (e->prev != NULL) { |
99 e = e->prev; | 128 e = e->prev; |
100 } | 129 } |
101 return e; | 130 return e; |