Tue, 28 Dec 2021 17:49:52 +0100
fix missing cleanup in test_hl_linked_list_from_array
universe@398 | 1 | /* |
universe@398 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@398 | 3 | * |
universe@398 | 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
universe@398 | 5 | * |
universe@398 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@398 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@398 | 8 | * |
universe@398 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@398 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@398 | 11 | * |
universe@398 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@398 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@398 | 14 | * documentation and/or other materials provided with the distribution. |
universe@398 | 15 | * |
universe@398 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@398 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@398 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@398 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@398 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@398 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@398 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@398 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@398 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@398 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@398 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@398 | 27 | */ |
universe@398 | 28 | |
universe@398 | 29 | #include "cx/linked_list.h" |
universe@401 | 30 | #include <stdint.h> |
universe@401 | 31 | #include <string.h> |
universe@453 | 32 | #include <assert.h> |
universe@398 | 33 | |
universe@398 | 34 | /* LOW LEVEL LINKED LIST FUNCTIONS */ |
universe@398 | 35 | |
universe@468 | 36 | #define CX_LL_PTR(cur, off) (*(void**)(((char*)cur)+off)) |
universe@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@480 | 40 | #define ll_data(node) (follow_ptr?CX_LL_PTR(node, loc_data):(((char*)node)+loc_data)) |
universe@398 | 41 | |
universe@481 | 42 | void *cx_linked_list_at( |
universe@481 | 43 | void *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@446 | 51 | void *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@438 | 56 | return 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@487 | 63 | bool follow_ptr, |
universe@481 | 64 | CxListComparator cmp_func, |
universe@489 | 65 | void const *elem |
universe@481 | 66 | ) { |
universe@480 | 67 | assert(start != NULL); |
universe@480 | 68 | assert(loc_advance >= 0); |
universe@480 | 69 | assert(loc_data >= 0); |
universe@480 | 70 | assert(cmp_func); |
universe@480 | 71 | |
universe@489 | 72 | void const *node = start; |
universe@480 | 73 | size_t index = 0; |
universe@480 | 74 | do { |
universe@480 | 75 | void *current = ll_data(node); |
universe@480 | 76 | if (cmp_func(current, elem) == 0) { |
universe@480 | 77 | return index; |
universe@480 | 78 | } |
universe@480 | 79 | node = ll_advance(node); |
universe@480 | 80 | index++; |
universe@480 | 81 | } while (node != NULL); |
universe@480 | 82 | return index; |
universe@480 | 83 | } |
universe@480 | 84 | |
universe@481 | 85 | void *cx_linked_list_first( |
universe@481 | 86 | void *node, |
universe@481 | 87 | ptrdiff_t loc_prev |
universe@481 | 88 | ) { |
universe@475 | 89 | return cx_linked_list_last(node, loc_prev); |
universe@475 | 90 | } |
universe@475 | 91 | |
universe@481 | 92 | void *cx_linked_list_last( |
universe@481 | 93 | void *node, |
universe@481 | 94 | ptrdiff_t loc_next |
universe@481 | 95 | ) { |
universe@478 | 96 | assert(node != NULL); |
universe@473 | 97 | assert(loc_next >= 0); |
universe@398 | 98 | |
universe@475 | 99 | void *cur = node; |
universe@456 | 100 | void *last; |
universe@456 | 101 | do { |
universe@456 | 102 | last = cur; |
universe@480 | 103 | } while ((cur = ll_next(cur)) != NULL); |
universe@398 | 104 | |
universe@456 | 105 | return last; |
universe@398 | 106 | } |
universe@398 | 107 | |
universe@481 | 108 | void *cx_linked_list_prev( |
universe@481 | 109 | void *begin, |
universe@481 | 110 | ptrdiff_t loc_next, |
universe@481 | 111 | void *node |
universe@481 | 112 | ) { |
universe@473 | 113 | assert(begin != NULL); |
universe@478 | 114 | assert(node != NULL); |
universe@473 | 115 | assert(loc_next >= 0); |
universe@473 | 116 | if (begin == node) return NULL; |
universe@473 | 117 | void *cur = begin; |
universe@473 | 118 | void *next; |
universe@473 | 119 | while (1) { |
universe@480 | 120 | next = ll_next(cur); |
universe@473 | 121 | if (next == node) return cur; |
universe@473 | 122 | cur = next; |
universe@473 | 123 | } |
universe@473 | 124 | } |
universe@473 | 125 | |
universe@481 | 126 | void cx_linked_list_link( |
universe@481 | 127 | void *left, |
universe@481 | 128 | void *right, |
universe@481 | 129 | ptrdiff_t loc_prev, |
universe@481 | 130 | ptrdiff_t loc_next |
universe@481 | 131 | ) { |
universe@473 | 132 | assert(loc_next >= 0); |
universe@481 | 133 | ll_next(left) = right; |
universe@481 | 134 | if (loc_prev >= 0) { |
universe@481 | 135 | ll_prev(right) = left; |
universe@481 | 136 | } |
universe@481 | 137 | } |
universe@481 | 138 | |
universe@481 | 139 | void cx_linked_list_unlink( |
universe@481 | 140 | void *left, |
universe@481 | 141 | void *right, |
universe@481 | 142 | ptrdiff_t loc_prev, |
universe@481 | 143 | ptrdiff_t loc_next |
universe@481 | 144 | ) { |
universe@481 | 145 | assert (loc_next >= 0); |
universe@481 | 146 | assert(ll_next(left) == right); |
universe@481 | 147 | ll_next(left) = NULL; |
universe@481 | 148 | if (loc_prev >= 0) { |
universe@481 | 149 | assert(ll_prev(right) == left); |
universe@481 | 150 | ll_prev(right) = NULL; |
universe@481 | 151 | } |
universe@481 | 152 | } |
universe@481 | 153 | |
universe@481 | 154 | void cx_linked_list_add( |
universe@481 | 155 | void **begin, |
universe@481 | 156 | void **end, |
universe@481 | 157 | ptrdiff_t loc_prev, |
universe@481 | 158 | ptrdiff_t loc_next, |
universe@481 | 159 | void *new_node |
universe@481 | 160 | ) { |
universe@456 | 161 | void *last; |
universe@456 | 162 | if (end == NULL) { |
universe@456 | 163 | assert(begin != NULL); |
universe@478 | 164 | last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next); |
universe@456 | 165 | } else { |
universe@456 | 166 | last = *end; |
universe@456 | 167 | } |
universe@481 | 168 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node); |
universe@481 | 169 | } |
universe@481 | 170 | |
universe@481 | 171 | void cx_linked_list_prepend( |
universe@481 | 172 | void **begin, |
universe@481 | 173 | void **end, |
universe@481 | 174 | ptrdiff_t loc_prev, |
universe@481 | 175 | ptrdiff_t loc_next, |
universe@481 | 176 | void *new_node |
universe@481 | 177 | ) { |
universe@481 | 178 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node); |
universe@481 | 179 | } |
universe@481 | 180 | |
universe@481 | 181 | void cx_linked_list_insert( |
universe@481 | 182 | void **begin, |
universe@481 | 183 | void **end, |
universe@481 | 184 | ptrdiff_t loc_prev, |
universe@481 | 185 | ptrdiff_t loc_next, |
universe@481 | 186 | void *node, |
universe@481 | 187 | void *new_node |
universe@481 | 188 | ) { |
universe@481 | 189 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node); |
universe@481 | 190 | } |
universe@481 | 191 | |
universe@481 | 192 | void cx_linked_list_insert_chain( |
universe@481 | 193 | void **begin, |
universe@481 | 194 | void **end, |
universe@481 | 195 | ptrdiff_t loc_prev, |
universe@481 | 196 | ptrdiff_t loc_next, |
universe@481 | 197 | void *node, |
universe@481 | 198 | void *insert_begin, |
universe@481 | 199 | void *insert_end |
universe@481 | 200 | ) { |
universe@481 | 201 | // find the end of the chain, if not specified |
universe@481 | 202 | if (insert_end == NULL) { |
universe@481 | 203 | insert_end = cx_linked_list_last(insert_begin, loc_next); |
universe@481 | 204 | } |
universe@481 | 205 | |
universe@481 | 206 | // determine the successor |
universe@481 | 207 | void *successor; |
universe@481 | 208 | if (node == NULL) { |
universe@481 | 209 | assert(begin != NULL || (end != NULL && loc_prev >= 0)); |
universe@475 | 210 | if (begin != NULL) { |
universe@481 | 211 | successor = *begin; |
universe@481 | 212 | *begin = insert_begin; |
universe@481 | 213 | } else { |
universe@481 | 214 | successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev); |
universe@475 | 215 | } |
universe@428 | 216 | } else { |
universe@481 | 217 | successor = ll_next(node); |
universe@481 | 218 | cx_linked_list_link(node, insert_begin, loc_prev, loc_next); |
universe@398 | 219 | } |
universe@398 | 220 | |
universe@481 | 221 | if (successor == NULL) { |
universe@481 | 222 | // the list ends with the new chain |
universe@481 | 223 | if (end != NULL) { |
universe@481 | 224 | *end = insert_end; |
universe@481 | 225 | } |
universe@481 | 226 | } else { |
universe@481 | 227 | cx_linked_list_link(insert_end, successor, loc_prev, loc_next); |
universe@398 | 228 | } |
universe@398 | 229 | } |
universe@398 | 230 | |
universe@481 | 231 | void cx_linked_list_remove( |
universe@481 | 232 | void **begin, |
universe@481 | 233 | void **end, |
universe@481 | 234 | ptrdiff_t loc_prev, |
universe@481 | 235 | ptrdiff_t loc_next, |
universe@481 | 236 | void *node |
universe@481 | 237 | ) { |
universe@477 | 238 | assert(node != NULL); |
universe@473 | 239 | assert(loc_next >= 0); |
universe@473 | 240 | assert(loc_prev >= 0 || begin != NULL); |
universe@473 | 241 | |
universe@473 | 242 | // find adjacent nodes |
universe@480 | 243 | void *next = ll_next(node); |
universe@473 | 244 | void *prev; |
universe@473 | 245 | if (loc_prev >= 0) { |
universe@480 | 246 | prev = ll_prev(node); |
universe@473 | 247 | } else { |
universe@473 | 248 | prev = cx_linked_list_prev(*begin, loc_next, node); |
universe@473 | 249 | } |
universe@473 | 250 | |
universe@476 | 251 | // update next pointer of prev node, or set begin |
universe@476 | 252 | if (prev == NULL) { |
universe@476 | 253 | if (begin != NULL) { |
universe@476 | 254 | *begin = next; |
universe@476 | 255 | } |
universe@476 | 256 | } else { |
universe@480 | 257 | ll_next(prev) = next; |
universe@473 | 258 | } |
universe@476 | 259 | |
universe@476 | 260 | // update prev pointer of next node, or set end |
universe@476 | 261 | if (next == NULL) { |
universe@476 | 262 | if (end != NULL) { |
universe@476 | 263 | *end = prev; |
universe@476 | 264 | } |
universe@476 | 265 | } else if (loc_prev >= 0) { |
universe@480 | 266 | ll_prev(next) = prev; |
universe@473 | 267 | } |
universe@473 | 268 | } |
universe@473 | 269 | |
universe@481 | 270 | size_t cx_linked_list_size( |
universe@489 | 271 | void const *node, |
universe@481 | 272 | ptrdiff_t loc_next |
universe@481 | 273 | ) { |
universe@473 | 274 | assert(loc_next >= 0); |
universe@468 | 275 | size_t size = 0; |
universe@468 | 276 | while (node != NULL) { |
universe@480 | 277 | node = ll_next(node); |
universe@468 | 278 | size++; |
universe@468 | 279 | } |
universe@468 | 280 | return size; |
universe@468 | 281 | } |
universe@468 | 282 | |
universe@481 | 283 | static void *cx_linked_list_sort_merge( |
universe@481 | 284 | ptrdiff_t loc_prev, |
universe@481 | 285 | ptrdiff_t loc_next, |
universe@481 | 286 | ptrdiff_t loc_data, |
universe@487 | 287 | bool follow_ptr, |
universe@481 | 288 | size_t length, |
universe@481 | 289 | void *ls, |
universe@481 | 290 | void *le, |
universe@481 | 291 | void *re, |
universe@481 | 292 | CxListComparator cmp_func |
universe@481 | 293 | ) { |
universe@468 | 294 | const size_t sbo_len = 1024; |
universe@468 | 295 | void *sbo[sbo_len]; |
universe@468 | 296 | void **sorted = (length >= sbo_len) ? malloc(sizeof(void *) * length) : sbo; |
universe@468 | 297 | void *rc, *lc; |
universe@468 | 298 | |
universe@468 | 299 | lc = ls; |
universe@468 | 300 | rc = le; |
universe@468 | 301 | size_t n = 0; |
universe@468 | 302 | while (lc && lc != le && rc != re) { |
universe@468 | 303 | if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) { |
universe@468 | 304 | sorted[n] = lc; |
universe@468 | 305 | lc = ll_next(lc); |
universe@468 | 306 | } else { |
universe@468 | 307 | sorted[n] = rc; |
universe@468 | 308 | rc = ll_next(rc); |
universe@468 | 309 | } |
universe@468 | 310 | n++; |
universe@468 | 311 | } |
universe@468 | 312 | while (lc && lc != le) { |
universe@468 | 313 | sorted[n] = lc; |
universe@468 | 314 | lc = ll_next(lc); |
universe@468 | 315 | n++; |
universe@468 | 316 | } |
universe@468 | 317 | while (rc && rc != re) { |
universe@468 | 318 | sorted[n] = rc; |
universe@468 | 319 | rc = ll_next(rc); |
universe@468 | 320 | n++; |
universe@468 | 321 | } |
universe@468 | 322 | |
universe@468 | 323 | // Update pointer |
universe@468 | 324 | if (loc_prev >= 0) ll_prev(sorted[0]) = NULL; |
universe@468 | 325 | for (size_t i = 0; i < length - 1; i++) { |
universe@481 | 326 | cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next); |
universe@468 | 327 | } |
universe@468 | 328 | ll_next(sorted[length - 1]) = NULL; |
universe@468 | 329 | |
universe@468 | 330 | void *ret = sorted[0]; |
universe@468 | 331 | if (sorted != sbo) { |
universe@468 | 332 | free(sorted); |
universe@468 | 333 | } |
universe@468 | 334 | return ret; |
universe@468 | 335 | } |
universe@468 | 336 | |
universe@476 | 337 | void cx_linked_list_sort( /* NOLINT(misc-no-recursion) - purposely recursive function */ |
universe@481 | 338 | void **begin, |
universe@481 | 339 | void **end, |
universe@481 | 340 | ptrdiff_t loc_prev, |
universe@481 | 341 | ptrdiff_t loc_next, |
universe@481 | 342 | ptrdiff_t loc_data, |
universe@487 | 343 | bool follow_ptr, |
universe@481 | 344 | CxListComparator cmp_func |
universe@481 | 345 | ) { |
universe@468 | 346 | assert(begin != NULL); |
universe@473 | 347 | assert(loc_next >= 0); |
universe@473 | 348 | assert(loc_data >= 0); |
universe@473 | 349 | assert(cmp_func); |
universe@468 | 350 | |
universe@468 | 351 | void *lc, *ls, *le, *re; |
universe@468 | 352 | |
universe@468 | 353 | // set start node |
universe@468 | 354 | ls = *begin; |
universe@468 | 355 | |
universe@468 | 356 | // check how many elements are already sorted |
universe@468 | 357 | lc = ls; |
universe@468 | 358 | size_t ln = 1; |
universe@468 | 359 | while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) { |
universe@468 | 360 | lc = ll_next(lc); |
universe@468 | 361 | ln++; |
universe@468 | 362 | } |
universe@468 | 363 | le = ll_next(lc); |
universe@468 | 364 | |
universe@468 | 365 | // if first unsorted node is NULL, the list is already completely sorted |
universe@468 | 366 | if (le != NULL) { |
universe@468 | 367 | void *rc; |
universe@468 | 368 | size_t rn = 1; |
universe@468 | 369 | rc = le; |
universe@468 | 370 | // skip already sorted elements |
universe@468 | 371 | while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) { |
universe@468 | 372 | rc = ll_next(rc); |
universe@468 | 373 | rn++; |
universe@468 | 374 | } |
universe@468 | 375 | re = ll_next(rc); |
universe@468 | 376 | |
universe@468 | 377 | // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them |
universe@468 | 378 | void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr, |
universe@468 | 379 | ln + rn, ls, le, re, cmp_func); |
universe@468 | 380 | |
universe@468 | 381 | // Something left? Sort it! |
universe@468 | 382 | size_t remainder_length = cx_linked_list_size(re, loc_next); |
universe@468 | 383 | if (remainder_length > 0) { |
universe@468 | 384 | void *remainder = re; |
universe@468 | 385 | cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, follow_ptr, cmp_func); |
universe@468 | 386 | |
universe@468 | 387 | // merge sorted list with (also sorted) remainder |
universe@468 | 388 | *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr, |
universe@468 | 389 | ln + rn + remainder_length, |
universe@468 | 390 | sorted, remainder, NULL, cmp_func); |
universe@468 | 391 | } else { |
universe@468 | 392 | // no remainder - we've got our sorted list |
universe@468 | 393 | *begin = sorted; |
universe@468 | 394 | } |
universe@468 | 395 | if (end) *end = cx_linked_list_last(sorted, loc_next); |
universe@468 | 396 | } |
universe@468 | 397 | } |
universe@468 | 398 | |
universe@486 | 399 | int cx_linked_list_compare( |
universe@489 | 400 | void const *begin_left, |
universe@489 | 401 | void const *begin_right, |
universe@486 | 402 | ptrdiff_t loc_advance, |
universe@486 | 403 | ptrdiff_t loc_data, |
universe@487 | 404 | bool follow_ptr, |
universe@486 | 405 | CxListComparator cmp_func |
universe@486 | 406 | ) { |
universe@489 | 407 | void const *left = begin_left, *right = begin_right; |
universe@486 | 408 | |
universe@486 | 409 | while (left != NULL && right != NULL) { |
universe@486 | 410 | int result = cmp_func(ll_data(left), ll_data(right)); |
universe@486 | 411 | if (result != 0) return result; |
universe@486 | 412 | left = ll_advance(left); |
universe@486 | 413 | right = ll_advance(right); |
universe@486 | 414 | } |
universe@486 | 415 | |
universe@486 | 416 | if (left != NULL) { return 1; } |
universe@486 | 417 | else if (right != NULL) { return -1; } |
universe@486 | 418 | else { return 0; } |
universe@486 | 419 | } |
universe@486 | 420 | |
universe@481 | 421 | void cx_linked_list_reverse( |
universe@481 | 422 | void **begin, |
universe@481 | 423 | void **end, |
universe@481 | 424 | ptrdiff_t loc_prev, |
universe@481 | 425 | ptrdiff_t loc_next |
universe@481 | 426 | ) { |
universe@473 | 427 | assert(begin != NULL); |
universe@473 | 428 | assert(loc_next >= 0); |
universe@473 | 429 | |
universe@473 | 430 | // swap all links |
universe@473 | 431 | void *prev = NULL; |
universe@473 | 432 | void *cur = *begin; |
universe@473 | 433 | while (cur != NULL) { |
universe@480 | 434 | void *next = ll_next(cur); |
universe@473 | 435 | |
universe@480 | 436 | ll_next(cur) = prev; |
universe@473 | 437 | if (loc_prev >= 0) { |
universe@480 | 438 | ll_prev(cur) = next; |
universe@473 | 439 | } |
universe@473 | 440 | |
universe@473 | 441 | prev = cur; |
universe@473 | 442 | cur = next; |
universe@473 | 443 | } |
universe@473 | 444 | |
universe@473 | 445 | // update begin and end |
universe@473 | 446 | if (end != NULL) { |
universe@473 | 447 | *end = *begin; |
universe@473 | 448 | } |
universe@473 | 449 | *begin = prev; |
universe@473 | 450 | } |
universe@473 | 451 | |
universe@398 | 452 | /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ |
universe@398 | 453 | |
universe@447 | 454 | typedef struct cx_linked_list_node cx_linked_list_node; |
universe@447 | 455 | struct cx_linked_list_node { |
universe@447 | 456 | cx_linked_list_node *prev; |
universe@447 | 457 | cx_linked_list_node *next; |
universe@403 | 458 | char payload[]; |
universe@447 | 459 | }; |
universe@402 | 460 | |
universe@446 | 461 | #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev) |
universe@446 | 462 | #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next) |
universe@469 | 463 | #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload) |
universe@439 | 464 | |
universe@398 | 465 | typedef struct { |
universe@435 | 466 | cx_list_s base; |
universe@446 | 467 | cx_linked_list_node *begin; |
universe@446 | 468 | cx_linked_list_node *end; |
universe@398 | 469 | } cx_linked_list; |
universe@398 | 470 | |
universe@481 | 471 | static cx_linked_list_node *cx_ll_node_at( |
universe@489 | 472 | cx_linked_list const *list, |
universe@481 | 473 | size_t index |
universe@481 | 474 | ) { |
universe@447 | 475 | if (index >= list->base.size) { |
universe@447 | 476 | return NULL; |
universe@447 | 477 | } else if (index > list->base.size / 2) { |
universe@468 | 478 | return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index); |
universe@447 | 479 | } else { |
universe@447 | 480 | return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index); |
universe@447 | 481 | } |
universe@447 | 482 | } |
universe@447 | 483 | |
universe@481 | 484 | static int cx_ll_insert( |
universe@481 | 485 | cx_list_s *list, |
universe@481 | 486 | size_t index, |
universe@489 | 487 | void const *elem |
universe@481 | 488 | ) { |
universe@448 | 489 | // out-of bounds check |
universe@448 | 490 | if (index > list->size) return 1; |
universe@448 | 491 | |
universe@448 | 492 | // cast a linked list pointer |
universe@437 | 493 | cx_linked_list *ll = (cx_linked_list *) list; |
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 | // find position efficiently |
universe@481 | 507 | cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at(ll, index - 1); |
universe@448 | 508 | |
universe@481 | 509 | // insert |
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@481 | 521 | static int cx_ll_add( |
universe@481 | 522 | cx_list_s *list, |
universe@489 | 523 | void const *elem |
universe@481 | 524 | ) { |
universe@448 | 525 | return cx_ll_insert(list, list->size, elem); |
universe@398 | 526 | } |
universe@398 | 527 | |
universe@481 | 528 | static int cx_pll_insert( |
universe@481 | 529 | cx_list_s *list, |
universe@481 | 530 | size_t index, |
universe@489 | 531 | void const *elem |
universe@481 | 532 | ) { |
universe@466 | 533 | return cx_ll_insert(list, index, &elem); |
universe@466 | 534 | } |
universe@466 | 535 | |
universe@481 | 536 | static int cx_pll_add( |
universe@481 | 537 | cx_list_s *list, |
universe@489 | 538 | void const *elem |
universe@481 | 539 | ) { |
universe@466 | 540 | return cx_ll_insert(list, list->size, &elem); |
universe@466 | 541 | } |
universe@466 | 542 | |
universe@481 | 543 | static int cx_ll_remove( |
universe@481 | 544 | cx_list_s *list, |
universe@481 | 545 | size_t index |
universe@481 | 546 | ) { |
universe@447 | 547 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@447 | 548 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@447 | 549 | |
universe@447 | 550 | // out-of-bounds check |
universe@447 | 551 | if (node == NULL) return 1; |
universe@447 | 552 | |
universe@476 | 553 | // remove |
universe@476 | 554 | cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end, |
universe@476 | 555 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node); |
universe@447 | 556 | |
universe@447 | 557 | // adjust size |
universe@447 | 558 | list->size--; |
universe@447 | 559 | |
universe@447 | 560 | // free and return |
universe@447 | 561 | cxFree(list->allocator, node); |
universe@447 | 562 | |
universe@438 | 563 | return 0; |
universe@398 | 564 | } |
universe@398 | 565 | |
universe@481 | 566 | static void *cx_ll_at( |
universe@489 | 567 | cx_list_s const *list, |
universe@481 | 568 | size_t index |
universe@481 | 569 | ) { |
universe@439 | 570 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@447 | 571 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@466 | 572 | return node == NULL ? NULL : node->payload; |
universe@466 | 573 | } |
universe@466 | 574 | |
universe@481 | 575 | static void *cx_pll_at( |
universe@489 | 576 | cx_list_s const *list, |
universe@481 | 577 | size_t index |
universe@481 | 578 | ) { |
universe@466 | 579 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@466 | 580 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@468 | 581 | return node == NULL ? NULL : *(void **) node->payload; |
universe@439 | 582 | } |
universe@439 | 583 | |
universe@481 | 584 | static size_t cx_ll_find( |
universe@489 | 585 | cx_list_s const *list, |
universe@489 | 586 | void const *elem |
universe@481 | 587 | ) { |
universe@480 | 588 | return cx_linked_list_find(((cx_linked_list *) list)->begin, |
universe@480 | 589 | CX_LL_LOC_NEXT, CX_LL_LOC_DATA, |
universe@487 | 590 | false, list->cmpfunc, elem); |
universe@398 | 591 | } |
universe@398 | 592 | |
universe@481 | 593 | static size_t cx_pll_find( |
universe@489 | 594 | cx_list_s const *list, |
universe@489 | 595 | void const *elem |
universe@481 | 596 | ) { |
universe@480 | 597 | return cx_linked_list_find(((cx_linked_list *) list)->begin, |
universe@480 | 598 | CX_LL_LOC_NEXT, CX_LL_LOC_DATA, |
universe@487 | 599 | true, list->cmpfunc, elem); |
universe@466 | 600 | } |
universe@466 | 601 | |
universe@469 | 602 | static void cx_ll_sort(cx_list_s *list) { |
universe@469 | 603 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@469 | 604 | cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end, |
universe@469 | 605 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA, |
universe@487 | 606 | false, list->cmpfunc); |
universe@469 | 607 | } |
universe@469 | 608 | |
universe@469 | 609 | static void cx_pll_sort(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@487 | 613 | true, list->cmpfunc); |
universe@469 | 614 | } |
universe@469 | 615 | |
universe@490 | 616 | static void cx_ll_reverse(cx_list_s *list) { |
universe@490 | 617 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@490 | 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@489 | 622 | cx_list_s const *list, |
universe@489 | 623 | 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@488 | 629 | false, list->cmpfunc); |
universe@488 | 630 | } |
universe@488 | 631 | |
universe@488 | 632 | static int cx_pll_compare( |
universe@489 | 633 | cx_list_s const *list, |
universe@489 | 634 | cx_list_s const *other |
universe@488 | 635 | ) { |
universe@488 | 636 | cx_linked_list *left = (cx_linked_list *) list; |
universe@488 | 637 | cx_linked_list *right = (cx_linked_list *) other; |
universe@488 | 638 | return cx_linked_list_compare(left->begin, right->begin, |
universe@488 | 639 | CX_LL_LOC_NEXT, CX_LL_LOC_DATA, |
universe@488 | 640 | true, list->cmpfunc); |
universe@488 | 641 | } |
universe@488 | 642 | |
universe@451 | 643 | static cx_list_class cx_linked_list_class = { |
universe@398 | 644 | cx_ll_add, |
universe@398 | 645 | cx_ll_insert, |
universe@398 | 646 | cx_ll_remove, |
universe@439 | 647 | cx_ll_at, |
universe@404 | 648 | cx_ll_find, |
universe@488 | 649 | cx_ll_sort, |
universe@490 | 650 | cx_ll_compare, |
universe@490 | 651 | cx_ll_reverse |
universe@398 | 652 | }; |
universe@398 | 653 | |
universe@466 | 654 | static cx_list_class cx_pointer_linked_list_class = { |
universe@466 | 655 | cx_pll_add, |
universe@466 | 656 | cx_pll_insert, |
universe@466 | 657 | cx_ll_remove, |
universe@466 | 658 | cx_pll_at, |
universe@466 | 659 | cx_pll_find, |
universe@488 | 660 | cx_pll_sort, |
universe@490 | 661 | cx_pll_compare, |
universe@490 | 662 | cx_ll_reverse |
universe@466 | 663 | }; |
universe@466 | 664 | |
universe@481 | 665 | CxList cxLinkedListCreate( |
universe@481 | 666 | CxAllocator allocator, |
universe@481 | 667 | CxListComparator comparator, |
universe@481 | 668 | size_t item_size |
universe@481 | 669 | ) { |
universe@446 | 670 | cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); |
universe@398 | 671 | if (list == NULL) |
universe@398 | 672 | return NULL; |
universe@398 | 673 | |
universe@435 | 674 | list->base.cl = &cx_linked_list_class; |
universe@435 | 675 | list->base.allocator = allocator; |
universe@435 | 676 | list->base.cmpfunc = comparator; |
universe@435 | 677 | list->base.itemsize = item_size; |
universe@435 | 678 | list->base.capacity = SIZE_MAX; |
universe@441 | 679 | list->base.size = 0; |
universe@406 | 680 | |
universe@435 | 681 | list->begin = NULL; |
universe@441 | 682 | list->end = NULL; |
universe@406 | 683 | |
universe@441 | 684 | return (CxList) list; |
universe@406 | 685 | } |
universe@406 | 686 | |
universe@481 | 687 | CxList cxPointerLinkedListCreate( |
universe@481 | 688 | CxAllocator allocator, |
universe@481 | 689 | CxListComparator comparator |
universe@481 | 690 | ) { |
universe@466 | 691 | cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); |
universe@466 | 692 | if (list == NULL) |
universe@466 | 693 | return NULL; |
universe@466 | 694 | |
universe@466 | 695 | list->base.cl = &cx_pointer_linked_list_class; |
universe@466 | 696 | list->base.allocator = allocator; |
universe@466 | 697 | list->base.cmpfunc = comparator; |
universe@468 | 698 | list->base.itemsize = sizeof(void *); |
universe@466 | 699 | list->base.capacity = SIZE_MAX; |
universe@466 | 700 | list->base.size = 0; |
universe@466 | 701 | |
universe@466 | 702 | list->begin = NULL; |
universe@466 | 703 | list->end = NULL; |
universe@466 | 704 | |
universe@466 | 705 | return (CxList) list; |
universe@466 | 706 | } |
universe@466 | 707 | |
universe@488 | 708 | CxList cxLinkedListFromArray( |
universe@488 | 709 | CxAllocator allocator, |
universe@488 | 710 | CxListComparator comparator, |
universe@488 | 711 | size_t item_size, |
universe@488 | 712 | size_t num_items, |
universe@489 | 713 | void const *array |
universe@488 | 714 | ) { |
universe@488 | 715 | CxList list = cxLinkedListCreate(allocator, comparator, item_size); |
universe@488 | 716 | if (list == NULL) return NULL; |
universe@488 | 717 | for (size_t i = 0; i < num_items; i++) { |
universe@488 | 718 | if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) { |
universe@488 | 719 | cxLinkedListDestroy(list); |
universe@488 | 720 | return NULL; |
universe@488 | 721 | } |
universe@488 | 722 | } |
universe@488 | 723 | return list; |
universe@488 | 724 | } |
universe@488 | 725 | |
universe@409 | 726 | void cxLinkedListDestroy(CxList list) { |
universe@446 | 727 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@436 | 728 | |
universe@446 | 729 | cx_linked_list_node *node = ll->begin; |
universe@436 | 730 | while (node) { |
universe@446 | 731 | void *next = node->next; |
universe@436 | 732 | cxFree(list->allocator, node); |
universe@436 | 733 | node = next; |
universe@436 | 734 | } |
universe@436 | 735 | |
universe@435 | 736 | cxFree(list->allocator, list); |
universe@409 | 737 | } |