Mon, 20 Mar 2023 18:05:12 +0100
fix hashmap iterator skipping the second element in some cases
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 <string.h> |
universe@453 | 32 | #include <assert.h> |
universe@398 | 33 | |
universe@628 | 34 | // LOW LEVEL LINKED LIST FUNCTIONS |
universe@398 | 35 | |
universe@592 | 36 | #define CX_LL_PTR(cur, off) (*(void**)(((char*)(cur))+(off))) |
universe@480 | 37 | #define ll_prev(node) CX_LL_PTR(node, loc_prev) |
universe@480 | 38 | #define ll_next(node) CX_LL_PTR(node, loc_next) |
universe@480 | 39 | #define ll_advance(node) CX_LL_PTR(node, loc_advance) |
universe@639 | 40 | #define ll_data(node) (((char*)(node))+loc_data) |
universe@398 | 41 | |
universe@481 | 42 | void *cx_linked_list_at( |
universe@508 | 43 | void const *start, |
universe@481 | 44 | size_t start_index, |
universe@481 | 45 | ptrdiff_t loc_advance, |
universe@481 | 46 | size_t index |
universe@481 | 47 | ) { |
universe@473 | 48 | assert(start != NULL); |
universe@473 | 49 | assert(loc_advance >= 0); |
universe@438 | 50 | size_t i = start_index; |
universe@508 | 51 | void const *cur = start; |
universe@438 | 52 | while (i != index && cur != NULL) { |
universe@480 | 53 | cur = ll_advance(cur); |
universe@438 | 54 | i < index ? i++ : i--; |
universe@438 | 55 | } |
universe@508 | 56 | return (void *) cur; |
universe@438 | 57 | } |
universe@438 | 58 | |
universe@481 | 59 | size_t cx_linked_list_find( |
universe@489 | 60 | void const *start, |
universe@481 | 61 | ptrdiff_t loc_advance, |
universe@481 | 62 | ptrdiff_t loc_data, |
universe@481 | 63 | CxListComparator cmp_func, |
universe@489 | 64 | void const *elem |
universe@481 | 65 | ) { |
universe@480 | 66 | assert(start != NULL); |
universe@480 | 67 | assert(loc_advance >= 0); |
universe@480 | 68 | assert(loc_data >= 0); |
universe@480 | 69 | assert(cmp_func); |
universe@480 | 70 | |
universe@489 | 71 | void const *node = start; |
universe@480 | 72 | size_t index = 0; |
universe@480 | 73 | do { |
universe@480 | 74 | void *current = ll_data(node); |
universe@480 | 75 | if (cmp_func(current, elem) == 0) { |
universe@480 | 76 | return index; |
universe@480 | 77 | } |
universe@480 | 78 | node = ll_advance(node); |
universe@480 | 79 | index++; |
universe@480 | 80 | } while (node != NULL); |
universe@480 | 81 | return index; |
universe@480 | 82 | } |
universe@480 | 83 | |
universe@481 | 84 | void *cx_linked_list_first( |
universe@508 | 85 | void const *node, |
universe@481 | 86 | ptrdiff_t loc_prev |
universe@481 | 87 | ) { |
universe@475 | 88 | return cx_linked_list_last(node, loc_prev); |
universe@475 | 89 | } |
universe@475 | 90 | |
universe@481 | 91 | void *cx_linked_list_last( |
universe@508 | 92 | void const *node, |
universe@481 | 93 | ptrdiff_t loc_next |
universe@481 | 94 | ) { |
universe@478 | 95 | assert(node != NULL); |
universe@473 | 96 | assert(loc_next >= 0); |
universe@398 | 97 | |
universe@508 | 98 | void const *cur = node; |
universe@508 | 99 | void const *last; |
universe@456 | 100 | do { |
universe@456 | 101 | last = cur; |
universe@480 | 102 | } while ((cur = ll_next(cur)) != NULL); |
universe@398 | 103 | |
universe@508 | 104 | return (void *) last; |
universe@398 | 105 | } |
universe@398 | 106 | |
universe@481 | 107 | void *cx_linked_list_prev( |
universe@508 | 108 | void const *begin, |
universe@481 | 109 | ptrdiff_t loc_next, |
universe@508 | 110 | void const *node |
universe@481 | 111 | ) { |
universe@473 | 112 | assert(begin != NULL); |
universe@478 | 113 | assert(node != NULL); |
universe@473 | 114 | assert(loc_next >= 0); |
universe@473 | 115 | if (begin == node) return NULL; |
universe@508 | 116 | void const *cur = begin; |
universe@508 | 117 | void const *next; |
universe@473 | 118 | while (1) { |
universe@480 | 119 | next = ll_next(cur); |
universe@508 | 120 | if (next == node) return (void *) cur; |
universe@473 | 121 | cur = next; |
universe@473 | 122 | } |
universe@473 | 123 | } |
universe@473 | 124 | |
universe@481 | 125 | void cx_linked_list_link( |
universe@481 | 126 | void *left, |
universe@481 | 127 | void *right, |
universe@481 | 128 | ptrdiff_t loc_prev, |
universe@481 | 129 | ptrdiff_t loc_next |
universe@481 | 130 | ) { |
universe@473 | 131 | assert(loc_next >= 0); |
universe@481 | 132 | ll_next(left) = right; |
universe@481 | 133 | if (loc_prev >= 0) { |
universe@481 | 134 | ll_prev(right) = left; |
universe@481 | 135 | } |
universe@481 | 136 | } |
universe@481 | 137 | |
universe@481 | 138 | void cx_linked_list_unlink( |
universe@481 | 139 | void *left, |
universe@481 | 140 | void *right, |
universe@481 | 141 | ptrdiff_t loc_prev, |
universe@481 | 142 | ptrdiff_t loc_next |
universe@481 | 143 | ) { |
universe@481 | 144 | assert (loc_next >= 0); |
universe@481 | 145 | assert(ll_next(left) == right); |
universe@481 | 146 | ll_next(left) = NULL; |
universe@481 | 147 | if (loc_prev >= 0) { |
universe@481 | 148 | assert(ll_prev(right) == left); |
universe@481 | 149 | ll_prev(right) = NULL; |
universe@481 | 150 | } |
universe@481 | 151 | } |
universe@481 | 152 | |
universe@481 | 153 | void cx_linked_list_add( |
universe@481 | 154 | void **begin, |
universe@481 | 155 | void **end, |
universe@481 | 156 | ptrdiff_t loc_prev, |
universe@481 | 157 | ptrdiff_t loc_next, |
universe@481 | 158 | void *new_node |
universe@481 | 159 | ) { |
universe@456 | 160 | void *last; |
universe@456 | 161 | if (end == NULL) { |
universe@456 | 162 | assert(begin != NULL); |
universe@478 | 163 | last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next); |
universe@456 | 164 | } else { |
universe@456 | 165 | last = *end; |
universe@456 | 166 | } |
universe@481 | 167 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node); |
universe@481 | 168 | } |
universe@481 | 169 | |
universe@481 | 170 | void cx_linked_list_prepend( |
universe@481 | 171 | void **begin, |
universe@481 | 172 | void **end, |
universe@481 | 173 | ptrdiff_t loc_prev, |
universe@481 | 174 | ptrdiff_t loc_next, |
universe@481 | 175 | void *new_node |
universe@481 | 176 | ) { |
universe@481 | 177 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node); |
universe@481 | 178 | } |
universe@481 | 179 | |
universe@481 | 180 | void cx_linked_list_insert( |
universe@481 | 181 | void **begin, |
universe@481 | 182 | void **end, |
universe@481 | 183 | ptrdiff_t loc_prev, |
universe@481 | 184 | ptrdiff_t loc_next, |
universe@481 | 185 | void *node, |
universe@481 | 186 | void *new_node |
universe@481 | 187 | ) { |
universe@481 | 188 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node); |
universe@481 | 189 | } |
universe@481 | 190 | |
universe@481 | 191 | void cx_linked_list_insert_chain( |
universe@481 | 192 | void **begin, |
universe@481 | 193 | void **end, |
universe@481 | 194 | ptrdiff_t loc_prev, |
universe@481 | 195 | ptrdiff_t loc_next, |
universe@481 | 196 | void *node, |
universe@481 | 197 | void *insert_begin, |
universe@481 | 198 | void *insert_end |
universe@481 | 199 | ) { |
universe@481 | 200 | // find the end of the chain, if not specified |
universe@481 | 201 | if (insert_end == NULL) { |
universe@481 | 202 | insert_end = cx_linked_list_last(insert_begin, loc_next); |
universe@481 | 203 | } |
universe@481 | 204 | |
universe@481 | 205 | // determine the successor |
universe@481 | 206 | void *successor; |
universe@481 | 207 | if (node == NULL) { |
universe@481 | 208 | assert(begin != NULL || (end != NULL && loc_prev >= 0)); |
universe@475 | 209 | if (begin != NULL) { |
universe@481 | 210 | successor = *begin; |
universe@481 | 211 | *begin = insert_begin; |
universe@481 | 212 | } else { |
universe@481 | 213 | successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev); |
universe@475 | 214 | } |
universe@428 | 215 | } else { |
universe@481 | 216 | successor = ll_next(node); |
universe@481 | 217 | cx_linked_list_link(node, insert_begin, loc_prev, loc_next); |
universe@398 | 218 | } |
universe@398 | 219 | |
universe@481 | 220 | if (successor == NULL) { |
universe@481 | 221 | // the list ends with the new chain |
universe@481 | 222 | if (end != NULL) { |
universe@481 | 223 | *end = insert_end; |
universe@481 | 224 | } |
universe@481 | 225 | } else { |
universe@481 | 226 | cx_linked_list_link(insert_end, successor, loc_prev, loc_next); |
universe@398 | 227 | } |
universe@398 | 228 | } |
universe@398 | 229 | |
universe@481 | 230 | void cx_linked_list_remove( |
universe@481 | 231 | void **begin, |
universe@481 | 232 | void **end, |
universe@481 | 233 | ptrdiff_t loc_prev, |
universe@481 | 234 | ptrdiff_t loc_next, |
universe@481 | 235 | void *node |
universe@481 | 236 | ) { |
universe@477 | 237 | assert(node != NULL); |
universe@473 | 238 | assert(loc_next >= 0); |
universe@473 | 239 | assert(loc_prev >= 0 || begin != NULL); |
universe@473 | 240 | |
universe@473 | 241 | // find adjacent nodes |
universe@480 | 242 | void *next = ll_next(node); |
universe@473 | 243 | void *prev; |
universe@473 | 244 | if (loc_prev >= 0) { |
universe@480 | 245 | prev = ll_prev(node); |
universe@473 | 246 | } else { |
universe@473 | 247 | prev = cx_linked_list_prev(*begin, loc_next, node); |
universe@473 | 248 | } |
universe@473 | 249 | |
universe@476 | 250 | // update next pointer of prev node, or set begin |
universe@476 | 251 | if (prev == NULL) { |
universe@476 | 252 | if (begin != NULL) { |
universe@476 | 253 | *begin = next; |
universe@476 | 254 | } |
universe@476 | 255 | } else { |
universe@480 | 256 | ll_next(prev) = next; |
universe@473 | 257 | } |
universe@476 | 258 | |
universe@476 | 259 | // update prev pointer of next node, or set end |
universe@476 | 260 | if (next == NULL) { |
universe@476 | 261 | if (end != NULL) { |
universe@476 | 262 | *end = prev; |
universe@476 | 263 | } |
universe@476 | 264 | } else if (loc_prev >= 0) { |
universe@480 | 265 | ll_prev(next) = prev; |
universe@473 | 266 | } |
universe@473 | 267 | } |
universe@473 | 268 | |
universe@481 | 269 | size_t cx_linked_list_size( |
universe@489 | 270 | void const *node, |
universe@481 | 271 | ptrdiff_t loc_next |
universe@481 | 272 | ) { |
universe@473 | 273 | assert(loc_next >= 0); |
universe@468 | 274 | size_t size = 0; |
universe@468 | 275 | while (node != NULL) { |
universe@480 | 276 | node = ll_next(node); |
universe@468 | 277 | size++; |
universe@468 | 278 | } |
universe@468 | 279 | return size; |
universe@468 | 280 | } |
universe@468 | 281 | |
universe@661 | 282 | #ifndef CX_LINKED_LIST_SORT_SBO_SIZE |
universe@661 | 283 | #define CX_LINKED_LIST_SORT_SBO_SIZE 1024 |
universe@661 | 284 | #endif |
universe@661 | 285 | |
universe@481 | 286 | static void *cx_linked_list_sort_merge( |
universe@481 | 287 | ptrdiff_t loc_prev, |
universe@481 | 288 | ptrdiff_t loc_next, |
universe@481 | 289 | ptrdiff_t loc_data, |
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@661 | 296 | void *sbo[CX_LINKED_LIST_SORT_SBO_SIZE]; |
universe@661 | 297 | void **sorted = length >= CX_LINKED_LIST_SORT_SBO_SIZE ? |
universe@662 | 298 | 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@628 | 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@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@639 | 380 | void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, |
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@639 | 387 | cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, cmp_func); |
universe@468 | 388 | |
universe@468 | 389 | // merge sorted list with (also sorted) remainder |
universe@639 | 390 | *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, |
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@486 | 406 | CxListComparator cmp_func |
universe@486 | 407 | ) { |
universe@489 | 408 | void const *left = begin_left, *right = begin_right; |
universe@486 | 409 | |
universe@486 | 410 | while (left != NULL && right != NULL) { |
universe@639 | 411 | void const *left_data = ll_data(left); |
universe@639 | 412 | void const *right_data = ll_data(right); |
universe@552 | 413 | int result = cmp_func(left_data, right_data); |
universe@486 | 414 | if (result != 0) return result; |
universe@486 | 415 | left = ll_advance(left); |
universe@486 | 416 | right = ll_advance(right); |
universe@486 | 417 | } |
universe@486 | 418 | |
universe@486 | 419 | if (left != NULL) { return 1; } |
universe@486 | 420 | else if (right != NULL) { return -1; } |
universe@486 | 421 | else { return 0; } |
universe@486 | 422 | } |
universe@486 | 423 | |
universe@481 | 424 | void cx_linked_list_reverse( |
universe@481 | 425 | void **begin, |
universe@481 | 426 | void **end, |
universe@481 | 427 | ptrdiff_t loc_prev, |
universe@481 | 428 | ptrdiff_t loc_next |
universe@481 | 429 | ) { |
universe@473 | 430 | assert(begin != NULL); |
universe@473 | 431 | assert(loc_next >= 0); |
universe@473 | 432 | |
universe@473 | 433 | // swap all links |
universe@473 | 434 | void *prev = NULL; |
universe@473 | 435 | void *cur = *begin; |
universe@473 | 436 | while (cur != NULL) { |
universe@480 | 437 | void *next = ll_next(cur); |
universe@473 | 438 | |
universe@480 | 439 | ll_next(cur) = prev; |
universe@473 | 440 | if (loc_prev >= 0) { |
universe@480 | 441 | ll_prev(cur) = next; |
universe@473 | 442 | } |
universe@473 | 443 | |
universe@473 | 444 | prev = cur; |
universe@473 | 445 | cur = next; |
universe@473 | 446 | } |
universe@473 | 447 | |
universe@473 | 448 | // update begin and end |
universe@473 | 449 | if (end != NULL) { |
universe@473 | 450 | *end = *begin; |
universe@473 | 451 | } |
universe@473 | 452 | *begin = prev; |
universe@473 | 453 | } |
universe@473 | 454 | |
universe@628 | 455 | // HIGH LEVEL LINKED LIST IMPLEMENTATION |
universe@398 | 456 | |
universe@647 | 457 | bool CX_DISABLE_LINKED_LIST_SWAP_SBO = false; |
universe@647 | 458 | |
universe@447 | 459 | typedef struct cx_linked_list_node cx_linked_list_node; |
universe@447 | 460 | struct cx_linked_list_node { |
universe@447 | 461 | cx_linked_list_node *prev; |
universe@447 | 462 | cx_linked_list_node *next; |
universe@403 | 463 | char payload[]; |
universe@447 | 464 | }; |
universe@402 | 465 | |
universe@446 | 466 | #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev) |
universe@446 | 467 | #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next) |
universe@469 | 468 | #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload) |
universe@439 | 469 | |
universe@398 | 470 | typedef struct { |
universe@500 | 471 | struct cx_list_s base; |
universe@446 | 472 | cx_linked_list_node *begin; |
universe@446 | 473 | cx_linked_list_node *end; |
universe@398 | 474 | } cx_linked_list; |
universe@398 | 475 | |
universe@481 | 476 | static cx_linked_list_node *cx_ll_node_at( |
universe@489 | 477 | cx_linked_list const *list, |
universe@481 | 478 | size_t index |
universe@481 | 479 | ) { |
universe@447 | 480 | if (index >= list->base.size) { |
universe@447 | 481 | return NULL; |
universe@447 | 482 | } else if (index > list->base.size / 2) { |
universe@468 | 483 | return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index); |
universe@447 | 484 | } else { |
universe@447 | 485 | return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index); |
universe@447 | 486 | } |
universe@447 | 487 | } |
universe@447 | 488 | |
universe@499 | 489 | static int cx_ll_insert_at( |
universe@500 | 490 | struct cx_list_s *list, |
universe@499 | 491 | cx_linked_list_node *node, |
universe@489 | 492 | void const *elem |
universe@481 | 493 | ) { |
universe@448 | 494 | |
universe@481 | 495 | // create the new new_node |
universe@481 | 496 | cx_linked_list_node *new_node = cxMalloc(list->allocator, |
universe@481 | 497 | sizeof(cx_linked_list_node) + list->itemsize); |
universe@448 | 498 | |
universe@448 | 499 | // sortir if failed |
universe@481 | 500 | if (new_node == NULL) return 1; |
universe@448 | 501 | |
universe@481 | 502 | // initialize new new_node |
universe@481 | 503 | new_node->prev = new_node->next = NULL; |
universe@481 | 504 | memcpy(new_node->payload, elem, list->itemsize); |
universe@448 | 505 | |
universe@481 | 506 | // insert |
universe@499 | 507 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@481 | 508 | cx_linked_list_insert_chain( |
universe@481 | 509 | (void **) &ll->begin, (void **) &ll->end, |
universe@481 | 510 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, |
universe@481 | 511 | node, new_node, new_node |
universe@481 | 512 | ); |
universe@448 | 513 | |
universe@448 | 514 | // increase the size and return |
universe@448 | 515 | list->size++; |
universe@448 | 516 | return 0; |
universe@448 | 517 | } |
universe@448 | 518 | |
universe@638 | 519 | static size_t cx_ll_insert_array( |
universe@638 | 520 | struct cx_list_s *list, |
universe@638 | 521 | size_t index, |
universe@638 | 522 | void const *array, |
universe@638 | 523 | size_t n |
universe@638 | 524 | ) { |
universe@638 | 525 | // out-of bounds and corner case check |
universe@638 | 526 | if (index > list->size || n == 0) return 0; |
universe@638 | 527 | |
universe@638 | 528 | // find position efficiently |
universe@638 | 529 | cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1); |
universe@638 | 530 | |
universe@638 | 531 | // perform first insert |
universe@638 | 532 | if (0 != cx_ll_insert_at(list, node, array)) { |
universe@638 | 533 | return 1; |
universe@638 | 534 | } |
universe@638 | 535 | |
universe@638 | 536 | // is there more? |
universe@638 | 537 | if (n == 1) return 1; |
universe@638 | 538 | |
universe@638 | 539 | // we now know exactly where we are |
universe@638 | 540 | node = node == NULL ? ((cx_linked_list *) list)->begin : node->next; |
universe@638 | 541 | |
universe@638 | 542 | // we can add the remaining nodes and immedately advance to the inserted node |
universe@638 | 543 | char const *source = array; |
universe@638 | 544 | for (size_t i = 1; i < n; i++) { |
universe@638 | 545 | source += list->itemsize; |
universe@638 | 546 | if (0 != cx_ll_insert_at(list, node, source)) { |
universe@638 | 547 | return i; |
universe@638 | 548 | } |
universe@638 | 549 | node = node->next; |
universe@638 | 550 | } |
universe@638 | 551 | return n; |
universe@638 | 552 | } |
universe@638 | 553 | |
universe@641 | 554 | static int cx_ll_insert_element( |
universe@641 | 555 | struct cx_list_s *list, |
universe@641 | 556 | size_t index, |
universe@641 | 557 | void const *element |
universe@641 | 558 | ) { |
universe@641 | 559 | return 1 != cx_ll_insert_array(list, index, element, 1); |
universe@641 | 560 | } |
universe@641 | 561 | |
universe@481 | 562 | static int cx_ll_remove( |
universe@500 | 563 | struct cx_list_s *list, |
universe@481 | 564 | size_t index |
universe@481 | 565 | ) { |
universe@447 | 566 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@447 | 567 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@447 | 568 | |
universe@447 | 569 | // out-of-bounds check |
universe@447 | 570 | if (node == NULL) return 1; |
universe@447 | 571 | |
universe@664 | 572 | // element destruction |
universe@664 | 573 | if (list->content_destructor_type != CX_DESTRUCTOR_NONE) { |
universe@664 | 574 | cx_list_invoke_destructor(list, node->payload); |
universe@664 | 575 | } |
universe@664 | 576 | |
universe@476 | 577 | // remove |
universe@476 | 578 | cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end, |
universe@476 | 579 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node); |
universe@447 | 580 | |
universe@447 | 581 | // adjust size |
universe@447 | 582 | list->size--; |
universe@447 | 583 | |
universe@447 | 584 | // free and return |
universe@447 | 585 | cxFree(list->allocator, node); |
universe@447 | 586 | |
universe@438 | 587 | return 0; |
universe@398 | 588 | } |
universe@398 | 589 | |
universe@664 | 590 | static void cx_ll_clear(struct cx_list_s *list) { |
universe@664 | 591 | if (list->size == 0) return; |
universe@664 | 592 | |
universe@664 | 593 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@664 | 594 | cx_linked_list_node *node = ll->begin; |
universe@664 | 595 | |
universe@664 | 596 | // looks super redundant, but avoids repeatedly checking |
universe@664 | 597 | // the destructor type for each element |
universe@664 | 598 | switch (list->content_destructor_type) { |
universe@664 | 599 | case CX_DESTRUCTOR_SIMPLE: { |
universe@664 | 600 | while (node != NULL) { |
universe@664 | 601 | list->simple_destructor(node->payload); |
universe@664 | 602 | cx_linked_list_node *next = node->next; |
universe@664 | 603 | cxFree(list->allocator, node); |
universe@664 | 604 | node = next; |
universe@664 | 605 | } |
universe@664 | 606 | break; |
universe@664 | 607 | } |
universe@664 | 608 | case CX_DESTRUCTOR_ADVANCED: { |
universe@664 | 609 | while (node != NULL) { |
universe@664 | 610 | list->advanced_destructor.func(list->advanced_destructor.data, |
universe@664 | 611 | node->payload); |
universe@664 | 612 | cx_linked_list_node *next = node->next; |
universe@664 | 613 | cxFree(list->allocator, node); |
universe@664 | 614 | node = next; |
universe@664 | 615 | } |
universe@664 | 616 | break; |
universe@664 | 617 | } |
universe@664 | 618 | case CX_DESTRUCTOR_NONE: { |
universe@664 | 619 | while (node != NULL) { |
universe@664 | 620 | cx_linked_list_node *next = node->next; |
universe@664 | 621 | cxFree(list->allocator, node); |
universe@664 | 622 | node = next; |
universe@664 | 623 | } |
universe@664 | 624 | break; |
universe@664 | 625 | } |
universe@664 | 626 | } |
universe@664 | 627 | |
universe@664 | 628 | ll->begin = ll->end = NULL; |
universe@664 | 629 | list->size = 0; |
universe@664 | 630 | } |
universe@664 | 631 | |
universe@647 | 632 | #ifndef CX_LINKED_LIST_SWAP_SBO_SIZE |
universe@647 | 633 | #define CX_LINKED_LIST_SWAP_SBO_SIZE 16 |
universe@647 | 634 | #endif |
universe@647 | 635 | |
universe@647 | 636 | static int cx_ll_swap( |
universe@647 | 637 | struct cx_list_s *list, |
universe@647 | 638 | size_t i, |
universe@647 | 639 | size_t j |
universe@647 | 640 | ) { |
universe@647 | 641 | if (i >= list->size || j >= list->size) return 1; |
universe@647 | 642 | if (i == j) return 0; |
universe@647 | 643 | |
universe@647 | 644 | // perform an optimized search that finds both elements in one run |
universe@647 | 645 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@647 | 646 | size_t mid = list->size / 2; |
universe@647 | 647 | size_t left, right; |
universe@647 | 648 | if (i < j) { |
universe@647 | 649 | left = i; |
universe@647 | 650 | right = j; |
universe@647 | 651 | } else { |
universe@647 | 652 | left = j; |
universe@647 | 653 | right = i; |
universe@647 | 654 | } |
universe@647 | 655 | cx_linked_list_node *nleft, *nright; |
universe@647 | 656 | if (left < mid && right < mid) { |
universe@647 | 657 | // case 1: both items left from mid |
universe@647 | 658 | nleft = cx_ll_node_at(ll, left); |
universe@647 | 659 | nright = nleft; |
universe@647 | 660 | for (size_t c = left; c < right; c++) { |
universe@647 | 661 | nright = nright->next; |
universe@647 | 662 | } |
universe@647 | 663 | } else if (left >= mid && right >= mid) { |
universe@647 | 664 | // case 2: both items right from mid |
universe@647 | 665 | nright = cx_ll_node_at(ll, right); |
universe@647 | 666 | nleft = nright; |
universe@647 | 667 | for (size_t c = right; c > left; c--) { |
universe@647 | 668 | nleft = nleft->prev; |
universe@647 | 669 | } |
universe@647 | 670 | } else { |
universe@647 | 671 | // case 3: one item left, one item right |
universe@647 | 672 | |
universe@647 | 673 | // chose the closest to begin / end |
universe@647 | 674 | size_t closest; |
universe@647 | 675 | size_t other; |
universe@647 | 676 | size_t diff2boundary = list->size - right - 1; |
universe@647 | 677 | if (left <= diff2boundary) { |
universe@647 | 678 | closest = left; |
universe@647 | 679 | other = right; |
universe@647 | 680 | nleft = cx_ll_node_at(ll, left); |
universe@647 | 681 | } else { |
universe@647 | 682 | closest = right; |
universe@647 | 683 | other = left; |
universe@647 | 684 | diff2boundary = left; |
universe@647 | 685 | nright = cx_ll_node_at(ll, right); |
universe@647 | 686 | } |
universe@647 | 687 | |
universe@647 | 688 | // is other element closer to us or closer to boundary? |
universe@647 | 689 | if (right - left <= diff2boundary) { |
universe@647 | 690 | // search other element starting from already found element |
universe@647 | 691 | if (closest == left) { |
universe@647 | 692 | nright = nleft; |
universe@647 | 693 | for (size_t c = left; c < right; c++) { |
universe@647 | 694 | nright = nright->next; |
universe@647 | 695 | } |
universe@647 | 696 | } else { |
universe@647 | 697 | nleft = nright; |
universe@647 | 698 | for (size_t c = right; c > left; c--) { |
universe@647 | 699 | nleft = nleft->prev; |
universe@647 | 700 | } |
universe@647 | 701 | } |
universe@647 | 702 | } else { |
universe@647 | 703 | // search other element starting at the boundary |
universe@647 | 704 | if (closest == left) { |
universe@647 | 705 | nright = cx_ll_node_at(ll, other); |
universe@647 | 706 | } else { |
universe@647 | 707 | nleft = cx_ll_node_at(ll, other); |
universe@647 | 708 | } |
universe@647 | 709 | } |
universe@647 | 710 | } |
universe@647 | 711 | |
universe@647 | 712 | if (list->itemsize > CX_LINKED_LIST_SWAP_SBO_SIZE || CX_DISABLE_LINKED_LIST_SWAP_SBO) { |
universe@647 | 713 | cx_linked_list_node *prev = nleft->prev; |
universe@647 | 714 | cx_linked_list_node *next = nright->next; |
universe@647 | 715 | cx_linked_list_node *midstart = nleft->next; |
universe@647 | 716 | cx_linked_list_node *midend = nright->prev; |
universe@647 | 717 | |
universe@647 | 718 | if (prev == NULL) { |
universe@647 | 719 | ll->begin = nright; |
universe@647 | 720 | } else { |
universe@647 | 721 | prev->next = nright; |
universe@647 | 722 | } |
universe@647 | 723 | nright->prev = prev; |
universe@647 | 724 | if (midstart == nright) { |
universe@647 | 725 | // special case: both nodes are adjacent |
universe@647 | 726 | nright->next = nleft; |
universe@647 | 727 | nleft->prev = nright; |
universe@647 | 728 | } else { |
universe@647 | 729 | // likely case: a chain is between the two nodes |
universe@647 | 730 | nright->next = midstart; |
universe@647 | 731 | midstart->prev = nright; |
universe@647 | 732 | midend->next = nleft; |
universe@647 | 733 | nleft->prev = midend; |
universe@647 | 734 | } |
universe@647 | 735 | nleft->next = next; |
universe@647 | 736 | if (next == NULL) { |
universe@647 | 737 | ll->end = nleft; |
universe@647 | 738 | } else { |
universe@647 | 739 | next->prev = nleft; |
universe@647 | 740 | } |
universe@647 | 741 | } else { |
universe@647 | 742 | // swap payloads to avoid relinking |
universe@647 | 743 | char buf[CX_LINKED_LIST_SWAP_SBO_SIZE]; |
universe@647 | 744 | memcpy(buf, nleft->payload, list->itemsize); |
universe@647 | 745 | memcpy(nleft->payload, nright->payload, list->itemsize); |
universe@647 | 746 | memcpy(nright->payload, buf, list->itemsize); |
universe@647 | 747 | } |
universe@647 | 748 | |
universe@647 | 749 | return 0; |
universe@647 | 750 | } |
universe@647 | 751 | |
universe@481 | 752 | static void *cx_ll_at( |
universe@500 | 753 | struct cx_list_s const *list, |
universe@481 | 754 | size_t index |
universe@481 | 755 | ) { |
universe@439 | 756 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@447 | 757 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@466 | 758 | return node == NULL ? NULL : node->payload; |
universe@466 | 759 | } |
universe@466 | 760 | |
universe@481 | 761 | static size_t cx_ll_find( |
universe@500 | 762 | struct cx_list_s const *list, |
universe@489 | 763 | void const *elem |
universe@481 | 764 | ) { |
universe@480 | 765 | return cx_linked_list_find(((cx_linked_list *) list)->begin, |
universe@480 | 766 | CX_LL_LOC_NEXT, CX_LL_LOC_DATA, |
universe@639 | 767 | list->cmpfunc, elem); |
universe@466 | 768 | } |
universe@466 | 769 | |
universe@500 | 770 | static void cx_ll_sort(struct cx_list_s *list) { |
universe@469 | 771 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@469 | 772 | cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end, |
universe@469 | 773 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA, |
universe@639 | 774 | list->cmpfunc); |
universe@469 | 775 | } |
universe@469 | 776 | |
universe@500 | 777 | static void cx_ll_reverse(struct cx_list_s *list) { |
universe@490 | 778 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@521 | 779 | cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT); |
universe@490 | 780 | } |
universe@490 | 781 | |
universe@488 | 782 | static int cx_ll_compare( |
universe@500 | 783 | struct cx_list_s const *list, |
universe@500 | 784 | struct cx_list_s const *other |
universe@488 | 785 | ) { |
universe@488 | 786 | cx_linked_list *left = (cx_linked_list *) list; |
universe@488 | 787 | cx_linked_list *right = (cx_linked_list *) other; |
universe@488 | 788 | return cx_linked_list_compare(left->begin, right->begin, |
universe@488 | 789 | CX_LL_LOC_NEXT, CX_LL_LOC_DATA, |
universe@639 | 790 | list->cmpfunc); |
universe@488 | 791 | } |
universe@488 | 792 | |
universe@630 | 793 | static bool cx_ll_iter_valid(void const *it) { |
universe@630 | 794 | struct cx_iterator_s const *iter = it; |
universe@495 | 795 | return iter->elem_handle != NULL; |
universe@494 | 796 | } |
universe@494 | 797 | |
universe@630 | 798 | static void cx_ll_iter_next(void *it) { |
universe@630 | 799 | struct cx_iterator_base_s *itbase = it; |
universe@630 | 800 | if (itbase->remove) { |
universe@630 | 801 | itbase->remove = false; |
universe@630 | 802 | struct cx_mut_iterator_s *iter = it; |
universe@664 | 803 | struct cx_list_s *list = iter->src_handle; |
universe@495 | 804 | cx_linked_list *ll = iter->src_handle; |
universe@495 | 805 | cx_linked_list_node *node = iter->elem_handle; |
universe@495 | 806 | iter->elem_handle = node->next; |
universe@664 | 807 | if (list->content_destructor_type != CX_DESTRUCTOR_NONE) { |
universe@664 | 808 | cx_list_invoke_destructor(list, node->payload); |
universe@664 | 809 | } |
universe@495 | 810 | cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end, |
universe@495 | 811 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node); |
universe@664 | 812 | list->size--; |
universe@664 | 813 | cxFree(list->allocator, node); |
universe@495 | 814 | } else { |
universe@630 | 815 | struct cx_iterator_s *iter = it; |
universe@495 | 816 | iter->index++; |
universe@495 | 817 | cx_linked_list_node *node = iter->elem_handle; |
universe@495 | 818 | iter->elem_handle = node->next; |
universe@495 | 819 | } |
universe@494 | 820 | } |
universe@494 | 821 | |
universe@655 | 822 | static void cx_ll_iter_prev(void *it) { |
universe@655 | 823 | struct cx_iterator_base_s *itbase = it; |
universe@655 | 824 | if (itbase->remove) { |
universe@655 | 825 | itbase->remove = false; |
universe@655 | 826 | struct cx_mut_iterator_s *iter = it; |
universe@664 | 827 | struct cx_list_s *list = iter->src_handle; |
universe@655 | 828 | cx_linked_list *ll = iter->src_handle; |
universe@655 | 829 | cx_linked_list_node *node = iter->elem_handle; |
universe@655 | 830 | iter->elem_handle = node->prev; |
universe@655 | 831 | iter->index--; |
universe@664 | 832 | if (list->content_destructor_type != CX_DESTRUCTOR_NONE) { |
universe@664 | 833 | cx_list_invoke_destructor(list, node->payload); |
universe@664 | 834 | } |
universe@655 | 835 | cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end, |
universe@655 | 836 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node); |
universe@664 | 837 | list->size--; |
universe@664 | 838 | cxFree(list->allocator, node); |
universe@655 | 839 | } else { |
universe@655 | 840 | struct cx_iterator_s *iter = it; |
universe@655 | 841 | iter->index--; |
universe@655 | 842 | cx_linked_list_node *node = iter->elem_handle; |
universe@655 | 843 | iter->elem_handle = node->prev; |
universe@655 | 844 | } |
universe@655 | 845 | } |
universe@655 | 846 | |
universe@630 | 847 | static void *cx_ll_iter_current(void const *it) { |
universe@630 | 848 | struct cx_iterator_s const *iter = it; |
universe@495 | 849 | cx_linked_list_node *node = iter->elem_handle; |
universe@494 | 850 | return node->payload; |
universe@494 | 851 | } |
universe@494 | 852 | |
universe@630 | 853 | static bool cx_ll_iter_flag_rm(void *it) { |
universe@630 | 854 | struct cx_iterator_base_s *iter = it; |
universe@630 | 855 | if (iter->mutating) { |
universe@630 | 856 | iter->remove = true; |
universe@630 | 857 | return true; |
universe@630 | 858 | } else { |
universe@630 | 859 | return false; |
universe@630 | 860 | } |
universe@630 | 861 | } |
universe@630 | 862 | |
universe@494 | 863 | static CxIterator cx_ll_iterator( |
universe@630 | 864 | struct cx_list_s const *list, |
universe@655 | 865 | size_t index, |
universe@655 | 866 | bool backwards |
universe@494 | 867 | ) { |
universe@494 | 868 | CxIterator iter; |
universe@494 | 869 | iter.index = index; |
universe@495 | 870 | iter.src_handle = list; |
universe@495 | 871 | iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index); |
universe@630 | 872 | iter.base.valid = cx_ll_iter_valid; |
universe@630 | 873 | iter.base.current = cx_ll_iter_current; |
universe@655 | 874 | iter.base.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next; |
universe@630 | 875 | iter.base.flag_removal = cx_ll_iter_flag_rm; |
universe@630 | 876 | iter.base.mutating = false; |
universe@630 | 877 | iter.base.remove = false; |
universe@494 | 878 | return iter; |
universe@494 | 879 | } |
universe@494 | 880 | |
universe@499 | 881 | static int cx_ll_insert_iter( |
universe@630 | 882 | CxMutIterator *iter, |
universe@499 | 883 | void const *elem, |
universe@499 | 884 | int prepend |
universe@499 | 885 | ) { |
universe@500 | 886 | struct cx_list_s *list = iter->src_handle; |
universe@499 | 887 | cx_linked_list_node *node = iter->elem_handle; |
universe@499 | 888 | if (node != NULL) { |
universe@499 | 889 | assert(prepend >= 0 && prepend <= 1); |
universe@499 | 890 | cx_linked_list_node *choice[2] = {node, node->prev}; |
universe@499 | 891 | int result = cx_ll_insert_at(list, choice[prepend], elem); |
universe@499 | 892 | iter->index += prepend * (0 == result); |
universe@499 | 893 | return result; |
universe@499 | 894 | } else { |
universe@641 | 895 | int result = cx_ll_insert_element(list, list->size, elem); |
universe@499 | 896 | iter->index = list->size; |
universe@499 | 897 | return result; |
universe@499 | 898 | } |
universe@499 | 899 | } |
universe@499 | 900 | |
universe@524 | 901 | static void cx_ll_destructor(CxList *list) { |
universe@524 | 902 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@524 | 903 | |
universe@524 | 904 | cx_linked_list_node *node = ll->begin; |
universe@524 | 905 | while (node) { |
universe@524 | 906 | void *next = node->next; |
universe@524 | 907 | cxFree(list->allocator, node); |
universe@524 | 908 | node = next; |
universe@524 | 909 | } |
universe@524 | 910 | // do not free the list pointer, this is just a destructor! |
universe@524 | 911 | } |
universe@524 | 912 | |
universe@451 | 913 | static cx_list_class cx_linked_list_class = { |
universe@524 | 914 | cx_ll_destructor, |
universe@641 | 915 | cx_ll_insert_element, |
universe@638 | 916 | cx_ll_insert_array, |
universe@499 | 917 | cx_ll_insert_iter, |
universe@398 | 918 | cx_ll_remove, |
universe@664 | 919 | cx_ll_clear, |
universe@647 | 920 | cx_ll_swap, |
universe@439 | 921 | cx_ll_at, |
universe@404 | 922 | cx_ll_find, |
universe@488 | 923 | cx_ll_sort, |
universe@490 | 924 | cx_ll_compare, |
universe@494 | 925 | cx_ll_reverse, |
universe@630 | 926 | cx_ll_iterator, |
universe@398 | 927 | }; |
universe@398 | 928 | |
universe@662 | 929 | static CxList *cx_linked_list_create( |
universe@508 | 930 | CxAllocator const *allocator, |
universe@481 | 931 | CxListComparator comparator, |
universe@481 | 932 | size_t item_size |
universe@481 | 933 | ) { |
universe@503 | 934 | cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list)); |
universe@528 | 935 | if (list == NULL) return NULL; |
universe@398 | 936 | |
universe@435 | 937 | list->base.cl = &cx_linked_list_class; |
universe@435 | 938 | list->base.allocator = allocator; |
universe@435 | 939 | list->base.cmpfunc = comparator; |
universe@435 | 940 | list->base.itemsize = item_size; |
universe@435 | 941 | list->base.capacity = SIZE_MAX; |
universe@406 | 942 | |
universe@500 | 943 | return (CxList *) list; |
universe@406 | 944 | } |
universe@662 | 945 | |
universe@662 | 946 | CxList *cxLinkedListCreate( |
universe@662 | 947 | CxAllocator const *allocator, |
universe@662 | 948 | CxListComparator comparator, |
universe@662 | 949 | size_t item_size |
universe@662 | 950 | ) { |
universe@662 | 951 | return cx_linked_list_create(allocator, comparator, item_size); |
universe@662 | 952 | } |
universe@662 | 953 | |
universe@662 | 954 | CxList *cxLinkedListCreateSimple(size_t item_size) { |
universe@662 | 955 | return cx_linked_list_create(cxDefaultAllocator, NULL, item_size); |
universe@662 | 956 | } |