Mon, 25 Feb 2013 16:26:50 +0100
some fixes + ucx_(d)list_contains
1 #include "dlist.h"
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 }
16 int ucx_dlist_equals(const UcxDlist *l1, const UcxDlist *l2,
17 cmp_func fnc, void* data) {
18 if (l1 == l2) return 1;
20 while (l1 != NULL && l2 != NULL) {
21 if (fnc == NULL) {
22 if (l1->data != l2->data) return 0;
23 } else {
24 if (fnc(l1->data, l2->data, data) != 0) return 0;
25 }
26 l1 = l1->next;
27 l2 = l2->next;
28 }
30 return (l1 == NULL && l2 == NULL);
31 }
33 void ucx_dlist_free(UcxDlist *l) {
34 UcxDlist *e = l, *f;
35 while (e != NULL) {
36 f = e;
37 e = e->next;
38 free(f);
39 }
40 }
42 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) {
43 UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist));
44 if (nl == NULL) return NULL;
46 nl->data = data;
47 nl->next = NULL;
48 if (l == NULL) {
49 nl->prev = NULL;
50 return nl;
51 } else {
52 UcxDlist *t = ucx_dlist_last(l);
53 t->next = nl;
54 nl->prev = t;
55 return l;
56 }
57 }
59 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) {
60 UcxDlist *nl = ucx_dlist_append(NULL, data);
61 if (nl == NULL) return NULL;
63 if (l != NULL) {
64 nl->next = l;
65 l->prev = nl;
66 }
67 return nl;
68 }
70 UcxDlist *ucx_dlist_concat(UcxDlist *restrict l1, UcxDlist *restrict l2) {
71 if (l1 == NULL) {
72 return l2;
73 } else {
74 UcxDlist *last = ucx_dlist_last(l1);
75 last->next = l2;
76 l2->prev = last;
77 return l1;
78 }
79 }
81 UcxDlist *ucx_dlist_last(const UcxDlist *l) {
82 if (l == NULL) return NULL;
84 const UcxDlist *e = l;
85 while (e->next != NULL) {
86 e = e->next;
87 }
88 return (UcxDlist*)e;
89 }
91 UcxDlist *ucx_dlist_get(const UcxDlist *l, int index) {
92 if (l == NULL) return NULL;
94 const UcxDlist *e = l;
95 while (e->next != NULL && index > 0) {
96 e = e->next;
97 index--;
98 }
100 return (UcxDlist*)(index == 0 ? e : NULL);
101 }
103 int ucx_dlist_contains(UcxDlist *l, void *elem, cmp_func fnc, void *cmpdata) {
104 UCX_FOREACH(UcxDlist*, l, e) {
105 if (!fnc(elem, e->data, cmpdata)) {
106 return 1;
107 }
108 }
109 return 0;
110 }
112 size_t ucx_dlist_size(const UcxDlist *l) {
113 if (l == NULL) return 0;
115 const UcxDlist *e = l;
116 size_t s = 1;
117 while (e->next != NULL) {
118 e = e->next;
119 s++;
120 }
122 return s;
123 }
125 UcxDlist *ucx_dlist_sort_merge(int length,
126 UcxDlist* restrict ls, UcxDlist* restrict le, UcxDlist* restrict re,
127 cmp_func fnc, void* data) {
129 UcxDlist** sorted = (UcxDlist**) malloc(sizeof(UcxDlist*)*length);
130 UcxDlist *rc, *lc;
132 lc = ls; rc = le;
133 int n = 0;
134 while (lc && lc != le && rc != re) {
135 if (fnc(lc->data, rc->data, data) <= 0) {
136 sorted[n] = lc;
137 lc = lc->next;
138 } else {
139 sorted[n] = rc;
140 rc = rc->next;
141 }
142 n++;
143 }
144 while (lc && lc != le) {
145 sorted[n] = lc;
146 lc = lc->next;
147 n++;
148 }
149 while (rc && rc != re) {
150 sorted[n] = rc;
151 rc = rc->next;
152 n++;
153 }
155 // Update pointer
156 sorted[0]->prev = NULL;
157 for (int i = 0 ; i < length-1 ; i++) {
158 sorted[i]->next = sorted[i+1];
159 sorted[i+1]->prev = sorted[i];
160 }
161 sorted[length-1]->next = NULL;
163 UcxDlist *ret = sorted[0];
164 free(sorted);
165 return ret;
166 }
168 UcxDlist *ucx_dlist_sort(UcxDlist *l, cmp_func fnc, void *data) {
169 if (l == NULL) {
170 return NULL;
171 }
173 UcxDlist *lc;
174 int ln = 1;
176 UcxDlist *restrict ls = l, *restrict le, *restrict re;
177 lc = ls;
178 while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
179 lc = lc->next;
180 ln++;
181 }
182 le = lc->next;
184 if (le == NULL) {
185 return l; // this list is already sorted :)
186 } else {
187 UcxDlist *rc;
188 int rn = 1;
189 rc = le;
190 while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
191 rc = rc->next;
192 rn++;
193 }
194 re = rc->next;
196 // Something left? Sort it!
197 UcxDlist *remainder = re;
198 size_t remainder_length = ucx_dlist_size(remainder);
199 if (remainder != NULL) {
200 remainder = ucx_dlist_sort(remainder, fnc, data);
201 }
203 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
204 UcxDlist *sorted = ucx_dlist_sort_merge(ln+rn,
205 ls, le, re,
206 fnc, data);
208 // merge sorted list with (also sorted) remainder
209 l = ucx_dlist_sort_merge(ln+rn+remainder_length,
210 sorted, remainder, NULL, fnc, data);
212 return l;
213 }
214 }
216 /* dlist specific functions */
217 UcxDlist *ucx_dlist_first(const UcxDlist *l) {
218 if (l == NULL) return NULL;
220 const UcxDlist *e = l;
221 while (e->prev != NULL) {
222 e = e->prev;
223 }
224 return (UcxDlist *)e;
225 }
227 UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e) {
228 if (e->prev == NULL) {
229 if(e->next != NULL) {
230 e->next->prev = NULL;
231 l = e->next;
232 } else {
233 l = NULL;
234 }
236 } else {
237 e->prev->next = e->next;
238 e->next->prev = e->prev;
239 }
240 free(e);
241 return l;
242 }