Thu, 18 Dec 2025 17:50:15 +0100
update ucx
| 174 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 28 | ||
| 29 | #include "cx/linked_list.h" | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
30 | #include "cx/compare.h" |
| 174 | 31 | #include <string.h> |
| 32 | #include <assert.h> | |
| 33 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
34 | #if __STDC_VERSION__ < 202311L |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
35 | // we cannot simply include stdalign.h |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
36 | // because Solaris is not entirely C11 complaint |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
37 | #ifndef __alignof_is_defined |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
38 | #define alignof _Alignof |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
39 | #define __alignof_is_defined 1 |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
40 | #endif |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
41 | #endif |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
42 | |
| 174 | 43 | // LOW LEVEL LINKED LIST FUNCTIONS |
| 44 | ||
| 45 | #define CX_LL_PTR(cur, off) (*(void**)(((char*)(cur))+(off))) | |
| 46 | #define ll_prev(node) CX_LL_PTR(node, loc_prev) | |
| 47 | #define ll_next(node) CX_LL_PTR(node, loc_next) | |
| 48 | #define ll_advance(node) CX_LL_PTR(node, loc_advance) | |
| 49 | #define ll_data(node) (((char*)(node))+loc_data) | |
| 50 | ||
| 51 | void *cx_linked_list_at( | |
| 324 | 52 | const void *start, |
| 174 | 53 | size_t start_index, |
| 54 | ptrdiff_t loc_advance, | |
| 55 | size_t index | |
| 56 | ) { | |
| 57 | assert(start != NULL); | |
| 58 | assert(loc_advance >= 0); | |
| 59 | size_t i = start_index; | |
| 324 | 60 | const void *cur = start; |
| 174 | 61 | while (i != index && cur != NULL) { |
| 62 | cur = ll_advance(cur); | |
| 63 | i < index ? i++ : i--; | |
| 64 | } | |
| 65 | return (void *) cur; | |
| 66 | } | |
| 67 | ||
| 1016 | 68 | void *cx_linked_list_find_c( |
| 324 | 69 | const void *start, |
| 174 | 70 | ptrdiff_t loc_advance, |
| 71 | ptrdiff_t loc_data, | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
72 | const void *elem, |
| 1016 | 73 | size_t *found_index, |
| 74 | cx_compare_func2 cmp_func, | |
| 75 | void *context | |
| 174 | 76 | ) { |
| 77 | assert(start != NULL); | |
| 78 | assert(loc_advance >= 0); | |
| 79 | assert(loc_data >= 0); | |
| 80 | assert(cmp_func); | |
| 81 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
82 | void *node = (void*) start; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
83 | size_t index = 0; |
| 174 | 84 | do { |
| 85 | void *current = ll_data(node); | |
| 1016 | 86 | if (cmp_func(current, elem, context) == 0) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
87 | if (found_index != NULL) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
88 | *found_index = index; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
89 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
90 | return node; |
| 174 | 91 | } |
| 92 | node = ll_advance(node); | |
| 93 | index++; | |
| 94 | } while (node != NULL); | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
95 | return NULL; |
| 174 | 96 | } |
| 97 | ||
| 1016 | 98 | void *cx_linked_list_find( |
| 99 | const void *start, | |
| 100 | ptrdiff_t loc_advance, | |
| 101 | ptrdiff_t loc_data, | |
| 102 | const void *elem, | |
| 103 | size_t *found_index, | |
| 104 | cx_compare_func cmp_func | |
| 105 | ) { | |
| 106 | cx_compare_func_wrapper wrapper = {cmp_func}; | |
| 107 | return cx_linked_list_find_c(start, loc_advance, loc_data, | |
| 108 | elem, found_index, cx_acmp_wrap, &wrapper); | |
| 109 | } | |
| 110 | ||
| 174 | 111 | void *cx_linked_list_first( |
| 324 | 112 | const void *node, |
| 174 | 113 | ptrdiff_t loc_prev |
| 114 | ) { | |
| 115 | return cx_linked_list_last(node, loc_prev); | |
| 116 | } | |
| 117 | ||
| 118 | void *cx_linked_list_last( | |
| 324 | 119 | const void *node, |
| 174 | 120 | ptrdiff_t loc_next |
| 121 | ) { | |
| 122 | assert(node != NULL); | |
| 123 | assert(loc_next >= 0); | |
| 124 | ||
| 324 | 125 | const void *cur = node; |
| 126 | const void *last; | |
| 174 | 127 | do { |
| 128 | last = cur; | |
| 129 | } while ((cur = ll_next(cur)) != NULL); | |
| 130 | ||
| 131 | return (void *) last; | |
| 132 | } | |
| 133 | ||
| 134 | void *cx_linked_list_prev( | |
| 324 | 135 | const void *begin, |
| 174 | 136 | ptrdiff_t loc_next, |
| 324 | 137 | const void *node |
| 174 | 138 | ) { |
| 139 | assert(begin != NULL); | |
| 140 | assert(node != NULL); | |
| 141 | assert(loc_next >= 0); | |
| 142 | if (begin == node) return NULL; | |
| 324 | 143 | const void *cur = begin; |
| 144 | const void *next; | |
| 174 | 145 | while (1) { |
| 146 | next = ll_next(cur); | |
| 147 | if (next == node) return (void *) cur; | |
| 148 | cur = next; | |
| 149 | } | |
| 150 | } | |
| 151 | ||
| 152 | void cx_linked_list_link( | |
| 153 | void *left, | |
| 154 | void *right, | |
| 155 | ptrdiff_t loc_prev, | |
| 156 | ptrdiff_t loc_next | |
| 157 | ) { | |
| 158 | assert(loc_next >= 0); | |
| 159 | ll_next(left) = right; | |
| 160 | if (loc_prev >= 0) { | |
| 161 | ll_prev(right) = left; | |
| 162 | } | |
| 163 | } | |
| 164 | ||
| 165 | void cx_linked_list_unlink( | |
| 166 | void *left, | |
| 167 | void *right, | |
| 168 | ptrdiff_t loc_prev, | |
| 169 | ptrdiff_t loc_next | |
| 170 | ) { | |
| 171 | assert (loc_next >= 0); | |
| 172 | assert(ll_next(left) == right); | |
| 173 | ll_next(left) = NULL; | |
| 174 | if (loc_prev >= 0) { | |
| 175 | assert(ll_prev(right) == left); | |
| 176 | ll_prev(right) = NULL; | |
| 177 | } | |
| 178 | } | |
| 179 | ||
| 180 | void cx_linked_list_add( | |
| 181 | void **begin, | |
| 182 | void **end, | |
| 183 | ptrdiff_t loc_prev, | |
| 184 | ptrdiff_t loc_next, | |
| 185 | void *new_node | |
| 186 | ) { | |
| 187 | void *last; | |
| 188 | if (end == NULL) { | |
| 189 | assert(begin != NULL); | |
| 190 | last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next); | |
| 191 | } else { | |
| 192 | last = *end; | |
| 193 | } | |
| 194 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node); | |
| 195 | } | |
| 196 | ||
| 197 | void cx_linked_list_prepend( | |
| 198 | void **begin, | |
| 199 | void **end, | |
| 200 | ptrdiff_t loc_prev, | |
| 201 | ptrdiff_t loc_next, | |
| 202 | void *new_node | |
| 203 | ) { | |
| 204 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node); | |
| 205 | } | |
| 206 | ||
| 207 | void cx_linked_list_insert( | |
| 208 | void **begin, | |
| 209 | void **end, | |
| 210 | ptrdiff_t loc_prev, | |
| 211 | ptrdiff_t loc_next, | |
| 212 | void *node, | |
| 213 | void *new_node | |
| 214 | ) { | |
| 215 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node); | |
| 216 | } | |
| 217 | ||
| 218 | void cx_linked_list_insert_chain( | |
| 219 | void **begin, | |
| 220 | void **end, | |
| 221 | ptrdiff_t loc_prev, | |
| 222 | ptrdiff_t loc_next, | |
| 223 | void *node, | |
| 224 | void *insert_begin, | |
| 225 | void *insert_end | |
| 226 | ) { | |
| 227 | // find the end of the chain, if not specified | |
| 228 | if (insert_end == NULL) { | |
| 229 | insert_end = cx_linked_list_last(insert_begin, loc_next); | |
| 230 | } | |
| 231 | ||
| 232 | // determine the successor | |
| 233 | void *successor; | |
| 234 | if (node == NULL) { | |
| 235 | assert(begin != NULL || (end != NULL && loc_prev >= 0)); | |
| 236 | if (begin != NULL) { | |
| 237 | successor = *begin; | |
| 238 | *begin = insert_begin; | |
| 239 | } else { | |
| 240 | successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev); | |
| 241 | } | |
| 242 | } else { | |
| 243 | successor = ll_next(node); | |
| 244 | cx_linked_list_link(node, insert_begin, loc_prev, loc_next); | |
| 245 | } | |
| 246 | ||
| 247 | if (successor == NULL) { | |
| 248 | // the list ends with the new chain | |
| 249 | if (end != NULL) { | |
| 250 | *end = insert_end; | |
| 251 | } | |
| 252 | } else { | |
| 253 | cx_linked_list_link(insert_end, successor, loc_prev, loc_next); | |
| 254 | } | |
| 255 | } | |
| 256 | ||
| 324 | 257 | void cx_linked_list_insert_sorted( |
| 258 | void **begin, | |
| 259 | void **end, | |
| 260 | ptrdiff_t loc_prev, | |
| 261 | ptrdiff_t loc_next, | |
| 262 | void *new_node, | |
| 263 | cx_compare_func cmp_func | |
| 264 | ) { | |
| 265 | assert(ll_next(new_node) == NULL); | |
| 266 | cx_linked_list_insert_sorted_chain( | |
| 267 | begin, end, loc_prev, loc_next, new_node, cmp_func); | |
| 268 | } | |
| 269 | ||
| 845 | 270 | static void *cx_linked_list_insert_sorted_chain_impl( |
| 271 | void **begin, | |
| 272 | void **end, | |
| 273 | ptrdiff_t loc_prev, | |
| 274 | ptrdiff_t loc_next, | |
| 275 | void *insert_begin, | |
| 1016 | 276 | cx_compare_func2 cmp_func, |
| 277 | void *context, | |
| 845 | 278 | bool allow_duplicates |
| 279 | ) { | |
| 280 | assert(begin != NULL); | |
| 281 | assert(loc_next >= 0); | |
| 282 | assert(insert_begin != NULL); | |
| 283 | ||
| 284 | // strategy: build completely new chains from scratch | |
| 285 | void *source_original = *begin; | |
| 286 | void *source_argument = insert_begin; | |
| 287 | void *new_begin = NULL; | |
| 288 | void *new_end = NULL; | |
| 289 | void *dup_begin = NULL; | |
| 290 | void *dup_end = NULL; | |
| 291 | ||
| 292 | // determine the new start | |
| 293 | { | |
| 1016 | 294 | int d = source_original == NULL ? 1 : cmp_func(source_original, source_argument, context); |
| 845 | 295 | if (d <= 0) { |
| 296 | // the new chain starts with the original chain | |
| 297 | new_begin = new_end = source_original; | |
| 298 | source_original = ll_next(source_original); | |
| 299 | if (d == 0) { | |
| 300 | if (allow_duplicates) { | |
| 301 | // duplicate allowed, append it to the chain | |
| 302 | cx_linked_list_link(new_end, source_argument, loc_prev, loc_next); | |
| 303 | new_end = source_argument; | |
| 304 | } else { | |
| 305 | // duplicate is not allowed, start a duplicate chain with the argument | |
| 306 | dup_begin = dup_end = source_argument; | |
| 307 | } | |
| 308 | source_argument = ll_next(source_argument); | |
| 309 | } | |
| 310 | } else { | |
| 311 | // input is smaller, or there is no original chain; | |
| 312 | // start the new chain with the source argument | |
| 313 | new_begin = new_end = source_argument; | |
| 314 | source_argument = ll_next(source_argument); | |
| 315 | } | |
| 316 | } | |
| 317 | ||
| 318 | // now successively compare the elements and add them to the correct chains | |
| 319 | while (source_original != NULL && source_argument != NULL) { | |
| 1016 | 320 | int d = cmp_func(source_original, source_argument, context); |
| 845 | 321 | if (d <= 0) { |
| 322 | // the original is not larger, add it to the chain | |
| 323 | cx_linked_list_link(new_end, source_original, loc_prev, loc_next); | |
| 324 | new_end = source_original; | |
| 325 | source_original = ll_next(source_original); | |
| 326 | if (d == 0) { | |
| 327 | if (allow_duplicates) { | |
| 328 | // duplicate allowed, append it to the chain | |
| 329 | cx_linked_list_link(new_end, source_argument, loc_prev, loc_next); | |
| 330 | new_end = source_argument; | |
| 331 | } else { | |
| 332 | // duplicate is not allowed, append it to the duplicate chain | |
| 333 | if (dup_end == NULL) { | |
| 334 | dup_begin = dup_end = source_argument; | |
| 335 | } else { | |
| 336 | cx_linked_list_link(dup_end, source_argument, loc_prev, loc_next); | |
| 337 | dup_end = source_argument; | |
| 338 | } | |
| 339 | } | |
| 340 | source_argument = ll_next(source_argument); | |
| 341 | } | |
| 342 | } else { | |
| 343 | // the original is larger, append the source argument to the chain | |
| 344 | // check if we must discard the source argument as duplicate | |
| 1016 | 345 | if (!allow_duplicates && cmp_func(new_end, source_argument, context) == 0) { |
| 845 | 346 | if (dup_end == NULL) { |
| 347 | dup_begin = dup_end = source_argument; | |
| 348 | } else { | |
| 349 | cx_linked_list_link(dup_end, source_argument, loc_prev, loc_next); | |
| 350 | dup_end = source_argument; | |
| 351 | } | |
| 352 | } else { | |
| 353 | // no duplicate or duplicates allowed | |
| 354 | cx_linked_list_link(new_end, source_argument, loc_prev, loc_next); | |
| 355 | new_end = source_argument; | |
| 356 | } | |
| 357 | source_argument = ll_next(source_argument); | |
| 358 | } | |
| 359 | } | |
| 360 | ||
| 361 | if (source_original != NULL) { | |
| 362 | // something is left from the original chain, append it | |
| 363 | cx_linked_list_link(new_end, source_original, loc_prev, loc_next); | |
| 364 | new_end = cx_linked_list_last(source_original, loc_next); | |
| 365 | } else if (source_argument != NULL) { | |
| 366 | // something is left from the input chain; | |
| 367 | // when we allow duplicates, append it | |
| 368 | if (allow_duplicates) { | |
| 369 | cx_linked_list_link(new_end, source_argument, loc_prev, loc_next); | |
| 370 | new_end = cx_linked_list_last(source_argument, loc_next); | |
| 371 | } else { | |
| 372 | // otherwise we must check one-by-one | |
| 373 | while (source_argument != NULL) { | |
| 1016 | 374 | if (cmp_func(new_end, source_argument, context) == 0) { |
| 845 | 375 | if (dup_end == NULL) { |
| 376 | dup_begin = dup_end = source_argument; | |
| 377 | } else { | |
| 378 | cx_linked_list_link(dup_end, source_argument, loc_prev, loc_next); | |
| 379 | dup_end = source_argument; | |
| 380 | } | |
| 381 | } else { | |
| 382 | cx_linked_list_link(new_end, source_argument, loc_prev, loc_next); | |
| 383 | new_end = source_argument; | |
| 384 | } | |
| 385 | source_argument = ll_next(source_argument); | |
| 386 | } | |
| 387 | } | |
| 388 | } | |
| 389 | ||
| 390 | // null the next pointers at the end of the chain | |
| 391 | ll_next(new_end) = NULL; | |
| 392 | if (dup_end != NULL) { | |
| 393 | ll_next(dup_end) = NULL; | |
| 394 | } | |
| 395 | ||
| 396 | // null the optional prev pointers | |
| 397 | if (loc_prev >= 0) { | |
| 398 | ll_prev(new_begin) = NULL; | |
| 399 | if (dup_begin != NULL) { | |
| 400 | ll_prev(dup_begin) = NULL; | |
| 401 | } | |
| 402 | } | |
| 403 | ||
| 404 | // output | |
| 405 | *begin = new_begin; | |
| 406 | if (end != NULL) { | |
| 407 | *end = new_end; | |
| 408 | } | |
| 409 | return dup_begin; | |
| 410 | } | |
| 411 | ||
| 324 | 412 | void cx_linked_list_insert_sorted_chain( |
| 413 | void **begin, | |
| 414 | void **end, | |
| 415 | ptrdiff_t loc_prev, | |
| 416 | ptrdiff_t loc_next, | |
| 417 | void *insert_begin, | |
| 418 | cx_compare_func cmp_func | |
| 419 | ) { | |
| 1016 | 420 | cx_compare_func_wrapper wrapper = {cmp_func}; |
| 845 | 421 | cx_linked_list_insert_sorted_chain_impl( |
| 422 | begin, end, loc_prev, loc_next, | |
| 1016 | 423 | insert_begin, cx_acmp_wrap, &wrapper, true); |
| 845 | 424 | } |
| 324 | 425 | |
| 845 | 426 | int cx_linked_list_insert_unique( |
| 427 | void **begin, | |
| 428 | void **end, | |
| 429 | ptrdiff_t loc_prev, | |
| 430 | ptrdiff_t loc_next, | |
| 431 | void *new_node, | |
| 432 | cx_compare_func cmp_func | |
| 433 | ) { | |
| 434 | assert(ll_next(new_node) == NULL); | |
| 435 | return NULL != cx_linked_list_insert_unique_chain( | |
| 436 | begin, end, loc_prev, loc_next, new_node, cmp_func); | |
| 437 | } | |
| 324 | 438 | |
| 845 | 439 | void *cx_linked_list_insert_unique_chain( |
| 440 | void **begin, | |
| 441 | void **end, | |
| 442 | ptrdiff_t loc_prev, | |
| 443 | ptrdiff_t loc_next, | |
| 444 | void *insert_begin, | |
| 445 | cx_compare_func cmp_func | |
| 446 | ) { | |
| 1016 | 447 | cx_compare_func_wrapper wrapper = {cmp_func}; |
| 845 | 448 | return cx_linked_list_insert_sorted_chain_impl( |
| 449 | begin, end, loc_prev, loc_next, | |
| 1016 | 450 | insert_begin, cx_acmp_wrap, &wrapper, false); |
| 324 | 451 | } |
| 452 | ||
| 440 | 453 | size_t cx_linked_list_remove_chain( |
| 174 | 454 | void **begin, |
| 455 | void **end, | |
| 456 | ptrdiff_t loc_prev, | |
| 457 | ptrdiff_t loc_next, | |
| 440 | 458 | void *node, |
| 459 | size_t num | |
| 174 | 460 | ) { |
| 461 | assert(node != NULL); | |
| 462 | assert(loc_next >= 0); | |
| 463 | assert(loc_prev >= 0 || begin != NULL); | |
| 464 | ||
| 440 | 465 | // easy exit |
| 466 | if (num == 0) return 0; | |
| 467 | ||
| 174 | 468 | // find adjacent nodes |
| 469 | void *prev; | |
| 470 | if (loc_prev >= 0) { | |
| 471 | prev = ll_prev(node); | |
| 472 | } else { | |
| 473 | prev = cx_linked_list_prev(*begin, loc_next, node); | |
| 474 | } | |
| 475 | ||
| 440 | 476 | void *next = ll_next(node); |
| 477 | size_t removed = 1; | |
| 478 | for (; removed < num && next != NULL ; removed++) { | |
| 479 | next = ll_next(next); | |
| 480 | } | |
| 481 | ||
| 174 | 482 | // update next pointer of prev node, or set begin |
| 483 | if (prev == NULL) { | |
| 484 | if (begin != NULL) { | |
| 485 | *begin = next; | |
| 486 | } | |
| 487 | } else { | |
| 488 | ll_next(prev) = next; | |
| 489 | } | |
| 490 | ||
| 491 | // update prev pointer of next node, or set end | |
| 492 | if (next == NULL) { | |
| 493 | if (end != NULL) { | |
| 494 | *end = prev; | |
| 495 | } | |
| 496 | } else if (loc_prev >= 0) { | |
| 497 | ll_prev(next) = prev; | |
| 498 | } | |
| 440 | 499 | |
| 500 | return removed; | |
| 174 | 501 | } |
| 502 | ||
| 870 | 503 | void cx_linked_list_remove( |
| 504 | void **begin, | |
| 505 | void **end, | |
| 506 | ptrdiff_t loc_prev, | |
| 507 | ptrdiff_t loc_next, | |
| 508 | void *node | |
| 509 | ) { | |
| 510 | cx_linked_list_remove_chain(begin, end, loc_prev, loc_next, node, 1); | |
| 511 | } | |
| 512 | ||
| 174 | 513 | size_t cx_linked_list_size( |
| 324 | 514 | const void *node, |
| 174 | 515 | ptrdiff_t loc_next |
| 516 | ) { | |
| 517 | assert(loc_next >= 0); | |
| 518 | size_t size = 0; | |
| 519 | while (node != NULL) { | |
| 520 | node = ll_next(node); | |
| 521 | size++; | |
| 522 | } | |
| 523 | return size; | |
| 524 | } | |
| 525 | ||
| 526 | #ifndef CX_LINKED_LIST_SORT_SBO_SIZE | |
| 527 | #define CX_LINKED_LIST_SORT_SBO_SIZE 1024 | |
| 528 | #endif | |
| 529 | ||
| 530 | static void cx_linked_list_sort_merge( | |
| 1016 | 531 | void **begin, |
| 532 | void **end, | |
| 174 | 533 | ptrdiff_t loc_prev, |
| 534 | ptrdiff_t loc_next, | |
| 535 | ptrdiff_t loc_data, | |
| 536 | size_t length, | |
| 537 | void *ls, | |
| 538 | void *le, | |
| 539 | void *re, | |
| 1016 | 540 | cx_compare_func2 cmp_func, |
| 541 | void *context | |
| 174 | 542 | ) { |
| 543 | void *sbo[CX_LINKED_LIST_SORT_SBO_SIZE]; | |
| 544 | void **sorted = length >= CX_LINKED_LIST_SORT_SBO_SIZE ? | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
545 | cxMallocDefault(sizeof(void *) * length) : sbo; |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
546 | if (sorted == NULL) abort(); // LCOV_EXCL_LINE |
| 174 | 547 | void *rc, *lc; |
| 548 | ||
| 549 | lc = ls; | |
| 550 | rc = le; | |
| 551 | size_t n = 0; | |
| 552 | while (lc && lc != le && rc != re) { | |
| 1016 | 553 | if (cmp_func(ll_data(lc), ll_data(rc), context) <= 0) { |
| 174 | 554 | sorted[n] = lc; |
| 555 | lc = ll_next(lc); | |
| 556 | } else { | |
| 557 | sorted[n] = rc; | |
| 558 | rc = ll_next(rc); | |
| 559 | } | |
| 560 | n++; | |
| 561 | } | |
| 562 | while (lc && lc != le) { | |
| 563 | sorted[n] = lc; | |
| 564 | lc = ll_next(lc); | |
| 565 | n++; | |
| 566 | } | |
| 567 | while (rc && rc != re) { | |
| 568 | sorted[n] = rc; | |
| 569 | rc = ll_next(rc); | |
| 570 | n++; | |
| 571 | } | |
| 572 | ||
| 573 | // Update pointer | |
| 574 | if (loc_prev >= 0) ll_prev(sorted[0]) = NULL; | |
| 440 | 575 | for (size_t i = 0 ; i < length - 1; i++) { |
| 174 | 576 | cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next); |
| 577 | } | |
| 578 | ll_next(sorted[length - 1]) = NULL; | |
| 579 | ||
| 580 | *begin = sorted[0]; | |
| 440 | 581 | *end = sorted[length - 1]; |
| 174 | 582 | if (sorted != sbo) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
583 | cxFreeDefault(sorted); |
| 174 | 584 | } |
| 585 | } | |
| 586 | ||
| 1016 | 587 | void cx_linked_list_sort_c( // NOLINT(misc-no-recursion) - purposely recursive function |
| 174 | 588 | void **begin, |
| 589 | void **end, | |
| 590 | ptrdiff_t loc_prev, | |
| 591 | ptrdiff_t loc_next, | |
| 592 | ptrdiff_t loc_data, | |
| 1016 | 593 | cx_compare_func2 cmp_func, |
| 594 | void *context | |
| 174 | 595 | ) { |
| 596 | assert(begin != NULL); | |
| 597 | assert(loc_next >= 0); | |
| 598 | assert(loc_data >= 0); | |
| 599 | assert(cmp_func); | |
| 600 | ||
| 601 | void *lc, *ls, *le, *re; | |
| 602 | ||
| 603 | // set start node | |
| 604 | ls = *begin; | |
| 605 | ||
| 606 | // early exit when this list is empty | |
| 607 | if (ls == NULL) return; | |
| 608 | ||
| 609 | // check how many elements are already sorted | |
| 610 | lc = ls; | |
| 611 | size_t ln = 1; | |
| 1016 | 612 | while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc), context) > 0) { |
| 174 | 613 | lc = ll_next(lc); |
| 614 | ln++; | |
| 615 | } | |
| 616 | le = ll_next(lc); | |
| 617 | ||
| 618 | // if first unsorted node is NULL, the list is already completely sorted | |
| 619 | if (le != NULL) { | |
| 620 | void *rc; | |
| 621 | size_t rn = 1; | |
| 622 | rc = le; | |
| 623 | // skip already sorted elements | |
| 1016 | 624 | while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc), context) > 0) { |
| 174 | 625 | rc = ll_next(rc); |
| 626 | rn++; | |
| 627 | } | |
| 628 | re = ll_next(rc); | |
| 629 | ||
| 630 | // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them | |
| 631 | void *sorted_begin, *sorted_end; | |
| 1016 | 632 | cx_linked_list_sort_merge(&sorted_begin, &sorted_end, |
| 633 | loc_prev, loc_next, loc_data, | |
| 174 | 634 | ln + rn, ls, le, re, cmp_func, |
| 1016 | 635 | context); |
| 174 | 636 | |
| 637 | // Something left? Sort it! | |
| 638 | size_t remainder_length = cx_linked_list_size(re, loc_next); | |
| 639 | if (remainder_length > 0) { | |
| 640 | void *remainder = re; | |
| 1016 | 641 | cx_linked_list_sort_c(&remainder, NULL, loc_prev, loc_next, loc_data, cmp_func, context); |
| 174 | 642 | |
| 643 | // merge sorted list with (also sorted) remainder | |
| 1016 | 644 | cx_linked_list_sort_merge(&sorted_begin, &sorted_end, |
| 645 | loc_prev, loc_next, loc_data, | |
| 174 | 646 | ln + rn + remainder_length, |
| 647 | sorted_begin, remainder, NULL, cmp_func, | |
| 1016 | 648 | context); |
| 174 | 649 | } |
| 650 | *begin = sorted_begin; | |
| 651 | if (end) *end = sorted_end; | |
| 652 | } | |
| 653 | } | |
| 654 | ||
| 1016 | 655 | void cx_linked_list_sort( |
| 656 | void **begin, | |
| 657 | void **end, | |
| 658 | ptrdiff_t loc_prev, | |
| 659 | ptrdiff_t loc_next, | |
| 660 | ptrdiff_t loc_data, | |
| 661 | cx_compare_func cmp_func | |
| 662 | ) { | |
| 663 | cx_compare_func_wrapper wrapper = {cmp_func}; | |
| 664 | cx_linked_list_sort_c(begin, end, loc_prev, loc_next, loc_data, cx_acmp_wrap, &wrapper); | |
| 665 | } | |
| 666 | ||
| 667 | int cx_linked_list_compare_c( | |
| 668 | const void *begin_left, | |
| 669 | const void *begin_right, | |
| 670 | ptrdiff_t loc_advance, | |
| 671 | ptrdiff_t loc_data, | |
| 672 | cx_compare_func2 cmp_func, | |
| 673 | void *context | |
| 674 | ) { | |
| 675 | const void *left = begin_left, *right = begin_right; | |
| 676 | ||
| 677 | while (left != NULL && right != NULL) { | |
| 678 | const void *left_data = ll_data(left); | |
| 679 | const void *right_data = ll_data(right); | |
| 680 | int result = cmp_func(left_data, right_data, context); | |
| 681 | if (result != 0) return result; | |
| 682 | left = ll_advance(left); | |
| 683 | right = ll_advance(right); | |
| 684 | } | |
| 685 | ||
| 686 | if (left != NULL) { return 1; } | |
| 687 | else if (right != NULL) { return -1; } | |
| 688 | else { return 0; } | |
| 689 | } | |
| 690 | ||
| 174 | 691 | int cx_linked_list_compare( |
| 324 | 692 | const void *begin_left, |
| 693 | const void *begin_right, | |
| 174 | 694 | ptrdiff_t loc_advance, |
| 695 | ptrdiff_t loc_data, | |
| 696 | cx_compare_func cmp_func | |
| 697 | ) { | |
| 1016 | 698 | cx_compare_func_wrapper wrapper = {cmp_func}; |
| 699 | return cx_linked_list_compare_c(begin_left, begin_right, | |
| 700 | loc_advance, loc_data, cx_acmp_wrap, &wrapper); | |
| 174 | 701 | } |
| 702 | ||
| 703 | void cx_linked_list_reverse( | |
| 704 | void **begin, | |
| 705 | void **end, | |
| 706 | ptrdiff_t loc_prev, | |
| 707 | ptrdiff_t loc_next | |
| 708 | ) { | |
| 709 | assert(begin != NULL); | |
| 710 | assert(loc_next >= 0); | |
| 711 | ||
| 712 | // swap all links | |
| 713 | void *prev = NULL; | |
| 714 | void *cur = *begin; | |
| 715 | while (cur != NULL) { | |
| 716 | void *next = ll_next(cur); | |
| 717 | ||
| 718 | ll_next(cur) = prev; | |
| 719 | if (loc_prev >= 0) { | |
| 720 | ll_prev(cur) = next; | |
| 721 | } | |
| 722 | ||
| 723 | prev = cur; | |
| 724 | cur = next; | |
| 725 | } | |
| 726 | ||
| 727 | // update begin and end | |
| 728 | if (end != NULL) { | |
| 729 | *end = *begin; | |
| 730 | } | |
| 731 | *begin = prev; | |
| 732 | } | |
| 733 | ||
| 734 | // HIGH LEVEL LINKED LIST IMPLEMENTATION | |
| 735 | ||
| 845 | 736 | static void *cx_ll_node_at( |
| 324 | 737 | const cx_linked_list *list, |
| 174 | 738 | size_t index |
| 739 | ) { | |
| 324 | 740 | if (index >= list->base.collection.size) { |
| 174 | 741 | return NULL; |
| 324 | 742 | } else if (index > list->base.collection.size / 2) { |
| 845 | 743 | return cx_linked_list_at(list->end, list->base.collection.size - 1, list->loc_prev, index); |
| 174 | 744 | } else { |
| 845 | 745 | return cx_linked_list_at(list->begin, 0, list->loc_next, index); |
| 174 | 746 | } |
| 747 | } | |
| 748 | ||
| 845 | 749 | static void *cx_ll_malloc_node(const cx_linked_list *list) { |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
750 | size_t n; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
751 | if (list->extra_data_len == 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
752 | n = list->loc_data + list->base.collection.elem_size; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
753 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
754 | n = list->loc_extra + list->extra_data_len; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
755 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
756 | return cxZalloc(list->base.collection.allocator, n); |
| 324 | 757 | } |
| 758 | ||
| 174 | 759 | static int cx_ll_insert_at( |
| 760 | struct cx_list_s *list, | |
| 845 | 761 | void *node, |
| 324 | 762 | const void *elem |
| 174 | 763 | ) { |
| 845 | 764 | cx_linked_list *ll = (cx_linked_list *) list; |
| 174 | 765 | |
| 766 | // create the new new_node | |
| 845 | 767 | void *new_node = cx_ll_malloc_node(ll); |
| 174 | 768 | |
| 769 | // sortir if failed | |
| 770 | if (new_node == NULL) return 1; | |
| 771 | ||
| 845 | 772 | // copy the data |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
773 | if (elem != NULL) { |
| 845 | 774 | memcpy((char*)new_node + ll->loc_data, elem, list->collection.elem_size); |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
775 | } |
| 174 | 776 | |
| 777 | // insert | |
| 778 | cx_linked_list_insert_chain( | |
| 845 | 779 | &ll->begin, &ll->end, |
| 780 | ll->loc_prev, ll->loc_next, | |
| 174 | 781 | node, new_node, new_node |
| 782 | ); | |
| 783 | ||
| 784 | // increase the size and return | |
| 324 | 785 | list->collection.size++; |
| 174 | 786 | return 0; |
| 787 | } | |
| 788 | ||
| 789 | static size_t cx_ll_insert_array( | |
| 790 | struct cx_list_s *list, | |
| 791 | size_t index, | |
| 324 | 792 | const void *array, |
| 174 | 793 | size_t n |
| 794 | ) { | |
| 795 | // out-of bounds and corner case check | |
| 324 | 796 | if (index > list->collection.size || n == 0) return 0; |
| 174 | 797 | |
| 798 | // find position efficiently | |
| 845 | 799 | void *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1); |
| 174 | 800 | |
| 801 | // perform first insert | |
| 440 | 802 | if (0 != cx_ll_insert_at(list, node, array)) return 1; |
| 174 | 803 | |
| 804 | // is there more? | |
| 805 | if (n == 1) return 1; | |
| 806 | ||
| 807 | // we now know exactly where we are | |
| 845 | 808 | cx_linked_list *ll = (cx_linked_list *) list; |
| 809 | node = node == NULL ? ((cx_linked_list *) list)->begin : CX_LL_PTR(node, ll->loc_next); | |
| 174 | 810 | |
| 324 | 811 | // we can add the remaining nodes and immediately advance to the inserted node |
| 812 | const char *source = array; | |
| 174 | 813 | for (size_t i = 1; i < n; i++) { |
| 870 | 814 | if (source != NULL) { |
| 815 | source += list->collection.elem_size; | |
| 816 | } | |
| 440 | 817 | if (0 != cx_ll_insert_at(list, node, source)) return i; |
| 845 | 818 | node = CX_LL_PTR(node, ll->loc_next); |
| 174 | 819 | } |
| 820 | return n; | |
| 821 | } | |
| 822 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
823 | static void *cx_ll_insert_element( |
| 174 | 824 | struct cx_list_s *list, |
| 825 | size_t index, | |
| 324 | 826 | const void *element |
| 174 | 827 | ) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
828 | // out-of-bounds check |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
829 | if (index > list->collection.size) return NULL; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
830 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
831 | // find position efficiently |
| 845 | 832 | void *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1); |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
833 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
834 | // perform first insert |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
835 | if (cx_ll_insert_at(list, node, element)) return NULL; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
836 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
837 | // return a pointer to the data of the inserted node |
| 845 | 838 | cx_linked_list *ll = (cx_linked_list *) list; |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
839 | if (node == NULL) { |
| 845 | 840 | return (char*)(ll->begin) + ll->loc_data; |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
841 | } else { |
| 845 | 842 | char *next = CX_LL_PTR(node, ll->loc_next); |
| 843 | return next + ll->loc_data; | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
844 | } |
| 174 | 845 | } |
| 846 | ||
| 1016 | 847 | static int cx_ll_insert_sorted_cmp_helper(const void *l, const void *r, void *c) { |
| 848 | cx_linked_list *list = c; | |
| 849 | const char *left = (const char*)l + list->loc_data; | |
| 850 | const char *right = (const char*)r + list->loc_data; | |
| 851 | return cx_list_compare_wrapper(left, right, list); | |
| 845 | 852 | } |
| 853 | ||
| 854 | static size_t cx_ll_insert_sorted_impl( | |
| 855 | struct cx_list_s *list, | |
| 856 | const void *array, | |
| 857 | size_t n, | |
| 858 | bool allow_duplicates | |
| 859 | ) { | |
| 860 | cx_linked_list *ll = (cx_linked_list *) list; | |
| 861 | ||
| 862 | // special case | |
| 863 | if (n == 0) return 0; | |
| 864 | ||
| 865 | // create a new chain of nodes | |
| 866 | void *chain = cx_ll_malloc_node(ll); | |
| 867 | if (chain == NULL) return 0; | |
| 868 | ||
| 869 | memcpy((char*)chain + ll->loc_data, array, list->collection.elem_size); | |
| 870 | ||
| 871 | // add all elements from the array to that chain | |
| 872 | void *prev = chain; | |
| 873 | const char *src = array; | |
| 874 | size_t inserted = 1; | |
| 875 | for (; inserted < n; inserted++) { | |
| 876 | void *next = cx_ll_malloc_node(ll); | |
| 877 | if (next == NULL) break; | |
| 878 | src += list->collection.elem_size; | |
| 879 | memcpy((char*)next + ll->loc_data, src, list->collection.elem_size); | |
| 880 | CX_LL_PTR(prev, ll->loc_next) = next; | |
| 881 | CX_LL_PTR(next, ll->loc_prev) = prev; | |
| 882 | prev = next; | |
| 883 | } | |
| 884 | CX_LL_PTR(prev, ll->loc_next) = NULL; | |
| 885 | ||
| 1016 | 886 | // invoke the low-level function |
| 887 | void *duplicates = cx_linked_list_insert_sorted_chain_impl( | |
| 888 | &ll->begin, | |
| 889 | &ll->end, | |
| 890 | ll->loc_prev, | |
| 891 | ll->loc_next, | |
| 892 | chain, | |
| 893 | cx_ll_insert_sorted_cmp_helper, | |
| 894 | list, | |
| 895 | allow_duplicates | |
| 896 | ); | |
| 897 | list->collection.size += inserted; | |
| 898 | if (!allow_duplicates) { | |
| 845 | 899 | // free the nodes that did not make it into the list |
| 900 | while (duplicates != NULL) { | |
| 901 | void *next = CX_LL_PTR(duplicates, ll->loc_next); | |
| 902 | cxFree(list->collection.allocator, duplicates); | |
| 903 | duplicates = next; | |
| 904 | list->collection.size--; | |
| 905 | } | |
| 906 | } | |
| 907 | ||
| 908 | return inserted; | |
| 324 | 909 | } |
| 910 | ||
| 911 | static size_t cx_ll_insert_sorted( | |
| 912 | struct cx_list_s *list, | |
| 913 | const void *array, | |
| 914 | size_t n | |
| 915 | ) { | |
| 845 | 916 | return cx_ll_insert_sorted_impl(list, array, n, true); |
| 917 | } | |
| 324 | 918 | |
| 845 | 919 | static size_t cx_ll_insert_unique( |
| 920 | struct cx_list_s *list, | |
| 921 | const void *array, | |
| 922 | size_t n | |
| 923 | ) { | |
| 924 | return cx_ll_insert_sorted_impl(list, array, n, false); | |
| 324 | 925 | } |
| 926 | ||
| 440 | 927 | static size_t cx_ll_remove( |
| 174 | 928 | struct cx_list_s *list, |
| 440 | 929 | size_t index, |
| 930 | size_t num, | |
| 931 | void *targetbuf | |
| 174 | 932 | ) { |
| 933 | cx_linked_list *ll = (cx_linked_list *) list; | |
| 845 | 934 | void *node = cx_ll_node_at(ll, index); |
| 174 | 935 | |
| 936 | // out-of-bounds check | |
| 440 | 937 | if (node == NULL) return 0; |
| 174 | 938 | |
| 939 | // remove | |
| 440 | 940 | size_t removed = cx_linked_list_remove_chain( |
| 941 | (void **) &ll->begin, | |
| 942 | (void **) &ll->end, | |
| 845 | 943 | ll->loc_prev, |
| 944 | ll->loc_next, | |
| 440 | 945 | node, |
| 946 | num | |
| 947 | ); | |
| 174 | 948 | |
| 949 | // adjust size | |
| 440 | 950 | list->collection.size -= removed; |
| 951 | ||
| 952 | // copy or destroy the removed chain | |
| 953 | if (targetbuf == NULL) { | |
| 845 | 954 | char *n = node; |
| 440 | 955 | for (size_t i = 0; i < removed; i++) { |
| 956 | // element destruction | |
| 845 | 957 | cx_invoke_destructor(list, n + ll->loc_data); |
| 174 | 958 | |
| 440 | 959 | // free the node and advance |
| 845 | 960 | void *next = CX_LL_PTR(n, ll->loc_next); |
| 440 | 961 | cxFree(list->collection.allocator, n); |
| 962 | n = next; | |
| 963 | } | |
| 964 | } else { | |
| 965 | char *dest = targetbuf; | |
| 845 | 966 | char *n = node; |
| 440 | 967 | for (size_t i = 0; i < removed; i++) { |
| 968 | // copy payload | |
| 845 | 969 | memcpy(dest, n + ll->loc_data, list->collection.elem_size); |
| 174 | 970 | |
| 440 | 971 | // advance target buffer |
| 972 | dest += list->collection.elem_size; | |
| 973 | ||
| 974 | // free the node and advance | |
| 845 | 975 | void *next = CX_LL_PTR(n, ll->loc_next); |
| 440 | 976 | cxFree(list->collection.allocator, n); |
| 977 | n = next; | |
| 978 | } | |
| 979 | } | |
| 980 | ||
| 981 | return removed; | |
| 174 | 982 | } |
| 983 | ||
| 984 | static void cx_ll_clear(struct cx_list_s *list) { | |
| 324 | 985 | if (list->collection.size == 0) return; |
| 174 | 986 | |
| 987 | cx_linked_list *ll = (cx_linked_list *) list; | |
| 845 | 988 | char *node = ll->begin; |
| 174 | 989 | while (node != NULL) { |
| 845 | 990 | cx_invoke_destructor(list, node + ll->loc_data); |
| 991 | void *next = CX_LL_PTR(node, ll->loc_next); | |
| 324 | 992 | cxFree(list->collection.allocator, node); |
| 174 | 993 | node = next; |
| 994 | } | |
| 995 | ll->begin = ll->end = NULL; | |
| 324 | 996 | list->collection.size = 0; |
| 174 | 997 | } |
| 998 | ||
| 999 | static int cx_ll_swap( | |
| 1000 | struct cx_list_s *list, | |
| 1001 | size_t i, | |
| 1002 | size_t j | |
| 1003 | ) { | |
| 324 | 1004 | if (i >= list->collection.size || j >= list->collection.size) return 1; |
| 174 | 1005 | if (i == j) return 0; |
| 1006 | ||
| 1007 | // perform an optimized search that finds both elements in one run | |
| 1008 | cx_linked_list *ll = (cx_linked_list *) list; | |
| 324 | 1009 | size_t mid = list->collection.size / 2; |
| 174 | 1010 | size_t left, right; |
| 1011 | if (i < j) { | |
| 1012 | left = i; | |
| 1013 | right = j; | |
| 1014 | } else { | |
| 1015 | left = j; | |
| 1016 | right = i; | |
| 1017 | } | |
| 845 | 1018 | void *nleft = NULL, *nright = NULL; |
| 174 | 1019 | if (left < mid && right < mid) { |
| 1020 | // case 1: both items left from mid | |
| 1021 | nleft = cx_ll_node_at(ll, left); | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
1022 | assert(nleft != NULL); |
| 174 | 1023 | nright = nleft; |
| 1024 | for (size_t c = left; c < right; c++) { | |
| 845 | 1025 | nright = CX_LL_PTR(nright, ll->loc_next); |
| 174 | 1026 | } |
| 1027 | } else if (left >= mid && right >= mid) { | |
| 1028 | // case 2: both items right from mid | |
| 1029 | nright = cx_ll_node_at(ll, right); | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
1030 | assert(nright != NULL); |
| 174 | 1031 | nleft = nright; |
| 1032 | for (size_t c = right; c > left; c--) { | |
| 845 | 1033 | nleft = CX_LL_PTR(nleft, ll->loc_prev); |
| 174 | 1034 | } |
| 1035 | } else { | |
| 1036 | // case 3: one item left, one item right | |
| 1037 | ||
| 1038 | // chose the closest to begin / end | |
| 1039 | size_t closest; | |
| 1040 | size_t other; | |
| 324 | 1041 | size_t diff2boundary = list->collection.size - right - 1; |
| 174 | 1042 | if (left <= diff2boundary) { |
| 1043 | closest = left; | |
| 1044 | other = right; | |
| 1045 | nleft = cx_ll_node_at(ll, left); | |
| 1046 | } else { | |
| 1047 | closest = right; | |
| 1048 | other = left; | |
| 1049 | diff2boundary = left; | |
| 1050 | nright = cx_ll_node_at(ll, right); | |
| 1051 | } | |
| 1052 | ||
| 1053 | // is other element closer to us or closer to boundary? | |
| 1054 | if (right - left <= diff2boundary) { | |
| 1055 | // search other element starting from already found element | |
| 1056 | if (closest == left) { | |
| 1057 | nright = nleft; | |
| 1058 | for (size_t c = left; c < right; c++) { | |
| 845 | 1059 | nright = CX_LL_PTR(nright, ll->loc_next); |
| 174 | 1060 | } |
| 1061 | } else { | |
| 1062 | nleft = nright; | |
| 1063 | for (size_t c = right; c > left; c--) { | |
| 845 | 1064 | nleft = CX_LL_PTR(nleft, ll->loc_prev); |
| 174 | 1065 | } |
| 1066 | } | |
| 1067 | } else { | |
| 1068 | // search other element starting at the boundary | |
| 1069 | if (closest == left) { | |
| 1070 | nright = cx_ll_node_at(ll, other); | |
| 1071 | } else { | |
| 1072 | nleft = cx_ll_node_at(ll, other); | |
| 1073 | } | |
| 1074 | } | |
| 1075 | } | |
| 1076 | ||
| 845 | 1077 | void *prev = CX_LL_PTR(nleft, ll->loc_prev); |
| 1078 | void *next = CX_LL_PTR(nright, ll->loc_next); | |
| 1079 | void *midstart = CX_LL_PTR(nleft, ll->loc_next); | |
| 1080 | void *midend = CX_LL_PTR(nright, ll->loc_prev); | |
| 174 | 1081 | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1082 | if (prev == NULL) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1083 | ll->begin = nright; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1084 | } else { |
| 845 | 1085 | CX_LL_PTR(prev, ll->loc_next) = nright; |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1086 | } |
| 845 | 1087 | CX_LL_PTR(nright, ll->loc_prev) = prev; |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1088 | if (midstart == nright) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1089 | // special case: both nodes are adjacent |
| 845 | 1090 | CX_LL_PTR(nright, ll->loc_next) = nleft; |
| 1091 | CX_LL_PTR(nleft, ll->loc_prev) = nright; | |
| 174 | 1092 | } else { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1093 | // likely case: a chain is between the two nodes |
| 845 | 1094 | CX_LL_PTR(nright, ll->loc_next) = midstart; |
| 1095 | CX_LL_PTR(midstart, ll->loc_prev) = nright; | |
| 1096 | CX_LL_PTR(midend, ll->loc_next) = nleft; | |
| 1097 | CX_LL_PTR(nleft, ll->loc_prev) = midend; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1098 | } |
| 845 | 1099 | CX_LL_PTR(nleft, ll->loc_next) = next; |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1100 | if (next == NULL) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1101 | ll->end = nleft; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1102 | } else { |
| 845 | 1103 | CX_LL_PTR(next, ll->loc_prev) = nleft; |
| 174 | 1104 | } |
| 1105 | ||
| 1106 | return 0; | |
| 1107 | } | |
| 1108 | ||
| 1109 | static void *cx_ll_at( | |
| 324 | 1110 | const struct cx_list_s *list, |
| 174 | 1111 | size_t index |
| 1112 | ) { | |
| 1113 | cx_linked_list *ll = (cx_linked_list *) list; | |
| 845 | 1114 | char *node = cx_ll_node_at(ll, index); |
| 1115 | return node == NULL ? NULL : node + ll->loc_data; | |
| 174 | 1116 | } |
| 1117 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1118 | static size_t cx_ll_find_remove( |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
1119 | struct cx_list_s *list, |
| 324 | 1120 | const void *elem, |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
1121 | bool remove |
| 174 | 1122 | ) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1123 | if (list->collection.size == 0) return 0; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1124 | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1125 | size_t index; |
| 845 | 1126 | cx_linked_list *ll = (cx_linked_list *) list; |
| 1016 | 1127 | char *node = cx_linked_list_find_c( |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1128 | ll->begin, |
| 845 | 1129 | ll->loc_next, ll->loc_data, |
| 1016 | 1130 | elem, &index, |
| 1131 | cx_list_compare_wrapper, | |
| 1132 | list); | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1133 | if (node == NULL) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1134 | return list->collection.size; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1135 | } |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
1136 | if (remove) { |
| 845 | 1137 | cx_invoke_destructor(list, node + ll->loc_data); |
| 1138 | cx_linked_list_remove(&ll->begin, &ll->end, | |
| 1139 | ll->loc_prev, ll->loc_next, node); | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1140 | list->collection.size--; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1141 | cxFree(list->collection.allocator, node); |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
1142 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1143 | return index; |
| 174 | 1144 | } |
| 1145 | ||
| 1146 | static void cx_ll_sort(struct cx_list_s *list) { | |
| 1147 | cx_linked_list *ll = (cx_linked_list *) list; | |
| 1016 | 1148 | cx_linked_list_sort_c(&ll->begin, &ll->end, |
| 845 | 1149 | ll->loc_prev, ll->loc_next, ll->loc_data, |
| 1016 | 1150 | cx_list_compare_wrapper, list); |
| 174 | 1151 | } |
| 1152 | ||
| 1153 | static void cx_ll_reverse(struct cx_list_s *list) { | |
| 1154 | cx_linked_list *ll = (cx_linked_list *) list; | |
| 845 | 1155 | cx_linked_list_reverse(&ll->begin, &ll->end, ll->loc_prev, ll->loc_next); |
| 174 | 1156 | } |
| 1157 | ||
| 1158 | static int cx_ll_compare( | |
| 324 | 1159 | const struct cx_list_s *list, |
| 1160 | const struct cx_list_s *other | |
| 174 | 1161 | ) { |
| 1162 | cx_linked_list *left = (cx_linked_list *) list; | |
| 1163 | cx_linked_list *right = (cx_linked_list *) other; | |
| 845 | 1164 | assert(left->loc_next == right->loc_next); |
| 1165 | assert(left->loc_data == right->loc_data); | |
| 1016 | 1166 | return cx_linked_list_compare_c(left->begin, right->begin, |
| 845 | 1167 | left->loc_next, left->loc_data, |
| 1016 | 1168 | cx_list_compare_wrapper, (void*)list); |
| 174 | 1169 | } |
| 1170 | ||
| 324 | 1171 | static bool cx_ll_iter_valid(const void *it) { |
| 1172 | const struct cx_iterator_s *iter = it; | |
| 174 | 1173 | return iter->elem_handle != NULL; |
| 1174 | } | |
| 1175 | ||
| 1176 | static void cx_ll_iter_next(void *it) { | |
| 324 | 1177 | struct cx_iterator_s *iter = it; |
| 870 | 1178 | cx_linked_list *ll = iter->src_handle; |
| 324 | 1179 | if (iter->base.remove) { |
| 1180 | iter->base.remove = false; | |
| 870 | 1181 | struct cx_list_s *list = iter->src_handle; |
| 845 | 1182 | char *node = iter->elem_handle; |
| 1183 | iter->elem_handle = CX_LL_PTR(node, ll->loc_next); | |
| 1184 | cx_invoke_destructor(list, node + ll->loc_data); | |
| 1185 | cx_linked_list_remove(&ll->begin, &ll->end, | |
| 1186 | ll->loc_prev, ll->loc_next, node); | |
| 324 | 1187 | list->collection.size--; |
| 845 | 1188 | iter->elem_count--; |
| 324 | 1189 | cxFree(list->collection.allocator, node); |
| 174 | 1190 | } else { |
| 1191 | iter->index++; | |
| 845 | 1192 | void *node = iter->elem_handle; |
| 1193 | iter->elem_handle = CX_LL_PTR(node, ll->loc_next); | |
| 174 | 1194 | } |
| 1195 | } | |
| 1196 | ||
| 1197 | static void cx_ll_iter_prev(void *it) { | |
| 324 | 1198 | struct cx_iterator_s *iter = it; |
| 870 | 1199 | cx_linked_list *ll = iter->src_handle; |
| 324 | 1200 | if (iter->base.remove) { |
| 1201 | iter->base.remove = false; | |
| 870 | 1202 | struct cx_list_s *list = iter->src_handle; |
| 845 | 1203 | char *node = iter->elem_handle; |
| 1204 | iter->elem_handle = CX_LL_PTR(node, ll->loc_prev); | |
| 174 | 1205 | iter->index--; |
| 845 | 1206 | cx_invoke_destructor(list, node + ll->loc_data); |
| 1207 | cx_linked_list_remove(&ll->begin, &ll->end, | |
| 1208 | ll->loc_prev, ll->loc_next, node); | |
| 324 | 1209 | list->collection.size--; |
| 845 | 1210 | iter->elem_count--; |
| 324 | 1211 | cxFree(list->collection.allocator, node); |
| 174 | 1212 | } else { |
| 1213 | iter->index--; | |
| 845 | 1214 | char *node = iter->elem_handle; |
| 1215 | iter->elem_handle = CX_LL_PTR(node, ll->loc_prev); | |
| 174 | 1216 | } |
| 1217 | } | |
| 1218 | ||
| 324 | 1219 | static void *cx_ll_iter_current(const void *it) { |
| 1220 | const struct cx_iterator_s *iter = it; | |
| 870 | 1221 | const cx_linked_list *ll = iter->src_handle; |
| 845 | 1222 | char *node = iter->elem_handle; |
| 1223 | return node + ll->loc_data; | |
| 174 | 1224 | } |
| 1225 | ||
| 1226 | static CxIterator cx_ll_iterator( | |
| 324 | 1227 | const struct cx_list_s *list, |
| 174 | 1228 | size_t index, |
| 1229 | bool backwards | |
| 1230 | ) { | |
| 1231 | CxIterator iter; | |
| 1232 | iter.index = index; | |
| 870 | 1233 | iter.src_handle = (void*)list; |
| 324 | 1234 | iter.elem_handle = cx_ll_node_at((const cx_linked_list *) list, index); |
| 1235 | iter.elem_size = list->collection.elem_size; | |
| 1236 | iter.elem_count = list->collection.size; | |
| 174 | 1237 | iter.base.valid = cx_ll_iter_valid; |
| 1238 | iter.base.current = cx_ll_iter_current; | |
| 1239 | iter.base.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next; | |
| 870 | 1240 | iter.base.allow_remove = true; |
| 174 | 1241 | iter.base.remove = false; |
| 1242 | return iter; | |
| 1243 | } | |
| 1244 | ||
| 1245 | static int cx_ll_insert_iter( | |
| 324 | 1246 | CxIterator *iter, |
| 1247 | const void *elem, | |
| 174 | 1248 | int prepend |
| 1249 | ) { | |
| 870 | 1250 | struct cx_list_s *list = iter->src_handle; |
| 1251 | cx_linked_list *ll = iter->src_handle; | |
| 845 | 1252 | void *node = iter->elem_handle; |
| 174 | 1253 | if (node != NULL) { |
| 1254 | assert(prepend >= 0 && prepend <= 1); | |
| 845 | 1255 | void *choice[2] = {node, CX_LL_PTR(node, ll->loc_prev)}; |
| 174 | 1256 | int result = cx_ll_insert_at(list, choice[prepend], elem); |
| 324 | 1257 | if (result == 0) { |
| 1258 | iter->elem_count++; | |
| 1259 | if (prepend) { | |
| 1260 | iter->index++; | |
| 1261 | } | |
| 1262 | } | |
| 174 | 1263 | return result; |
| 1264 | } else { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1265 | if (cx_ll_insert_element(list, list->collection.size, elem) == NULL) { |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1266 | return 1; // LCOV_EXCL_LINE |
| 324 | 1267 | } |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1268 | iter->elem_count++; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1269 | iter->index = list->collection.size; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1270 | return 0; |
| 174 | 1271 | } |
| 1272 | } | |
| 1273 | ||
| 1274 | static void cx_ll_destructor(CxList *list) { | |
| 1275 | cx_linked_list *ll = (cx_linked_list *) list; | |
| 1276 | ||
| 845 | 1277 | char *node = ll->begin; |
| 174 | 1278 | while (node) { |
| 845 | 1279 | cx_invoke_destructor(list, node + ll->loc_data); |
| 1280 | void *next = CX_LL_PTR(node, ll->loc_next); | |
| 324 | 1281 | cxFree(list->collection.allocator, node); |
| 174 | 1282 | node = next; |
| 1283 | } | |
| 1284 | ||
| 324 | 1285 | cxFree(list->collection.allocator, list); |
| 174 | 1286 | } |
| 1287 | ||
| 1288 | static cx_list_class cx_linked_list_class = { | |
| 1289 | cx_ll_destructor, | |
| 1290 | cx_ll_insert_element, | |
| 1291 | cx_ll_insert_array, | |
| 324 | 1292 | cx_ll_insert_sorted, |
| 845 | 1293 | cx_ll_insert_unique, |
| 174 | 1294 | cx_ll_insert_iter, |
| 1295 | cx_ll_remove, | |
| 1296 | cx_ll_clear, | |
| 1297 | cx_ll_swap, | |
| 1298 | cx_ll_at, | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
1299 | cx_ll_find_remove, |
| 174 | 1300 | cx_ll_sort, |
| 1301 | cx_ll_compare, | |
| 1302 | cx_ll_reverse, | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1303 | NULL, // no overallocation supported |
| 174 | 1304 | cx_ll_iterator, |
| 1305 | }; | |
| 1306 | ||
| 1307 | CxList *cxLinkedListCreate( | |
| 324 | 1308 | const CxAllocator *allocator, |
| 1309 | size_t elem_size | |
| 174 | 1310 | ) { |
| 1311 | if (allocator == NULL) { | |
| 1312 | allocator = cxDefaultAllocator; | |
| 1313 | } | |
| 1314 | ||
| 1315 | cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list)); | |
| 1316 | if (list == NULL) return NULL; | |
| 845 | 1317 | list->loc_prev = 0; |
| 1318 | list->loc_next = sizeof(void*); | |
| 1319 | list->loc_data = sizeof(void*)*2; | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1320 | list->loc_extra = -1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1321 | list->extra_data_len = 0; |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1322 | cx_list_init((CxList*)list, &cx_linked_list_class, |
| 1016 | 1323 | allocator, elem_size); |
| 174 | 1324 | |
| 1325 | return (CxList *) list; | |
| 1326 | } | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1327 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1328 | void cx_linked_list_extra_data(cx_linked_list *list, size_t len) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1329 | list->extra_data_len = len; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1330 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1331 | off_t loc_extra = list->loc_data + list->base.collection.elem_size; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1332 | size_t alignment = alignof(void*); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1333 | size_t padding = alignment - (loc_extra % alignment); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1334 | list->loc_extra = loc_extra + padding; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1335 | } |