ucx/list.c

Fri, 18 Nov 2016 15:17:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 18 Nov 2016 15:17:04 +0100
changeset 228
9f385abc72fb
parent 225
a1a068c2c4ef
child 229
9db71925eaa8
permissions
-rw-r--r--

adds ucx_list_append_once() and ucx_list_prepend_once()

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@225 4 * Copyright 2016 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@122 29 #include "list.h"
universe@4 30
universe@122 31 UcxList *ucx_list_clone(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@125 35 UcxList *ucx_list_clone_a(UcxAllocator *alloc, 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@211 80 while (list != NULL) {
universe@211 81 destr(list->data);
universe@211 82 list = list->next;
universe@211 83 }
universe@211 84 }
universe@211 85
universe@122 86 UcxList *ucx_list_append(UcxList *l, void *data) {
universe@125 87 return ucx_list_append_a(ucx_default_allocator(), l, data);
universe@125 88 }
universe@125 89
universe@125 90 UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data) {
universe@173 91 UcxList *nl = (UcxList*) almalloc(alloc, sizeof(UcxList));
universe@125 92 if (!nl) {
universe@125 93 return NULL;
universe@125 94 }
universe@7 95
universe@8 96 nl->data = data;
universe@8 97 nl->next = NULL;
universe@125 98 if (l) {
universe@122 99 UcxList *t = ucx_list_last(l);
universe@8 100 t->next = nl;
universe@8 101 nl->prev = t;
universe@8 102 return l;
universe@125 103 } else {
universe@125 104 nl->prev = NULL;
universe@125 105 return nl;
universe@8 106 }
universe@7 107 }
universe@7 108
universe@228 109 UcxList *ucx_list_append_once(UcxList *l, void *data,
universe@228 110 cmp_func cmpfnc, void *cmpdata) {
universe@228 111 return ucx_list_append_once_a(ucx_default_allocator(), l,
universe@228 112 data, cmpfnc, cmpdata);
universe@228 113 }
universe@228 114
universe@228 115 UcxList *ucx_list_append_once_a(UcxAllocator *alloc, UcxList *l, void *data,
universe@228 116 cmp_func cmpfnc, void *cmpdata) {
universe@228 117
universe@228 118 UcxList *last = NULL;
universe@228 119 {
universe@228 120 UcxList *e = l;
universe@228 121 while (e) {
universe@228 122 if (cmpfnc(e->data, data, cmpdata) == 0) {
universe@228 123 return l;
universe@228 124 }
universe@228 125 last = e;
universe@228 126 e = e->next;
universe@228 127 }
universe@228 128 }
universe@228 129
universe@228 130 UcxList *nl = ucx_list_append_a(alloc, NULL, data);
universe@228 131 if (!nl) {
universe@228 132 return NULL;
universe@228 133 }
universe@228 134
universe@228 135 if (last == NULL) {
universe@228 136 return nl;
universe@228 137 } else {
universe@228 138 nl->prev = last;
universe@228 139 last->next = nl;
universe@228 140 return l;
universe@228 141 }
universe@228 142 }
universe@228 143
universe@122 144 UcxList *ucx_list_prepend(UcxList *l, void *data) {
universe@125 145 return ucx_list_prepend_a(ucx_default_allocator(), l, data);
universe@125 146 }
universe@125 147
universe@125 148 UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) {
universe@125 149 UcxList *nl = ucx_list_append_a(alloc, NULL, data);
universe@125 150 if (!nl) {
universe@125 151 return NULL;
universe@125 152 }
universe@125 153 l = ucx_list_first(l);
universe@7 154
universe@125 155 if (l) {
universe@8 156 nl->next = l;
universe@8 157 l->prev = nl;
universe@8 158 }
universe@8 159 return nl;
universe@7 160 }
universe@7 161
universe@228 162 UcxList *ucx_list_prepend_once(UcxList *l, void *data,
universe@228 163 cmp_func cmpfnc, void* cmpdata) {
universe@228 164 return ucx_list_prepend_once_a(ucx_default_allocator(), l,
universe@228 165 data, cmpfnc, cmpdata);
universe@228 166 }
universe@228 167
universe@228 168 UcxList *ucx_list_prepend_once_a(UcxAllocator *alloc, UcxList *l, void *data,
universe@228 169 cmp_func cmpfnc, void *cmpdata) {
universe@228 170
universe@228 171 if (l) {
universe@228 172 int found = 0;
universe@228 173 UcxList *first;
universe@228 174 {
universe@228 175 UcxList *e = l;
universe@228 176 while (e) {
universe@228 177 found |= (cmpfnc(e->data, data, cmpdata) == 0);
universe@228 178 first = e;
universe@228 179 e = e->prev;
universe@228 180 }
universe@228 181 }
universe@228 182
universe@228 183 if (found) {
universe@228 184 return first;
universe@228 185 } else {
universe@228 186 UcxList *nl = ucx_list_append_a(alloc, NULL, data);
universe@228 187 nl->next = first;
universe@228 188 first->prev = nl;
universe@228 189 return nl;
universe@228 190 }
universe@228 191 } else {
universe@228 192 return ucx_list_append_a(alloc, NULL, data);
universe@228 193 }
universe@228 194 }
universe@228 195
universe@122 196 UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) {
universe@128 197 if (l1) {
universe@122 198 UcxList *last = ucx_list_last(l1);
universe@8 199 last->next = l2;
universe@172 200 if (l2) {
universe@172 201 l2->prev = last;
universe@172 202 }
universe@8 203 return l1;
universe@128 204 } else {
universe@128 205 return l2;
universe@8 206 }
universe@7 207 }
universe@7 208
universe@122 209 UcxList *ucx_list_last(const UcxList *l) {
universe@7 210 if (l == NULL) return NULL;
universe@7 211
universe@122 212 const UcxList *e = l;
universe@7 213 while (e->next != NULL) {
universe@7 214 e = e->next;
universe@7 215 }
universe@122 216 return (UcxList*)e;
universe@7 217 }
universe@7 218
universe@123 219 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem) {
universe@123 220 ssize_t index = 0;
universe@123 221 while (list) {
universe@123 222 if (list == elem) {
universe@123 223 return index;
universe@123 224 }
universe@123 225 list = list->next;
universe@123 226 index++;
universe@123 227 }
universe@123 228 return -1;
universe@123 229 }
universe@123 230
universe@172 231 UcxList *ucx_list_get(const UcxList *l, size_t index) {
universe@8 232 if (l == NULL) return NULL;
universe@8 233
universe@122 234 const UcxList *e = l;
universe@128 235 while (e->next && index > 0) {
universe@8 236 e = e->next;
universe@8 237 index--;
universe@8 238 }
universe@7 239
universe@122 240 return (UcxList*)(index == 0 ? e : NULL);
universe@7 241 }
universe@7 242
universe@123 243 ssize_t ucx_list_find(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) {
universe@123 244 ssize_t index = 0;
universe@123 245 UCX_FOREACH(e, l) {
universe@128 246 if (fnc) {
universe@128 247 if (fnc(elem, e->data, cmpdata) == 0) {
universe@128 248 return index;
universe@128 249 }
universe@128 250 } else {
universe@128 251 if (elem == e->data) {
universe@128 252 return index;
universe@128 253 }
universe@123 254 }
universe@123 255 index++;
universe@123 256 }
universe@123 257 return -1;
universe@123 258 }
universe@123 259
universe@122 260 int ucx_list_contains(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) {
universe@123 261 return ucx_list_find(l, elem, fnc, cmpdata) > -1;
universe@87 262 }
universe@87 263
universe@122 264 size_t ucx_list_size(const UcxList *l) {
universe@7 265 if (l == NULL) return 0;
universe@7 266
universe@122 267 const UcxList *e = l;
universe@7 268 size_t s = 1;
universe@7 269 while (e->next != NULL) {
universe@7 270 e = e->next;
universe@7 271 s++;
universe@7 272 }
universe@7 273
universe@7 274 return s;
universe@7 275 }
universe@7 276
universe@172 277 static UcxList *ucx_list_sort_merge(int length,
universe@122 278 UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re,
universe@37 279 cmp_func fnc, void* data) {
universe@73 280
universe@122 281 UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length);
universe@122 282 UcxList *rc, *lc;
universe@35 283
universe@67 284 lc = ls; rc = le;
universe@37 285 int n = 0;
universe@67 286 while (lc && lc != le && rc != re) {
universe@37 287 if (fnc(lc->data, rc->data, data) <= 0) {
universe@37 288 sorted[n] = lc;
universe@37 289 lc = lc->next;
universe@37 290 } else {
universe@37 291 sorted[n] = rc;
universe@37 292 rc = rc->next;
universe@35 293 }
universe@37 294 n++;
universe@37 295 }
universe@67 296 while (lc && lc != le) {
universe@37 297 sorted[n] = lc;
universe@37 298 lc = lc->next;
universe@37 299 n++;
universe@37 300 }
universe@67 301 while (rc && rc != re) {
universe@37 302 sorted[n] = rc;
universe@37 303 rc = rc->next;
universe@37 304 n++;
universe@35 305 }
universe@35 306
universe@37 307 // Update pointer
universe@37 308 sorted[0]->prev = NULL;
universe@37 309 for (int i = 0 ; i < length-1 ; i++) {
universe@37 310 sorted[i]->next = sorted[i+1];
universe@37 311 sorted[i+1]->prev = sorted[i];
universe@37 312 }
universe@37 313 sorted[length-1]->next = NULL;
universe@35 314
universe@122 315 UcxList *ret = sorted[0];
universe@73 316 free(sorted);
universe@69 317 return ret;
universe@35 318 }
universe@35 319
universe@122 320 UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) {
universe@35 321 if (l == NULL) {
universe@35 322 return NULL;
universe@35 323 }
universe@37 324
universe@122 325 UcxList *lc;
universe@37 326 int ln = 1;
universe@37 327
universe@122 328 UcxList *restrict ls = l, *restrict le, *restrict re;
universe@172 329
universe@172 330 // check how many elements are already sorted
universe@37 331 lc = ls;
universe@37 332 while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
universe@37 333 lc = lc->next;
universe@37 334 ln++;
universe@37 335 }
universe@37 336 le = lc->next;
universe@37 337
universe@67 338 if (le == NULL) {
universe@37 339 return l; // this list is already sorted :)
universe@37 340 } else {
universe@122 341 UcxList *rc;
universe@37 342 int rn = 1;
universe@67 343 rc = le;
universe@172 344 // skip already sorted elements
universe@37 345 while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
universe@37 346 rc = rc->next;
universe@37 347 rn++;
universe@37 348 }
universe@37 349 re = rc->next;
universe@37 350
universe@37 351 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
universe@122 352 UcxList *sorted = ucx_list_sort_merge(ln+rn,
universe@67 353 ls, le, re,
universe@37 354 fnc, data);
universe@172 355
universe@172 356 // Something left? Sort it!
universe@172 357 size_t remainder_length = ucx_list_size(re);
universe@172 358 if (remainder_length > 0) {
universe@172 359 UcxList *remainder = ucx_list_sort(re, fnc, data);
universe@37 360
universe@172 361 // merge sorted list with (also sorted) remainder
universe@172 362 l = ucx_list_sort_merge(ln+rn+remainder_length,
universe@172 363 sorted, remainder, NULL, fnc, data);
universe@172 364 } else {
universe@172 365 // no remainder - we've got our sorted list
universe@172 366 l = sorted;
universe@172 367 }
universe@37 368
universe@35 369 return l;
universe@35 370 }
universe@35 371 }
universe@35 372
universe@122 373 UcxList *ucx_list_first(const UcxList *l) {
universe@125 374 if (!l) {
universe@125 375 return NULL;
universe@125 376 }
universe@7 377
universe@122 378 const UcxList *e = l;
universe@125 379 while (e->prev) {
universe@8 380 e = e->prev;
universe@8 381 }
universe@122 382 return (UcxList *)e;
olaf@13 383 }
universe@22 384
universe@122 385 UcxList *ucx_list_remove(UcxList *l, UcxList *e) {
universe@125 386 return ucx_list_remove_a(ucx_default_allocator(), l, e);
universe@125 387 }
universe@125 388
universe@125 389 UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) {
universe@161 390 if (l == e) {
universe@161 391 l = e->next;
universe@161 392 }
universe@161 393
universe@161 394 if (e->next) {
universe@22 395 e->next->prev = e->prev;
universe@22 396 }
universe@161 397
universe@161 398 if (e->prev) {
universe@161 399 e->prev->next = e->next;
universe@161 400 }
universe@161 401
universe@173 402 alfree(alloc, e);
universe@22 403 return l;
universe@22 404 }

mercurial