|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2013 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "list.h" |
|
30 |
|
31 UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) { |
|
32 return ucx_list_clone_a(ucx_default_allocator(), l, fnc, data); |
|
33 } |
|
34 |
|
35 UcxList *ucx_list_clone_a(UcxAllocator *alloc, UcxList *l, |
|
36 copy_func fnc, void *data) { |
|
37 UcxList *ret = NULL; |
|
38 while (l) { |
|
39 if (fnc) { |
|
40 ret = ucx_list_append_a(alloc, ret, fnc(l->data, data)); |
|
41 } else { |
|
42 ret = ucx_list_append_a(alloc, ret, l->data); |
|
43 } |
|
44 l = l->next; |
|
45 } |
|
46 return ret; |
|
47 } |
|
48 |
|
49 int ucx_list_equals(const UcxList *l1, const UcxList *l2, |
|
50 cmp_func fnc, void* data) { |
|
51 if (l1 == l2) return 1; |
|
52 |
|
53 while (l1 != NULL && l2 != NULL) { |
|
54 if (fnc == NULL) { |
|
55 if (l1->data != l2->data) return 0; |
|
56 } else { |
|
57 if (fnc(l1->data, l2->data, data) != 0) return 0; |
|
58 } |
|
59 l1 = l1->next; |
|
60 l2 = l2->next; |
|
61 } |
|
62 |
|
63 return (l1 == NULL && l2 == NULL); |
|
64 } |
|
65 |
|
66 void ucx_list_free(UcxList *l) { |
|
67 ucx_list_free_a(ucx_default_allocator(), l); |
|
68 } |
|
69 |
|
70 void ucx_list_free_a(UcxAllocator *alloc, UcxList *l) { |
|
71 UcxList *e = l, *f; |
|
72 while (e != NULL) { |
|
73 f = e; |
|
74 e = e->next; |
|
75 alloc->free(alloc->pool, f); |
|
76 } |
|
77 } |
|
78 |
|
79 UcxList *ucx_list_append(UcxList *l, void *data) { |
|
80 return ucx_list_append_a(ucx_default_allocator(), l, data); |
|
81 } |
|
82 |
|
83 UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data) { |
|
84 UcxList *nl = (UcxList*) alloc->malloc(alloc->pool, sizeof(UcxList)); |
|
85 if (!nl) { |
|
86 return NULL; |
|
87 } |
|
88 |
|
89 nl->data = data; |
|
90 nl->next = NULL; |
|
91 if (l) { |
|
92 UcxList *t = ucx_list_last(l); |
|
93 t->next = nl; |
|
94 nl->prev = t; |
|
95 return l; |
|
96 } else { |
|
97 nl->prev = NULL; |
|
98 return nl; |
|
99 } |
|
100 } |
|
101 |
|
102 UcxList *ucx_list_prepend(UcxList *l, void *data) { |
|
103 return ucx_list_prepend_a(ucx_default_allocator(), l, data); |
|
104 } |
|
105 |
|
106 UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) { |
|
107 UcxList *nl = ucx_list_append_a(alloc, NULL, data); |
|
108 if (!nl) { |
|
109 return NULL; |
|
110 } |
|
111 l = ucx_list_first(l); |
|
112 |
|
113 if (l) { |
|
114 nl->next = l; |
|
115 l->prev = nl; |
|
116 } |
|
117 return nl; |
|
118 } |
|
119 |
|
120 UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) { |
|
121 if (l1) { |
|
122 UcxList *last = ucx_list_last(l1); |
|
123 last->next = l2; |
|
124 l2->prev = last; |
|
125 return l1; |
|
126 } else { |
|
127 return l2; |
|
128 } |
|
129 } |
|
130 |
|
131 UcxList *ucx_list_last(const UcxList *l) { |
|
132 if (l == NULL) return NULL; |
|
133 |
|
134 const UcxList *e = l; |
|
135 while (e->next != NULL) { |
|
136 e = e->next; |
|
137 } |
|
138 return (UcxList*)e; |
|
139 } |
|
140 |
|
141 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem) { |
|
142 ssize_t index = 0; |
|
143 while (list) { |
|
144 if (list == elem) { |
|
145 return index; |
|
146 } |
|
147 list = list->next; |
|
148 index++; |
|
149 } |
|
150 return -1; |
|
151 } |
|
152 |
|
153 UcxList *ucx_list_get(const UcxList *l, int index) { |
|
154 if (l == NULL) return NULL; |
|
155 |
|
156 const UcxList *e = l; |
|
157 while (e->next && index > 0) { |
|
158 e = e->next; |
|
159 index--; |
|
160 } |
|
161 |
|
162 return (UcxList*)(index == 0 ? e : NULL); |
|
163 } |
|
164 |
|
165 ssize_t ucx_list_find(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) { |
|
166 ssize_t index = 0; |
|
167 UCX_FOREACH(e, l) { |
|
168 if (fnc) { |
|
169 if (fnc(elem, e->data, cmpdata) == 0) { |
|
170 return index; |
|
171 } |
|
172 } else { |
|
173 if (elem == e->data) { |
|
174 return index; |
|
175 } |
|
176 } |
|
177 index++; |
|
178 } |
|
179 return -1; |
|
180 } |
|
181 |
|
182 int ucx_list_contains(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) { |
|
183 return ucx_list_find(l, elem, fnc, cmpdata) > -1; |
|
184 } |
|
185 |
|
186 size_t ucx_list_size(const UcxList *l) { |
|
187 if (l == NULL) return 0; |
|
188 |
|
189 const UcxList *e = l; |
|
190 size_t s = 1; |
|
191 while (e->next != NULL) { |
|
192 e = e->next; |
|
193 s++; |
|
194 } |
|
195 |
|
196 return s; |
|
197 } |
|
198 |
|
199 UcxList *ucx_list_sort_merge(int length, |
|
200 UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re, |
|
201 cmp_func fnc, void* data) { |
|
202 |
|
203 UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length); |
|
204 UcxList *rc, *lc; |
|
205 |
|
206 lc = ls; rc = le; |
|
207 int n = 0; |
|
208 while (lc && lc != le && rc != re) { |
|
209 if (fnc(lc->data, rc->data, data) <= 0) { |
|
210 sorted[n] = lc; |
|
211 lc = lc->next; |
|
212 } else { |
|
213 sorted[n] = rc; |
|
214 rc = rc->next; |
|
215 } |
|
216 n++; |
|
217 } |
|
218 while (lc && lc != le) { |
|
219 sorted[n] = lc; |
|
220 lc = lc->next; |
|
221 n++; |
|
222 } |
|
223 while (rc && rc != re) { |
|
224 sorted[n] = rc; |
|
225 rc = rc->next; |
|
226 n++; |
|
227 } |
|
228 |
|
229 // Update pointer |
|
230 sorted[0]->prev = NULL; |
|
231 for (int i = 0 ; i < length-1 ; i++) { |
|
232 sorted[i]->next = sorted[i+1]; |
|
233 sorted[i+1]->prev = sorted[i]; |
|
234 } |
|
235 sorted[length-1]->next = NULL; |
|
236 |
|
237 UcxList *ret = sorted[0]; |
|
238 free(sorted); |
|
239 return ret; |
|
240 } |
|
241 |
|
242 UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) { |
|
243 if (l == NULL) { |
|
244 return NULL; |
|
245 } |
|
246 |
|
247 UcxList *lc; |
|
248 int ln = 1; |
|
249 |
|
250 UcxList *restrict ls = l, *restrict le, *restrict re; |
|
251 lc = ls; |
|
252 while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) { |
|
253 lc = lc->next; |
|
254 ln++; |
|
255 } |
|
256 le = lc->next; |
|
257 |
|
258 if (le == NULL) { |
|
259 return l; // this list is already sorted :) |
|
260 } else { |
|
261 UcxList *rc; |
|
262 int rn = 1; |
|
263 rc = le; |
|
264 while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) { |
|
265 rc = rc->next; |
|
266 rn++; |
|
267 } |
|
268 re = rc->next; |
|
269 |
|
270 // Something left? Sort it! |
|
271 UcxList *remainder = re; |
|
272 size_t remainder_length = ucx_list_size(remainder); |
|
273 if (remainder != NULL) { |
|
274 remainder = ucx_list_sort(remainder, fnc, data); |
|
275 } |
|
276 |
|
277 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them |
|
278 UcxList *sorted = ucx_list_sort_merge(ln+rn, |
|
279 ls, le, re, |
|
280 fnc, data); |
|
281 |
|
282 // merge sorted list with (also sorted) remainder |
|
283 l = ucx_list_sort_merge(ln+rn+remainder_length, |
|
284 sorted, remainder, NULL, fnc, data); |
|
285 |
|
286 return l; |
|
287 } |
|
288 } |
|
289 |
|
290 UcxList *ucx_list_first(const UcxList *l) { |
|
291 if (!l) { |
|
292 return NULL; |
|
293 } |
|
294 |
|
295 const UcxList *e = l; |
|
296 while (e->prev) { |
|
297 e = e->prev; |
|
298 } |
|
299 return (UcxList *)e; |
|
300 } |
|
301 |
|
302 UcxList *ucx_list_remove(UcxList *l, UcxList *e) { |
|
303 return ucx_list_remove_a(ucx_default_allocator(), l, e); |
|
304 } |
|
305 |
|
306 UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) { |
|
307 if (e->prev == NULL) { |
|
308 if(e->next != NULL) { |
|
309 e->next->prev = NULL; |
|
310 l = e->next; |
|
311 } else { |
|
312 l = NULL; |
|
313 } |
|
314 |
|
315 } else { |
|
316 e->prev->next = e->next; |
|
317 e->next->prev = e->prev; |
|
318 } |
|
319 alloc->free(alloc->pool, e); |
|
320 return l; |
|
321 } |