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