2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
31 UcxList *ucx_list_clone(const UcxList *l, copy_func fnc, void *data) {
32 return ucx_list_clone_a(ucx_default_allocator(), l, fnc, data);
35 UcxList *ucx_list_clone_a(UcxAllocator *alloc, const UcxList *l,
36 copy_func fnc, void *data) {
40 ret = ucx_list_append_a(alloc, ret, fnc(l->data, data));
42 ret = ucx_list_append_a(alloc, ret, l->data);
49 int ucx_list_equals(const UcxList *l1, const UcxList *l2,
50 cmp_func fnc, void* data) {
51 if (l1 == l2) return 1;
53 while (l1 != NULL && l2 != NULL) {
55 if (l1->data != l2->data) return 0;
57 if (fnc(l1->data, l2->data, data) != 0) return 0;
63 return (l1 == NULL && l2 == NULL);
66 void ucx_list_free(UcxList *l) {
67 ucx_list_free_a(ucx_default_allocator(), l);
70 void ucx_list_free_a(UcxAllocator *alloc, UcxList *l) {
79 void ucx_list_free_content(UcxList* list, ucx_destructor destr) {
80 if (!destr) destr = free;
81 while (list != NULL) {
87 UcxList *ucx_list_append(UcxList *l, void *data) {
88 return ucx_list_append_a(ucx_default_allocator(), l, data);
91 UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data) {
92 UcxList *nl = (UcxList*) almalloc(alloc, sizeof(UcxList));
100 UcxList *t = ucx_list_last(l);
110 UcxList *ucx_list_prepend(UcxList *l, void *data) {
111 return ucx_list_prepend_a(ucx_default_allocator(), l, data);
114 UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) {
115 UcxList *nl = ucx_list_append_a(alloc, NULL, data);
119 l = ucx_list_first(l);
128 UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) {
130 UcxList *last = ucx_list_last(l1);
141 UcxList *ucx_list_last(const UcxList *l) {
142 if (l == NULL) return NULL;
144 const UcxList *e = l;
145 while (e->next != NULL) {
151 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem) {
163 UcxList *ucx_list_get(const UcxList *l, size_t index) {
164 if (l == NULL) return NULL;
166 const UcxList *e = l;
167 while (e->next && index > 0) {
172 return (UcxList*)(index == 0 ? e : NULL);
175 ssize_t ucx_list_find(const UcxList *l, void *elem,
176 cmp_func fnc, void *cmpdata) {
180 if (fnc(elem, e->data, cmpdata) == 0) {
184 if (elem == e->data) {
193 int ucx_list_contains(const UcxList *l, void *elem,
194 cmp_func fnc, void *cmpdata) {
195 return ucx_list_find(l, elem, fnc, cmpdata) > -1;
198 size_t ucx_list_size(const UcxList *l) {
199 if (l == NULL) return 0;
201 const UcxList *e = l;
203 while (e->next != NULL) {
211 static UcxList *ucx_list_sort_merge(size_t length,
212 UcxList* ls, UcxList* le, UcxList* re,
213 cmp_func fnc, void* data) {
215 UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length);
220 while (lc && lc != le && rc != re) {
221 if (fnc(lc->data, rc->data, data) <= 0) {
230 while (lc && lc != le) {
235 while (rc && rc != re) {
242 sorted[0]->prev = NULL;
243 for (int i = 0 ; i < length-1 ; i++) {
244 sorted[i]->next = sorted[i+1];
245 sorted[i+1]->prev = sorted[i];
247 sorted[length-1]->next = NULL;
249 UcxList *ret = sorted[0];
254 UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) {
262 UcxList *ls = l, *le, *re;
264 // check how many elements are already sorted
266 while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
273 return l; // this list is already sorted :)
278 // skip already sorted elements
279 while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
285 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
286 UcxList *sorted = ucx_list_sort_merge(ln+rn,
290 // Something left? Sort it!
291 size_t remainder_length = ucx_list_size(re);
292 if (remainder_length > 0) {
293 UcxList *remainder = ucx_list_sort(re, fnc, data);
295 // merge sorted list with (also sorted) remainder
296 l = ucx_list_sort_merge(ln+rn+remainder_length,
297 sorted, remainder, NULL, fnc, data);
299 // no remainder - we've got our sorted list
307 UcxList *ucx_list_first(const UcxList *l) {
312 const UcxList *e = l;
319 UcxList *ucx_list_remove(UcxList *l, UcxList *e) {
320 return ucx_list_remove_a(ucx_default_allocator(), l, e);
323 UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) {
329 e->next->prev = e->prev;
333 e->prev->next = e->next;
341 static UcxList* ucx_list_setoperation_a(UcxAllocator *allocator,
342 UcxList const *left, UcxList const *right,
343 cmp_func cmpfnc, void* cmpdata,
344 copy_func cpfnc, void* cpdata,
349 const UcxList *src = left;
352 UCX_FOREACH(node, src) {
353 void* elem = node->data;
355 (op == 0 && !ucx_list_contains(res, elem, cmpfnc, cmpdata)) ||
356 (op == 1 && ucx_list_contains(right, elem, cmpfnc, cmpdata)) ||
357 (op == 2 && !ucx_list_contains(right, elem, cmpfnc, cmpdata))) {
358 UcxList *nl = almalloc(allocator, sizeof(UcxList));
362 nl->data = cpfnc(elem, cpdata);
373 if (op == 0 && src == left)
377 } while (src != NULL);
382 UcxList* ucx_list_union(UcxList const *left, UcxList const *right,
383 cmp_func cmpfnc, void* cmpdata,
384 copy_func cpfnc, void* cpdata) {
385 return ucx_list_union_a(ucx_default_allocator(),
386 left, right, cmpfnc, cmpdata, cpfnc, cpdata);
389 UcxList* ucx_list_union_a(UcxAllocator *allocator,
390 UcxList const *left, UcxList const *right,
391 cmp_func cmpfnc, void* cmpdata,
392 copy_func cpfnc, void* cpdata) {
394 return ucx_list_setoperation_a(allocator, left, right,
395 cmpfnc, cmpdata, cpfnc, cpdata, 0);
398 UcxList* ucx_list_intersection(UcxList const *left, UcxList const *right,
399 cmp_func cmpfnc, void* cmpdata,
400 copy_func cpfnc, void* cpdata) {
401 return ucx_list_intersection_a(ucx_default_allocator(), left, right,
402 cmpfnc, cmpdata, cpfnc, cpdata);
405 UcxList* ucx_list_intersection_a(UcxAllocator *allocator,
406 UcxList const *left, UcxList const *right,
407 cmp_func cmpfnc, void* cmpdata,
408 copy_func cpfnc, void* cpdata) {
410 return ucx_list_setoperation_a(allocator, left, right,
411 cmpfnc, cmpdata, cpfnc, cpdata, 1);
414 UcxList* ucx_list_difference(UcxList const *left, UcxList const *right,
415 cmp_func cmpfnc, void* cmpdata,
416 copy_func cpfnc, void* cpdata) {
417 return ucx_list_difference_a(ucx_default_allocator(), left, right,
418 cmpfnc, cmpdata, cpfnc, cpdata);
421 UcxList* ucx_list_difference_a(UcxAllocator *allocator,
422 UcxList const *left, UcxList const *right,
423 cmp_func cmpfnc, void* cmpdata,
424 copy_func cpfnc, void* cpdata) {
426 return ucx_list_setoperation_a(allocator, left, right,
427 cmpfnc, cmpdata, cpfnc, cpdata, 2);