Tue, 20 Sep 2022 10:36:04 +0200
use EXPECT_STREQ instead of strcmp
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@509 | 30 | #include "cx/utils.h" |
universe@401 | 31 | #include <stdint.h> |
universe@401 | 32 | #include <string.h> |
universe@453 | 33 | #include <assert.h> |
universe@398 | 34 | |
universe@398 | 35 | /* LOW LEVEL LINKED LIST FUNCTIONS */ |
universe@398 | 36 | |
universe@468 | 37 | #define CX_LL_PTR(cur, off) (*(void**)(((char*)cur)+off)) |
universe@480 | 38 | #define ll_prev(node) CX_LL_PTR(node, loc_prev) |
universe@480 | 39 | #define ll_next(node) CX_LL_PTR(node, loc_next) |
universe@480 | 40 | #define ll_advance(node) CX_LL_PTR(node, loc_advance) |
universe@552 | 41 | #define ll_data_f(node, follow_ptr) ((follow_ptr)?CX_LL_PTR(node, loc_data):(((char*)node)+loc_data)) |
universe@552 | 42 | #define ll_data(node) ll_data_f(node,follow_ptr) |
universe@398 | 43 | |
universe@481 | 44 | void *cx_linked_list_at( |
universe@508 | 45 | void const *start, |
universe@481 | 46 | size_t start_index, |
universe@481 | 47 | ptrdiff_t loc_advance, |
universe@481 | 48 | size_t index |
universe@481 | 49 | ) { |
universe@473 | 50 | assert(start != NULL); |
universe@473 | 51 | assert(loc_advance >= 0); |
universe@438 | 52 | size_t i = start_index; |
universe@508 | 53 | void const *cur = start; |
universe@438 | 54 | while (i != index && cur != NULL) { |
universe@480 | 55 | cur = ll_advance(cur); |
universe@438 | 56 | i < index ? i++ : i--; |
universe@438 | 57 | } |
universe@508 | 58 | return (void *) cur; |
universe@438 | 59 | } |
universe@438 | 60 | |
universe@481 | 61 | size_t cx_linked_list_find( |
universe@489 | 62 | void const *start, |
universe@481 | 63 | ptrdiff_t loc_advance, |
universe@481 | 64 | ptrdiff_t loc_data, |
universe@487 | 65 | bool follow_ptr, |
universe@481 | 66 | CxListComparator cmp_func, |
universe@489 | 67 | void const *elem |
universe@481 | 68 | ) { |
universe@480 | 69 | assert(start != NULL); |
universe@480 | 70 | assert(loc_advance >= 0); |
universe@480 | 71 | assert(loc_data >= 0); |
universe@480 | 72 | assert(cmp_func); |
universe@480 | 73 | |
universe@489 | 74 | void const *node = start; |
universe@480 | 75 | size_t index = 0; |
universe@480 | 76 | do { |
universe@480 | 77 | void *current = ll_data(node); |
universe@480 | 78 | if (cmp_func(current, elem) == 0) { |
universe@480 | 79 | return index; |
universe@480 | 80 | } |
universe@480 | 81 | node = ll_advance(node); |
universe@480 | 82 | index++; |
universe@480 | 83 | } while (node != NULL); |
universe@480 | 84 | return index; |
universe@480 | 85 | } |
universe@480 | 86 | |
universe@481 | 87 | void *cx_linked_list_first( |
universe@508 | 88 | void const *node, |
universe@481 | 89 | ptrdiff_t loc_prev |
universe@481 | 90 | ) { |
universe@475 | 91 | return cx_linked_list_last(node, loc_prev); |
universe@475 | 92 | } |
universe@475 | 93 | |
universe@481 | 94 | void *cx_linked_list_last( |
universe@508 | 95 | void const *node, |
universe@481 | 96 | ptrdiff_t loc_next |
universe@481 | 97 | ) { |
universe@478 | 98 | assert(node != NULL); |
universe@473 | 99 | assert(loc_next >= 0); |
universe@398 | 100 | |
universe@508 | 101 | void const *cur = node; |
universe@508 | 102 | void const *last; |
universe@456 | 103 | do { |
universe@456 | 104 | last = cur; |
universe@480 | 105 | } while ((cur = ll_next(cur)) != NULL); |
universe@398 | 106 | |
universe@508 | 107 | return (void *) last; |
universe@398 | 108 | } |
universe@398 | 109 | |
universe@481 | 110 | void *cx_linked_list_prev( |
universe@508 | 111 | void const *begin, |
universe@481 | 112 | ptrdiff_t loc_next, |
universe@508 | 113 | void const *node |
universe@481 | 114 | ) { |
universe@473 | 115 | assert(begin != NULL); |
universe@478 | 116 | assert(node != NULL); |
universe@473 | 117 | assert(loc_next >= 0); |
universe@473 | 118 | if (begin == node) return NULL; |
universe@508 | 119 | void const *cur = begin; |
universe@508 | 120 | void const *next; |
universe@473 | 121 | while (1) { |
universe@480 | 122 | next = ll_next(cur); |
universe@508 | 123 | if (next == node) return (void *) cur; |
universe@473 | 124 | cur = next; |
universe@473 | 125 | } |
universe@473 | 126 | } |
universe@473 | 127 | |
universe@481 | 128 | void cx_linked_list_link( |
universe@481 | 129 | void *left, |
universe@481 | 130 | void *right, |
universe@481 | 131 | ptrdiff_t loc_prev, |
universe@481 | 132 | ptrdiff_t loc_next |
universe@481 | 133 | ) { |
universe@473 | 134 | assert(loc_next >= 0); |
universe@481 | 135 | ll_next(left) = right; |
universe@481 | 136 | if (loc_prev >= 0) { |
universe@481 | 137 | ll_prev(right) = left; |
universe@481 | 138 | } |
universe@481 | 139 | } |
universe@481 | 140 | |
universe@481 | 141 | void cx_linked_list_unlink( |
universe@481 | 142 | void *left, |
universe@481 | 143 | void *right, |
universe@481 | 144 | ptrdiff_t loc_prev, |
universe@481 | 145 | ptrdiff_t loc_next |
universe@481 | 146 | ) { |
universe@481 | 147 | assert (loc_next >= 0); |
universe@481 | 148 | assert(ll_next(left) == right); |
universe@481 | 149 | ll_next(left) = NULL; |
universe@481 | 150 | if (loc_prev >= 0) { |
universe@481 | 151 | assert(ll_prev(right) == left); |
universe@481 | 152 | ll_prev(right) = NULL; |
universe@481 | 153 | } |
universe@481 | 154 | } |
universe@481 | 155 | |
universe@481 | 156 | void cx_linked_list_add( |
universe@481 | 157 | void **begin, |
universe@481 | 158 | void **end, |
universe@481 | 159 | ptrdiff_t loc_prev, |
universe@481 | 160 | ptrdiff_t loc_next, |
universe@481 | 161 | void *new_node |
universe@481 | 162 | ) { |
universe@456 | 163 | void *last; |
universe@456 | 164 | if (end == NULL) { |
universe@456 | 165 | assert(begin != NULL); |
universe@478 | 166 | last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next); |
universe@456 | 167 | } else { |
universe@456 | 168 | last = *end; |
universe@456 | 169 | } |
universe@481 | 170 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node); |
universe@481 | 171 | } |
universe@481 | 172 | |
universe@481 | 173 | void cx_linked_list_prepend( |
universe@481 | 174 | void **begin, |
universe@481 | 175 | void **end, |
universe@481 | 176 | ptrdiff_t loc_prev, |
universe@481 | 177 | ptrdiff_t loc_next, |
universe@481 | 178 | void *new_node |
universe@481 | 179 | ) { |
universe@481 | 180 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node); |
universe@481 | 181 | } |
universe@481 | 182 | |
universe@481 | 183 | void cx_linked_list_insert( |
universe@481 | 184 | void **begin, |
universe@481 | 185 | void **end, |
universe@481 | 186 | ptrdiff_t loc_prev, |
universe@481 | 187 | ptrdiff_t loc_next, |
universe@481 | 188 | void *node, |
universe@481 | 189 | void *new_node |
universe@481 | 190 | ) { |
universe@481 | 191 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node); |
universe@481 | 192 | } |
universe@481 | 193 | |
universe@481 | 194 | void cx_linked_list_insert_chain( |
universe@481 | 195 | void **begin, |
universe@481 | 196 | void **end, |
universe@481 | 197 | ptrdiff_t loc_prev, |
universe@481 | 198 | ptrdiff_t loc_next, |
universe@481 | 199 | void *node, |
universe@481 | 200 | void *insert_begin, |
universe@481 | 201 | void *insert_end |
universe@481 | 202 | ) { |
universe@481 | 203 | // find the end of the chain, if not specified |
universe@481 | 204 | if (insert_end == NULL) { |
universe@481 | 205 | insert_end = cx_linked_list_last(insert_begin, loc_next); |
universe@481 | 206 | } |
universe@481 | 207 | |
universe@481 | 208 | // determine the successor |
universe@481 | 209 | void *successor; |
universe@481 | 210 | if (node == NULL) { |
universe@481 | 211 | assert(begin != NULL || (end != NULL && loc_prev >= 0)); |
universe@475 | 212 | if (begin != NULL) { |
universe@481 | 213 | successor = *begin; |
universe@481 | 214 | *begin = insert_begin; |
universe@481 | 215 | } else { |
universe@481 | 216 | successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev); |
universe@475 | 217 | } |
universe@428 | 218 | } else { |
universe@481 | 219 | successor = ll_next(node); |
universe@481 | 220 | cx_linked_list_link(node, insert_begin, loc_prev, loc_next); |
universe@398 | 221 | } |
universe@398 | 222 | |
universe@481 | 223 | if (successor == NULL) { |
universe@481 | 224 | // the list ends with the new chain |
universe@481 | 225 | if (end != NULL) { |
universe@481 | 226 | *end = insert_end; |
universe@481 | 227 | } |
universe@481 | 228 | } else { |
universe@481 | 229 | cx_linked_list_link(insert_end, successor, loc_prev, loc_next); |
universe@398 | 230 | } |
universe@398 | 231 | } |
universe@398 | 232 | |
universe@481 | 233 | void cx_linked_list_remove( |
universe@481 | 234 | void **begin, |
universe@481 | 235 | void **end, |
universe@481 | 236 | ptrdiff_t loc_prev, |
universe@481 | 237 | ptrdiff_t loc_next, |
universe@481 | 238 | void *node |
universe@481 | 239 | ) { |
universe@477 | 240 | assert(node != NULL); |
universe@473 | 241 | assert(loc_next >= 0); |
universe@473 | 242 | assert(loc_prev >= 0 || begin != NULL); |
universe@473 | 243 | |
universe@473 | 244 | // find adjacent nodes |
universe@480 | 245 | void *next = ll_next(node); |
universe@473 | 246 | void *prev; |
universe@473 | 247 | if (loc_prev >= 0) { |
universe@480 | 248 | prev = ll_prev(node); |
universe@473 | 249 | } else { |
universe@473 | 250 | prev = cx_linked_list_prev(*begin, loc_next, node); |
universe@473 | 251 | } |
universe@473 | 252 | |
universe@476 | 253 | // update next pointer of prev node, or set begin |
universe@476 | 254 | if (prev == NULL) { |
universe@476 | 255 | if (begin != NULL) { |
universe@476 | 256 | *begin = next; |
universe@476 | 257 | } |
universe@476 | 258 | } else { |
universe@480 | 259 | ll_next(prev) = next; |
universe@473 | 260 | } |
universe@476 | 261 | |
universe@476 | 262 | // update prev pointer of next node, or set end |
universe@476 | 263 | if (next == NULL) { |
universe@476 | 264 | if (end != NULL) { |
universe@476 | 265 | *end = prev; |
universe@476 | 266 | } |
universe@476 | 267 | } else if (loc_prev >= 0) { |
universe@480 | 268 | ll_prev(next) = prev; |
universe@473 | 269 | } |
universe@473 | 270 | } |
universe@473 | 271 | |
universe@481 | 272 | size_t cx_linked_list_size( |
universe@489 | 273 | void const *node, |
universe@481 | 274 | ptrdiff_t loc_next |
universe@481 | 275 | ) { |
universe@473 | 276 | assert(loc_next >= 0); |
universe@468 | 277 | size_t size = 0; |
universe@468 | 278 | while (node != NULL) { |
universe@480 | 279 | node = ll_next(node); |
universe@468 | 280 | size++; |
universe@468 | 281 | } |
universe@468 | 282 | return size; |
universe@468 | 283 | } |
universe@468 | 284 | |
universe@481 | 285 | static void *cx_linked_list_sort_merge( |
universe@481 | 286 | ptrdiff_t loc_prev, |
universe@481 | 287 | ptrdiff_t loc_next, |
universe@481 | 288 | ptrdiff_t loc_data, |
universe@487 | 289 | bool follow_ptr, |
universe@481 | 290 | size_t length, |
universe@481 | 291 | void *ls, |
universe@481 | 292 | void *le, |
universe@481 | 293 | void *re, |
universe@481 | 294 | CxListComparator cmp_func |
universe@481 | 295 | ) { |
universe@468 | 296 | const size_t sbo_len = 1024; |
universe@468 | 297 | void *sbo[sbo_len]; |
universe@468 | 298 | void **sorted = (length >= sbo_len) ? malloc(sizeof(void *) * length) : sbo; |
universe@508 | 299 | if (sorted == NULL) abort(); |
universe@468 | 300 | void *rc, *lc; |
universe@468 | 301 | |
universe@468 | 302 | lc = ls; |
universe@468 | 303 | rc = le; |
universe@468 | 304 | size_t n = 0; |
universe@468 | 305 | while (lc && lc != le && rc != re) { |
universe@468 | 306 | if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) { |
universe@468 | 307 | sorted[n] = lc; |
universe@468 | 308 | lc = ll_next(lc); |
universe@468 | 309 | } else { |
universe@468 | 310 | sorted[n] = rc; |
universe@468 | 311 | rc = ll_next(rc); |
universe@468 | 312 | } |
universe@468 | 313 | n++; |
universe@468 | 314 | } |
universe@468 | 315 | while (lc && lc != le) { |
universe@468 | 316 | sorted[n] = lc; |
universe@468 | 317 | lc = ll_next(lc); |
universe@468 | 318 | n++; |
universe@468 | 319 | } |
universe@468 | 320 | while (rc && rc != re) { |
universe@468 | 321 | sorted[n] = rc; |
universe@468 | 322 | rc = ll_next(rc); |
universe@468 | 323 | n++; |
universe@468 | 324 | } |
universe@468 | 325 | |
universe@468 | 326 | // Update pointer |
universe@468 | 327 | if (loc_prev >= 0) ll_prev(sorted[0]) = NULL; |
universe@509 | 328 | cx_for_n (i, length - 1) { |
universe@481 | 329 | cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next); |
universe@468 | 330 | } |
universe@468 | 331 | ll_next(sorted[length - 1]) = NULL; |
universe@468 | 332 | |
universe@468 | 333 | void *ret = sorted[0]; |
universe@468 | 334 | if (sorted != sbo) { |
universe@468 | 335 | free(sorted); |
universe@468 | 336 | } |
universe@468 | 337 | return ret; |
universe@468 | 338 | } |
universe@468 | 339 | |
universe@476 | 340 | void cx_linked_list_sort( /* NOLINT(misc-no-recursion) - purposely recursive function */ |
universe@481 | 341 | void **begin, |
universe@481 | 342 | void **end, |
universe@481 | 343 | ptrdiff_t loc_prev, |
universe@481 | 344 | ptrdiff_t loc_next, |
universe@481 | 345 | ptrdiff_t loc_data, |
universe@487 | 346 | bool follow_ptr, |
universe@481 | 347 | CxListComparator cmp_func |
universe@481 | 348 | ) { |
universe@468 | 349 | assert(begin != NULL); |
universe@473 | 350 | assert(loc_next >= 0); |
universe@473 | 351 | assert(loc_data >= 0); |
universe@473 | 352 | assert(cmp_func); |
universe@468 | 353 | |
universe@468 | 354 | void *lc, *ls, *le, *re; |
universe@468 | 355 | |
universe@468 | 356 | // set start node |
universe@468 | 357 | ls = *begin; |
universe@468 | 358 | |
universe@468 | 359 | // check how many elements are already sorted |
universe@468 | 360 | lc = ls; |
universe@468 | 361 | size_t ln = 1; |
universe@468 | 362 | while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) { |
universe@468 | 363 | lc = ll_next(lc); |
universe@468 | 364 | ln++; |
universe@468 | 365 | } |
universe@468 | 366 | le = ll_next(lc); |
universe@468 | 367 | |
universe@468 | 368 | // if first unsorted node is NULL, the list is already completely sorted |
universe@468 | 369 | if (le != NULL) { |
universe@468 | 370 | void *rc; |
universe@468 | 371 | size_t rn = 1; |
universe@468 | 372 | rc = le; |
universe@468 | 373 | // skip already sorted elements |
universe@468 | 374 | while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) { |
universe@468 | 375 | rc = ll_next(rc); |
universe@468 | 376 | rn++; |
universe@468 | 377 | } |
universe@468 | 378 | re = ll_next(rc); |
universe@468 | 379 | |
universe@468 | 380 | // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them |
universe@468 | 381 | void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr, |
universe@468 | 382 | ln + rn, ls, le, re, cmp_func); |
universe@468 | 383 | |
universe@468 | 384 | // Something left? Sort it! |
universe@468 | 385 | size_t remainder_length = cx_linked_list_size(re, loc_next); |
universe@468 | 386 | if (remainder_length > 0) { |
universe@468 | 387 | void *remainder = re; |
universe@468 | 388 | cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, follow_ptr, cmp_func); |
universe@468 | 389 | |
universe@468 | 390 | // merge sorted list with (also sorted) remainder |
universe@468 | 391 | *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr, |
universe@468 | 392 | ln + rn + remainder_length, |
universe@468 | 393 | sorted, remainder, NULL, cmp_func); |
universe@468 | 394 | } else { |
universe@468 | 395 | // no remainder - we've got our sorted list |
universe@468 | 396 | *begin = sorted; |
universe@468 | 397 | } |
universe@468 | 398 | if (end) *end = cx_linked_list_last(sorted, loc_next); |
universe@468 | 399 | } |
universe@468 | 400 | } |
universe@468 | 401 | |
universe@486 | 402 | int cx_linked_list_compare( |
universe@489 | 403 | void const *begin_left, |
universe@489 | 404 | void const *begin_right, |
universe@486 | 405 | ptrdiff_t loc_advance, |
universe@486 | 406 | ptrdiff_t loc_data, |
universe@552 | 407 | bool follow_ptr_left, |
universe@552 | 408 | bool follow_ptr_right, |
universe@486 | 409 | CxListComparator cmp_func |
universe@486 | 410 | ) { |
universe@489 | 411 | void const *left = begin_left, *right = begin_right; |
universe@486 | 412 | |
universe@486 | 413 | while (left != NULL && right != NULL) { |
universe@552 | 414 | void const *left_data = ll_data_f(left, follow_ptr_left); |
universe@552 | 415 | void const *right_data = ll_data_f(right, follow_ptr_right); |
universe@552 | 416 | int result = cmp_func(left_data, right_data); |
universe@486 | 417 | if (result != 0) return result; |
universe@486 | 418 | left = ll_advance(left); |
universe@486 | 419 | right = ll_advance(right); |
universe@486 | 420 | } |
universe@486 | 421 | |
universe@486 | 422 | if (left != NULL) { return 1; } |
universe@486 | 423 | else if (right != NULL) { return -1; } |
universe@486 | 424 | else { return 0; } |
universe@486 | 425 | } |
universe@486 | 426 | |
universe@481 | 427 | void cx_linked_list_reverse( |
universe@481 | 428 | void **begin, |
universe@481 | 429 | void **end, |
universe@481 | 430 | ptrdiff_t loc_prev, |
universe@481 | 431 | ptrdiff_t loc_next |
universe@481 | 432 | ) { |
universe@473 | 433 | assert(begin != NULL); |
universe@473 | 434 | assert(loc_next >= 0); |
universe@473 | 435 | |
universe@473 | 436 | // swap all links |
universe@473 | 437 | void *prev = NULL; |
universe@473 | 438 | void *cur = *begin; |
universe@473 | 439 | while (cur != NULL) { |
universe@480 | 440 | void *next = ll_next(cur); |
universe@473 | 441 | |
universe@480 | 442 | ll_next(cur) = prev; |
universe@473 | 443 | if (loc_prev >= 0) { |
universe@480 | 444 | ll_prev(cur) = next; |
universe@473 | 445 | } |
universe@473 | 446 | |
universe@473 | 447 | prev = cur; |
universe@473 | 448 | cur = next; |
universe@473 | 449 | } |
universe@473 | 450 | |
universe@473 | 451 | // update begin and end |
universe@473 | 452 | if (end != NULL) { |
universe@473 | 453 | *end = *begin; |
universe@473 | 454 | } |
universe@473 | 455 | *begin = prev; |
universe@473 | 456 | } |
universe@473 | 457 | |
universe@398 | 458 | /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ |
universe@398 | 459 | |
universe@447 | 460 | typedef struct cx_linked_list_node cx_linked_list_node; |
universe@447 | 461 | struct cx_linked_list_node { |
universe@447 | 462 | cx_linked_list_node *prev; |
universe@447 | 463 | cx_linked_list_node *next; |
universe@403 | 464 | char payload[]; |
universe@447 | 465 | }; |
universe@402 | 466 | |
universe@446 | 467 | #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev) |
universe@446 | 468 | #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next) |
universe@469 | 469 | #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload) |
universe@439 | 470 | |
universe@398 | 471 | typedef struct { |
universe@500 | 472 | struct cx_list_s base; |
universe@446 | 473 | cx_linked_list_node *begin; |
universe@446 | 474 | cx_linked_list_node *end; |
universe@552 | 475 | bool follow_ptr; |
universe@398 | 476 | } cx_linked_list; |
universe@398 | 477 | |
universe@481 | 478 | static cx_linked_list_node *cx_ll_node_at( |
universe@489 | 479 | cx_linked_list const *list, |
universe@481 | 480 | size_t index |
universe@481 | 481 | ) { |
universe@447 | 482 | if (index >= list->base.size) { |
universe@447 | 483 | return NULL; |
universe@447 | 484 | } else if (index > list->base.size / 2) { |
universe@468 | 485 | return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index); |
universe@447 | 486 | } else { |
universe@447 | 487 | return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index); |
universe@447 | 488 | } |
universe@447 | 489 | } |
universe@447 | 490 | |
universe@499 | 491 | static int cx_ll_insert_at( |
universe@500 | 492 | struct cx_list_s *list, |
universe@499 | 493 | cx_linked_list_node *node, |
universe@489 | 494 | void const *elem |
universe@481 | 495 | ) { |
universe@448 | 496 | |
universe@481 | 497 | // create the new new_node |
universe@481 | 498 | cx_linked_list_node *new_node = cxMalloc(list->allocator, |
universe@481 | 499 | sizeof(cx_linked_list_node) + list->itemsize); |
universe@448 | 500 | |
universe@448 | 501 | // sortir if failed |
universe@481 | 502 | if (new_node == NULL) return 1; |
universe@448 | 503 | |
universe@481 | 504 | // initialize new new_node |
universe@481 | 505 | new_node->prev = new_node->next = NULL; |
universe@481 | 506 | memcpy(new_node->payload, elem, list->itemsize); |
universe@448 | 507 | |
universe@481 | 508 | // insert |
universe@499 | 509 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@481 | 510 | cx_linked_list_insert_chain( |
universe@481 | 511 | (void **) &ll->begin, (void **) &ll->end, |
universe@481 | 512 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, |
universe@481 | 513 | node, new_node, new_node |
universe@481 | 514 | ); |
universe@448 | 515 | |
universe@448 | 516 | // increase the size and return |
universe@448 | 517 | list->size++; |
universe@448 | 518 | return 0; |
universe@448 | 519 | } |
universe@448 | 520 | |
universe@499 | 521 | static int cx_ll_insert( |
universe@500 | 522 | struct cx_list_s *list, |
universe@499 | 523 | size_t index, |
universe@499 | 524 | void const *elem |
universe@499 | 525 | ) { |
universe@499 | 526 | // out-of bounds check |
universe@499 | 527 | if (index > list->size) return 1; |
universe@499 | 528 | |
universe@499 | 529 | // find position efficiently |
universe@499 | 530 | cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1); |
universe@499 | 531 | |
universe@499 | 532 | // perform insert |
universe@499 | 533 | return cx_ll_insert_at(list, node, elem); |
universe@499 | 534 | } |
universe@499 | 535 | |
universe@481 | 536 | static int cx_ll_add( |
universe@500 | 537 | struct cx_list_s *list, |
universe@489 | 538 | void const *elem |
universe@481 | 539 | ) { |
universe@448 | 540 | return cx_ll_insert(list, list->size, elem); |
universe@398 | 541 | } |
universe@398 | 542 | |
universe@481 | 543 | static int cx_pll_insert( |
universe@500 | 544 | struct cx_list_s *list, |
universe@481 | 545 | size_t index, |
universe@489 | 546 | void const *elem |
universe@481 | 547 | ) { |
universe@466 | 548 | return cx_ll_insert(list, index, &elem); |
universe@466 | 549 | } |
universe@466 | 550 | |
universe@481 | 551 | static int cx_pll_add( |
universe@500 | 552 | struct cx_list_s *list, |
universe@489 | 553 | void const *elem |
universe@481 | 554 | ) { |
universe@466 | 555 | return cx_ll_insert(list, list->size, &elem); |
universe@466 | 556 | } |
universe@466 | 557 | |
universe@481 | 558 | static int cx_ll_remove( |
universe@500 | 559 | struct cx_list_s *list, |
universe@481 | 560 | size_t index |
universe@481 | 561 | ) { |
universe@447 | 562 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@447 | 563 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@447 | 564 | |
universe@447 | 565 | // out-of-bounds check |
universe@447 | 566 | if (node == NULL) return 1; |
universe@447 | 567 | |
universe@476 | 568 | // remove |
universe@476 | 569 | cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end, |
universe@476 | 570 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node); |
universe@447 | 571 | |
universe@447 | 572 | // adjust size |
universe@447 | 573 | list->size--; |
universe@447 | 574 | |
universe@447 | 575 | // free and return |
universe@447 | 576 | cxFree(list->allocator, node); |
universe@447 | 577 | |
universe@438 | 578 | return 0; |
universe@398 | 579 | } |
universe@398 | 580 | |
universe@481 | 581 | static void *cx_ll_at( |
universe@500 | 582 | struct cx_list_s const *list, |
universe@481 | 583 | size_t index |
universe@481 | 584 | ) { |
universe@439 | 585 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@447 | 586 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@466 | 587 | return node == NULL ? NULL : node->payload; |
universe@466 | 588 | } |
universe@466 | 589 | |
universe@481 | 590 | static void *cx_pll_at( |
universe@500 | 591 | struct cx_list_s const *list, |
universe@481 | 592 | size_t index |
universe@481 | 593 | ) { |
universe@466 | 594 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@466 | 595 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@468 | 596 | return node == NULL ? NULL : *(void **) node->payload; |
universe@439 | 597 | } |
universe@439 | 598 | |
universe@481 | 599 | static size_t cx_ll_find( |
universe@500 | 600 | struct cx_list_s const *list, |
universe@489 | 601 | void const *elem |
universe@481 | 602 | ) { |
universe@552 | 603 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@480 | 604 | return cx_linked_list_find(((cx_linked_list *) list)->begin, |
universe@480 | 605 | CX_LL_LOC_NEXT, CX_LL_LOC_DATA, |
universe@552 | 606 | ll->follow_ptr, list->cmpfunc, elem); |
universe@466 | 607 | } |
universe@466 | 608 | |
universe@500 | 609 | static void cx_ll_sort(struct cx_list_s *list) { |
universe@469 | 610 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@469 | 611 | cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end, |
universe@469 | 612 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA, |
universe@552 | 613 | ll->follow_ptr, list->cmpfunc); |
universe@469 | 614 | } |
universe@469 | 615 | |
universe@500 | 616 | static void cx_ll_reverse(struct cx_list_s *list) { |
universe@490 | 617 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@521 | 618 | cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT); |
universe@490 | 619 | } |
universe@490 | 620 | |
universe@488 | 621 | static int cx_ll_compare( |
universe@500 | 622 | struct cx_list_s const *list, |
universe@500 | 623 | struct cx_list_s const *other |
universe@488 | 624 | ) { |
universe@488 | 625 | cx_linked_list *left = (cx_linked_list *) list; |
universe@488 | 626 | cx_linked_list *right = (cx_linked_list *) other; |
universe@488 | 627 | return cx_linked_list_compare(left->begin, right->begin, |
universe@488 | 628 | CX_LL_LOC_NEXT, CX_LL_LOC_DATA, |
universe@552 | 629 | left->follow_ptr, right->follow_ptr, list->cmpfunc); |
universe@488 | 630 | } |
universe@488 | 631 | |
universe@494 | 632 | static bool cx_ll_iter_valid(CxIterator const *iter) { |
universe@495 | 633 | return iter->elem_handle != NULL; |
universe@494 | 634 | } |
universe@494 | 635 | |
universe@494 | 636 | static void cx_ll_iter_next(CxIterator *iter) { |
universe@495 | 637 | if (iter->remove) { |
universe@495 | 638 | iter->remove = false; |
universe@495 | 639 | cx_linked_list *ll = iter->src_handle; |
universe@495 | 640 | cx_linked_list_node *node = iter->elem_handle; |
universe@495 | 641 | iter->elem_handle = node->next; |
universe@495 | 642 | cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end, |
universe@495 | 643 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node); |
universe@495 | 644 | ll->base.size--; |
universe@495 | 645 | cxFree(ll->base.allocator, node); |
universe@495 | 646 | } else { |
universe@495 | 647 | iter->index++; |
universe@495 | 648 | cx_linked_list_node *node = iter->elem_handle; |
universe@495 | 649 | iter->elem_handle = node->next; |
universe@495 | 650 | } |
universe@494 | 651 | } |
universe@494 | 652 | |
universe@494 | 653 | static void *cx_ll_iter_current(CxIterator const *iter) { |
universe@495 | 654 | cx_linked_list_node *node = iter->elem_handle; |
universe@494 | 655 | return node->payload; |
universe@494 | 656 | } |
universe@494 | 657 | |
universe@494 | 658 | static void *cx_pll_iter_current(CxIterator const *iter) { |
universe@495 | 659 | cx_linked_list_node *node = iter->elem_handle; |
universe@494 | 660 | return *(void **) node->payload; |
universe@494 | 661 | } |
universe@494 | 662 | |
universe@494 | 663 | static CxIterator cx_ll_iterator( |
universe@500 | 664 | struct cx_list_s *list, |
universe@494 | 665 | size_t index |
universe@494 | 666 | ) { |
universe@494 | 667 | CxIterator iter; |
universe@494 | 668 | iter.index = index; |
universe@495 | 669 | iter.src_handle = list; |
universe@495 | 670 | iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index); |
universe@494 | 671 | iter.valid = cx_ll_iter_valid; |
universe@494 | 672 | iter.current = cx_ll_iter_current; |
universe@494 | 673 | iter.next = cx_ll_iter_next; |
universe@495 | 674 | iter.remove = false; |
universe@494 | 675 | return iter; |
universe@494 | 676 | } |
universe@494 | 677 | |
universe@494 | 678 | static CxIterator cx_pll_iterator( |
universe@500 | 679 | struct cx_list_s *list, |
universe@494 | 680 | size_t index |
universe@494 | 681 | ) { |
universe@494 | 682 | CxIterator iter = cx_ll_iterator(list, index); |
universe@494 | 683 | iter.current = cx_pll_iter_current; |
universe@494 | 684 | return iter; |
universe@494 | 685 | } |
universe@494 | 686 | |
universe@499 | 687 | static int cx_ll_insert_iter( |
universe@499 | 688 | CxIterator *iter, |
universe@499 | 689 | void const *elem, |
universe@499 | 690 | int prepend |
universe@499 | 691 | ) { |
universe@500 | 692 | struct cx_list_s *list = iter->src_handle; |
universe@499 | 693 | cx_linked_list_node *node = iter->elem_handle; |
universe@499 | 694 | if (node != NULL) { |
universe@499 | 695 | assert(prepend >= 0 && prepend <= 1); |
universe@499 | 696 | cx_linked_list_node *choice[2] = {node, node->prev}; |
universe@499 | 697 | int result = cx_ll_insert_at(list, choice[prepend], elem); |
universe@499 | 698 | iter->index += prepend * (0 == result); |
universe@499 | 699 | return result; |
universe@499 | 700 | } else { |
universe@499 | 701 | int result = cx_ll_insert(list, list->size, elem); |
universe@499 | 702 | iter->index = list->size; |
universe@499 | 703 | return result; |
universe@499 | 704 | } |
universe@499 | 705 | } |
universe@499 | 706 | |
universe@499 | 707 | static int cx_pll_insert_iter( |
universe@499 | 708 | CxIterator *iter, |
universe@499 | 709 | void const *elem, |
universe@499 | 710 | int prepend |
universe@499 | 711 | ) { |
universe@499 | 712 | return cx_ll_insert_iter(iter, &elem, prepend); |
universe@499 | 713 | } |
universe@499 | 714 | |
universe@524 | 715 | static void cx_ll_destructor(CxList *list) { |
universe@524 | 716 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@524 | 717 | |
universe@524 | 718 | cx_linked_list_node *node = ll->begin; |
universe@524 | 719 | while (node) { |
universe@524 | 720 | void *next = node->next; |
universe@524 | 721 | cxFree(list->allocator, node); |
universe@524 | 722 | node = next; |
universe@524 | 723 | } |
universe@524 | 724 | // do not free the list pointer, this is just a destructor! |
universe@524 | 725 | } |
universe@524 | 726 | |
universe@451 | 727 | static cx_list_class cx_linked_list_class = { |
universe@524 | 728 | cx_ll_destructor, |
universe@398 | 729 | cx_ll_add, |
universe@398 | 730 | cx_ll_insert, |
universe@499 | 731 | cx_ll_insert_iter, |
universe@398 | 732 | cx_ll_remove, |
universe@439 | 733 | cx_ll_at, |
universe@404 | 734 | cx_ll_find, |
universe@488 | 735 | cx_ll_sort, |
universe@490 | 736 | cx_ll_compare, |
universe@494 | 737 | cx_ll_reverse, |
universe@499 | 738 | cx_ll_iterator |
universe@398 | 739 | }; |
universe@398 | 740 | |
universe@466 | 741 | static cx_list_class cx_pointer_linked_list_class = { |
universe@524 | 742 | cx_ll_destructor, |
universe@466 | 743 | cx_pll_add, |
universe@466 | 744 | cx_pll_insert, |
universe@499 | 745 | cx_pll_insert_iter, |
universe@466 | 746 | cx_ll_remove, |
universe@466 | 747 | cx_pll_at, |
universe@552 | 748 | cx_ll_find, |
universe@552 | 749 | cx_ll_sort, |
universe@552 | 750 | cx_ll_compare, |
universe@494 | 751 | cx_ll_reverse, |
universe@494 | 752 | cx_pll_iterator, |
universe@466 | 753 | }; |
universe@466 | 754 | |
universe@500 | 755 | CxList *cxLinkedListCreate( |
universe@508 | 756 | CxAllocator const *allocator, |
universe@481 | 757 | CxListComparator comparator, |
universe@481 | 758 | size_t item_size |
universe@481 | 759 | ) { |
universe@503 | 760 | cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list)); |
universe@528 | 761 | if (list == NULL) return NULL; |
universe@398 | 762 | |
universe@552 | 763 | list->follow_ptr = false; |
universe@435 | 764 | list->base.cl = &cx_linked_list_class; |
universe@435 | 765 | list->base.allocator = allocator; |
universe@435 | 766 | list->base.cmpfunc = comparator; |
universe@435 | 767 | list->base.itemsize = item_size; |
universe@435 | 768 | list->base.capacity = SIZE_MAX; |
universe@406 | 769 | |
universe@500 | 770 | return (CxList *) list; |
universe@406 | 771 | } |
universe@406 | 772 | |
universe@500 | 773 | CxList *cxPointerLinkedListCreate( |
universe@508 | 774 | CxAllocator const *allocator, |
universe@481 | 775 | CxListComparator comparator |
universe@481 | 776 | ) { |
universe@503 | 777 | cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list)); |
universe@528 | 778 | if (list == NULL) return NULL; |
universe@466 | 779 | |
universe@552 | 780 | list->follow_ptr = true; |
universe@466 | 781 | list->base.cl = &cx_pointer_linked_list_class; |
universe@466 | 782 | list->base.allocator = allocator; |
universe@466 | 783 | list->base.cmpfunc = comparator; |
universe@468 | 784 | list->base.itemsize = sizeof(void *); |
universe@466 | 785 | list->base.capacity = SIZE_MAX; |
universe@466 | 786 | |
universe@500 | 787 | return (CxList *) list; |
universe@466 | 788 | } |
universe@466 | 789 | |
universe@500 | 790 | CxList *cxLinkedListFromArray( |
universe@508 | 791 | CxAllocator const *allocator, |
universe@488 | 792 | CxListComparator comparator, |
universe@488 | 793 | size_t item_size, |
universe@488 | 794 | size_t num_items, |
universe@489 | 795 | void const *array |
universe@488 | 796 | ) { |
universe@500 | 797 | CxList *list = cxLinkedListCreate(allocator, comparator, item_size); |
universe@488 | 798 | if (list == NULL) return NULL; |
universe@509 | 799 | cx_for_n (i, num_items) { |
universe@488 | 800 | if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) { |
universe@524 | 801 | cx_ll_destructor(list); |
universe@524 | 802 | cxFree(allocator, list); |
universe@524 | 803 | return NULL; |
universe@488 | 804 | } |
universe@488 | 805 | } |
universe@488 | 806 | return list; |
universe@488 | 807 | } |