Mon, 13 Oct 2025 21:31:58 +0200
update ucx
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved. |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | /** |
| 440 | 29 | * @file tree.h |
| 30 | * @brief Interface for tree implementations. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | */ |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | #ifndef UCX_TREE_H |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | #define UCX_TREE_H |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | #include "common.h" |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | |
| 324 | 41 | #include "collection.h" |
| 42 | ||
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | #ifdef __cplusplus |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | extern "C" { |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | #endif |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | |
| 324 | 47 | /** |
| 48 | * A depth-first tree iterator. | |
| 49 | * | |
| 50 | * This iterator is not position-aware in a strict sense, as it does not assume | |
| 51 | * a particular order of elements in the tree. However, the iterator keeps track | |
| 845 | 52 | * of the number of nodes it has passed in a counter-variable. |
| 324 | 53 | * Each node, regardless of the number of passes, is counted only once. |
| 54 | * | |
| 55 | * @note Objects that are pointed to by an iterator are mutable through that | |
| 56 | * iterator. However, if the | |
| 845 | 57 | * underlying data structure is mutated by other means than this iterator (e.g., |
| 324 | 58 | * elements added or removed), the iterator becomes invalid (regardless of what |
| 59 | * cxIteratorValid() returns). | |
| 60 | * | |
| 61 | * @see CxIterator | |
| 62 | */ | |
| 63 | typedef struct cx_tree_iterator_s { | |
| 64 | /** | |
| 65 | * Base members. | |
| 66 | */ | |
| 67 | CX_ITERATOR_BASE; | |
| 68 | /** | |
| 69 | * Indicates whether the subtree below the current node shall be skipped. | |
| 70 | */ | |
| 71 | bool skip; | |
| 72 | /** | |
| 73 | * Set to true, when the iterator shall visit a node again | |
| 845 | 74 | * when all its children have been processed. |
| 324 | 75 | */ |
| 76 | bool visit_on_exit; | |
| 77 | /** | |
| 78 | * True, if this iterator is currently leaving the node. | |
| 79 | */ | |
| 80 | bool exiting; | |
| 81 | /** | |
| 82 | * Offset in the node struct for the children linked list. | |
| 83 | */ | |
| 84 | ptrdiff_t loc_children; | |
| 85 | /** | |
| 86 | * Offset in the node struct for the next pointer. | |
| 87 | */ | |
| 88 | ptrdiff_t loc_next; | |
| 89 | /** | |
| 90 | * The total number of distinct nodes that have been passed so far. | |
| 91 | */ | |
| 92 | size_t counter; | |
| 93 | /** | |
| 94 | * The currently observed node. | |
| 95 | * | |
| 96 | * This is the same what cxIteratorCurrent() would return. | |
| 97 | */ | |
| 98 | void *node; | |
| 99 | /** | |
| 845 | 100 | * Stores a copy of the pointer to the successor of the visited node. |
| 324 | 101 | * Allows freeing a node on exit without corrupting the iteration. |
| 102 | */ | |
| 103 | void *node_next; | |
| 104 | /** | |
| 105 | * Internal stack. | |
| 106 | * Will be automatically freed once the iterator becomes invalid. | |
| 107 | * | |
| 108 | * If you want to discard the iterator before, you need to manually | |
| 109 | * call cxTreeIteratorDispose(). | |
| 110 | */ | |
| 111 | void **stack; | |
| 112 | /** | |
| 113 | * Internal capacity of the stack. | |
| 114 | */ | |
| 115 | size_t stack_capacity; | |
| 116 | union { | |
| 117 | /** | |
| 118 | * Internal stack size. | |
| 119 | */ | |
| 120 | size_t stack_size; | |
| 121 | /** | |
| 122 | * The current depth in the tree. | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
123 | * The node with which the iteration starts has depth 1. |
| 324 | 124 | */ |
| 125 | size_t depth; | |
| 126 | }; | |
| 127 | } CxTreeIterator; | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
128 | |
| 324 | 129 | /** |
| 130 | * An element in a visitor queue. | |
| 131 | */ | |
| 132 | struct cx_tree_visitor_queue_s { | |
| 133 | /** | |
| 134 | * The tree node to visit. | |
| 135 | */ | |
| 136 | void *node; | |
| 137 | /** | |
| 138 | * The depth of the node. | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
139 | * The first visited node has depth 1. |
| 324 | 140 | */ |
| 141 | size_t depth; | |
| 142 | /** | |
| 440 | 143 | * The next element in the queue or @c NULL. |
| 324 | 144 | */ |
| 145 | struct cx_tree_visitor_queue_s *next; | |
| 146 | }; | |
| 147 | ||
| 148 | /** | |
| 149 | * A breadth-first tree iterator. | |
| 150 | * | |
| 151 | * This iterator needs to maintain a visitor queue that will be automatically | |
| 152 | * freed once the iterator becomes invalid. | |
| 153 | * If you want to discard the iterator before, you MUST manually call | |
| 154 | * cxTreeVisitorDispose(). | |
| 155 | * | |
| 156 | * This iterator is not position-aware in a strict sense, as it does not assume | |
| 157 | * a particular order of elements in the tree. However, the iterator keeps track | |
| 845 | 158 | * of the number of nodes it has passed in a counter-variable. |
| 324 | 159 | * Each node, regardless of the number of passes, is counted only once. |
| 160 | * | |
| 161 | * @note Objects that are pointed to by an iterator are mutable through that | |
| 162 | * iterator. However, if the | |
| 845 | 163 | * underlying data structure is mutated by other means than this iterator (e.g., |
| 324 | 164 | * elements added or removed), the iterator becomes invalid (regardless of what |
| 165 | * cxIteratorValid() returns). | |
| 166 | * | |
| 167 | * @see CxIterator | |
| 168 | */ | |
| 169 | typedef struct cx_tree_visitor_s { | |
| 170 | /** | |
| 171 | * Base members. | |
| 172 | */ | |
| 173 | CX_ITERATOR_BASE; | |
| 174 | /** | |
| 175 | * Indicates whether the subtree below the current node shall be skipped. | |
| 176 | */ | |
| 177 | bool skip; | |
| 178 | /** | |
| 179 | * Offset in the node struct for the children linked list. | |
| 180 | */ | |
| 181 | ptrdiff_t loc_children; | |
| 182 | /** | |
| 183 | * Offset in the node struct for the next pointer. | |
| 184 | */ | |
| 185 | ptrdiff_t loc_next; | |
| 186 | /** | |
| 187 | * The total number of distinct nodes that have been passed so far. | |
| 188 | */ | |
| 189 | size_t counter; | |
| 190 | /** | |
| 191 | * The currently observed node. | |
| 192 | * | |
| 193 | * This is the same what cxIteratorCurrent() would return. | |
| 194 | */ | |
| 195 | void *node; | |
| 196 | /** | |
| 197 | * The current depth in the tree. | |
| 198 | */ | |
| 199 | size_t depth; | |
| 200 | /** | |
| 201 | * The next element in the visitor queue. | |
| 202 | */ | |
| 203 | struct cx_tree_visitor_queue_s *queue_next; | |
| 204 | /** | |
| 205 | * The last element in the visitor queue. | |
| 206 | */ | |
| 207 | struct cx_tree_visitor_queue_s *queue_last; | |
| 208 | } CxTreeVisitor; | |
| 209 | ||
| 210 | /** | |
| 211 | * Releases internal memory of the given tree iterator. | |
| 212 | * @param iter the iterator | |
| 213 | */ | |
| 440 | 214 | cx_attr_nonnull |
| 324 | 215 | static inline void cxTreeIteratorDispose(CxTreeIterator *iter) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
216 | cxFreeDefault(iter->stack); |
| 324 | 217 | iter->stack = NULL; |
| 218 | } | |
| 219 | ||
| 220 | /** | |
| 221 | * Releases internal memory of the given tree visitor. | |
| 222 | * @param visitor the visitor | |
| 223 | */ | |
| 440 | 224 | cx_attr_nonnull |
| 324 | 225 | static inline void cxTreeVisitorDispose(CxTreeVisitor *visitor) { |
| 226 | struct cx_tree_visitor_queue_s *q = visitor->queue_next; | |
| 227 | while (q != NULL) { | |
| 228 | struct cx_tree_visitor_queue_s *next = q->next; | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
229 | cxFreeDefault(q); |
| 324 | 230 | q = next; |
| 231 | } | |
| 232 | } | |
| 233 | ||
| 234 | /** | |
| 235 | * Advises the iterator to skip the subtree below the current node and | |
| 236 | * also continues the current loop. | |
| 237 | * | |
| 440 | 238 | * @param iterator (@c CxTreeIterator) the iterator |
| 324 | 239 | */ |
| 240 | #define cxTreeIteratorContinue(iterator) (iterator).skip = true; continue | |
| 241 | ||
| 242 | /** | |
| 243 | * Advises the visitor to skip the subtree below the current node and | |
| 244 | * also continues the current loop. | |
| 245 | * | |
| 440 | 246 | * @param visitor (@c CxTreeVisitor) the visitor |
| 324 | 247 | */ |
| 248 | #define cxTreeVisitorContinue(visitor) cxTreeIteratorContinue(visitor) | |
| 249 | ||
| 250 | /** | |
| 251 | * Links a node to a (new) parent. | |
| 252 | * | |
| 845 | 253 | * If the node already has a parent, it is unlinked, first. |
| 440 | 254 | * If the parent has children already, the node is @em appended to the list |
| 324 | 255 | * of all currently existing children. |
| 256 | * | |
| 257 | * @param parent the parent node | |
| 258 | * @param node the node that shall be linked | |
| 259 | * @param loc_parent offset in the node struct for the parent pointer | |
| 260 | * @param loc_children offset in the node struct for the children linked list | |
| 261 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 262 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 263 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 264 | * @param loc_next offset in the node struct for the next pointer |
| 265 | * @see cx_tree_unlink() | |
| 266 | */ | |
| 440 | 267 | 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
|
268 | cx_attr_export |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
269 | void cx_tree_link( |
| 440 | 270 | void *parent, |
| 271 | void *node, | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
272 | ptrdiff_t loc_parent, |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
273 | ptrdiff_t loc_children, |
| 324 | 274 | ptrdiff_t loc_last_child, |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
275 | ptrdiff_t loc_prev, |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
276 | ptrdiff_t loc_next |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
277 | ); |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
278 | |
| 324 | 279 | /** |
| 280 | * Unlinks a node from its parent. | |
| 281 | * | |
| 282 | * If the node has no parent, this function does nothing. | |
| 283 | * | |
| 284 | * @param node the node that shall be unlinked from its parent | |
| 285 | * @param loc_parent offset in the node struct for the parent pointer | |
| 286 | * @param loc_children offset in the node struct for the children linked list | |
| 287 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 288 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 289 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 290 | * @param loc_next offset in the node struct for the next pointer |
| 291 | * @see cx_tree_link() | |
| 292 | */ | |
| 440 | 293 | 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
|
294 | cx_attr_export |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
295 | void cx_tree_unlink( |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
296 | void *node, |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
297 | ptrdiff_t loc_parent, |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
298 | ptrdiff_t loc_children, |
| 324 | 299 | ptrdiff_t loc_last_child, |
| 300 | ptrdiff_t loc_prev, | |
| 301 | ptrdiff_t loc_next | |
| 302 | ); | |
| 303 | ||
| 304 | /** | |
| 440 | 305 | * Macro that can be used instead of the magic value for infinite search depth. |
| 306 | */ | |
| 307 | #define CX_TREE_SEARCH_INFINITE_DEPTH 0 | |
| 308 | ||
| 309 | /** | |
| 324 | 310 | * Function pointer for a search function. |
| 311 | * | |
| 440 | 312 | * A function of this kind shall check if the specified @p node |
| 313 | * contains the given @p data or if one of the children might contain | |
| 324 | 314 | * the data. |
| 315 | * | |
| 316 | * The function should use the returned integer to indicate how close the | |
| 317 | * match is, where a negative number means that it does not match at all. | |
| 440 | 318 | * Zero means exact match and a positive number is an implementation defined |
| 319 | * measure for the distance to an exact match. | |
| 324 | 320 | * |
| 845 | 321 | * For example, consider a tree that stores file path information. |
| 322 | * A node which is describing a parent directory of a searched file shall | |
| 324 | 323 | * return a positive number to indicate that a child node might contain the |
| 324 | * searched item. On the other hand, if the node denotes a path that is not a | |
| 325 | * prefix of the searched filename, the function would return -1 to indicate | |
| 326 | * that the search does not need to be continued in that branch. | |
| 327 | * | |
| 328 | * @param node the node that is currently investigated | |
| 329 | * @param data the data that is searched for | |
| 330 | * | |
| 331 | * @return 0 if the node contains the data, | |
| 332 | * positive if one of the children might contain the data, | |
| 845 | 333 | * negative if neither the node nor the children contains the data |
| 324 | 334 | */ |
| 440 | 335 | cx_attr_nonnull |
| 324 | 336 | typedef int (*cx_tree_search_data_func)(const void *node, const void *data); |
| 337 | ||
| 338 | ||
| 339 | /** | |
| 340 | * Function pointer for a search function. | |
| 341 | * | |
| 440 | 342 | * A function of this kind shall check if the specified @p node |
| 343 | * contains the same @p data as @p new_node or if one of the children might | |
| 324 | 344 | * contain the data. |
| 345 | * | |
| 346 | * The function should use the returned integer to indicate how close the | |
| 347 | * match is, where a negative number means that it does not match at all. | |
| 440 | 348 | * Zero means exact match and a positive number is an implementation defined |
| 349 | * measure for the distance to an exact match. | |
| 324 | 350 | * |
| 845 | 351 | * For example, consider a tree that stores file path information. |
| 352 | * A node which is describing a parent directory of a searched file shall | |
| 324 | 353 | * return a positive number to indicate that a child node might contain the |
| 354 | * searched item. On the other hand, if the node denotes a path that is not a | |
| 355 | * prefix of the searched filename, the function would return -1 to indicate | |
| 356 | * that the search does not need to be continued in that branch. | |
| 357 | * | |
| 358 | * @param node the node that is currently investigated | |
| 359 | * @param new_node a new node with the information which is searched | |
| 360 | * | |
| 440 | 361 | * @return 0 if @p node contains the same data as @p new_node, |
| 324 | 362 | * positive if one of the children might contain the data, |
| 845 | 363 | * negative if neither the node nor the children contains the data |
| 324 | 364 | */ |
| 440 | 365 | cx_attr_nonnull |
| 324 | 366 | typedef int (*cx_tree_search_func)(const void *node, const void *new_node); |
| 367 | ||
| 368 | /** | |
| 369 | * Searches for data in a tree. | |
| 370 | * | |
| 845 | 371 | * When the data cannot be found exactly, the search function might return the |
| 372 | * closest result, which might be a good starting point for adding a new node | |
| 324 | 373 | * to the tree (see also #cx_tree_add()). |
| 374 | * | |
| 845 | 375 | * Depending on the tree structure, it is not necessarily guaranteed that the |
| 324 | 376 | * "closest" match is uniquely defined. This function will search for a node |
| 440 | 377 | * with the best match according to the @p sfunc (meaning: the return value of |
| 378 | * @p sfunc which is closest to zero). If that is also ambiguous, an arbitrary | |
| 324 | 379 | * node matching the criteria is returned. |
| 380 | * | |
| 381 | * @param root the root node | |
| 440 | 382 | * @param depth the maximum depth (zero=indefinite, one=just root) |
| 324 | 383 | * @param data the data to search for |
| 384 | * @param sfunc the search function | |
| 385 | * @param result where the result shall be stored | |
| 386 | * @param loc_children offset in the node struct for the children linked list | |
| 387 | * @param loc_next offset in the node struct for the next pointer | |
| 388 | * @return zero if the node was found exactly, positive if a node was found that | |
| 389 | * could contain the node (but doesn't right now), negative if the tree does not | |
| 390 | * contain any node that might be related to the searched data | |
| 391 | */ | |
| 440 | 392 | cx_attr_nonnull |
| 393 | cx_attr_access_w(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
|
394 | cx_attr_export |
| 324 | 395 | int cx_tree_search_data( |
| 396 | const void *root, | |
| 440 | 397 | size_t depth, |
| 324 | 398 | const void *data, |
| 399 | cx_tree_search_data_func sfunc, | |
| 400 | void **result, | |
| 401 | ptrdiff_t loc_children, | |
| 402 | ptrdiff_t loc_next | |
| 403 | ); | |
| 404 | ||
| 405 | /** | |
| 406 | * Searches for a node in a tree. | |
| 407 | * | |
| 408 | * When no node with the same data can be found, the search function might | |
| 845 | 409 | * return the closest result, which might be a good starting point for adding the |
| 324 | 410 | * new node to the tree (see also #cx_tree_add()). |
| 411 | * | |
| 845 | 412 | * Depending on the tree structure, it is not necessarily guaranteed that the |
| 324 | 413 | * "closest" match is uniquely defined. This function will search for a node |
| 440 | 414 | * with the best match according to the @p sfunc (meaning: the return value of |
| 415 | * @p sfunc which is closest to zero). If that is also ambiguous, an arbitrary | |
| 324 | 416 | * node matching the criteria is returned. |
| 417 | * | |
| 418 | * @param root the root node | |
| 440 | 419 | * @param depth the maximum depth (zero=indefinite, one=just root) |
| 324 | 420 | * @param node the node to search for |
| 421 | * @param sfunc the search function | |
| 422 | * @param result where the result shall be stored | |
| 423 | * @param loc_children offset in the node struct for the children linked list | |
| 424 | * @param loc_next offset in the node struct for the next pointer | |
| 425 | * @return zero if the node was found exactly, positive if a node was found that | |
| 426 | * could contain the node (but doesn't right now), negative if the tree does not | |
| 427 | * contain any node that might be related to the searched data | |
| 428 | */ | |
| 440 | 429 | cx_attr_nonnull |
| 430 | cx_attr_access_w(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
|
431 | cx_attr_export |
| 324 | 432 | int cx_tree_search( |
| 433 | const void *root, | |
| 440 | 434 | size_t depth, |
| 324 | 435 | const void *node, |
| 436 | cx_tree_search_func sfunc, | |
| 437 | void **result, | |
| 438 | ptrdiff_t loc_children, | |
| 439 | ptrdiff_t loc_next | |
| 440 | ); | |
| 441 | ||
| 442 | /** | |
| 443 | * Creates a depth-first iterator for a tree with the specified root node. | |
| 444 | * | |
| 445 | * @note A tree iterator needs to maintain a stack of visited nodes, which is | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
446 | * allocated using the cxDefaultAllocator. |
| 324 | 447 | * When the iterator becomes invalid, this memory is automatically released. |
| 448 | * However, if you wish to cancel the iteration before the iterator becomes | |
| 449 | * invalid by itself, you MUST call cxTreeIteratorDispose() manually to release | |
| 450 | * the memory. | |
| 451 | * | |
| 452 | * @remark The returned iterator does not support cxIteratorFlagRemoval(). | |
| 453 | * | |
| 454 | * @param root the root node | |
| 455 | * @param visit_on_exit set to true, when the iterator shall visit a node again | |
| 456 | * after processing all children | |
| 457 | * @param loc_children offset in the node struct for the children linked list | |
| 458 | * @param loc_next offset in the node struct for the next pointer | |
| 459 | * @return the new tree iterator | |
| 460 | * @see cxTreeIteratorDispose() | |
| 461 | */ | |
| 440 | 462 | 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
|
463 | cx_attr_export |
| 324 | 464 | CxTreeIterator cx_tree_iterator( |
| 465 | void *root, | |
| 466 | bool visit_on_exit, | |
| 467 | ptrdiff_t loc_children, | |
| 468 | ptrdiff_t loc_next | |
| 469 | ); | |
| 470 | ||
| 471 | /** | |
| 472 | * Creates a breadth-first iterator for a tree with the specified root node. | |
| 473 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
474 | * @note A tree visitor needs to maintain a queue of to-be visited nodes, which |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
475 | * is allocated using the cxDefaultAllocator. |
| 324 | 476 | * When the visitor becomes invalid, this memory is automatically released. |
| 477 | * However, if you wish to cancel the iteration before the visitor becomes | |
| 478 | * invalid by itself, you MUST call cxTreeVisitorDispose() manually to release | |
| 479 | * the memory. | |
| 480 | * | |
| 481 | * @remark The returned iterator does not support cxIteratorFlagRemoval(). | |
| 482 | * | |
| 483 | * @param root the root node | |
| 484 | * @param loc_children offset in the node struct for the children linked list | |
| 485 | * @param loc_next offset in the node struct for the next pointer | |
| 486 | * @return the new tree visitor | |
| 487 | * @see cxTreeVisitorDispose() | |
| 488 | */ | |
| 440 | 489 | 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
|
490 | cx_attr_export |
| 324 | 491 | CxTreeVisitor cx_tree_visitor( |
| 492 | void *root, | |
| 493 | ptrdiff_t loc_children, | |
| 494 | ptrdiff_t loc_next | |
| 495 | ); | |
| 496 | ||
| 497 | /** | |
| 498 | * Describes a function that creates a tree node from the specified data. | |
| 845 | 499 | * The first argument points to the data the node shall contain, and |
| 500 | * the second argument may be used for additional data (e.g., an allocator). | |
| 324 | 501 | * Functions of this type shall either return a new pointer to a newly |
| 440 | 502 | * created node or @c NULL when allocation fails. |
| 324 | 503 | * |
| 440 | 504 | * @note the function may leave the node pointers in the struct uninitialized. |
| 324 | 505 | * The caller is responsible to set them according to the intended use case. |
| 506 | */ | |
| 440 | 507 | cx_attr_nonnull_arg(1) |
| 324 | 508 | typedef void *(*cx_tree_node_create_func)(const void *, void *); |
| 509 | ||
| 510 | /** | |
| 511 | * The local search depth for a new subtree when adding multiple elements. | |
| 512 | * The default value is 3. | |
| 513 | * This variable is used by #cx_tree_add_array() and #cx_tree_add_iter() to | |
| 514 | * implement optimized insertion of multiple elements into a tree. | |
| 515 | */ | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
516 | cx_attr_export |
| 324 | 517 | extern unsigned int cx_tree_add_look_around_depth; |
| 518 | ||
| 519 | /** | |
| 520 | * Adds multiple elements efficiently to a tree. | |
| 521 | * | |
| 522 | * Once an element cannot be added to the tree, this function returns, leaving | |
| 523 | * the iterator in a valid state pointing to the element that could not be | |
| 524 | * added. | |
| 440 | 525 | * Also, the pointer of the created node will be stored to @p failed. |
| 324 | 526 | * The integer returned by this function denotes the number of elements obtained |
| 440 | 527 | * from the @p iter that have been successfully processed. |
| 528 | * When all elements could be processed, a @c NULL pointer will be written to | |
| 529 | * @p failed. | |
| 324 | 530 | * |
| 531 | * The advantage of this function compared to multiple invocations of | |
| 532 | * #cx_tree_add() is that the search for the insert locations is not always | |
| 533 | * started from the root node. | |
| 534 | * Instead, the function checks #cx_tree_add_look_around_depth many parent nodes | |
| 535 | * of the current insert location before starting from the root node again. | |
| 536 | * When the variable is set to zero, only the last found location is checked | |
| 537 | * again. | |
| 538 | * | |
| 539 | * Refer to the documentation of #cx_tree_add() for more details. | |
| 540 | * | |
| 541 | * @param iter a pointer to an arbitrary iterator | |
| 542 | * @param num the maximum number of elements to obtain from the iterator | |
| 543 | * @param sfunc a search function | |
| 544 | * @param cfunc a node creation function | |
| 545 | * @param cdata optional additional data | |
| 546 | * @param root the root node of the tree | |
| 547 | * @param failed location where the pointer to a failed node shall be stored | |
| 548 | * @param loc_parent offset in the node struct for the parent pointer | |
| 549 | * @param loc_children offset in the node struct for the children linked list | |
| 550 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 551 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 552 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 553 | * @param loc_next offset in the node struct for the next pointer |
| 554 | * @return the number of nodes created and added | |
| 555 | * @see cx_tree_add() | |
| 556 | */ | |
| 440 | 557 | cx_attr_nonnull_arg(1, 3, 4, 6, 7) |
| 558 | cx_attr_access_w(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
|
559 | cx_attr_export |
| 324 | 560 | size_t cx_tree_add_iter( |
| 561 | struct cx_iterator_base_s *iter, | |
| 562 | size_t num, | |
| 563 | cx_tree_search_func sfunc, | |
| 564 | cx_tree_node_create_func cfunc, | |
| 565 | void *cdata, | |
| 566 | void **failed, | |
| 567 | void *root, | |
| 568 | ptrdiff_t loc_parent, | |
| 569 | ptrdiff_t loc_children, | |
| 570 | ptrdiff_t loc_last_child, | |
| 571 | ptrdiff_t loc_prev, | |
| 572 | ptrdiff_t loc_next | |
| 573 | ); | |
| 574 | ||
| 575 | /** | |
| 576 | * Adds multiple elements efficiently to a tree. | |
| 577 | * | |
| 578 | * Once an element cannot be added to the tree, this function returns, storing | |
| 440 | 579 | * the pointer of the created node to @p failed. |
| 324 | 580 | * The integer returned by this function denotes the number of elements from |
| 440 | 581 | * the @p src array that have been successfully processed. |
| 582 | * When all elements could be processed, a @c NULL pointer will be written to | |
| 583 | * @p failed. | |
| 324 | 584 | * |
| 585 | * The advantage of this function compared to multiple invocations of | |
| 586 | * #cx_tree_add() is that the search for the insert locations is not always | |
| 587 | * started from the root node. | |
| 588 | * Instead, the function checks #cx_tree_add_look_around_depth many parent nodes | |
| 589 | * of the current insert location before starting from the root node again. | |
| 590 | * When the variable is set to zero, only the last found location is checked | |
| 591 | * again. | |
| 592 | * | |
| 593 | * Refer to the documentation of #cx_tree_add() for more details. | |
| 594 | * | |
| 595 | * @param src a pointer to the source data array | |
| 440 | 596 | * @param num the number of elements in the @p src array |
| 597 | * @param elem_size the size of each element in the @p src array | |
| 324 | 598 | * @param sfunc a search function |
| 599 | * @param cfunc a node creation function | |
| 600 | * @param cdata optional additional data | |
| 601 | * @param failed location where the pointer to a failed node shall be stored | |
| 602 | * @param root the root node of the tree | |
| 603 | * @param loc_parent offset in the node struct for the parent pointer | |
| 604 | * @param loc_children offset in the node struct for the children linked list | |
| 605 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 606 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 607 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 608 | * @param loc_next offset in the node struct for the next pointer |
| 609 | * @return the number of array elements successfully processed | |
| 610 | * @see cx_tree_add() | |
| 611 | */ | |
| 440 | 612 | cx_attr_nonnull_arg(1, 4, 5, 7, 8) |
| 613 | cx_attr_access_w(7) | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
614 | cx_attr_export |
| 324 | 615 | size_t cx_tree_add_array( |
| 616 | const void *src, | |
| 617 | size_t num, | |
| 618 | size_t elem_size, | |
| 619 | cx_tree_search_func sfunc, | |
| 620 | cx_tree_node_create_func cfunc, | |
| 621 | void *cdata, | |
| 622 | void **failed, | |
| 623 | void *root, | |
| 624 | ptrdiff_t loc_parent, | |
| 625 | ptrdiff_t loc_children, | |
| 626 | ptrdiff_t loc_last_child, | |
| 627 | ptrdiff_t loc_prev, | |
| 628 | ptrdiff_t loc_next | |
| 629 | ); | |
| 630 | ||
| 631 | /** | |
| 632 | * Adds data to a tree. | |
| 633 | * | |
| 634 | * An adequate location where to add the new tree node is searched with the | |
| 440 | 635 | * specified @p sfunc. |
| 324 | 636 | * |
| 440 | 637 | * When a location is found, the @p cfunc will be invoked with @p cdata. |
| 324 | 638 | * |
| 440 | 639 | * The node returned by @p cfunc will be linked into the tree. |
| 845 | 640 | * When @p sfunc returns a positive integer, the new node will be linked as a |
| 324 | 641 | * child. The other children (now siblings of the new node) are then checked |
| 440 | 642 | * with @p sfunc, whether they could be children of the new node and re-linked |
| 324 | 643 | * accordingly. |
| 644 | * | |
| 845 | 645 | * When @p sfunc returns zero and the found node has a parent, the new |
| 646 | * node will be added as a sibling - otherwise, the new node will be added | |
| 324 | 647 | * as a child. |
| 648 | * | |
| 845 | 649 | * When @p sfunc returns a negative value, the new node will not be added to |
| 650 | * the tree, and this function returns a non-zero value. | |
| 440 | 651 | * The caller should check if @p cnode contains a node pointer and deal with the |
| 324 | 652 | * node that could not be added. |
| 653 | * | |
| 440 | 654 | * This function also returns a non-zero value when @p cfunc tries to allocate |
| 655 | * a new node but fails to do so. In that case, the pointer stored to @p cnode | |
| 656 | * will be @c NULL. | |
| 324 | 657 | * |
| 658 | * Multiple elements can be added more efficiently with | |
| 659 | * #cx_tree_add_array() or #cx_tree_add_iter(). | |
| 660 | * | |
| 661 | * @param src a pointer to the data | |
| 662 | * @param sfunc a search function | |
| 663 | * @param cfunc a node creation function | |
| 664 | * @param cdata optional additional data | |
| 665 | * @param cnode the location where a pointer to the new node is stored | |
| 666 | * @param root the root node of the tree | |
| 667 | * @param loc_parent offset in the node struct for the parent pointer | |
| 668 | * @param loc_children offset in the node struct for the children linked list | |
| 669 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 670 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 671 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 672 | * @param loc_next offset in the node struct for the next pointer |
| 673 | * @return zero when a new node was created and added to the tree, | |
| 674 | * non-zero otherwise | |
| 675 | */ | |
| 440 | 676 | cx_attr_nonnull_arg(1, 2, 3, 5, 6) |
| 677 | cx_attr_access_w(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
|
678 | cx_attr_export |
| 324 | 679 | int cx_tree_add( |
| 680 | const void *src, | |
| 681 | cx_tree_search_func sfunc, | |
| 682 | cx_tree_node_create_func cfunc, | |
| 683 | void *cdata, | |
| 684 | void **cnode, | |
| 685 | void *root, | |
| 686 | ptrdiff_t loc_parent, | |
| 687 | ptrdiff_t loc_children, | |
| 688 | ptrdiff_t loc_last_child, | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
689 | ptrdiff_t loc_prev, |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
690 | ptrdiff_t loc_next |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
691 | ); |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
692 | |
| 324 | 693 | |
| 694 | /** | |
| 695 | * Tree class type. | |
| 696 | */ | |
| 697 | typedef struct cx_tree_class_s cx_tree_class; | |
| 698 | ||
| 699 | /** | |
| 700 | * Base structure that can be used for tree nodes in a #CxTree. | |
| 701 | */ | |
| 702 | struct cx_tree_node_base_s { | |
| 703 | /** | |
| 704 | * Pointer to the parent. | |
| 705 | */ | |
| 706 | struct cx_tree_node_base_s *parent; | |
| 707 | /** | |
| 708 | * Pointer to the first child. | |
| 709 | */ | |
| 710 | struct cx_tree_node_base_s *children; | |
| 711 | /** | |
| 712 | * Pointer to the last child. | |
| 713 | */ | |
| 714 | struct cx_tree_node_base_s *last_child; | |
| 715 | /** | |
| 716 | * Pointer to the previous sibling. | |
| 717 | */ | |
| 718 | struct cx_tree_node_base_s *prev; | |
| 719 | /** | |
| 720 | * Pointer to the next sibling. | |
| 721 | */ | |
| 722 | struct cx_tree_node_base_s *next; | |
| 723 | }; | |
| 724 | ||
| 725 | /** | |
| 726 | * Structure for holding the base data of a tree. | |
| 727 | */ | |
| 728 | struct cx_tree_s { | |
| 729 | /** | |
| 730 | * The tree class definition. | |
| 731 | */ | |
| 732 | const cx_tree_class *cl; | |
| 733 | ||
| 734 | /** | |
| 735 | * Allocator to allocate new nodes. | |
| 736 | */ | |
| 737 | const CxAllocator *allocator; | |
| 738 | ||
| 739 | /** | |
| 740 | * A pointer to the root node. | |
| 741 | * | |
| 440 | 742 | * Will be @c NULL when @c size is 0. |
| 324 | 743 | */ |
| 744 | void *root; | |
| 745 | ||
| 746 | /** | |
| 747 | * A function to create new nodes. | |
| 748 | * | |
| 749 | * Invocations to this function will receive a pointer to this tree | |
| 845 | 750 | * structure as the second argument. |
| 324 | 751 | * |
| 845 | 752 | * Nodes MAY use #cx_tree_node_base_s as the base layout, but do not need to. |
| 324 | 753 | */ |
| 754 | cx_tree_node_create_func node_create; | |
| 755 | ||
| 756 | /** | |
| 757 | * An optional simple destructor for the tree nodes. | |
| 758 | */ | |
| 759 | cx_destructor_func simple_destructor; | |
| 760 | ||
| 761 | /** | |
| 762 | * An optional advanced destructor for the tree nodes. | |
| 763 | */ | |
| 764 | cx_destructor_func2 advanced_destructor; | |
| 765 | ||
| 766 | /** | |
| 767 | * The pointer to additional data that is passed to the advanced destructor. | |
| 768 | */ | |
| 769 | void *destructor_data; | |
| 770 | ||
| 771 | /** | |
| 772 | * A function to compare two nodes. | |
| 773 | */ | |
| 774 | cx_tree_search_func search; | |
| 775 | ||
| 776 | /** | |
| 777 | * A function to compare a node with data. | |
| 778 | */ | |
| 779 | cx_tree_search_data_func search_data; | |
| 780 | ||
| 781 | /** | |
| 782 | * The number of currently stored elements. | |
| 783 | */ | |
| 784 | size_t size; | |
| 785 | ||
| 786 | /** | |
| 787 | * Offset in the node struct for the parent pointer. | |
| 788 | */ | |
| 789 | ptrdiff_t loc_parent; | |
| 790 | ||
| 791 | /** | |
| 792 | * Offset in the node struct for the children linked list. | |
| 793 | */ | |
| 794 | ptrdiff_t loc_children; | |
| 795 | ||
| 796 | /** | |
| 797 | * Optional offset in the node struct for the pointer to the last child | |
| 798 | * in the linked list (negative if there is no such pointer). | |
| 799 | */ | |
| 800 | ptrdiff_t loc_last_child; | |
| 801 | ||
| 802 | /** | |
| 803 | * Offset in the node struct for the previous sibling pointer. | |
| 804 | */ | |
| 805 | ptrdiff_t loc_prev; | |
| 806 | ||
| 807 | /** | |
| 808 | * Offset in the node struct for the next sibling pointer. | |
| 809 | */ | |
| 810 | ptrdiff_t loc_next; | |
| 811 | }; | |
| 812 | ||
| 813 | /** | |
| 814 | * Macro to roll out the #cx_tree_node_base_s structure with a custom | |
| 815 | * node type. | |
| 440 | 816 | * |
| 845 | 817 | * Must be used as the first member in your custom tree struct. |
| 440 | 818 | * |
| 819 | * @param type the data type for the nodes | |
| 324 | 820 | */ |
| 821 | #define CX_TREE_NODE_BASE(type) \ | |
| 822 | type *parent; \ | |
| 823 | type *children;\ | |
| 824 | type *last_child;\ | |
| 825 | type *prev;\ | |
| 826 | type *next | |
| 827 | ||
| 828 | /** | |
| 829 | * Macro for specifying the layout of a base node tree. | |
| 440 | 830 | * |
| 831 | * When your tree uses #CX_TREE_NODE_BASE, you can use this | |
| 832 | * macro in all tree functions that expect the layout parameters | |
| 833 | * @c loc_parent, @c loc_children, @c loc_last_child, @c loc_prev, | |
| 834 | * and @c loc_next. | |
| 324 | 835 | */ |
| 836 | #define cx_tree_node_base_layout \ | |
| 837 | offsetof(struct cx_tree_node_base_s, parent),\ | |
| 838 | offsetof(struct cx_tree_node_base_s, children),\ | |
| 839 | offsetof(struct cx_tree_node_base_s, last_child),\ | |
| 840 | offsetof(struct cx_tree_node_base_s, prev), \ | |
| 841 | offsetof(struct cx_tree_node_base_s, next) | |
| 842 | ||
| 843 | /** | |
| 844 | * The class definition for arbitrary trees. | |
| 845 | */ | |
| 846 | struct cx_tree_class_s { | |
| 847 | /** | |
| 848 | * Member function for inserting a single element. | |
| 849 | * | |
| 440 | 850 | * Implementations SHALL NOT simply invoke @p insert_many as this comes |
| 324 | 851 | * with too much overhead. |
| 852 | */ | |
| 853 | int (*insert_element)( | |
| 854 | struct cx_tree_s *tree, | |
| 855 | const void *data | |
| 856 | ); | |
| 857 | ||
| 858 | /** | |
| 859 | * Member function for inserting multiple elements. | |
| 860 | * | |
| 845 | 861 | * Implementations SHALL avoid performing a full search in the tree for |
| 324 | 862 | * every element even though the source data MAY be unsorted. |
| 863 | */ | |
| 864 | size_t (*insert_many)( | |
| 865 | struct cx_tree_s *tree, | |
| 866 | struct cx_iterator_base_s *iter, | |
| 867 | size_t n | |
| 868 | ); | |
| 869 | ||
| 870 | /** | |
| 871 | * Member function for finding a node. | |
| 872 | */ | |
| 873 | void *(*find)( | |
| 874 | struct cx_tree_s *tree, | |
| 875 | const void *subtree, | |
| 440 | 876 | const void *data, |
| 877 | size_t depth | |
| 324 | 878 | ); |
| 879 | }; | |
| 880 | ||
| 881 | /** | |
| 882 | * Common type for all tree implementations. | |
| 883 | */ | |
| 884 | typedef struct cx_tree_s CxTree; | |
| 885 | ||
| 440 | 886 | |
| 887 | /** | |
| 845 | 888 | * Destroys a node and its subtree. |
| 440 | 889 | * |
| 890 | * It is guaranteed that the simple destructor is invoked before | |
| 891 | * the advanced destructor, starting with the leaf nodes of the subtree. | |
| 892 | * | |
| 893 | * When this function is invoked on the root node of the tree, it destroys the | |
| 894 | * tree contents, but - in contrast to #cxTreeFree() - not the tree | |
| 895 | * structure, leaving an empty tree behind. | |
| 896 | * | |
| 897 | * @note The destructor function, if any, will @em not be invoked. That means | |
| 898 | * you will need to free the removed subtree by yourself, eventually. | |
| 899 | * | |
| 900 | * @attention This function will not free the memory of the nodes with the | |
| 901 | * tree's allocator, because that is usually done by the advanced destructor | |
| 902 | * and would therefore result in a double-free. | |
| 903 | * | |
| 904 | * @param tree the tree | |
| 905 | * @param node the node to remove | |
| 906 | * @see cxTreeFree() | |
| 907 | */ | |
| 908 | 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
|
909 | cx_attr_export |
| 440 | 910 | void cxTreeDestroySubtree(CxTree *tree, void *node); |
| 911 | ||
| 912 | ||
| 913 | /** | |
| 914 | * Destroys the tree contents. | |
| 915 | * | |
| 916 | * It is guaranteed that the simple destructor is invoked before | |
| 917 | * the advanced destructor, starting with the leaf nodes of the subtree. | |
| 918 | * | |
| 919 | * This is a convenience macro for invoking #cxTreeDestroySubtree() on the | |
| 920 | * root node of the tree. | |
| 921 | * | |
| 922 | * @attention Be careful when calling this function when no destructor function | |
| 923 | * is registered that actually frees the memory of nodes. In that case you will | |
| 845 | 924 | * need a reference to the (former) root node of the tree somewhere, or |
| 440 | 925 | * otherwise you will be leaking memory. |
| 926 | * | |
| 927 | * @param tree the tree | |
| 928 | * @see cxTreeDestroySubtree() | |
| 929 | */ | |
| 930 | #define cxTreeClear(tree) cxTreeDestroySubtree(tree, tree->root) | |
| 931 | ||
| 932 | /** | |
| 933 | * Deallocates the tree structure. | |
| 934 | * | |
| 935 | * The destructor functions are invoked for each node, starting with the leaf | |
| 936 | * nodes. | |
| 937 | * It is guaranteed that for each node the simple destructor is invoked before | |
| 938 | * the advanced destructor. | |
| 939 | * | |
| 940 | * @attention This function will only invoke the destructor functions | |
| 941 | * on the nodes. | |
| 942 | * It will NOT additionally free the nodes with the tree's allocator, because | |
| 943 | * that would cause a double-free in most scenarios where the advanced | |
| 944 | * destructor is already freeing the memory. | |
| 945 | * | |
| 946 | * @param tree the tree to free | |
| 947 | */ | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
948 | cx_attr_export |
| 440 | 949 | void cxTreeFree(CxTree *tree); |
| 950 | ||
| 324 | 951 | /** |
| 952 | * Creates a new tree structure based on the specified layout. | |
| 953 | * | |
| 440 | 954 | * The specified @p allocator will be used for creating the tree struct |
| 955 | * and SHALL be used by @p create_func to allocate memory for the nodes. | |
| 324 | 956 | * |
| 440 | 957 | * @note This function will also register an advanced destructor which |
| 324 | 958 | * will free the nodes with the allocator's free() method. |
| 959 | * | |
| 960 | * @param allocator the allocator that shall be used | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
961 | * (if @c NULL, the cxDefaultAllocator will be used) |
| 324 | 962 | * @param create_func a function that creates new nodes |
| 963 | * @param search_func a function that compares two nodes | |
| 964 | * @param search_data_func a function that compares a node with data | |
| 965 | * @param loc_parent offset in the node struct for the parent pointer | |
| 966 | * @param loc_children offset in the node struct for the children linked list | |
| 967 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 968 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 969 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 970 | * @param loc_next offset in the node struct for the next pointer |
| 971 | * @return the new tree | |
| 972 | * @see cxTreeCreateSimple() | |
| 973 | * @see cxTreeCreateWrapped() | |
| 974 | */ | |
| 440 | 975 | cx_attr_nonnull_arg(2, 3, 4) |
| 976 | cx_attr_nodiscard | |
| 977 | cx_attr_malloc | |
| 978 | cx_attr_dealloc(cxTreeFree, 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
|
979 | cx_attr_export |
| 324 | 980 | CxTree *cxTreeCreate( |
| 981 | const CxAllocator *allocator, | |
| 982 | cx_tree_node_create_func create_func, | |
| 983 | cx_tree_search_func search_func, | |
| 984 | cx_tree_search_data_func search_data_func, | |
| 985 | ptrdiff_t loc_parent, | |
| 986 | ptrdiff_t loc_children, | |
| 987 | ptrdiff_t loc_last_child, | |
| 988 | ptrdiff_t loc_prev, | |
| 989 | ptrdiff_t loc_next | |
| 990 | ); | |
| 991 | ||
| 992 | /** | |
| 993 | * Creates a new tree structure based on a default layout. | |
| 994 | * | |
| 845 | 995 | * Nodes created by @p create_func MUST contain #cx_tree_node_base_s as the first |
| 324 | 996 | * member (or at least respect the default offsets specified in the tree |
| 845 | 997 | * struct), and they MUST be allocated with the specified allocator. |
| 324 | 998 | * |
| 440 | 999 | * @note This function will also register an advanced destructor which |
| 324 | 1000 | * will free the nodes with the allocator's free() method. |
| 1001 | * | |
| 440 | 1002 | * @param allocator (@c CxAllocator*) the allocator that shall be used |
| 1003 | * @param create_func (@c cx_tree_node_create_func) a function that creates new nodes | |
| 1004 | * @param search_func (@c cx_tree_search_func) a function that compares two nodes | |
| 1005 | * @param search_data_func (@c cx_tree_search_data_func) a function that compares a node with data | |
| 1006 | * @return (@c CxTree*) the new tree | |
| 324 | 1007 | * @see cxTreeCreate() |
| 1008 | */ | |
| 440 | 1009 | #define cxTreeCreateSimple(\ |
| 1010 | allocator, create_func, search_func, search_data_func \ | |
| 1011 | ) cxTreeCreate(allocator, create_func, search_func, search_data_func, \ | |
| 1012 | cx_tree_node_base_layout) | |
| 324 | 1013 | |
| 1014 | /** | |
| 1015 | * Creates a new tree structure based on an existing tree. | |
| 1016 | * | |
| 440 | 1017 | * The specified @p allocator will be used for creating the tree struct. |
| 324 | 1018 | * |
| 440 | 1019 | * @attention This function will create an incompletely defined tree structure |
| 324 | 1020 | * where neither the create function, the search function, nor a destructor |
| 1021 | * will be set. If you wish to use any of this functionality for the wrapped | |
| 845 | 1022 | * tree, you need to specify those functions afterward. |
| 324 | 1023 | * |
| 440 | 1024 | * @param allocator the allocator that was used for nodes of the wrapped tree |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1025 | * (if @c NULL, the cxDefaultAllocator is assumed) |
| 324 | 1026 | * @param root the root node of the tree that shall be wrapped |
| 1027 | * @param loc_parent offset in the node struct for the parent pointer | |
| 1028 | * @param loc_children offset in the node struct for the children linked list | |
| 1029 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 1030 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 1031 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 1032 | * @param loc_next offset in the node struct for the next pointer |
| 1033 | * @return the new tree | |
| 1034 | * @see cxTreeCreate() | |
| 1035 | */ | |
| 440 | 1036 | cx_attr_nonnull_arg(2) |
| 1037 | cx_attr_nodiscard | |
| 1038 | cx_attr_malloc | |
| 1039 | cx_attr_dealloc(cxTreeFree, 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
|
1040 | cx_attr_export |
| 324 | 1041 | CxTree *cxTreeCreateWrapped( |
| 1042 | const CxAllocator *allocator, | |
| 1043 | void *root, | |
| 1044 | ptrdiff_t loc_parent, | |
| 1045 | ptrdiff_t loc_children, | |
| 1046 | ptrdiff_t loc_last_child, | |
| 1047 | ptrdiff_t loc_prev, | |
| 1048 | ptrdiff_t loc_next | |
| 1049 | ); | |
| 1050 | ||
| 1051 | /** | |
| 1052 | * Inserts data into the tree. | |
| 1053 | * | |
| 440 | 1054 | * @remark For this function to work, the tree needs specified search and |
| 324 | 1055 | * create functions, which might not be available for wrapped trees |
| 1056 | * (see #cxTreeCreateWrapped()). | |
| 1057 | * | |
| 1058 | * @param tree the tree | |
| 1059 | * @param data the data to insert | |
| 440 | 1060 | * @retval zero success |
| 1061 | * @retval non-zero failure | |
| 324 | 1062 | */ |
| 440 | 1063 | cx_attr_nonnull |
| 324 | 1064 | static inline int cxTreeInsert( |
| 1065 | CxTree *tree, | |
| 1066 | const void *data | |
| 1067 | ) { | |
| 1068 | return tree->cl->insert_element(tree, data); | |
| 1069 | } | |
| 1070 | ||
| 1071 | /** | |
| 1072 | * Inserts elements provided by an iterator efficiently into the tree. | |
| 1073 | * | |
| 440 | 1074 | * @remark For this function to work, the tree needs specified search and |
| 324 | 1075 | * create functions, which might not be available for wrapped trees |
| 1076 | * (see #cxTreeCreateWrapped()). | |
| 1077 | * | |
| 1078 | * @param tree the tree | |
| 1079 | * @param iter the iterator providing the elements | |
| 1080 | * @param n the maximum number of elements to insert | |
| 1081 | * @return the number of elements that could be successfully inserted | |
| 1082 | */ | |
| 440 | 1083 | cx_attr_nonnull |
| 324 | 1084 | static inline size_t cxTreeInsertIter( |
| 1085 | CxTree *tree, | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1086 | CxIteratorBase *iter, |
| 324 | 1087 | size_t n |
| 1088 | ) { | |
| 1089 | return tree->cl->insert_many(tree, iter, n); | |
| 1090 | } | |
| 1091 | ||
| 1092 | /** | |
| 1093 | * Inserts an array of data efficiently into the tree. | |
| 1094 | * | |
| 440 | 1095 | * @remark For this function to work, the tree needs specified search and |
| 324 | 1096 | * create functions, which might not be available for wrapped trees |
| 1097 | * (see #cxTreeCreateWrapped()). | |
| 1098 | * | |
| 1099 | * @param tree the tree | |
| 1100 | * @param data the array of data to insert | |
| 1101 | * @param elem_size the size of each element in the array | |
| 1102 | * @param n the number of elements in the array | |
| 1103 | * @return the number of elements that could be successfully inserted | |
| 1104 | */ | |
| 440 | 1105 | cx_attr_nonnull |
| 324 | 1106 | static inline size_t cxTreeInsertArray( |
| 1107 | CxTree *tree, | |
| 1108 | const void *data, | |
| 1109 | size_t elem_size, | |
| 1110 | size_t n | |
| 1111 | ) { | |
| 1112 | if (n == 0) return 0; | |
| 1113 | if (n == 1) return 0 == cxTreeInsert(tree, data) ? 1 : 0; | |
| 1114 | CxIterator iter = cxIterator(data, elem_size, n); | |
| 1115 | return cxTreeInsertIter(tree, cxIteratorRef(iter), n); | |
| 1116 | } | |
| 1117 | ||
| 1118 | /** | |
| 1119 | * Searches the data in the specified tree. | |
| 1120 | * | |
| 440 | 1121 | * @remark For this function to work, the tree needs a specified @c search_data |
| 324 | 1122 | * function, which might not be available wrapped trees |
| 1123 | * (see #cxTreeCreateWrapped()). | |
| 1124 | * | |
| 1125 | * @param tree the tree | |
| 1126 | * @param data the data to search for | |
| 440 | 1127 | * @return the first matching node, or @c NULL when the data cannot be found |
| 324 | 1128 | */ |
| 440 | 1129 | cx_attr_nonnull |
| 1130 | cx_attr_nodiscard | |
| 324 | 1131 | static inline void *cxTreeFind( |
| 1132 | CxTree *tree, | |
| 1133 | const void *data | |
| 1134 | ) { | |
| 440 | 1135 | return tree->cl->find(tree, tree->root, data, 0); |
| 324 | 1136 | } |
| 1137 | ||
| 1138 | /** | |
| 1139 | * Searches the data in the specified subtree. | |
| 1140 | * | |
| 440 | 1141 | * When @p max_depth is zero, the depth is not limited. |
| 1142 | * The @p subtree_root itself is on depth 1 and its children have depth 2. | |
| 1143 | * | |
| 1144 | * @note When @p subtree_root is not part of the @p tree, the behavior is | |
| 324 | 1145 | * undefined. |
| 1146 | * | |
| 440 | 1147 | * @remark For this function to work, the tree needs a specified @c search_data |
| 324 | 1148 | * function, which might not be the case for wrapped trees |
| 1149 | * (see #cxTreeCreateWrapped()). | |
| 1150 | * | |
| 1151 | * @param tree the tree | |
| 1152 | * @param data the data to search for | |
| 1153 | * @param subtree_root the node where to start | |
| 440 | 1154 | * @param max_depth the maximum search depth |
| 1155 | * @return the first matching node, or @c NULL when the data cannot be found | |
| 324 | 1156 | */ |
| 440 | 1157 | cx_attr_nonnull |
| 1158 | cx_attr_nodiscard | |
| 324 | 1159 | static inline void *cxTreeFindInSubtree( |
| 1160 | CxTree *tree, | |
| 1161 | const void *data, | |
| 440 | 1162 | void *subtree_root, |
| 1163 | size_t max_depth | |
| 324 | 1164 | ) { |
| 440 | 1165 | return tree->cl->find(tree, subtree_root, data, max_depth); |
| 324 | 1166 | } |
| 1167 | ||
| 1168 | /** | |
| 1169 | * Determines the size of the specified subtree. | |
| 1170 | * | |
| 1171 | * @param tree the tree | |
| 1172 | * @param subtree_root the root node of the subtree | |
| 1173 | * @return the number of nodes in the specified subtree | |
| 1174 | */ | |
| 440 | 1175 | cx_attr_nonnull |
| 1176 | 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
|
1177 | cx_attr_export |
| 324 | 1178 | size_t cxTreeSubtreeSize(CxTree *tree, void *subtree_root); |
| 1179 | ||
| 1180 | /** | |
| 1181 | * Determines the depth of the specified subtree. | |
| 1182 | * | |
| 1183 | * @param tree the tree | |
| 1184 | * @param subtree_root the root node of the subtree | |
| 440 | 1185 | * @return the tree depth including the @p subtree_root |
| 324 | 1186 | */ |
| 440 | 1187 | cx_attr_nonnull |
| 1188 | 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
|
1189 | cx_attr_export |
| 324 | 1190 | size_t cxTreeSubtreeDepth(CxTree *tree, void *subtree_root); |
| 1191 | ||
| 1192 | /** | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1193 | * Determines the size of the entire tree. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1194 | * |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1195 | * @param tree the tree |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1196 | * @return the tree size, counting the root as one |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1197 | */ |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1198 | cx_attr_nonnull |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1199 | cx_attr_nodiscard |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1200 | static inline size_t cxTreeSize(CxTree *tree) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1201 | return tree->size; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1202 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1203 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1204 | /** |
| 324 | 1205 | * Determines the depth of the entire tree. |
| 1206 | * | |
| 1207 | * @param tree the tree | |
| 1208 | * @return the tree depth, counting the root as one | |
| 1209 | */ | |
| 440 | 1210 | cx_attr_nonnull |
| 1211 | 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
|
1212 | cx_attr_export |
| 324 | 1213 | size_t cxTreeDepth(CxTree *tree); |
| 1214 | ||
| 1215 | /** | |
| 440 | 1216 | * Creates a depth-first iterator for the specified tree starting in @p node. |
| 1217 | * | |
| 1218 | * If the node is not part of the tree, the behavior is undefined. | |
| 1219 | * | |
| 1220 | * @param tree the tree to iterate | |
| 1221 | * @param node the node where to start | |
| 1222 | * @param visit_on_exit true, if the iterator shall visit a node again when | |
| 1223 | * leaving the subtree | |
| 1224 | * @return a tree iterator (depth-first) | |
| 1225 | * @see cxTreeVisit() | |
| 1226 | */ | |
| 1227 | cx_attr_nonnull | |
| 1228 | cx_attr_nodiscard | |
| 1229 | static inline CxTreeIterator cxTreeIterateSubtree( | |
| 1230 | CxTree *tree, | |
| 1231 | void *node, | |
| 1232 | bool visit_on_exit | |
| 1233 | ) { | |
| 1234 | return cx_tree_iterator( | |
| 1235 | node, visit_on_exit, | |
| 1236 | tree->loc_children, tree->loc_next | |
| 1237 | ); | |
| 1238 | } | |
| 1239 | ||
| 1240 | /** | |
| 1241 | * Creates a breadth-first iterator for the specified tree starting in @p node. | |
| 1242 | * | |
| 1243 | * If the node is not part of the tree, the behavior is undefined. | |
| 1244 | * | |
| 1245 | * @param tree the tree to iterate | |
| 1246 | * @param node the node where to start | |
| 1247 | * @return a tree visitor (a.k.a. breadth-first iterator) | |
| 1248 | * @see cxTreeIterate() | |
| 1249 | */ | |
| 1250 | cx_attr_nonnull | |
| 1251 | cx_attr_nodiscard | |
| 1252 | static inline CxTreeVisitor cxTreeVisitSubtree(CxTree *tree, void *node) { | |
| 1253 | return cx_tree_visitor( | |
| 1254 | node, tree->loc_children, tree->loc_next | |
| 1255 | ); | |
| 1256 | } | |
| 1257 | ||
| 1258 | /** | |
| 324 | 1259 | * Creates a depth-first iterator for the specified tree. |
| 1260 | * | |
| 1261 | * @param tree the tree to iterate | |
| 1262 | * @param visit_on_exit true, if the iterator shall visit a node again when | |
| 440 | 1263 | * leaving the subtree |
| 324 | 1264 | * @return a tree iterator (depth-first) |
| 440 | 1265 | * @see cxTreeVisit() |
| 324 | 1266 | */ |
| 440 | 1267 | cx_attr_nonnull |
| 1268 | cx_attr_nodiscard | |
| 1269 | static inline CxTreeIterator cxTreeIterate( | |
| 324 | 1270 | CxTree *tree, |
| 1271 | bool visit_on_exit | |
| 1272 | ) { | |
| 440 | 1273 | return cxTreeIterateSubtree(tree, tree->root, visit_on_exit); |
| 324 | 1274 | } |
| 1275 | ||
| 1276 | /** | |
| 1277 | * Creates a breadth-first iterator for the specified tree. | |
| 1278 | * | |
| 1279 | * @param tree the tree to iterate | |
| 1280 | * @return a tree visitor (a.k.a. breadth-first iterator) | |
| 440 | 1281 | * @see cxTreeIterate() |
| 324 | 1282 | */ |
| 440 | 1283 | cx_attr_nonnull |
| 1284 | cx_attr_nodiscard | |
| 1285 | static inline CxTreeVisitor cxTreeVisit(CxTree *tree) { | |
| 1286 | return cxTreeVisitSubtree(tree, tree->root); | |
| 324 | 1287 | } |
| 1288 | ||
| 1289 | /** | |
| 440 | 1290 | * Sets the (new) parent of the specified child. |
| 1291 | * | |
| 845 | 1292 | * If the @p child is not already a member of the tree, this function behaves |
| 440 | 1293 | * as #cxTreeAddChildNode(). |
| 1294 | * | |
| 1295 | * @param tree the tree | |
| 1296 | * @param parent the (new) parent of the child | |
| 1297 | * @param child the node to add | |
| 1298 | * @see cxTreeAddChildNode() | |
| 1299 | */ | |
| 1300 | 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
|
1301 | cx_attr_export |
| 440 | 1302 | void cxTreeSetParent( |
| 1303 | CxTree *tree, | |
| 1304 | void *parent, | |
| 1305 | void *child | |
| 1306 | ); | |
| 1307 | ||
| 1308 | /** | |
| 324 | 1309 | * Adds a new node to the tree. |
| 1310 | * | |
| 845 | 1311 | * If the @p child is already a member of the tree, the behavior is undefined. |
| 440 | 1312 | * Use #cxTreeSetParent() if you want to move a subtree to another location. |
| 1313 | * | |
| 1314 | * @attention The node may be externally created, but MUST obey the same rules | |
| 845 | 1315 | * as if it was created by the tree itself with #cxTreeAddChild() (e.g., use |
| 324 | 1316 | * the same allocator). |
| 1317 | * | |
| 1318 | * @param tree the tree | |
| 1319 | * @param parent the parent of the node to add | |
| 1320 | * @param child the node to add | |
| 440 | 1321 | * @see cxTreeSetParent() |
| 324 | 1322 | */ |
| 440 | 1323 | 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
|
1324 | cx_attr_export |
| 440 | 1325 | void cxTreeAddChildNode( |
| 324 | 1326 | CxTree *tree, |
| 1327 | void *parent, | |
| 440 | 1328 | void *child |
| 1329 | ); | |
| 324 | 1330 | |
| 1331 | /** | |
| 1332 | * Creates a new node and adds it to the tree. | |
| 1333 | * | |
| 1334 | * With this function you can decide where exactly the new node shall be added. | |
| 1335 | * If you specified an appropriate search function, you may want to consider | |
| 1336 | * leaving this task to the tree by using #cxTreeInsert(). | |
| 1337 | * | |
| 1338 | * Be aware that adding nodes at arbitrary locations in the tree might cause | |
| 845 | 1339 | * wrong or undesired results when subsequently invoking #cxTreeInsert(), and |
| 324 | 1340 | * the invariant imposed by the search function does not hold any longer. |
| 1341 | * | |
| 1342 | * @param tree the tree | |
| 1343 | * @param parent the parent node of the new node | |
| 1344 | * @param data the data that will be submitted to the create function | |
| 1345 | * @return zero when the new node was created, non-zero on allocation failure | |
| 1346 | * @see cxTreeInsert() | |
| 1347 | */ | |
| 440 | 1348 | 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
|
1349 | cx_attr_export |
| 324 | 1350 | int cxTreeAddChild( |
| 1351 | CxTree *tree, | |
| 1352 | void *parent, | |
| 1353 | const void *data | |
| 1354 | ); | |
| 1355 | ||
| 1356 | /** | |
| 1357 | * A function that is invoked when a node needs to be re-linked to a new parent. | |
| 1358 | * | |
| 1359 | * When a node is re-linked, sometimes the contents need to be updated. | |
| 440 | 1360 | * This callback is invoked by #cxTreeRemoveNode() and #cxTreeDestroyNode() |
| 1361 | * so that those updates can be applied when re-linking the children of the | |
| 1362 | * removed node. | |
| 324 | 1363 | * |
| 1364 | * @param node the affected node | |
| 1365 | * @param old_parent the old parent of the node | |
| 1366 | * @param new_parent the new parent of the node | |
| 1367 | */ | |
| 440 | 1368 | cx_attr_nonnull |
| 324 | 1369 | typedef void (*cx_tree_relink_func)( |
| 1370 | void *node, | |
| 1371 | const void *old_parent, | |
| 1372 | const void *new_parent | |
| 1373 | ); | |
| 1374 | ||
| 1375 | /** | |
| 1376 | * Removes a node and re-links its children to its former parent. | |
| 1377 | * | |
| 1378 | * If the node is not part of the tree, the behavior is undefined. | |
| 1379 | * | |
| 440 | 1380 | * @note The destructor function, if any, will @em not be invoked. That means |
| 324 | 1381 | * you will need to free the removed node by yourself, eventually. |
| 1382 | * | |
| 1383 | * @param tree the tree | |
| 1384 | * @param node the node to remove (must not be the root node) | |
| 1385 | * @param relink_func optional callback to update the content of each re-linked | |
| 1386 | * node | |
| 440 | 1387 | * @return zero on success, non-zero if @p node is the root node of the tree |
| 324 | 1388 | */ |
| 440 | 1389 | cx_attr_nonnull_arg(1, 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
|
1390 | cx_attr_export |
| 440 | 1391 | int cxTreeRemoveNode( |
| 324 | 1392 | CxTree *tree, |
| 1393 | void *node, | |
| 1394 | cx_tree_relink_func relink_func | |
| 1395 | ); | |
| 1396 | ||
| 1397 | /** | |
| 845 | 1398 | * Removes a node and its subtree from the tree. |
| 324 | 1399 | * |
| 1400 | * If the node is not part of the tree, the behavior is undefined. | |
| 1401 | * | |
| 440 | 1402 | * @note The destructor function, if any, will @em not be invoked. That means |
| 324 | 1403 | * you will need to free the removed subtree by yourself, eventually. |
| 1404 | * | |
| 1405 | * @param tree the tree | |
| 1406 | * @param node the node to remove | |
| 1407 | */ | |
| 440 | 1408 | 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
|
1409 | cx_attr_export |
| 324 | 1410 | void cxTreeRemoveSubtree(CxTree *tree, void *node); |
| 1411 | ||
| 440 | 1412 | /** |
| 1413 | * Destroys a node and re-links its children to its former parent. | |
| 1414 | * | |
| 1415 | * If the node is not part of the tree, the behavior is undefined. | |
| 1416 | * | |
| 1417 | * It is guaranteed that the simple destructor is invoked before | |
| 1418 | * the advanced destructor. | |
| 1419 | * | |
| 1420 | * @attention This function will not free the memory of the node with the | |
| 1421 | * tree's allocator, because that is usually done by the advanced destructor | |
| 1422 | * and would therefore result in a double-free. | |
| 1423 | * | |
| 1424 | * @param tree the tree | |
| 1425 | * @param node the node to destroy (must not be the root node) | |
| 1426 | * @param relink_func optional callback to update the content of each re-linked | |
| 1427 | * node | |
| 1428 | * @return zero on success, non-zero if @p node is the root node of the tree | |
| 1429 | */ | |
| 1430 | cx_attr_nonnull_arg(1, 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
|
1431 | cx_attr_export |
| 440 | 1432 | int cxTreeDestroyNode( |
| 1433 | CxTree *tree, | |
| 1434 | void *node, | |
| 1435 | cx_tree_relink_func relink_func | |
| 1436 | ); | |
| 1437 | ||
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1438 | #ifdef __cplusplus |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1439 | } // extern "C" |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1440 | #endif |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1441 | |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1442 | #endif //UCX_TREE_H |