Fri, 20 Dec 2019 14:29:41 +0100
adds proper cmake build targets
universe@103 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@103 | 3 | * |
universe@259 | 4 | * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@103 | 27 | */ |
universe@103 | 28 | |
universe@251 | 29 | #include "ucx/list.h" |
universe@4 | 30 | |
universe@371 | 31 | UcxList *ucx_list_clone(const UcxList *l, copy_func fnc, void *data) { |
universe@125 | 32 | return ucx_list_clone_a(ucx_default_allocator(), l, fnc, data); |
universe@125 | 33 | } |
universe@125 | 34 | |
universe@371 | 35 | UcxList *ucx_list_clone_a(UcxAllocator *alloc, const UcxList *l, |
universe@125 | 36 | copy_func fnc, void *data) { |
universe@122 | 37 | UcxList *ret = NULL; |
universe@125 | 38 | while (l) { |
universe@125 | 39 | if (fnc) { |
universe@125 | 40 | ret = ucx_list_append_a(alloc, ret, fnc(l->data, data)); |
universe@18 | 41 | } else { |
universe@125 | 42 | ret = ucx_list_append_a(alloc, ret, l->data); |
universe@18 | 43 | } |
universe@18 | 44 | l = l->next; |
universe@18 | 45 | } |
universe@18 | 46 | return ret; |
universe@18 | 47 | } |
universe@18 | 48 | |
universe@122 | 49 | int ucx_list_equals(const UcxList *l1, const UcxList *l2, |
universe@67 | 50 | cmp_func fnc, void* data) { |
universe@18 | 51 | if (l1 == l2) return 1; |
universe@18 | 52 | |
universe@18 | 53 | while (l1 != NULL && l2 != NULL) { |
universe@18 | 54 | if (fnc == NULL) { |
universe@18 | 55 | if (l1->data != l2->data) return 0; |
universe@18 | 56 | } else { |
universe@18 | 57 | if (fnc(l1->data, l2->data, data) != 0) return 0; |
universe@18 | 58 | } |
universe@18 | 59 | l1 = l1->next; |
universe@18 | 60 | l2 = l2->next; |
universe@18 | 61 | } |
universe@18 | 62 | |
universe@18 | 63 | return (l1 == NULL && l2 == NULL); |
universe@18 | 64 | } |
universe@18 | 65 | |
universe@122 | 66 | void ucx_list_free(UcxList *l) { |
universe@125 | 67 | ucx_list_free_a(ucx_default_allocator(), l); |
universe@125 | 68 | } |
universe@125 | 69 | |
universe@125 | 70 | void ucx_list_free_a(UcxAllocator *alloc, UcxList *l) { |
universe@122 | 71 | UcxList *e = l, *f; |
universe@8 | 72 | while (e != NULL) { |
universe@8 | 73 | f = e; |
universe@8 | 74 | e = e->next; |
universe@173 | 75 | alfree(alloc, f); |
universe@8 | 76 | } |
universe@8 | 77 | } |
universe@8 | 78 | |
universe@212 | 79 | void ucx_list_free_content(UcxList* list, ucx_destructor destr) { |
universe@277 | 80 | if (!destr) destr = free; |
universe@211 | 81 | while (list != NULL) { |
universe@211 | 82 | destr(list->data); |
universe@211 | 83 | list = list->next; |
universe@211 | 84 | } |
universe@211 | 85 | } |
universe@211 | 86 | |
universe@122 | 87 | UcxList *ucx_list_append(UcxList *l, void *data) { |
universe@125 | 88 | return ucx_list_append_a(ucx_default_allocator(), l, data); |
universe@125 | 89 | } |
universe@125 | 90 | |
universe@125 | 91 | UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data) { |
universe@173 | 92 | UcxList *nl = (UcxList*) almalloc(alloc, sizeof(UcxList)); |
universe@125 | 93 | if (!nl) { |
universe@125 | 94 | return NULL; |
universe@125 | 95 | } |
universe@7 | 96 | |
universe@8 | 97 | nl->data = data; |
universe@8 | 98 | nl->next = NULL; |
universe@125 | 99 | if (l) { |
universe@122 | 100 | UcxList *t = ucx_list_last(l); |
universe@8 | 101 | t->next = nl; |
universe@8 | 102 | nl->prev = t; |
universe@8 | 103 | return l; |
universe@125 | 104 | } else { |
universe@125 | 105 | nl->prev = NULL; |
universe@125 | 106 | return nl; |
universe@8 | 107 | } |
universe@7 | 108 | } |
universe@7 | 109 | |
universe@122 | 110 | UcxList *ucx_list_prepend(UcxList *l, void *data) { |
universe@125 | 111 | return ucx_list_prepend_a(ucx_default_allocator(), l, data); |
universe@125 | 112 | } |
universe@125 | 113 | |
universe@125 | 114 | UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) { |
universe@125 | 115 | UcxList *nl = ucx_list_append_a(alloc, NULL, data); |
universe@125 | 116 | if (!nl) { |
universe@125 | 117 | return NULL; |
universe@125 | 118 | } |
universe@125 | 119 | l = ucx_list_first(l); |
universe@7 | 120 | |
universe@125 | 121 | if (l) { |
universe@8 | 122 | nl->next = l; |
universe@8 | 123 | l->prev = nl; |
universe@8 | 124 | } |
universe@8 | 125 | return nl; |
universe@7 | 126 | } |
universe@7 | 127 | |
universe@122 | 128 | UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) { |
universe@128 | 129 | if (l1) { |
universe@122 | 130 | UcxList *last = ucx_list_last(l1); |
universe@8 | 131 | last->next = l2; |
universe@172 | 132 | if (l2) { |
universe@172 | 133 | l2->prev = last; |
universe@172 | 134 | } |
universe@8 | 135 | return l1; |
universe@128 | 136 | } else { |
universe@128 | 137 | return l2; |
universe@8 | 138 | } |
universe@7 | 139 | } |
universe@7 | 140 | |
universe@122 | 141 | UcxList *ucx_list_last(const UcxList *l) { |
universe@7 | 142 | if (l == NULL) return NULL; |
universe@7 | 143 | |
universe@122 | 144 | const UcxList *e = l; |
universe@7 | 145 | while (e->next != NULL) { |
universe@7 | 146 | e = e->next; |
universe@7 | 147 | } |
universe@122 | 148 | return (UcxList*)e; |
universe@7 | 149 | } |
universe@7 | 150 | |
universe@123 | 151 | ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem) { |
universe@123 | 152 | ssize_t index = 0; |
universe@123 | 153 | while (list) { |
universe@123 | 154 | if (list == elem) { |
universe@123 | 155 | return index; |
universe@123 | 156 | } |
universe@123 | 157 | list = list->next; |
universe@123 | 158 | index++; |
universe@123 | 159 | } |
universe@123 | 160 | return -1; |
universe@123 | 161 | } |
universe@123 | 162 | |
universe@172 | 163 | UcxList *ucx_list_get(const UcxList *l, size_t index) { |
universe@8 | 164 | if (l == NULL) return NULL; |
universe@8 | 165 | |
universe@122 | 166 | const UcxList *e = l; |
universe@128 | 167 | while (e->next && index > 0) { |
universe@8 | 168 | e = e->next; |
universe@8 | 169 | index--; |
universe@8 | 170 | } |
universe@7 | 171 | |
universe@122 | 172 | return (UcxList*)(index == 0 ? e : NULL); |
universe@7 | 173 | } |
universe@7 | 174 | |
universe@371 | 175 | ssize_t ucx_list_find(const UcxList *l, void *elem, |
universe@371 | 176 | cmp_func fnc, void *cmpdata) { |
universe@123 | 177 | ssize_t index = 0; |
universe@123 | 178 | UCX_FOREACH(e, l) { |
universe@128 | 179 | if (fnc) { |
universe@128 | 180 | if (fnc(elem, e->data, cmpdata) == 0) { |
universe@128 | 181 | return index; |
universe@128 | 182 | } |
universe@128 | 183 | } else { |
universe@128 | 184 | if (elem == e->data) { |
universe@128 | 185 | return index; |
universe@128 | 186 | } |
universe@123 | 187 | } |
universe@123 | 188 | index++; |
universe@123 | 189 | } |
universe@123 | 190 | return -1; |
universe@123 | 191 | } |
universe@123 | 192 | |
universe@371 | 193 | int ucx_list_contains(const UcxList *l, void *elem, |
universe@371 | 194 | cmp_func fnc, void *cmpdata) { |
universe@123 | 195 | return ucx_list_find(l, elem, fnc, cmpdata) > -1; |
universe@87 | 196 | } |
universe@87 | 197 | |
universe@122 | 198 | size_t ucx_list_size(const UcxList *l) { |
universe@7 | 199 | if (l == NULL) return 0; |
universe@7 | 200 | |
universe@122 | 201 | const UcxList *e = l; |
universe@7 | 202 | size_t s = 1; |
universe@7 | 203 | while (e->next != NULL) { |
universe@7 | 204 | e = e->next; |
universe@7 | 205 | s++; |
universe@7 | 206 | } |
universe@7 | 207 | |
universe@7 | 208 | return s; |
universe@7 | 209 | } |
universe@7 | 210 | |
universe@335 | 211 | static UcxList *ucx_list_sort_merge(size_t length, |
universe@253 | 212 | UcxList* ls, UcxList* le, UcxList* re, |
universe@37 | 213 | cmp_func fnc, void* data) { |
universe@73 | 214 | |
universe@122 | 215 | UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length); |
universe@122 | 216 | UcxList *rc, *lc; |
universe@35 | 217 | |
universe@67 | 218 | lc = ls; rc = le; |
universe@335 | 219 | size_t n = 0; |
universe@67 | 220 | while (lc && lc != le && rc != re) { |
universe@37 | 221 | if (fnc(lc->data, rc->data, data) <= 0) { |
universe@37 | 222 | sorted[n] = lc; |
universe@37 | 223 | lc = lc->next; |
universe@37 | 224 | } else { |
universe@37 | 225 | sorted[n] = rc; |
universe@37 | 226 | rc = rc->next; |
universe@35 | 227 | } |
universe@37 | 228 | n++; |
universe@37 | 229 | } |
universe@67 | 230 | while (lc && lc != le) { |
universe@37 | 231 | sorted[n] = lc; |
universe@37 | 232 | lc = lc->next; |
universe@37 | 233 | n++; |
universe@37 | 234 | } |
universe@67 | 235 | while (rc && rc != re) { |
universe@37 | 236 | sorted[n] = rc; |
universe@37 | 237 | rc = rc->next; |
universe@37 | 238 | n++; |
universe@35 | 239 | } |
universe@35 | 240 | |
universe@37 | 241 | // Update pointer |
universe@37 | 242 | sorted[0]->prev = NULL; |
universe@37 | 243 | for (int i = 0 ; i < length-1 ; i++) { |
universe@37 | 244 | sorted[i]->next = sorted[i+1]; |
universe@37 | 245 | sorted[i+1]->prev = sorted[i]; |
universe@37 | 246 | } |
universe@37 | 247 | sorted[length-1]->next = NULL; |
universe@35 | 248 | |
universe@122 | 249 | UcxList *ret = sorted[0]; |
universe@73 | 250 | free(sorted); |
universe@69 | 251 | return ret; |
universe@35 | 252 | } |
universe@35 | 253 | |
universe@122 | 254 | UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) { |
universe@35 | 255 | if (l == NULL) { |
universe@35 | 256 | return NULL; |
universe@35 | 257 | } |
universe@37 | 258 | |
universe@122 | 259 | UcxList *lc; |
universe@335 | 260 | size_t ln = 1; |
universe@37 | 261 | |
universe@253 | 262 | UcxList *ls = l, *le, *re; |
universe@172 | 263 | |
universe@172 | 264 | // check how many elements are already sorted |
universe@37 | 265 | lc = ls; |
universe@37 | 266 | while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) { |
universe@37 | 267 | lc = lc->next; |
universe@37 | 268 | ln++; |
universe@37 | 269 | } |
universe@37 | 270 | le = lc->next; |
universe@37 | 271 | |
universe@67 | 272 | if (le == NULL) { |
universe@37 | 273 | return l; // this list is already sorted :) |
universe@37 | 274 | } else { |
universe@122 | 275 | UcxList *rc; |
universe@335 | 276 | size_t rn = 1; |
universe@67 | 277 | rc = le; |
universe@172 | 278 | // skip already sorted elements |
universe@37 | 279 | while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) { |
universe@37 | 280 | rc = rc->next; |
universe@37 | 281 | rn++; |
universe@37 | 282 | } |
universe@37 | 283 | re = rc->next; |
universe@37 | 284 | |
universe@37 | 285 | // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them |
universe@122 | 286 | UcxList *sorted = ucx_list_sort_merge(ln+rn, |
universe@67 | 287 | ls, le, re, |
universe@37 | 288 | fnc, data); |
universe@172 | 289 | |
universe@172 | 290 | // Something left? Sort it! |
universe@172 | 291 | size_t remainder_length = ucx_list_size(re); |
universe@172 | 292 | if (remainder_length > 0) { |
universe@172 | 293 | UcxList *remainder = ucx_list_sort(re, fnc, data); |
universe@37 | 294 | |
universe@172 | 295 | // merge sorted list with (also sorted) remainder |
universe@172 | 296 | l = ucx_list_sort_merge(ln+rn+remainder_length, |
universe@172 | 297 | sorted, remainder, NULL, fnc, data); |
universe@172 | 298 | } else { |
universe@172 | 299 | // no remainder - we've got our sorted list |
universe@172 | 300 | l = sorted; |
universe@172 | 301 | } |
universe@37 | 302 | |
universe@35 | 303 | return l; |
universe@35 | 304 | } |
universe@35 | 305 | } |
universe@35 | 306 | |
universe@122 | 307 | UcxList *ucx_list_first(const UcxList *l) { |
universe@125 | 308 | if (!l) { |
universe@125 | 309 | return NULL; |
universe@125 | 310 | } |
universe@7 | 311 | |
universe@122 | 312 | const UcxList *e = l; |
universe@125 | 313 | while (e->prev) { |
universe@8 | 314 | e = e->prev; |
universe@8 | 315 | } |
universe@122 | 316 | return (UcxList *)e; |
olaf@13 | 317 | } |
universe@22 | 318 | |
universe@122 | 319 | UcxList *ucx_list_remove(UcxList *l, UcxList *e) { |
universe@125 | 320 | return ucx_list_remove_a(ucx_default_allocator(), l, e); |
universe@125 | 321 | } |
universe@125 | 322 | |
universe@125 | 323 | UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) { |
universe@161 | 324 | if (l == e) { |
universe@161 | 325 | l = e->next; |
universe@161 | 326 | } |
universe@161 | 327 | |
universe@161 | 328 | if (e->next) { |
universe@22 | 329 | e->next->prev = e->prev; |
universe@22 | 330 | } |
universe@161 | 331 | |
universe@161 | 332 | if (e->prev) { |
universe@161 | 333 | e->prev->next = e->next; |
universe@161 | 334 | } |
universe@161 | 335 | |
universe@173 | 336 | alfree(alloc, e); |
universe@22 | 337 | return l; |
universe@22 | 338 | } |
universe@371 | 339 | |
universe@371 | 340 | |
universe@371 | 341 | static UcxList* ucx_list_setoperation_a(UcxAllocator *allocator, |
universe@371 | 342 | UcxList const *left, UcxList const *right, |
universe@371 | 343 | cmp_func cmpfnc, void* cmpdata, |
universe@371 | 344 | copy_func cpfnc, void* cpdata, |
universe@371 | 345 | int op) { |
universe@371 | 346 | |
universe@371 | 347 | UcxList *res = NULL; |
universe@371 | 348 | UcxList *cur = NULL; |
universe@371 | 349 | const UcxList *src = left; |
universe@371 | 350 | |
universe@371 | 351 | do { |
universe@371 | 352 | UCX_FOREACH(node, src) { |
universe@371 | 353 | void* elem = node->data; |
universe@371 | 354 | if ( |
universe@371 | 355 | (op == 0 && !ucx_list_contains(res, elem, cmpfnc, cmpdata)) || |
universe@371 | 356 | (op == 1 && ucx_list_contains(right, elem, cmpfnc, cmpdata)) || |
universe@371 | 357 | (op == 2 && !ucx_list_contains(right, elem, cmpfnc, cmpdata))) { |
universe@371 | 358 | UcxList *nl = almalloc(allocator, sizeof(UcxList)); |
universe@371 | 359 | nl->prev = cur; |
universe@371 | 360 | nl->next = NULL; |
universe@371 | 361 | if (cpfnc) { |
universe@371 | 362 | nl->data = cpfnc(elem, cpdata); |
universe@371 | 363 | } else { |
universe@371 | 364 | nl->data = elem; |
universe@371 | 365 | } |
universe@371 | 366 | if (cur != NULL) |
universe@371 | 367 | cur->next = nl; |
universe@371 | 368 | cur = nl; |
universe@371 | 369 | if (res == NULL) |
universe@371 | 370 | res = cur; |
universe@371 | 371 | } |
universe@371 | 372 | } |
universe@371 | 373 | if (op == 0 && src == left) |
universe@371 | 374 | src = right; |
universe@371 | 375 | else |
universe@371 | 376 | src = NULL; |
universe@371 | 377 | } while (src != NULL); |
universe@371 | 378 | |
universe@371 | 379 | return res; |
universe@371 | 380 | } |
universe@371 | 381 | |
universe@371 | 382 | UcxList* ucx_list_union(UcxList const *left, UcxList const *right, |
universe@371 | 383 | cmp_func cmpfnc, void* cmpdata, |
universe@371 | 384 | copy_func cpfnc, void* cpdata) { |
universe@371 | 385 | return ucx_list_union_a(ucx_default_allocator(), |
universe@371 | 386 | left, right, cmpfnc, cmpdata, cpfnc, cpdata); |
universe@371 | 387 | } |
universe@371 | 388 | |
universe@371 | 389 | UcxList* ucx_list_union_a(UcxAllocator *allocator, |
universe@371 | 390 | UcxList const *left, UcxList const *right, |
universe@371 | 391 | cmp_func cmpfnc, void* cmpdata, |
universe@371 | 392 | copy_func cpfnc, void* cpdata) { |
universe@371 | 393 | |
universe@371 | 394 | return ucx_list_setoperation_a(allocator, left, right, |
universe@371 | 395 | cmpfnc, cmpdata, cpfnc, cpdata, 0); |
universe@371 | 396 | } |
universe@371 | 397 | |
universe@371 | 398 | UcxList* ucx_list_intersection(UcxList const *left, UcxList const *right, |
universe@371 | 399 | cmp_func cmpfnc, void* cmpdata, |
universe@371 | 400 | copy_func cpfnc, void* cpdata) { |
universe@371 | 401 | return ucx_list_intersection_a(ucx_default_allocator(), left, right, |
universe@371 | 402 | cmpfnc, cmpdata, cpfnc, cpdata); |
universe@371 | 403 | } |
universe@371 | 404 | |
universe@371 | 405 | UcxList* ucx_list_intersection_a(UcxAllocator *allocator, |
universe@371 | 406 | UcxList const *left, UcxList const *right, |
universe@371 | 407 | cmp_func cmpfnc, void* cmpdata, |
universe@371 | 408 | copy_func cpfnc, void* cpdata) { |
universe@371 | 409 | |
universe@371 | 410 | return ucx_list_setoperation_a(allocator, left, right, |
universe@371 | 411 | cmpfnc, cmpdata, cpfnc, cpdata, 1); |
universe@371 | 412 | } |
universe@371 | 413 | |
universe@371 | 414 | UcxList* ucx_list_difference(UcxList const *left, UcxList const *right, |
universe@371 | 415 | cmp_func cmpfnc, void* cmpdata, |
universe@371 | 416 | copy_func cpfnc, void* cpdata) { |
universe@371 | 417 | return ucx_list_difference_a(ucx_default_allocator(), left, right, |
universe@371 | 418 | cmpfnc, cmpdata, cpfnc, cpdata); |
universe@371 | 419 | } |
universe@371 | 420 | |
universe@371 | 421 | UcxList* ucx_list_difference_a(UcxAllocator *allocator, |
universe@371 | 422 | UcxList const *left, UcxList const *right, |
universe@371 | 423 | cmp_func cmpfnc, void* cmpdata, |
universe@371 | 424 | copy_func cpfnc, void* cpdata) { |
universe@371 | 425 | |
universe@371 | 426 | return ucx_list_setoperation_a(allocator, left, right, |
universe@371 | 427 | cmpfnc, cmpdata, cpfnc, cpdata, 2); |
universe@371 | 428 | } |