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