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