Mon, 13 Oct 2025 21:31:58 +0200
update ucx
| 174 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 28 | /** | |
| 440 | 29 | * @file linked_list.h |
| 30 | * @brief Linked list implementation. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
| 174 | 34 | */ |
| 35 | ||
| 36 | #ifndef UCX_LINKED_LIST_H | |
| 37 | #define UCX_LINKED_LIST_H | |
| 38 | ||
| 39 | #include "common.h" | |
| 40 | #include "list.h" | |
| 41 | ||
| 42 | #ifdef __cplusplus | |
| 43 | extern "C" { | |
| 44 | #endif | |
| 45 | ||
| 46 | /** | |
| 845 | 47 | * Metadata for a linked list. |
| 48 | */ | |
| 49 | typedef struct cx_linked_list_s { | |
| 50 | /** Base members. */ | |
| 51 | struct cx_list_s base; | |
| 52 | /** | |
| 53 | * Location of the prev pointer (mandatory). | |
| 54 | */ | |
| 55 | off_t loc_prev; | |
| 56 | /** | |
| 57 | * Location of the next pointer (mandatory). | |
| 58 | */ | |
| 59 | off_t loc_next; | |
| 60 | /** | |
| 61 | * Location of the payload (mandatory). | |
| 62 | */ | |
| 63 | off_t loc_data; | |
| 64 | /** | |
| 65 | * Additional bytes to allocate @em behind the payload (e.g. for metadata). | |
| 66 | */ | |
| 67 | size_t extra_data_len; | |
| 68 | /** | |
| 69 | * Pointer to the first node. | |
| 70 | */ | |
| 71 | void *begin; | |
| 72 | /** | |
| 73 | * Pointer to the last node. | |
| 74 | */ | |
| 75 | void *end; | |
| 76 | } cx_linked_list; | |
| 77 | ||
| 78 | /** | |
| 440 | 79 | * Allocates a linked list for storing elements with @p elem_size bytes each. |
| 174 | 80 | * |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
81 | * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of |
| 845 | 82 | * copies of the added elements, and the compare function will be automatically set |
| 83 | * to cx_cmp_ptr() if none is given. | |
| 174 | 84 | * |
| 85 | * @param allocator the allocator for allocating the list nodes | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
86 | * (if @c NULL, the cxDefaultAllocator will be used) |
| 174 | 87 | * @param comparator the comparator for the elements |
| 440 | 88 | * (if @c NULL, and the list is not storing pointers, sort and find |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
89 | * functions will not work) |
| 324 | 90 | * @param elem_size the size of each element in bytes |
| 174 | 91 | * @return the created list |
| 92 | */ | |
| 440 | 93 | cx_attr_nodiscard |
| 94 | cx_attr_malloc | |
| 95 | cx_attr_dealloc(cxListFree, 1) | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
96 | cx_attr_export |
| 174 | 97 | CxList *cxLinkedListCreate( |
| 324 | 98 | const CxAllocator *allocator, |
| 174 | 99 | cx_compare_func comparator, |
| 324 | 100 | size_t elem_size |
| 174 | 101 | ); |
| 102 | ||
| 103 | /** | |
| 440 | 104 | * Allocates a linked list for storing elements with @p elem_size bytes each. |
| 174 | 105 | * |
| 106 | * The list will use cxDefaultAllocator and no comparator function. If you want | |
| 107 | * to call functions that need a comparator, you must either set one immediately | |
| 108 | * after list creation or use cxLinkedListCreate(). | |
| 109 | * | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
110 | * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of |
| 845 | 111 | * copies of the added elements, and the compare function will be automatically set |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
112 | * to cx_cmp_ptr(). |
| 174 | 113 | * |
| 440 | 114 | * @param elem_size (@c size_t) the size of each element in bytes |
| 115 | * @return (@c CxList*) the created list | |
| 174 | 116 | */ |
| 324 | 117 | #define cxLinkedListCreateSimple(elem_size) \ |
| 118 | cxLinkedListCreate(NULL, NULL, elem_size) | |
| 174 | 119 | |
| 120 | /** | |
| 121 | * Finds the node at a certain index. | |
| 122 | * | |
| 123 | * This function can be used to start at an arbitrary position within the list. | |
| 845 | 124 | * If the search index is larger than the start index, @p loc_advance must denote |
| 125 | * the location of a @c next pointer (i.e., a pointer to the next node). | |
| 174 | 126 | * But it is also possible that the search index is smaller than the start index |
| 845 | 127 | * (e.g., in cases where traversing a list backwards is faster). |
| 128 | * In that case @p loc_advance must denote the location of a @c prev pointer | |
| 129 | * (i.e., a pointer to the previous node). | |
| 174 | 130 | * |
| 131 | * @param start a pointer to the start node | |
| 132 | * @param start_index the start index | |
| 133 | * @param loc_advance the location of the pointer to advance | |
| 134 | * @param index the search index | |
| 135 | * @return the node found at the specified index | |
| 136 | */ | |
| 440 | 137 | cx_attr_nonnull |
| 138 | cx_attr_nodiscard | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
139 | cx_attr_export |
| 174 | 140 | void *cx_linked_list_at( |
| 324 | 141 | const void *start, |
| 174 | 142 | size_t start_index, |
| 143 | ptrdiff_t loc_advance, | |
| 144 | size_t index | |
| 324 | 145 | ); |
| 174 | 146 | |
| 147 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
148 | * Finds the node containing an element within a linked list. |
| 174 | 149 | * |
| 150 | * @param start a pointer to the start node | |
| 151 | * @param loc_advance the location of the pointer to advance | |
| 440 | 152 | * @param loc_data the location of the @c data pointer within your node struct |
| 153 | * @param cmp_func a compare function to compare @p elem against the node data | |
| 174 | 154 | * @param elem a pointer to the element to 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
|
155 | * @param found_index an optional pointer where the index of the found node |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
156 | * (given that @p start has index 0) is stored |
| 845 | 157 | * @return a pointer to the found node or @c NULL if no matching node was found |
| 174 | 158 | */ |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
159 | cx_attr_nonnull_arg(1, 4, 5) |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
160 | cx_attr_export |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
161 | void *cx_linked_list_find( |
| 324 | 162 | const void *start, |
| 174 | 163 | ptrdiff_t loc_advance, |
| 164 | ptrdiff_t loc_data, | |
| 165 | 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
|
166 | 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
|
167 | size_t *found_index |
| 324 | 168 | ); |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
169 | |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
170 | /** |
| 174 | 171 | * Finds the first node in a linked list. |
| 172 | * | |
| 440 | 173 | * The function starts with the pointer denoted by @p node and traverses the list |
| 174 | 174 | * along a prev pointer whose location within the node struct is |
| 440 | 175 | * denoted by @p loc_prev. |
| 174 | 176 | * |
| 177 | * @param node a pointer to a node in the list | |
| 440 | 178 | * @param loc_prev the location of the @c prev pointer |
| 174 | 179 | * @return a pointer to the first node |
| 180 | */ | |
| 440 | 181 | cx_attr_nonnull |
| 182 | cx_attr_returns_nonnull | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
183 | cx_attr_export |
| 174 | 184 | void *cx_linked_list_first( |
| 324 | 185 | const void *node, |
| 174 | 186 | ptrdiff_t loc_prev |
| 324 | 187 | ); |
| 174 | 188 | |
| 189 | /** | |
| 190 | * Finds the last node in a linked list. | |
| 191 | * | |
| 440 | 192 | * The function starts with the pointer denoted by @p node and traverses the list |
| 174 | 193 | * along a next pointer whose location within the node struct is |
| 440 | 194 | * denoted by @p loc_next. |
| 174 | 195 | * |
| 196 | * @param node a pointer to a node in the list | |
| 440 | 197 | * @param loc_next the location of the @c next pointer |
| 174 | 198 | * @return a pointer to the last node |
| 199 | */ | |
| 440 | 200 | cx_attr_nonnull |
| 201 | cx_attr_returns_nonnull | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
202 | cx_attr_export |
| 174 | 203 | void *cx_linked_list_last( |
| 324 | 204 | const void *node, |
| 174 | 205 | ptrdiff_t loc_next |
| 324 | 206 | ); |
| 174 | 207 | |
| 208 | /** | |
| 209 | * Finds the predecessor of a node in case it is not linked. | |
| 210 | * | |
| 440 | 211 | * @remark If @p node is not contained in the list starting with @p begin, the behavior is undefined. |
| 174 | 212 | * |
| 213 | * @param begin the node where to start the search | |
| 440 | 214 | * @param loc_next the location of the @c next pointer |
| 174 | 215 | * @param node the successor of the node to find |
| 440 | 216 | * @return the node or @c NULL if @p node has no predecessor |
| 174 | 217 | */ |
| 440 | 218 | cx_attr_nonnull |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
219 | cx_attr_export |
| 174 | 220 | void *cx_linked_list_prev( |
| 324 | 221 | const void *begin, |
| 174 | 222 | ptrdiff_t loc_next, |
| 324 | 223 | const void *node |
| 224 | ); | |
| 174 | 225 | |
| 226 | /** | |
| 227 | * Adds a new node to a linked list. | |
| 845 | 228 | * The node must not be part of any list yet. |
| 174 | 229 | * |
| 440 | 230 | * @remark One of the pointers @p begin or @p end may be @c NULL, but not both. |
| 174 | 231 | * |
| 440 | 232 | * @param begin a pointer to the beginning node pointer (if your list has one) |
| 174 | 233 | * @param end a pointer to the end node pointer (if your list has one) |
| 440 | 234 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) |
| 235 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 174 | 236 | * @param new_node a pointer to the node that shall be appended |
| 237 | */ | |
| 440 | 238 | cx_attr_nonnull_arg(5) |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
239 | cx_attr_export |
| 174 | 240 | void cx_linked_list_add( |
| 241 | void **begin, | |
| 242 | void **end, | |
| 243 | ptrdiff_t loc_prev, | |
| 244 | ptrdiff_t loc_next, | |
| 245 | void *new_node | |
| 324 | 246 | ); |
| 174 | 247 | |
| 248 | /** | |
| 249 | * Prepends a new node to a linked list. | |
| 845 | 250 | * The node must not be part of any list yet. |
| 174 | 251 | * |
| 440 | 252 | * @remark One of the pointers @p begin or @p end may be @c NULL, but not both. |
| 174 | 253 | * |
| 440 | 254 | * @param begin a pointer to the beginning node pointer (if your list has one) |
| 174 | 255 | * @param end a pointer to the end node pointer (if your list has one) |
| 440 | 256 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) |
| 257 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 174 | 258 | * @param new_node a pointer to the node that shall be prepended |
| 259 | */ | |
| 440 | 260 | cx_attr_nonnull_arg(5) |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
261 | cx_attr_export |
| 174 | 262 | void cx_linked_list_prepend( |
| 263 | void **begin, | |
| 264 | void **end, | |
| 265 | ptrdiff_t loc_prev, | |
| 266 | ptrdiff_t loc_next, | |
| 267 | void *new_node | |
| 324 | 268 | ); |
| 174 | 269 | |
| 270 | /** | |
| 271 | * Links two nodes. | |
| 272 | * | |
| 440 | 273 | * @param left the new predecessor of @p right |
| 274 | * @param right the new successor of @p left | |
| 275 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) | |
| 276 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 174 | 277 | */ |
| 440 | 278 | cx_attr_nonnull |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
279 | cx_attr_export |
| 174 | 280 | void cx_linked_list_link( |
| 281 | void *left, | |
| 282 | void *right, | |
| 283 | ptrdiff_t loc_prev, | |
| 284 | ptrdiff_t loc_next | |
| 324 | 285 | ); |
| 174 | 286 | |
| 287 | /** | |
| 288 | * Unlinks two nodes. | |
| 289 | * | |
| 290 | * If right is not the successor of left, the behavior is undefined. | |
| 291 | * | |
| 440 | 292 | * @param left the predecessor of @p right |
| 293 | * @param right the successor of @p left | |
| 294 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) | |
| 295 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 174 | 296 | */ |
| 440 | 297 | cx_attr_nonnull |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
298 | cx_attr_export |
| 174 | 299 | void cx_linked_list_unlink( |
| 300 | void *left, | |
| 301 | void *right, | |
| 302 | ptrdiff_t loc_prev, | |
| 303 | ptrdiff_t loc_next | |
| 324 | 304 | ); |
| 174 | 305 | |
| 306 | /** | |
| 307 | * Inserts a new node after a given node of a linked list. | |
| 845 | 308 | * The new node must not be part of any list yet. |
| 174 | 309 | * |
| 440 | 310 | * @note If you specify @c NULL as the @p node to insert after, this function needs either the @p begin or |
| 311 | * the @p end pointer to determine the start of the list. Then the new node will be prepended to the list. | |
| 174 | 312 | * |
| 440 | 313 | * @param begin a pointer to the beginning node pointer (if your list has one) |
| 174 | 314 | * @param end a pointer to the end node pointer (if your list has one) |
| 440 | 315 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) |
| 316 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 317 | * @param node the node after which to insert (@c NULL if you want to prepend the node to the list) | |
| 324 | 318 | * @param new_node a pointer to the node that shall be inserted |
| 174 | 319 | */ |
| 440 | 320 | cx_attr_nonnull_arg(6) |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
321 | cx_attr_export |
| 174 | 322 | void cx_linked_list_insert( |
| 323 | void **begin, | |
| 324 | void **end, | |
| 325 | ptrdiff_t loc_prev, | |
| 326 | ptrdiff_t loc_next, | |
| 327 | void *node, | |
| 328 | void *new_node | |
| 324 | 329 | ); |
| 174 | 330 | |
| 331 | /** | |
| 332 | * Inserts a chain of nodes after a given node of a linked list. | |
| 845 | 333 | * The chain must not be part of any list yet. |
| 174 | 334 | * |
| 335 | * If you do not explicitly specify the end of the chain, it will be determined by traversing | |
| 440 | 336 | * the @c next pointer. |
| 174 | 337 | * |
| 440 | 338 | * @note If you specify @c NULL as the @p node to insert after, this function needs either the @p begin or |
| 339 | * the @p end pointer to determine the start of the list. If only the @p end pointer is specified, you also need | |
| 340 | * to provide a valid @p loc_prev location. | |
| 174 | 341 | * Then the chain will be prepended to the list. |
| 342 | * | |
| 440 | 343 | * @param begin a pointer to the beginning node pointer (if your list has one) |
| 174 | 344 | * @param end a pointer to the end node pointer (if your list has one) |
| 440 | 345 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) |
| 346 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 347 | * @param node the node after which to insert (@c NULL to prepend the chain to the list) | |
| 174 | 348 | * @param insert_begin a pointer to the first node of the chain that shall be inserted |
| 349 | * @param insert_end a pointer to the last node of the chain (or NULL if the last node shall be determined) | |
| 350 | */ | |
| 440 | 351 | cx_attr_nonnull_arg(6) |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
352 | cx_attr_export |
| 174 | 353 | void cx_linked_list_insert_chain( |
| 354 | void **begin, | |
| 355 | void **end, | |
| 356 | ptrdiff_t loc_prev, | |
| 357 | ptrdiff_t loc_next, | |
| 358 | void *node, | |
| 359 | void *insert_begin, | |
| 360 | void *insert_end | |
| 324 | 361 | ); |
| 362 | ||
| 363 | /** | |
| 364 | * Inserts a node into a sorted linked list. | |
| 845 | 365 | * The new node must not be part of any list yet. |
| 324 | 366 | * |
| 440 | 367 | * If the list starting with the node pointed to by @p begin is not sorted |
| 324 | 368 | * already, the behavior is undefined. |
| 369 | * | |
| 440 | 370 | * @param begin a pointer to the beginning node pointer (required) |
| 324 | 371 | * @param end a pointer to the end node pointer (if your list has one) |
| 440 | 372 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) |
| 373 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 324 | 374 | * @param new_node a pointer to the node that shall be inserted |
| 375 | * @param cmp_func a compare function that will receive the node pointers | |
| 376 | */ | |
| 440 | 377 | cx_attr_nonnull_arg(1, 5, 6) |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
378 | cx_attr_export |
| 324 | 379 | void cx_linked_list_insert_sorted( |
| 380 | void **begin, | |
| 381 | void **end, | |
| 382 | ptrdiff_t loc_prev, | |
| 383 | ptrdiff_t loc_next, | |
| 384 | void *new_node, | |
| 385 | cx_compare_func cmp_func | |
| 386 | ); | |
| 387 | ||
| 388 | /** | |
| 389 | * Inserts a chain of nodes into a sorted linked list. | |
| 845 | 390 | * The chain must not be part of any list yet. |
| 324 | 391 | * |
| 440 | 392 | * If either the list starting with the node pointed to by @p begin or the list |
| 393 | * starting with @p insert_begin is not sorted, the behavior is undefined. | |
| 324 | 394 | * |
| 440 | 395 | * @attention In contrast to cx_linked_list_insert_chain(), the source chain |
| 324 | 396 | * will be broken and inserted into the target list so that the resulting list |
| 845 | 397 | * will be sorted according to @p cmp_func. That means each node in the source |
| 324 | 398 | * chain may be re-linked with nodes from the target list. |
| 399 | * | |
| 440 | 400 | * @param begin a pointer to the beginning node pointer (required) |
| 324 | 401 | * @param end a pointer to the end node pointer (if your list has one) |
| 440 | 402 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) |
| 403 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 324 | 404 | * @param insert_begin a pointer to the first node of the chain that shall be inserted |
| 405 | * @param cmp_func a compare function that will receive the node pointers | |
| 406 | */ | |
| 440 | 407 | cx_attr_nonnull_arg(1, 5, 6) |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
408 | cx_attr_export |
| 324 | 409 | void cx_linked_list_insert_sorted_chain( |
| 410 | void **begin, | |
| 411 | void **end, | |
| 412 | ptrdiff_t loc_prev, | |
| 413 | ptrdiff_t loc_next, | |
| 414 | void *insert_begin, | |
| 415 | cx_compare_func cmp_func | |
| 416 | ); | |
| 174 | 417 | |
| 418 | /** | |
| 845 | 419 | * Inserts a node into a sorted linked list if no other node with the same value already exists. |
| 420 | * The new node must not be part of any list yet. | |
| 421 | * | |
| 422 | * If the list starting with the node pointed to by @p begin is not sorted | |
| 423 | * already, the behavior is undefined. | |
| 424 | * | |
| 425 | * @param begin a pointer to the beginning node pointer (required) | |
| 426 | * @param end a pointer to the end node pointer (if your list has one) | |
| 427 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) | |
| 428 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 429 | * @param new_node a pointer to the node that shall be inserted | |
| 430 | * @param cmp_func a compare function that will receive the node pointers | |
| 431 | * @retval zero when the node was inserted | |
| 432 | * @retval non-zero when a node with the same value already exists | |
| 433 | */ | |
| 434 | cx_attr_nonnull_arg(1, 5, 6) | |
| 435 | cx_attr_export | |
| 436 | int cx_linked_list_insert_unique( | |
| 437 | void **begin, | |
| 438 | void **end, | |
| 439 | ptrdiff_t loc_prev, | |
| 440 | ptrdiff_t loc_next, | |
| 441 | void *new_node, | |
| 442 | cx_compare_func cmp_func | |
| 443 | ); | |
| 444 | ||
| 445 | /** | |
| 446 | * Inserts a chain of nodes into a sorted linked list, avoiding duplicates. | |
| 447 | * The chain must not be part of any list yet. | |
| 448 | * | |
| 449 | * If either the list starting with the node pointed to by @p begin or the list | |
| 450 | * starting with @p insert_begin is not sorted, the behavior is undefined. | |
| 451 | * | |
| 452 | * @attention In contrast to cx_linked_list_insert_sorted(), not all nodes of the | |
| 453 | * chain might be added. This function returns a new chain consisting of all the duplicates. | |
| 454 | * | |
| 455 | * @param begin a pointer to the beginning node pointer (required) | |
| 456 | * @param end a pointer to the end node pointer (if your list has one) | |
| 457 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) | |
| 458 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 459 | * @param insert_begin a pointer to the first node of the chain that shall be inserted | |
| 460 | * @param cmp_func a compare function that will receive the node pointers | |
| 461 | * @return a pointer to a new chain with all duplicates that were not inserted (or @c NULL when there were no duplicates) | |
| 462 | */ | |
| 463 | cx_attr_nonnull_arg(1, 5, 6) | |
| 464 | cx_attr_nodiscard | |
| 465 | cx_attr_export | |
| 466 | void *cx_linked_list_insert_unique_chain( | |
| 467 | void **begin, | |
| 468 | void **end, | |
| 469 | ptrdiff_t loc_prev, | |
| 470 | ptrdiff_t loc_next, | |
| 471 | void *insert_begin, | |
| 472 | cx_compare_func cmp_func | |
| 473 | ); | |
| 474 | ||
| 475 | /** | |
| 440 | 476 | * Removes a chain of nodes from the linked list. |
| 174 | 477 | * |
| 845 | 478 | * If one of the nodes to remove is the beginning (resp. end) node of the list, and if @p begin (resp. @p end) |
| 174 | 479 | * addresses are provided, the pointers are adjusted accordingly. |
| 480 | * | |
| 481 | * The following combinations of arguments are valid (more arguments are optional): | |
| 440 | 482 | * @li @p loc_next and @p loc_prev (ancestor node is determined by using the prev pointer, overall O(1) performance) |
| 483 | * @li @p loc_next and @p begin (ancestor node is determined by list traversal, overall O(n) performance) | |
| 484 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
485 | * @remark The @c next and @c prev pointers of the removed chain are not cleared by this function and may still be used |
| 440 | 486 | * to traverse to a former adjacent node in the list, or within the chain. |
| 174 | 487 | * |
| 440 | 488 | * @param begin a pointer to the beginning node pointer (optional) |
| 489 | * @param end a pointer to the end node pointer (optional) | |
| 490 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) | |
| 491 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 492 | * @param node the start node of the chain | |
| 493 | * @param num the number of nodes to remove | |
| 494 | * @return the actual number of nodes that were removed (can be less when the list did not have enough nodes) | |
| 495 | */ | |
| 496 | cx_attr_nonnull_arg(5) | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
497 | cx_attr_export |
| 440 | 498 | size_t cx_linked_list_remove_chain( |
| 499 | void **begin, | |
| 500 | void **end, | |
| 501 | ptrdiff_t loc_prev, | |
| 502 | ptrdiff_t loc_next, | |
| 503 | void *node, | |
| 504 | size_t num | |
| 505 | ); | |
| 506 | ||
| 507 | /** | |
| 508 | * Removes a node from the linked list. | |
| 509 | * | |
| 845 | 510 | * If the node to remove is the beginning (resp. end) node of the list, and if @p begin (resp. @p end) |
| 440 | 511 | * addresses are provided, the pointers are adjusted accordingly. |
| 512 | * | |
| 513 | * The following combinations of arguments are valid (more arguments are optional): | |
| 514 | * @li @p loc_next and @p loc_prev (ancestor node is determined by using the prev pointer, overall O(1) performance) | |
| 515 | * @li @p loc_next and @p begin (ancestor node is determined by list traversal, overall O(n) performance) | |
| 516 | * | |
| 517 | * @remark The @c next and @c prev pointers of the removed node are not cleared by this function and may still be used | |
| 174 | 518 | * to traverse to a former adjacent node in the list. |
| 519 | * | |
| 440 | 520 | * @param begin a pointer to the beginning node pointer (optional) |
| 174 | 521 | * @param end a pointer to the end node pointer (optional) |
| 440 | 522 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) |
| 523 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 174 | 524 | * @param node the node to remove |
| 525 | */ | |
| 440 | 526 | cx_attr_nonnull_arg(5) |
| 527 | static inline void cx_linked_list_remove( | |
| 174 | 528 | void **begin, |
| 529 | void **end, | |
| 530 | ptrdiff_t loc_prev, | |
| 531 | ptrdiff_t loc_next, | |
| 532 | void *node | |
| 440 | 533 | ) { |
| 534 | cx_linked_list_remove_chain(begin, end, loc_prev, loc_next, node, 1); | |
| 535 | } | |
| 174 | 536 | |
| 537 | /** | |
| 440 | 538 | * Determines the size of a linked list starting with @p node. |
| 539 | * | |
| 174 | 540 | * @param node the first node |
| 440 | 541 | * @param loc_next the location of the @c next pointer within the node struct |
| 542 | * @return the size of the list or zero if @p node is @c NULL | |
| 174 | 543 | */ |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
544 | cx_attr_nodiscard |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
545 | cx_attr_export |
| 174 | 546 | size_t cx_linked_list_size( |
| 324 | 547 | const void *node, |
| 174 | 548 | ptrdiff_t loc_next |
| 549 | ); | |
| 550 | ||
| 551 | /** | |
| 552 | * Sorts a linked list based on a comparison function. | |
| 553 | * | |
| 554 | * This function can work with linked lists of the following structure: | |
| 440 | 555 | * @code |
| 174 | 556 | * typedef struct node node; |
| 557 | * struct node { | |
| 558 | * node* prev; | |
| 559 | * node* next; | |
| 560 | * my_payload data; | |
| 561 | * } | |
| 440 | 562 | * @endcode |
| 174 | 563 | * |
| 564 | * @note This is a recursive function with at most logarithmic recursion depth. | |
| 565 | * | |
| 440 | 566 | * @param begin a pointer to the beginning node pointer (required) |
| 174 | 567 | * @param end a pointer to the end node pointer (optional) |
| 440 | 568 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if not present) |
| 569 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 570 | * @param loc_data the location of the @c data pointer within your node struct | |
| 174 | 571 | * @param cmp_func the compare function defining the sort order |
| 572 | */ | |
| 440 | 573 | cx_attr_nonnull_arg(1, 6) |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
574 | cx_attr_export |
| 174 | 575 | void cx_linked_list_sort( |
| 576 | void **begin, | |
| 577 | void **end, | |
| 578 | ptrdiff_t loc_prev, | |
| 579 | ptrdiff_t loc_next, | |
| 580 | ptrdiff_t loc_data, | |
| 581 | cx_compare_func cmp_func | |
| 324 | 582 | ); |
| 174 | 583 | |
| 584 | ||
| 585 | /** | |
| 586 | * Compares two lists element wise. | |
| 587 | * | |
| 845 | 588 | * @attention Both lists must have the same structure. |
| 174 | 589 | * |
| 440 | 590 | * @param begin_left the beginning of the left list (@c NULL denotes an empty list) |
| 591 | * @param begin_right the beginning of the right list (@c NULL denotes an empty list) | |
| 174 | 592 | * @param loc_advance the location of the pointer to advance |
| 440 | 593 | * @param loc_data the location of the @c data pointer within your node struct |
| 174 | 594 | * @param cmp_func the function to compare the elements |
| 440 | 595 | * @return the first non-zero result of invoking @p cmp_func or: negative if the left list is smaller than the |
| 174 | 596 | * right list, positive if the left list is larger than the right list, zero if both lists are equal. |
| 597 | */ | |
| 440 | 598 | cx_attr_nonnull_arg(5) |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
599 | cx_attr_export |
| 174 | 600 | int cx_linked_list_compare( |
| 324 | 601 | const void *begin_left, |
| 602 | const void *begin_right, | |
| 174 | 603 | ptrdiff_t loc_advance, |
| 604 | ptrdiff_t loc_data, | |
| 605 | cx_compare_func cmp_func | |
| 324 | 606 | ); |
| 174 | 607 | |
| 608 | /** | |
| 609 | * Reverses the order of the nodes in a linked list. | |
| 610 | * | |
| 440 | 611 | * @param begin a pointer to the beginning node pointer (required) |
| 174 | 612 | * @param end a pointer to the end node pointer (optional) |
| 440 | 613 | * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one) |
| 614 | * @param loc_next the location of a @c next pointer within your node struct (required) | |
| 174 | 615 | */ |
| 440 | 616 | cx_attr_nonnull_arg(1) |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
617 | cx_attr_export |
| 174 | 618 | void cx_linked_list_reverse( |
| 619 | void **begin, | |
| 620 | void **end, | |
| 621 | ptrdiff_t loc_prev, | |
| 622 | ptrdiff_t loc_next | |
| 324 | 623 | ); |
| 174 | 624 | |
| 625 | #ifdef __cplusplus | |
| 626 | } // extern "C" | |
| 627 | #endif | |
| 628 | ||
| 629 | #endif // UCX_LINKED_LIST_H |