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