src/linked_list.c

Tue, 05 Oct 2021 16:33:11 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 05 Oct 2021 16:33:11 +0200
changeset 468
75ae1dccd101
parent 467
95e42a963520
child 469
0458bff0b1cd
permissions
-rw-r--r--

add cx_linked_list_sort()

universe@398 1 /*
universe@398 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@398 3 *
universe@398 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@398 5 *
universe@398 6 * Redistribution and use in source and binary forms, with or without
universe@398 7 * modification, are permitted provided that the following conditions are met:
universe@398 8 *
universe@398 9 * 1. Redistributions of source code must retain the above copyright
universe@398 10 * notice, this list of conditions and the following disclaimer.
universe@398 11 *
universe@398 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@398 13 * notice, this list of conditions and the following disclaimer in the
universe@398 14 * documentation and/or other materials provided with the distribution.
universe@398 15 *
universe@398 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@398 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@398 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@398 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@398 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@398 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@398 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@398 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@398 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@398 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@398 26 * POSSIBILITY OF SUCH DAMAGE.
universe@398 27 */
universe@398 28
universe@398 29 #include "cx/linked_list.h"
universe@401 30 #include <stdint.h>
universe@401 31 #include <string.h>
universe@453 32 #include <assert.h>
universe@398 33
universe@398 34 /* LOW LEVEL LINKED LIST FUNCTIONS */
universe@398 35
universe@468 36 #define CX_LL_PTR(cur, off) (*(void**)(((char*)cur)+off))
universe@398 37
universe@438 38 void *cx_linked_list_at(void *start, size_t start_index, ptrdiff_t loc_advance, size_t index) {
universe@438 39 size_t i = start_index;
universe@446 40 void *cur = start;
universe@438 41 while (i != index && cur != NULL) {
universe@468 42 cur = CX_LL_PTR(cur, loc_advance);
universe@438 43 i < index ? i++ : i--;
universe@438 44 }
universe@438 45 return cur;
universe@438 46 }
universe@438 47
universe@456 48 void *cx_linked_list_last(void *begin, ptrdiff_t loc_next) {
universe@456 49 if (begin == NULL)
universe@456 50 return NULL;
universe@398 51
universe@456 52 void *cur = begin;
universe@456 53 void *last;
universe@456 54 do {
universe@456 55 last = cur;
universe@468 56 } while ((cur = CX_LL_PTR(cur, loc_next)) != NULL);
universe@398 57
universe@456 58 return last;
universe@398 59 }
universe@398 60
universe@453 61 void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
universe@456 62 void *last;
universe@456 63 if (end == NULL) {
universe@456 64 assert(begin != NULL);
universe@456 65 last = cx_linked_list_last(*begin, loc_next);
universe@456 66 } else {
universe@456 67 last = *end;
universe@456 68 }
universe@398 69 if (last == NULL) {
universe@453 70 assert(begin != NULL);
universe@453 71 *begin = new_node;
universe@428 72 } else {
universe@428 73 // if there is a last node, update its next pointer
universe@468 74 CX_LL_PTR(last, loc_next) = new_node;
universe@398 75 }
universe@398 76
universe@428 77 // if there is an end pointer, update it
universe@408 78 if (end != NULL) {
universe@456 79 *end = cx_linked_list_last(new_node, loc_next);
universe@408 80 }
universe@428 81
universe@428 82 // if the nodes use a prev pointer, update it
universe@398 83 if (loc_prev >= 0) {
universe@468 84 CX_LL_PTR(new_node, loc_prev) = last;
universe@398 85 }
universe@398 86 }
universe@398 87
universe@468 88 size_t cx_linked_list_size(void *node, ptrdiff_t loc_next) {
universe@468 89 size_t size = 0;
universe@468 90 while (node != NULL) {
universe@468 91 node = CX_LL_PTR(node, loc_next);
universe@468 92 size++;
universe@468 93 }
universe@468 94 return size;
universe@468 95 }
universe@468 96
universe@468 97 #define ll_prev(node) CX_LL_PTR(node, loc_prev)
universe@468 98 #define ll_next(node) CX_LL_PTR(node, loc_next)
universe@468 99 #define ll_data(node) (follow_ptr?CX_LL_PTR(node, loc_data):(((char*)node)+loc_data))
universe@468 100
universe@468 101 static void *cx_linked_list_sort_merge(ptrdiff_t loc_prev, ptrdiff_t loc_next,
universe@468 102 ptrdiff_t loc_data, int follow_ptr,
universe@468 103 size_t length, void *ls, void *le, void *re,
universe@468 104 CxListComparator cmp_func) {
universe@468 105 const size_t sbo_len = 1024;
universe@468 106 void *sbo[sbo_len];
universe@468 107 void **sorted = (length >= sbo_len) ? malloc(sizeof(void *) * length) : sbo;
universe@468 108 void *rc, *lc;
universe@468 109
universe@468 110 lc = ls;
universe@468 111 rc = le;
universe@468 112 size_t n = 0;
universe@468 113 while (lc && lc != le && rc != re) {
universe@468 114 if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) {
universe@468 115 sorted[n] = lc;
universe@468 116 lc = ll_next(lc);
universe@468 117 } else {
universe@468 118 sorted[n] = rc;
universe@468 119 rc = ll_next(rc);
universe@468 120 }
universe@468 121 n++;
universe@468 122 }
universe@468 123 while (lc && lc != le) {
universe@468 124 sorted[n] = lc;
universe@468 125 lc = ll_next(lc);
universe@468 126 n++;
universe@468 127 }
universe@468 128 while (rc && rc != re) {
universe@468 129 sorted[n] = rc;
universe@468 130 rc = ll_next(rc);
universe@468 131 n++;
universe@468 132 }
universe@468 133
universe@468 134 // Update pointer
universe@468 135 if (loc_prev >= 0) ll_prev(sorted[0]) = NULL;
universe@468 136 for (size_t i = 0; i < length - 1; i++) {
universe@468 137 ll_next(sorted[i]) = sorted[i + 1];
universe@468 138 if (loc_prev >= 0) ll_prev(sorted[i + 1]) = sorted[i];
universe@468 139 }
universe@468 140 ll_next(sorted[length - 1]) = NULL;
universe@468 141
universe@468 142 void *ret = sorted[0];
universe@468 143 if (sorted != sbo) {
universe@468 144 free(sorted);
universe@468 145 }
universe@468 146 return ret;
universe@468 147 }
universe@468 148
universe@468 149 void cx_linked_list_sort(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next,
universe@468 150 ptrdiff_t loc_data, int follow_ptr, CxListComparator cmp_func) {
universe@468 151 assert(begin != NULL);
universe@468 152
universe@468 153 void *lc, *ls, *le, *re;
universe@468 154
universe@468 155 // set start node
universe@468 156 ls = *begin;
universe@468 157
universe@468 158 // check how many elements are already sorted
universe@468 159 lc = ls;
universe@468 160 size_t ln = 1;
universe@468 161 while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
universe@468 162 lc = ll_next(lc);
universe@468 163 ln++;
universe@468 164 }
universe@468 165 le = ll_next(lc);
universe@468 166
universe@468 167 // if first unsorted node is NULL, the list is already completely sorted
universe@468 168 if (le != NULL) {
universe@468 169 void *rc;
universe@468 170 size_t rn = 1;
universe@468 171 rc = le;
universe@468 172 // skip already sorted elements
universe@468 173 while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
universe@468 174 rc = ll_next(rc);
universe@468 175 rn++;
universe@468 176 }
universe@468 177 re = ll_next(rc);
universe@468 178
universe@468 179 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
universe@468 180 void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
universe@468 181 ln + rn, ls, le, re, cmp_func);
universe@468 182
universe@468 183 // Something left? Sort it!
universe@468 184 size_t remainder_length = cx_linked_list_size(re, loc_next);
universe@468 185 if (remainder_length > 0) {
universe@468 186 void *remainder = re;
universe@468 187 cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, follow_ptr, cmp_func);
universe@468 188
universe@468 189 // merge sorted list with (also sorted) remainder
universe@468 190 *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
universe@468 191 ln + rn + remainder_length,
universe@468 192 sorted, remainder, NULL, cmp_func);
universe@468 193 } else {
universe@468 194 // no remainder - we've got our sorted list
universe@468 195 *begin = sorted;
universe@468 196 }
universe@468 197 if (end) *end = cx_linked_list_last(sorted, loc_next);
universe@468 198 }
universe@468 199 }
universe@468 200
universe@468 201 #undef ll_next
universe@468 202 #undef ll_data
universe@468 203
universe@398 204 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
universe@398 205
universe@447 206 typedef struct cx_linked_list_node cx_linked_list_node;
universe@447 207 struct cx_linked_list_node {
universe@447 208 cx_linked_list_node *prev;
universe@447 209 cx_linked_list_node *next;
universe@403 210 char payload[];
universe@447 211 };
universe@402 212
universe@446 213 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
universe@446 214 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
universe@439 215
universe@398 216 typedef struct {
universe@435 217 cx_list_s base;
universe@446 218 cx_linked_list_node *begin;
universe@446 219 cx_linked_list_node *end;
universe@398 220 } cx_linked_list;
universe@398 221
universe@448 222 static cx_linked_list_node *cx_ll_node_at(cx_linked_list *list, size_t index) {
universe@447 223 if (index >= list->base.size) {
universe@447 224 return NULL;
universe@447 225 } else if (index > list->base.size / 2) {
universe@468 226 return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
universe@447 227 } else {
universe@447 228 return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
universe@447 229 }
universe@447 230 }
universe@447 231
universe@438 232 static int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
universe@448 233 // out-of bounds check
universe@448 234 if (index > list->size) return 1;
universe@448 235
universe@448 236 // cast a linked list pointer
universe@437 237 cx_linked_list *ll = (cx_linked_list *) list;
universe@448 238
universe@448 239 // create the new node
universe@448 240 cx_linked_list_node *node = cxMalloc(list->allocator,
universe@448 241 sizeof(cx_linked_list_node) + list->itemsize);
universe@448 242
universe@448 243 // sortir if failed
universe@448 244 if (node == NULL) return 1;
universe@448 245
universe@448 246 // copy payload to the new node
universe@448 247 memcpy(node->payload, elem, list->itemsize);
universe@448 248
universe@448 249 // check if this is the first node
universe@448 250 if (ll->begin == NULL) {
universe@448 251 node->prev = node->next = NULL;
universe@448 252 ll->begin = ll->end = node;
universe@448 253 } else {
universe@448 254 // check if this shall be the new end node
universe@448 255 if (index == list->size) {
universe@448 256 ll->end->next = node;
universe@448 257 node->prev = ll->end;
universe@448 258 node->next = NULL;
universe@448 259 ll->end = node;
universe@448 260 }
universe@468 261 // check if this shall be the new start node
universe@448 262 else if (index == 0) {
universe@448 263 ll->begin->prev = node;
universe@448 264 node->next = ll->begin;
universe@448 265 node->prev = NULL;
universe@448 266 ll->begin = node;
universe@448 267 } else {
universe@448 268 // find the node at the current index
universe@448 269 cx_linked_list_node *cur = cx_ll_node_at(ll, index);
universe@448 270
universe@448 271 // insert before that node
universe@448 272 // (we know all ptr are non-null because we handled all other cases before)
universe@448 273 node->next = cur;
universe@448 274 node->prev = cur->prev;
universe@448 275 cur->prev = node;
universe@448 276 node->prev->next = node;
universe@448 277 }
universe@448 278 }
universe@448 279
universe@448 280 // increase the size and return
universe@448 281 list->size++;
universe@448 282 return 0;
universe@448 283 }
universe@448 284
universe@448 285 static int cx_ll_add(cx_list_s *list, void *elem) {
universe@448 286 return cx_ll_insert(list, list->size, elem);
universe@398 287 }
universe@398 288
universe@466 289 static int cx_pll_insert(cx_list_s *list, size_t index, void *elem) {
universe@466 290 return cx_ll_insert(list, index, &elem);
universe@466 291 }
universe@466 292
universe@466 293 static int cx_pll_add(cx_list_s *list, void *elem) {
universe@466 294 return cx_ll_insert(list, list->size, &elem);
universe@466 295 }
universe@466 296
universe@438 297 static int cx_ll_remove(cx_list_s *list, size_t index) {
universe@447 298 cx_linked_list *ll = (cx_linked_list *) list;
universe@447 299 cx_linked_list_node *node = cx_ll_node_at(ll, index);
universe@447 300
universe@447 301 // out-of-bounds check
universe@447 302 if (node == NULL) return 1;
universe@447 303
universe@447 304 // change left side connection
universe@447 305 if (node->prev == NULL) {
universe@447 306 ll->begin = node->next;
universe@447 307 } else {
universe@447 308 node->prev->next = node->next;
universe@438 309 }
universe@447 310
universe@447 311 // change right side connection
universe@447 312 if (node->next == NULL) {
universe@447 313 ll->end = node->prev;
universe@447 314 } else {
universe@447 315 node->next->prev = node->prev;
universe@447 316 }
universe@447 317
universe@447 318 // adjust size
universe@447 319 list->size--;
universe@447 320
universe@447 321 // free and return
universe@447 322 cxFree(list->allocator, node);
universe@447 323
universe@438 324 return 0;
universe@398 325 }
universe@398 326
universe@439 327 static void *cx_ll_at(cx_list_s *list, size_t index) {
universe@439 328 cx_linked_list *ll = (cx_linked_list *) list;
universe@447 329 cx_linked_list_node *node = cx_ll_node_at(ll, index);
universe@466 330 return node == NULL ? NULL : node->payload;
universe@466 331 }
universe@466 332
universe@466 333 static void *cx_pll_at(cx_list_s *list, size_t index) {
universe@466 334 cx_linked_list *ll = (cx_linked_list *) list;
universe@466 335 cx_linked_list_node *node = cx_ll_node_at(ll, index);
universe@468 336 return node == NULL ? NULL : *(void **) node->payload;
universe@439 337 }
universe@439 338
universe@438 339 static size_t cx_ll_find(cx_list_s *list, void *elem) {
universe@437 340 CxListComparator cmp = list->cmpfunc;
universe@437 341 cx_linked_list *ll = (cx_linked_list *) list;
universe@437 342
universe@437 343 size_t index;
universe@446 344 cx_linked_list_node *node = ll->begin;
universe@446 345 for (index = 0; index < list->size; index++) {
universe@446 346 void *current = node->payload;
universe@437 347 if (cmp(current, elem) == 0) {
universe@437 348 return index;
universe@437 349 }
universe@437 350 node = node->next;
universe@437 351 }
universe@437 352 return index;
universe@398 353 }
universe@398 354
universe@466 355 static size_t cx_pll_find(cx_list_s *list, void *elem) {
universe@466 356 CxListComparator cmp = list->cmpfunc;
universe@466 357 cx_linked_list *ll = (cx_linked_list *) list;
universe@466 358
universe@466 359 size_t index;
universe@466 360 cx_linked_list_node *node = ll->begin;
universe@466 361 for (index = 0; index < list->size; index++) {
universe@468 362 void *current = *(void **) node->payload;
universe@466 363 if (cmp(current, elem) == 0) {
universe@466 364 return index;
universe@466 365 }
universe@466 366 node = node->next;
universe@466 367 }
universe@466 368 return index;
universe@466 369 }
universe@466 370
universe@438 371 static void *cx_ll_last(cx_list_s *list) {
universe@439 372 cx_linked_list *ll = (cx_linked_list *) list;
universe@456 373 cx_linked_list_node *last = ll->end;
universe@466 374 return last == NULL ? NULL : last->payload;
universe@466 375 }
universe@466 376
universe@466 377 static void *cx_pll_last(cx_list_s *list) {
universe@466 378 cx_linked_list *ll = (cx_linked_list *) list;
universe@466 379 cx_linked_list_node *last = ll->end;
universe@468 380 return last == NULL ? NULL : *(void **) last->payload;
universe@404 381 }
universe@398 382
universe@451 383 static cx_list_class cx_linked_list_class = {
universe@398 384 cx_ll_add,
universe@398 385 cx_ll_insert,
universe@398 386 cx_ll_remove,
universe@439 387 cx_ll_at,
universe@404 388 cx_ll_find,
universe@404 389 cx_ll_last
universe@398 390 };
universe@398 391
universe@466 392 static cx_list_class cx_pointer_linked_list_class = {
universe@466 393 cx_pll_add,
universe@466 394 cx_pll_insert,
universe@466 395 cx_ll_remove,
universe@466 396 cx_pll_at,
universe@466 397 cx_pll_find,
universe@466 398 cx_pll_last
universe@466 399 };
universe@466 400
universe@406 401 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
universe@446 402 cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list));
universe@398 403 if (list == NULL)
universe@398 404 return NULL;
universe@398 405
universe@435 406 list->base.cl = &cx_linked_list_class;
universe@435 407 list->base.allocator = allocator;
universe@435 408 list->base.cmpfunc = comparator;
universe@435 409 list->base.itemsize = item_size;
universe@435 410 list->base.capacity = SIZE_MAX;
universe@441 411 list->base.size = 0;
universe@406 412
universe@435 413 list->begin = NULL;
universe@441 414 list->end = NULL;
universe@406 415
universe@441 416 return (CxList) list;
universe@406 417 }
universe@406 418
universe@466 419 CxList cxPointerLinkedListCreate(CxAllocator allocator, CxListComparator comparator) {
universe@466 420 cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list));
universe@466 421 if (list == NULL)
universe@466 422 return NULL;
universe@466 423
universe@466 424 list->base.cl = &cx_pointer_linked_list_class;
universe@466 425 list->base.allocator = allocator;
universe@466 426 list->base.cmpfunc = comparator;
universe@468 427 list->base.itemsize = sizeof(void *);
universe@466 428 list->base.capacity = SIZE_MAX;
universe@466 429 list->base.size = 0;
universe@466 430
universe@466 431 list->begin = NULL;
universe@466 432 list->end = NULL;
universe@466 433
universe@466 434 return (CxList) list;
universe@466 435 }
universe@466 436
universe@409 437 void cxLinkedListDestroy(CxList list) {
universe@446 438 cx_linked_list *ll = (cx_linked_list *) list;
universe@436 439
universe@446 440 cx_linked_list_node *node = ll->begin;
universe@436 441 while (node) {
universe@446 442 void *next = node->next;
universe@436 443 cxFree(list->allocator, node);
universe@436 444 node = next;
universe@436 445 }
universe@436 446
universe@435 447 cxFree(list->allocator, list);
universe@409 448 }

mercurial