Tue, 21 Oct 2025 16:20:51 +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 |
| 870 | 215 | CX_EXPORT void cxTreeIteratorDispose(CxTreeIterator *iter); |
| 324 | 216 | |
| 217 | /** | |
| 218 | * Releases internal memory of the given tree visitor. | |
| 219 | * @param visitor the visitor | |
| 220 | */ | |
| 440 | 221 | cx_attr_nonnull |
| 870 | 222 | CX_EXPORT void cxTreeVisitorDispose(CxTreeVisitor *visitor); |
| 324 | 223 | |
| 224 | /** | |
| 225 | * Advises the iterator to skip the subtree below the current node and | |
| 226 | * also continues the current loop. | |
| 227 | * | |
| 440 | 228 | * @param iterator (@c CxTreeIterator) the iterator |
| 324 | 229 | */ |
| 230 | #define cxTreeIteratorContinue(iterator) (iterator).skip = true; continue | |
| 231 | ||
| 232 | /** | |
| 233 | * Advises the visitor to skip the subtree below the current node and | |
| 234 | * also continues the current loop. | |
| 235 | * | |
| 440 | 236 | * @param visitor (@c CxTreeVisitor) the visitor |
| 324 | 237 | */ |
| 238 | #define cxTreeVisitorContinue(visitor) cxTreeIteratorContinue(visitor) | |
| 239 | ||
| 240 | /** | |
| 241 | * Links a node to a (new) parent. | |
| 242 | * | |
| 845 | 243 | * If the node already has a parent, it is unlinked, first. |
| 440 | 244 | * If the parent has children already, the node is @em appended to the list |
| 324 | 245 | * of all currently existing children. |
| 246 | * | |
| 247 | * @param parent the parent node | |
| 248 | * @param node the node that shall be linked | |
| 249 | * @param loc_parent offset in the node struct for the parent pointer | |
| 250 | * @param loc_children offset in the node struct for the children linked list | |
| 251 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 252 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 253 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 254 | * @param loc_next offset in the node struct for the next pointer |
| 255 | * @see cx_tree_unlink() | |
| 256 | */ | |
| 440 | 257 | cx_attr_nonnull |
| 870 | 258 | CX_EXPORT void cx_tree_link(void *parent, void *node, |
| 259 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 260 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
261 | |
| 324 | 262 | /** |
| 263 | * Unlinks a node from its parent. | |
| 264 | * | |
| 265 | * If the node has no parent, this function does nothing. | |
| 266 | * | |
| 267 | * @param node the node that shall be unlinked from its parent | |
| 268 | * @param loc_parent offset in the node struct for the parent pointer | |
| 269 | * @param loc_children offset in the node struct for the children linked list | |
| 270 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 271 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 272 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 273 | * @param loc_next offset in the node struct for the next pointer |
| 274 | * @see cx_tree_link() | |
| 275 | */ | |
| 440 | 276 | cx_attr_nonnull |
| 870 | 277 | CX_EXPORT void cx_tree_unlink(void *node, |
| 278 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 279 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
| 324 | 280 | |
| 281 | /** | |
| 440 | 282 | * Macro that can be used instead of the magic value for infinite search depth. |
| 283 | */ | |
| 284 | #define CX_TREE_SEARCH_INFINITE_DEPTH 0 | |
| 285 | ||
| 286 | /** | |
| 324 | 287 | * Function pointer for a search function. |
| 288 | * | |
| 440 | 289 | * A function of this kind shall check if the specified @p node |
| 290 | * contains the given @p data or if one of the children might contain | |
| 324 | 291 | * the data. |
| 292 | * | |
| 293 | * The function should use the returned integer to indicate how close the | |
| 294 | * match is, where a negative number means that it does not match at all. | |
| 440 | 295 | * Zero means exact match and a positive number is an implementation defined |
| 296 | * measure for the distance to an exact match. | |
| 324 | 297 | * |
| 845 | 298 | * For example, consider a tree that stores file path information. |
| 299 | * A node which is describing a parent directory of a searched file shall | |
| 324 | 300 | * return a positive number to indicate that a child node might contain the |
| 301 | * searched item. On the other hand, if the node denotes a path that is not a | |
| 302 | * prefix of the searched filename, the function would return -1 to indicate | |
| 303 | * that the search does not need to be continued in that branch. | |
| 304 | * | |
| 305 | * @param node the node that is currently investigated | |
| 306 | * @param data the data that is searched for | |
| 307 | * | |
| 308 | * @return 0 if the node contains the data, | |
| 309 | * positive if one of the children might contain the data, | |
| 845 | 310 | * negative if neither the node nor the children contains the data |
| 324 | 311 | */ |
| 312 | typedef int (*cx_tree_search_data_func)(const void *node, const void *data); | |
| 313 | ||
| 314 | ||
| 315 | /** | |
| 316 | * Function pointer for a search function. | |
| 317 | * | |
| 440 | 318 | * A function of this kind shall check if the specified @p node |
| 319 | * contains the same @p data as @p new_node or if one of the children might | |
| 324 | 320 | * contain the data. |
| 321 | * | |
| 322 | * The function should use the returned integer to indicate how close the | |
| 323 | * match is, where a negative number means that it does not match at all. | |
| 440 | 324 | * Zero means exact match and a positive number is an implementation defined |
| 325 | * measure for the distance to an exact match. | |
| 324 | 326 | * |
| 845 | 327 | * For example, consider a tree that stores file path information. |
| 328 | * A node which is describing a parent directory of a searched file shall | |
| 324 | 329 | * return a positive number to indicate that a child node might contain the |
| 330 | * searched item. On the other hand, if the node denotes a path that is not a | |
| 331 | * prefix of the searched filename, the function would return -1 to indicate | |
| 332 | * that the search does not need to be continued in that branch. | |
| 333 | * | |
| 334 | * @param node the node that is currently investigated | |
| 335 | * @param new_node a new node with the information which is searched | |
| 336 | * | |
| 440 | 337 | * @return 0 if @p node contains the same data as @p new_node, |
| 324 | 338 | * positive if one of the children might contain the data, |
| 845 | 339 | * negative if neither the node nor the children contains the data |
| 324 | 340 | */ |
| 341 | typedef int (*cx_tree_search_func)(const void *node, const void *new_node); | |
| 342 | ||
| 343 | /** | |
| 344 | * Searches for data in a tree. | |
| 345 | * | |
| 845 | 346 | * When the data cannot be found exactly, the search function might return the |
| 347 | * closest result, which might be a good starting point for adding a new node | |
| 324 | 348 | * to the tree (see also #cx_tree_add()). |
| 349 | * | |
| 845 | 350 | * Depending on the tree structure, it is not necessarily guaranteed that the |
| 324 | 351 | * "closest" match is uniquely defined. This function will search for a node |
| 440 | 352 | * with the best match according to the @p sfunc (meaning: the return value of |
| 353 | * @p sfunc which is closest to zero). If that is also ambiguous, an arbitrary | |
| 324 | 354 | * node matching the criteria is returned. |
| 355 | * | |
| 356 | * @param root the root node | |
| 440 | 357 | * @param depth the maximum depth (zero=indefinite, one=just root) |
| 324 | 358 | * @param data the data to search for |
| 359 | * @param sfunc the search function | |
| 360 | * @param result where the result shall be stored | |
| 361 | * @param loc_children offset in the node struct for the children linked list | |
| 362 | * @param loc_next offset in the node struct for the next pointer | |
| 363 | * @return zero if the node was found exactly, positive if a node was found that | |
| 364 | * could contain the node (but doesn't right now), negative if the tree does not | |
| 365 | * contain any node that might be related to the searched data | |
| 366 | */ | |
| 870 | 367 | cx_attr_nonnull cx_attr_access_w(5) |
| 368 | CX_EXPORT int cx_tree_search_data(const void *root, size_t depth, | |
| 369 | const void *data, cx_tree_search_data_func sfunc, | |
| 370 | void **result, ptrdiff_t loc_children, ptrdiff_t loc_next); | |
| 324 | 371 | |
| 372 | /** | |
| 373 | * Searches for a node in a tree. | |
| 374 | * | |
| 375 | * When no node with the same data can be found, the search function might | |
| 845 | 376 | * return the closest result, which might be a good starting point for adding the |
| 324 | 377 | * new node to the tree (see also #cx_tree_add()). |
| 378 | * | |
| 845 | 379 | * Depending on the tree structure, it is not necessarily guaranteed that the |
| 324 | 380 | * "closest" match is uniquely defined. This function will search for a node |
| 440 | 381 | * with the best match according to the @p sfunc (meaning: the return value of |
| 382 | * @p sfunc which is closest to zero). If that is also ambiguous, an arbitrary | |
| 324 | 383 | * node matching the criteria is returned. |
| 384 | * | |
| 385 | * @param root the root node | |
| 440 | 386 | * @param depth the maximum depth (zero=indefinite, one=just root) |
| 324 | 387 | * @param node the node to search for |
| 388 | * @param sfunc the search function | |
| 389 | * @param result where the result shall be stored | |
| 390 | * @param loc_children offset in the node struct for the children linked list | |
| 391 | * @param loc_next offset in the node struct for the next pointer | |
| 392 | * @return zero if the node was found exactly, positive if a node was found that | |
| 393 | * could contain the node (but doesn't right now), negative if the tree does not | |
| 394 | * contain any node that might be related to the searched data | |
| 395 | */ | |
| 870 | 396 | cx_attr_nonnull cx_attr_access_w(5) |
| 397 | CX_EXPORT int cx_tree_search(const void *root, size_t depth, | |
| 398 | const void *node, cx_tree_search_func sfunc, | |
| 399 | void **result, ptrdiff_t loc_children, ptrdiff_t loc_next); | |
| 324 | 400 | |
| 401 | /** | |
| 402 | * Creates a depth-first iterator for a tree with the specified root node. | |
| 403 | * | |
| 404 | * @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
|
405 | * allocated using the cxDefaultAllocator. |
| 324 | 406 | * When the iterator becomes invalid, this memory is automatically released. |
| 407 | * However, if you wish to cancel the iteration before the iterator becomes | |
| 408 | * invalid by itself, you MUST call cxTreeIteratorDispose() manually to release | |
| 409 | * the memory. | |
| 410 | * | |
| 411 | * @remark The returned iterator does not support cxIteratorFlagRemoval(). | |
| 412 | * | |
| 413 | * @param root the root node | |
| 414 | * @param visit_on_exit set to true, when the iterator shall visit a node again | |
| 415 | * after processing all children | |
| 416 | * @param loc_children offset in the node struct for the children linked list | |
| 417 | * @param loc_next offset in the node struct for the next pointer | |
| 418 | * @return the new tree iterator | |
| 419 | * @see cxTreeIteratorDispose() | |
| 420 | */ | |
| 440 | 421 | cx_attr_nodiscard |
| 870 | 422 | CX_EXPORT CxTreeIterator cx_tree_iterator(void *root, bool visit_on_exit, |
| 423 | ptrdiff_t loc_children, ptrdiff_t loc_next); | |
| 324 | 424 | |
| 425 | /** | |
| 426 | * Creates a breadth-first iterator for a tree with the specified root node. | |
| 427 | * | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
428 | * @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
|
429 | * is allocated using the cxDefaultAllocator. |
| 324 | 430 | * When the visitor becomes invalid, this memory is automatically released. |
| 431 | * However, if you wish to cancel the iteration before the visitor becomes | |
| 432 | * invalid by itself, you MUST call cxTreeVisitorDispose() manually to release | |
| 433 | * the memory. | |
| 434 | * | |
| 435 | * @remark The returned iterator does not support cxIteratorFlagRemoval(). | |
| 436 | * | |
| 437 | * @param root the root node | |
| 438 | * @param loc_children offset in the node struct for the children linked list | |
| 439 | * @param loc_next offset in the node struct for the next pointer | |
| 440 | * @return the new tree visitor | |
| 441 | * @see cxTreeVisitorDispose() | |
| 442 | */ | |
| 440 | 443 | cx_attr_nodiscard |
| 870 | 444 | CX_EXPORT CxTreeVisitor cx_tree_visitor(void *root, |
| 445 | ptrdiff_t loc_children, ptrdiff_t loc_next); | |
| 324 | 446 | |
| 447 | /** | |
| 448 | * Describes a function that creates a tree node from the specified data. | |
| 845 | 449 | * The first argument points to the data the node shall contain, and |
| 450 | * the second argument may be used for additional data (e.g., an allocator). | |
| 324 | 451 | * Functions of this type shall either return a new pointer to a newly |
| 440 | 452 | * created node or @c NULL when allocation fails. |
| 324 | 453 | * |
| 440 | 454 | * @note the function may leave the node pointers in the struct uninitialized. |
| 324 | 455 | * The caller is responsible to set them according to the intended use case. |
| 456 | */ | |
| 457 | typedef void *(*cx_tree_node_create_func)(const void *, void *); | |
| 458 | ||
| 459 | /** | |
| 460 | * The local search depth for a new subtree when adding multiple elements. | |
| 461 | * The default value is 3. | |
| 462 | * This variable is used by #cx_tree_add_array() and #cx_tree_add_iter() to | |
| 463 | * implement optimized insertion of multiple elements into a tree. | |
| 464 | */ | |
| 870 | 465 | CX_EXPORT extern unsigned int cx_tree_add_look_around_depth; |
| 324 | 466 | |
| 467 | /** | |
| 468 | * Adds multiple elements efficiently to a tree. | |
| 469 | * | |
| 470 | * Once an element cannot be added to the tree, this function returns, leaving | |
| 471 | * the iterator in a valid state pointing to the element that could not be | |
| 472 | * added. | |
| 440 | 473 | * Also, the pointer of the created node will be stored to @p failed. |
| 324 | 474 | * The integer returned by this function denotes the number of elements obtained |
| 440 | 475 | * from the @p iter that have been successfully processed. |
| 476 | * When all elements could be processed, a @c NULL pointer will be written to | |
| 477 | * @p failed. | |
| 324 | 478 | * |
| 479 | * The advantage of this function compared to multiple invocations of | |
| 480 | * #cx_tree_add() is that the search for the insert locations is not always | |
| 481 | * started from the root node. | |
| 482 | * Instead, the function checks #cx_tree_add_look_around_depth many parent nodes | |
| 483 | * of the current insert location before starting from the root node again. | |
| 484 | * When the variable is set to zero, only the last found location is checked | |
| 485 | * again. | |
| 486 | * | |
| 487 | * Refer to the documentation of #cx_tree_add() for more details. | |
| 488 | * | |
| 489 | * @param iter a pointer to an arbitrary iterator | |
| 490 | * @param num the maximum number of elements to obtain from the iterator | |
| 491 | * @param sfunc a search function | |
| 492 | * @param cfunc a node creation function | |
| 493 | * @param cdata optional additional data | |
| 494 | * @param root the root node of the tree | |
| 495 | * @param failed location where the pointer to a failed node shall be stored | |
| 496 | * @param loc_parent offset in the node struct for the parent pointer | |
| 497 | * @param loc_children offset in the node struct for the children linked list | |
| 498 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 499 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 500 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 501 | * @param loc_next offset in the node struct for the next pointer |
| 502 | * @return the number of nodes created and added | |
| 503 | * @see cx_tree_add() | |
| 504 | */ | |
| 870 | 505 | cx_attr_nonnull_arg(1, 3, 4, 6, 7) cx_attr_access_w(6) |
| 506 | CX_EXPORT size_t cx_tree_add_iter(struct cx_iterator_base_s *iter, size_t num, | |
| 507 | cx_tree_search_func sfunc, cx_tree_node_create_func cfunc, | |
| 508 | void *cdata, void **failed, void *root, | |
| 509 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 510 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
| 324 | 511 | |
| 512 | /** | |
| 513 | * Adds multiple elements efficiently to a tree. | |
| 514 | * | |
| 515 | * Once an element cannot be added to the tree, this function returns, storing | |
| 440 | 516 | * the pointer of the created node to @p failed. |
| 324 | 517 | * The integer returned by this function denotes the number of elements from |
| 440 | 518 | * the @p src array that have been successfully processed. |
| 519 | * When all elements could be processed, a @c NULL pointer will be written to | |
| 520 | * @p failed. | |
| 324 | 521 | * |
| 522 | * The advantage of this function compared to multiple invocations of | |
| 523 | * #cx_tree_add() is that the search for the insert locations is not always | |
| 524 | * started from the root node. | |
| 525 | * Instead, the function checks #cx_tree_add_look_around_depth many parent nodes | |
| 526 | * of the current insert location before starting from the root node again. | |
| 527 | * When the variable is set to zero, only the last found location is checked | |
| 528 | * again. | |
| 529 | * | |
| 530 | * Refer to the documentation of #cx_tree_add() for more details. | |
| 531 | * | |
| 532 | * @param src a pointer to the source data array | |
| 440 | 533 | * @param num the number of elements in the @p src array |
| 534 | * @param elem_size the size of each element in the @p src array | |
| 324 | 535 | * @param sfunc a search function |
| 536 | * @param cfunc a node creation function | |
| 537 | * @param cdata optional additional data | |
| 538 | * @param failed location where the pointer to a failed node shall be stored | |
| 539 | * @param root the root node of the tree | |
| 540 | * @param loc_parent offset in the node struct for the parent pointer | |
| 541 | * @param loc_children offset in the node struct for the children linked list | |
| 542 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 543 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 544 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 545 | * @param loc_next offset in the node struct for the next pointer |
| 546 | * @return the number of array elements successfully processed | |
| 547 | * @see cx_tree_add() | |
| 548 | */ | |
| 870 | 549 | cx_attr_nonnull_arg(1, 4, 5, 7, 8) cx_attr_access_w(7) |
| 550 | CX_EXPORT size_t cx_tree_add_array(const void *src, size_t num, size_t elem_size, | |
| 551 | cx_tree_search_func sfunc, cx_tree_node_create_func cfunc, | |
| 552 | void *cdata, void **failed, void *root, | |
| 553 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 554 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
| 324 | 555 | |
| 556 | /** | |
| 557 | * Adds data to a tree. | |
| 558 | * | |
| 559 | * An adequate location where to add the new tree node is searched with the | |
| 440 | 560 | * specified @p sfunc. |
| 324 | 561 | * |
| 440 | 562 | * When a location is found, the @p cfunc will be invoked with @p cdata. |
| 324 | 563 | * |
| 440 | 564 | * The node returned by @p cfunc will be linked into the tree. |
| 845 | 565 | * When @p sfunc returns a positive integer, the new node will be linked as a |
| 324 | 566 | * child. The other children (now siblings of the new node) are then checked |
| 440 | 567 | * with @p sfunc, whether they could be children of the new node and re-linked |
| 324 | 568 | * accordingly. |
| 569 | * | |
| 845 | 570 | * When @p sfunc returns zero and the found node has a parent, the new |
| 571 | * node will be added as a sibling - otherwise, the new node will be added | |
| 324 | 572 | * as a child. |
| 573 | * | |
| 845 | 574 | * When @p sfunc returns a negative value, the new node will not be added to |
| 575 | * the tree, and this function returns a non-zero value. | |
| 440 | 576 | * The caller should check if @p cnode contains a node pointer and deal with the |
| 324 | 577 | * node that could not be added. |
| 578 | * | |
| 440 | 579 | * This function also returns a non-zero value when @p cfunc tries to allocate |
| 580 | * a new node but fails to do so. In that case, the pointer stored to @p cnode | |
| 581 | * will be @c NULL. | |
| 324 | 582 | * |
| 583 | * Multiple elements can be added more efficiently with | |
| 584 | * #cx_tree_add_array() or #cx_tree_add_iter(). | |
| 585 | * | |
| 586 | * @param src a pointer to the data | |
| 587 | * @param sfunc a search function | |
| 588 | * @param cfunc a node creation function | |
| 589 | * @param cdata optional additional data | |
| 590 | * @param cnode the location where a pointer to the new node is stored | |
| 591 | * @param root the root node of the tree | |
| 592 | * @param loc_parent offset in the node struct for the parent pointer | |
| 593 | * @param loc_children offset in the node struct for the children linked list | |
| 594 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 595 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 596 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 597 | * @param loc_next offset in the node struct for the next pointer |
| 598 | * @return zero when a new node was created and added to the tree, | |
| 599 | * non-zero otherwise | |
| 600 | */ | |
| 870 | 601 | cx_attr_nonnull_arg(1, 2, 3, 5, 6) cx_attr_access_w(5) |
| 602 | CX_EXPORT int cx_tree_add(const void *src, | |
| 603 | cx_tree_search_func sfunc, cx_tree_node_create_func cfunc, | |
| 604 | void *cdata, void **cnode, void *root, | |
| 605 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 606 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
607 | |
| 324 | 608 | |
| 609 | /** | |
| 610 | * Tree class type. | |
| 611 | */ | |
| 612 | typedef struct cx_tree_class_s cx_tree_class; | |
| 613 | ||
| 614 | /** | |
| 615 | * Base structure that can be used for tree nodes in a #CxTree. | |
| 616 | */ | |
| 617 | struct cx_tree_node_base_s { | |
| 618 | /** | |
| 619 | * Pointer to the parent. | |
| 620 | */ | |
| 621 | struct cx_tree_node_base_s *parent; | |
| 622 | /** | |
| 623 | * Pointer to the first child. | |
| 624 | */ | |
| 625 | struct cx_tree_node_base_s *children; | |
| 626 | /** | |
| 627 | * Pointer to the last child. | |
| 628 | */ | |
| 629 | struct cx_tree_node_base_s *last_child; | |
| 630 | /** | |
| 631 | * Pointer to the previous sibling. | |
| 632 | */ | |
| 633 | struct cx_tree_node_base_s *prev; | |
| 634 | /** | |
| 635 | * Pointer to the next sibling. | |
| 636 | */ | |
| 637 | struct cx_tree_node_base_s *next; | |
| 638 | }; | |
| 639 | ||
| 640 | /** | |
| 641 | * Structure for holding the base data of a tree. | |
| 642 | */ | |
| 643 | struct cx_tree_s { | |
| 644 | /** | |
| 645 | * The tree class definition. | |
| 646 | */ | |
| 647 | const cx_tree_class *cl; | |
| 648 | ||
| 649 | /** | |
| 650 | * Allocator to allocate new nodes. | |
| 651 | */ | |
| 652 | const CxAllocator *allocator; | |
| 653 | ||
| 654 | /** | |
| 655 | * A pointer to the root node. | |
| 656 | * | |
| 440 | 657 | * Will be @c NULL when @c size is 0. |
| 324 | 658 | */ |
| 659 | void *root; | |
| 660 | ||
| 661 | /** | |
| 662 | * A function to create new nodes. | |
| 663 | * | |
| 664 | * Invocations to this function will receive a pointer to this tree | |
| 845 | 665 | * structure as the second argument. |
| 324 | 666 | * |
| 845 | 667 | * Nodes MAY use #cx_tree_node_base_s as the base layout, but do not need to. |
| 324 | 668 | */ |
| 669 | cx_tree_node_create_func node_create; | |
| 670 | ||
| 671 | /** | |
| 672 | * An optional simple destructor for the tree nodes. | |
| 673 | */ | |
| 674 | cx_destructor_func simple_destructor; | |
| 675 | ||
| 676 | /** | |
| 677 | * An optional advanced destructor for the tree nodes. | |
| 678 | */ | |
| 679 | cx_destructor_func2 advanced_destructor; | |
| 680 | ||
| 681 | /** | |
| 682 | * The pointer to additional data that is passed to the advanced destructor. | |
| 683 | */ | |
| 684 | void *destructor_data; | |
| 685 | ||
| 686 | /** | |
| 687 | * A function to compare two nodes. | |
| 688 | */ | |
| 689 | cx_tree_search_func search; | |
| 690 | ||
| 691 | /** | |
| 692 | * A function to compare a node with data. | |
| 693 | */ | |
| 694 | cx_tree_search_data_func search_data; | |
| 695 | ||
| 696 | /** | |
| 697 | * The number of currently stored elements. | |
| 698 | */ | |
| 699 | size_t size; | |
| 700 | ||
| 701 | /** | |
| 702 | * Offset in the node struct for the parent pointer. | |
| 703 | */ | |
| 704 | ptrdiff_t loc_parent; | |
| 705 | ||
| 706 | /** | |
| 707 | * Offset in the node struct for the children linked list. | |
| 708 | */ | |
| 709 | ptrdiff_t loc_children; | |
| 710 | ||
| 711 | /** | |
| 712 | * Optional offset in the node struct for the pointer to the last child | |
| 713 | * in the linked list (negative if there is no such pointer). | |
| 714 | */ | |
| 715 | ptrdiff_t loc_last_child; | |
| 716 | ||
| 717 | /** | |
| 718 | * Offset in the node struct for the previous sibling pointer. | |
| 719 | */ | |
| 720 | ptrdiff_t loc_prev; | |
| 721 | ||
| 722 | /** | |
| 723 | * Offset in the node struct for the next sibling pointer. | |
| 724 | */ | |
| 725 | ptrdiff_t loc_next; | |
| 726 | }; | |
| 727 | ||
| 728 | /** | |
| 729 | * Macro to roll out the #cx_tree_node_base_s structure with a custom | |
| 730 | * node type. | |
| 440 | 731 | * |
| 845 | 732 | * Must be used as the first member in your custom tree struct. |
| 440 | 733 | * |
| 734 | * @param type the data type for the nodes | |
| 324 | 735 | */ |
| 736 | #define CX_TREE_NODE_BASE(type) \ | |
| 737 | type *parent; \ | |
| 738 | type *children;\ | |
| 739 | type *last_child;\ | |
| 740 | type *prev;\ | |
| 741 | type *next | |
| 742 | ||
| 743 | /** | |
| 744 | * Macro for specifying the layout of a base node tree. | |
| 440 | 745 | * |
| 746 | * When your tree uses #CX_TREE_NODE_BASE, you can use this | |
| 747 | * macro in all tree functions that expect the layout parameters | |
| 748 | * @c loc_parent, @c loc_children, @c loc_last_child, @c loc_prev, | |
| 749 | * and @c loc_next. | |
| 324 | 750 | */ |
| 751 | #define cx_tree_node_base_layout \ | |
| 752 | offsetof(struct cx_tree_node_base_s, parent),\ | |
| 753 | offsetof(struct cx_tree_node_base_s, children),\ | |
| 754 | offsetof(struct cx_tree_node_base_s, last_child),\ | |
| 755 | offsetof(struct cx_tree_node_base_s, prev), \ | |
| 756 | offsetof(struct cx_tree_node_base_s, next) | |
| 757 | ||
| 758 | /** | |
| 759 | * The class definition for arbitrary trees. | |
| 760 | */ | |
| 761 | struct cx_tree_class_s { | |
| 762 | /** | |
| 763 | * Member function for inserting a single element. | |
| 764 | * | |
| 440 | 765 | * Implementations SHALL NOT simply invoke @p insert_many as this comes |
| 324 | 766 | * with too much overhead. |
| 767 | */ | |
| 870 | 768 | int (*insert_element)(struct cx_tree_s *tree, const void *data); |
| 324 | 769 | |
| 770 | /** | |
| 771 | * Member function for inserting multiple elements. | |
| 772 | * | |
| 845 | 773 | * Implementations SHALL avoid performing a full search in the tree for |
| 324 | 774 | * every element even though the source data MAY be unsorted. |
| 775 | */ | |
| 870 | 776 | size_t (*insert_many)(struct cx_tree_s *tree, struct cx_iterator_base_s *iter, size_t n); |
| 324 | 777 | |
| 778 | /** | |
| 779 | * Member function for finding a node. | |
| 780 | */ | |
| 870 | 781 | void *(*find)(struct cx_tree_s *tree, const void *subtree, const void *data, size_t depth); |
| 324 | 782 | }; |
| 783 | ||
| 784 | /** | |
| 785 | * Common type for all tree implementations. | |
| 786 | */ | |
| 787 | typedef struct cx_tree_s CxTree; | |
| 788 | ||
| 440 | 789 | |
| 790 | /** | |
| 845 | 791 | * Destroys a node and its subtree. |
| 440 | 792 | * |
| 793 | * It is guaranteed that the simple destructor is invoked before | |
| 794 | * the advanced destructor, starting with the leaf nodes of the subtree. | |
| 795 | * | |
| 796 | * When this function is invoked on the root node of the tree, it destroys the | |
| 797 | * tree contents, but - in contrast to #cxTreeFree() - not the tree | |
| 798 | * structure, leaving an empty tree behind. | |
| 799 | * | |
| 800 | * @note The destructor function, if any, will @em not be invoked. That means | |
| 801 | * you will need to free the removed subtree by yourself, eventually. | |
| 802 | * | |
| 803 | * @attention This function will not free the memory of the nodes with the | |
| 804 | * tree's allocator, because that is usually done by the advanced destructor | |
| 805 | * and would therefore result in a double-free. | |
| 806 | * | |
| 807 | * @param tree the tree | |
| 808 | * @param node the node to remove | |
| 809 | * @see cxTreeFree() | |
| 810 | */ | |
| 811 | cx_attr_nonnull | |
| 870 | 812 | CX_EXPORT void cxTreeDestroySubtree(CxTree *tree, void *node); |
| 440 | 813 | |
| 814 | ||
| 815 | /** | |
| 816 | * Destroys the tree contents. | |
| 817 | * | |
| 818 | * It is guaranteed that the simple destructor is invoked before | |
| 819 | * the advanced destructor, starting with the leaf nodes of the subtree. | |
| 820 | * | |
| 821 | * This is a convenience macro for invoking #cxTreeDestroySubtree() on the | |
| 822 | * root node of the tree. | |
| 823 | * | |
| 824 | * @attention Be careful when calling this function when no destructor function | |
| 825 | * is registered that actually frees the memory of nodes. In that case you will | |
| 845 | 826 | * need a reference to the (former) root node of the tree somewhere, or |
| 440 | 827 | * otherwise you will be leaking memory. |
| 828 | * | |
| 829 | * @param tree the tree | |
| 830 | * @see cxTreeDestroySubtree() | |
| 831 | */ | |
| 832 | #define cxTreeClear(tree) cxTreeDestroySubtree(tree, tree->root) | |
| 833 | ||
| 834 | /** | |
| 835 | * Deallocates the tree structure. | |
| 836 | * | |
| 837 | * The destructor functions are invoked for each node, starting with the leaf | |
| 838 | * nodes. | |
| 839 | * It is guaranteed that for each node the simple destructor is invoked before | |
| 840 | * the advanced destructor. | |
| 841 | * | |
| 842 | * @attention This function will only invoke the destructor functions | |
| 843 | * on the nodes. | |
| 844 | * It will NOT additionally free the nodes with the tree's allocator, because | |
| 845 | * that would cause a double-free in most scenarios where the advanced | |
| 846 | * destructor is already freeing the memory. | |
| 847 | * | |
| 848 | * @param tree the tree to free | |
| 849 | */ | |
| 870 | 850 | CX_EXPORT void cxTreeFree(CxTree *tree); |
| 440 | 851 | |
| 324 | 852 | /** |
| 853 | * Creates a new tree structure based on the specified layout. | |
| 854 | * | |
| 440 | 855 | * The specified @p allocator will be used for creating the tree struct |
| 856 | * and SHALL be used by @p create_func to allocate memory for the nodes. | |
| 324 | 857 | * |
| 440 | 858 | * @note This function will also register an advanced destructor which |
| 324 | 859 | * will free the nodes with the allocator's free() method. |
| 860 | * | |
| 861 | * @param allocator the allocator that shall be used | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
862 | * (if @c NULL, the cxDefaultAllocator will be used) |
| 324 | 863 | * @param create_func a function that creates new nodes |
| 864 | * @param search_func a function that compares two nodes | |
| 865 | * @param search_data_func a function that compares a node with data | |
| 866 | * @param loc_parent offset in the node struct for the parent pointer | |
| 867 | * @param loc_children offset in the node struct for the children linked list | |
| 868 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 869 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 870 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 871 | * @param loc_next offset in the node struct for the next pointer |
| 872 | * @return the new tree | |
| 873 | * @see cxTreeCreateSimple() | |
| 874 | * @see cxTreeCreateWrapped() | |
| 875 | */ | |
| 870 | 876 | cx_attr_nonnull_arg(2, 3, 4) cx_attr_nodiscard cx_attr_malloc cx_attr_dealloc(cxTreeFree, 1) |
| 877 | CX_EXPORT CxTree *cxTreeCreate(const CxAllocator *allocator, cx_tree_node_create_func create_func, | |
| 878 | cx_tree_search_func search_func, cx_tree_search_data_func search_data_func, | |
| 879 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 880 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
| 324 | 881 | |
| 882 | /** | |
| 883 | * Creates a new tree structure based on a default layout. | |
| 884 | * | |
| 845 | 885 | * Nodes created by @p create_func MUST contain #cx_tree_node_base_s as the first |
| 324 | 886 | * member (or at least respect the default offsets specified in the tree |
| 845 | 887 | * struct), and they MUST be allocated with the specified allocator. |
| 324 | 888 | * |
| 440 | 889 | * @note This function will also register an advanced destructor which |
| 324 | 890 | * will free the nodes with the allocator's free() method. |
| 891 | * | |
| 440 | 892 | * @param allocator (@c CxAllocator*) the allocator that shall be used |
| 893 | * @param create_func (@c cx_tree_node_create_func) a function that creates new nodes | |
| 894 | * @param search_func (@c cx_tree_search_func) a function that compares two nodes | |
| 895 | * @param search_data_func (@c cx_tree_search_data_func) a function that compares a node with data | |
| 896 | * @return (@c CxTree*) the new tree | |
| 324 | 897 | * @see cxTreeCreate() |
| 898 | */ | |
| 870 | 899 | #define cxTreeCreateSimple(allocator, create_func, search_func, search_data_func) \ |
| 900 | cxTreeCreate(allocator, create_func, search_func, search_data_func, cx_tree_node_base_layout) | |
| 324 | 901 | |
| 902 | /** | |
| 903 | * Creates a new tree structure based on an existing tree. | |
| 904 | * | |
| 440 | 905 | * The specified @p allocator will be used for creating the tree struct. |
| 324 | 906 | * |
| 440 | 907 | * @attention This function will create an incompletely defined tree structure |
| 324 | 908 | * where neither the create function, the search function, nor a destructor |
| 909 | * will be set. If you wish to use any of this functionality for the wrapped | |
| 845 | 910 | * tree, you need to specify those functions afterward. |
| 324 | 911 | * |
| 440 | 912 | * @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
|
913 | * (if @c NULL, the cxDefaultAllocator is assumed) |
| 324 | 914 | * @param root the root node of the tree that shall be wrapped |
| 915 | * @param loc_parent offset in the node struct for the parent pointer | |
| 916 | * @param loc_children offset in the node struct for the children linked list | |
| 917 | * @param loc_last_child optional offset in the node struct for the pointer to | |
| 918 | * the last child in the linked list (negative if there is no such pointer) | |
| 440 | 919 | * @param loc_prev optional offset in the node struct for the prev pointer |
| 324 | 920 | * @param loc_next offset in the node struct for the next pointer |
| 921 | * @return the new tree | |
| 922 | * @see cxTreeCreate() | |
| 923 | */ | |
| 870 | 924 | cx_attr_nonnull_arg(2) cx_attr_nodiscard cx_attr_malloc cx_attr_dealloc(cxTreeFree, 1) |
| 925 | CX_EXPORT CxTree *cxTreeCreateWrapped(const CxAllocator *allocator, void *root, | |
| 926 | ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child, | |
| 927 | ptrdiff_t loc_prev, ptrdiff_t loc_next); | |
| 324 | 928 | |
| 929 | /** | |
| 930 | * Inserts data into the tree. | |
| 931 | * | |
| 440 | 932 | * @remark For this function to work, the tree needs specified search and |
| 324 | 933 | * create functions, which might not be available for wrapped trees |
| 934 | * (see #cxTreeCreateWrapped()). | |
| 935 | * | |
| 936 | * @param tree the tree | |
| 937 | * @param data the data to insert | |
| 440 | 938 | * @retval zero success |
| 939 | * @retval non-zero failure | |
| 324 | 940 | */ |
| 440 | 941 | cx_attr_nonnull |
| 870 | 942 | CX_EXPORT int cxTreeInsert(CxTree *tree, const void *data); |
| 324 | 943 | |
| 944 | /** | |
| 945 | * Inserts elements provided by an iterator efficiently into the tree. | |
| 946 | * | |
| 440 | 947 | * @remark For this function to work, the tree needs specified search and |
| 324 | 948 | * create functions, which might not be available for wrapped trees |
| 949 | * (see #cxTreeCreateWrapped()). | |
| 950 | * | |
| 951 | * @param tree the tree | |
| 952 | * @param iter the iterator providing the elements | |
| 953 | * @param n the maximum number of elements to insert | |
| 954 | * @return the number of elements that could be successfully inserted | |
| 955 | */ | |
| 440 | 956 | cx_attr_nonnull |
| 870 | 957 | CX_EXPORT size_t cxTreeInsertIter(CxTree *tree, CxIteratorBase *iter, size_t n); |
| 324 | 958 | |
| 959 | /** | |
| 960 | * Inserts an array of data efficiently into the tree. | |
| 961 | * | |
| 440 | 962 | * @remark For this function to work, the tree needs specified search and |
| 324 | 963 | * create functions, which might not be available for wrapped trees |
| 964 | * (see #cxTreeCreateWrapped()). | |
| 965 | * | |
| 966 | * @param tree the tree | |
| 967 | * @param data the array of data to insert | |
| 968 | * @param elem_size the size of each element in the array | |
| 969 | * @param n the number of elements in the array | |
| 970 | * @return the number of elements that could be successfully inserted | |
| 971 | */ | |
| 440 | 972 | cx_attr_nonnull |
| 870 | 973 | CX_EXPORT size_t cxTreeInsertArray(CxTree *tree, const void *data, size_t elem_size, size_t n); |
| 324 | 974 | |
| 975 | /** | |
| 976 | * Searches the data in the specified tree. | |
| 977 | * | |
| 440 | 978 | * @remark For this function to work, the tree needs a specified @c search_data |
| 324 | 979 | * function, which might not be available wrapped trees |
| 980 | * (see #cxTreeCreateWrapped()). | |
| 981 | * | |
| 982 | * @param tree the tree | |
| 983 | * @param data the data to search for | |
| 440 | 984 | * @return the first matching node, or @c NULL when the data cannot be found |
| 324 | 985 | */ |
| 870 | 986 | cx_attr_nonnull cx_attr_nodiscard |
| 987 | CX_EXPORT void *cxTreeFind(CxTree *tree, const void *data); | |
| 324 | 988 | |
| 989 | /** | |
| 990 | * Searches the data in the specified subtree. | |
| 991 | * | |
| 440 | 992 | * When @p max_depth is zero, the depth is not limited. |
| 993 | * The @p subtree_root itself is on depth 1 and its children have depth 2. | |
| 994 | * | |
| 995 | * @note When @p subtree_root is not part of the @p tree, the behavior is | |
| 324 | 996 | * undefined. |
| 997 | * | |
| 440 | 998 | * @remark For this function to work, the tree needs a specified @c search_data |
| 324 | 999 | * function, which might not be the case for wrapped trees |
| 1000 | * (see #cxTreeCreateWrapped()). | |
| 1001 | * | |
| 1002 | * @param tree the tree | |
| 1003 | * @param data the data to search for | |
| 1004 | * @param subtree_root the node where to start | |
| 440 | 1005 | * @param max_depth the maximum search depth |
| 1006 | * @return the first matching node, or @c NULL when the data cannot be found | |
| 324 | 1007 | */ |
| 870 | 1008 | cx_attr_nonnull cx_attr_nodiscard |
| 1009 | CX_EXPORT void *cxTreeFindInSubtree(CxTree *tree, const void *data, void *subtree_root, size_t max_depth); | |
| 324 | 1010 | |
| 1011 | /** | |
| 1012 | * Determines the size of the specified subtree. | |
| 1013 | * | |
| 1014 | * @param tree the tree | |
| 1015 | * @param subtree_root the root node of the subtree | |
| 1016 | * @return the number of nodes in the specified subtree | |
| 1017 | */ | |
| 870 | 1018 | cx_attr_nonnull cx_attr_nodiscard |
| 1019 | CX_EXPORT size_t cxTreeSubtreeSize(CxTree *tree, void *subtree_root); | |
| 324 | 1020 | |
| 1021 | /** | |
| 1022 | * Determines the depth of the specified subtree. | |
| 1023 | * | |
| 1024 | * @param tree the tree | |
| 1025 | * @param subtree_root the root node of the subtree | |
| 440 | 1026 | * @return the tree depth including the @p subtree_root |
| 324 | 1027 | */ |
| 870 | 1028 | cx_attr_nonnull cx_attr_nodiscard |
| 1029 | CX_EXPORT size_t cxTreeSubtreeDepth(CxTree *tree, void *subtree_root); | |
| 324 | 1030 | |
| 1031 | /** | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1032 | * Determines the size of the entire tree. |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1033 | * |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1034 | * @param tree the tree |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1035 | * @return the tree size, counting the root as one |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1036 | */ |
| 870 | 1037 | cx_attr_nonnull cx_attr_nodiscard |
| 1038 | CX_EXPORT size_t cxTreeSize(CxTree *tree); | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1039 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1040 | /** |
| 324 | 1041 | * Determines the depth of the entire tree. |
| 1042 | * | |
| 1043 | * @param tree the tree | |
| 1044 | * @return the tree depth, counting the root as one | |
| 1045 | */ | |
| 870 | 1046 | cx_attr_nonnull cx_attr_nodiscard |
| 1047 | CX_EXPORT size_t cxTreeDepth(CxTree *tree); | |
| 324 | 1048 | |
| 1049 | /** | |
| 440 | 1050 | * Creates a depth-first iterator for the specified tree starting in @p node. |
| 1051 | * | |
| 1052 | * If the node is not part of the tree, the behavior is undefined. | |
| 1053 | * | |
| 1054 | * @param tree the tree to iterate | |
| 1055 | * @param node the node where to start | |
| 1056 | * @param visit_on_exit true, if the iterator shall visit a node again when | |
| 1057 | * leaving the subtree | |
| 1058 | * @return a tree iterator (depth-first) | |
| 1059 | * @see cxTreeVisit() | |
| 1060 | */ | |
| 870 | 1061 | cx_attr_nonnull cx_attr_nodiscard |
| 1062 | CX_EXPORT CxTreeIterator cxTreeIterateSubtree(CxTree *tree, void *node, bool visit_on_exit); | |
| 440 | 1063 | |
| 1064 | /** | |
| 1065 | * Creates a breadth-first iterator for the specified tree starting in @p node. | |
| 1066 | * | |
| 1067 | * If the node is not part of the tree, the behavior is undefined. | |
| 1068 | * | |
| 1069 | * @param tree the tree to iterate | |
| 1070 | * @param node the node where to start | |
| 1071 | * @return a tree visitor (a.k.a. breadth-first iterator) | |
| 1072 | * @see cxTreeIterate() | |
| 1073 | */ | |
| 870 | 1074 | cx_attr_nonnull cx_attr_nodiscard |
| 1075 | CX_EXPORT CxTreeVisitor cxTreeVisitSubtree(CxTree *tree, void *node); | |
| 440 | 1076 | |
| 1077 | /** | |
| 324 | 1078 | * Creates a depth-first iterator for the specified tree. |
| 1079 | * | |
| 1080 | * @param tree the tree to iterate | |
| 1081 | * @param visit_on_exit true, if the iterator shall visit a node again when | |
| 440 | 1082 | * leaving the subtree |
| 324 | 1083 | * @return a tree iterator (depth-first) |
| 440 | 1084 | * @see cxTreeVisit() |
| 324 | 1085 | */ |
| 870 | 1086 | cx_attr_nonnull cx_attr_nodiscard |
| 1087 | CX_EXPORT CxTreeIterator cxTreeIterate(CxTree *tree, bool visit_on_exit); | |
| 324 | 1088 | |
| 1089 | /** | |
| 1090 | * Creates a breadth-first iterator for the specified tree. | |
| 1091 | * | |
| 1092 | * @param tree the tree to iterate | |
| 1093 | * @return a tree visitor (a.k.a. breadth-first iterator) | |
| 440 | 1094 | * @see cxTreeIterate() |
| 324 | 1095 | */ |
| 870 | 1096 | cx_attr_nonnull cx_attr_nodiscard |
| 1097 | CxTreeVisitor cxTreeVisit(CxTree *tree); | |
| 324 | 1098 | |
| 1099 | /** | |
| 440 | 1100 | * Sets the (new) parent of the specified child. |
| 1101 | * | |
| 845 | 1102 | * If the @p child is not already a member of the tree, this function behaves |
| 440 | 1103 | * as #cxTreeAddChildNode(). |
| 1104 | * | |
| 1105 | * @param tree the tree | |
| 1106 | * @param parent the (new) parent of the child | |
| 1107 | * @param child the node to add | |
| 1108 | * @see cxTreeAddChildNode() | |
| 1109 | */ | |
| 1110 | cx_attr_nonnull | |
| 870 | 1111 | CX_EXPORT void cxTreeSetParent(CxTree *tree, void *parent, void *child); |
| 440 | 1112 | |
| 1113 | /** | |
| 324 | 1114 | * Adds a new node to the tree. |
| 1115 | * | |
| 845 | 1116 | * If the @p child is already a member of the tree, the behavior is undefined. |
| 440 | 1117 | * Use #cxTreeSetParent() if you want to move a subtree to another location. |
| 1118 | * | |
| 1119 | * @attention The node may be externally created, but MUST obey the same rules | |
| 845 | 1120 | * as if it was created by the tree itself with #cxTreeAddChild() (e.g., use |
| 324 | 1121 | * the same allocator). |
| 1122 | * | |
| 1123 | * @param tree the tree | |
| 1124 | * @param parent the parent of the node to add | |
| 1125 | * @param child the node to add | |
| 440 | 1126 | * @see cxTreeSetParent() |
| 324 | 1127 | */ |
| 440 | 1128 | cx_attr_nonnull |
| 870 | 1129 | CX_EXPORT void cxTreeAddChildNode(CxTree *tree, void *parent, void *child); |
| 324 | 1130 | |
| 1131 | /** | |
| 1132 | * Creates a new node and adds it to the tree. | |
| 1133 | * | |
| 1134 | * With this function you can decide where exactly the new node shall be added. | |
| 1135 | * If you specified an appropriate search function, you may want to consider | |
| 1136 | * leaving this task to the tree by using #cxTreeInsert(). | |
| 1137 | * | |
| 1138 | * Be aware that adding nodes at arbitrary locations in the tree might cause | |
| 845 | 1139 | * wrong or undesired results when subsequently invoking #cxTreeInsert(), and |
| 324 | 1140 | * the invariant imposed by the search function does not hold any longer. |
| 1141 | * | |
| 1142 | * @param tree the tree | |
| 1143 | * @param parent the parent node of the new node | |
| 1144 | * @param data the data that will be submitted to the create function | |
| 1145 | * @return zero when the new node was created, non-zero on allocation failure | |
| 1146 | * @see cxTreeInsert() | |
| 1147 | */ | |
| 440 | 1148 | cx_attr_nonnull |
| 870 | 1149 | CX_EXPORT int cxTreeAddChild(CxTree *tree, void *parent, const void *data); |
| 324 | 1150 | |
| 1151 | /** | |
| 1152 | * A function that is invoked when a node needs to be re-linked to a new parent. | |
| 1153 | * | |
| 1154 | * When a node is re-linked, sometimes the contents need to be updated. | |
| 440 | 1155 | * This callback is invoked by #cxTreeRemoveNode() and #cxTreeDestroyNode() |
| 1156 | * so that those updates can be applied when re-linking the children of the | |
| 1157 | * removed node. | |
| 324 | 1158 | * |
| 1159 | * @param node the affected node | |
| 1160 | * @param old_parent the old parent of the node | |
| 1161 | * @param new_parent the new parent of the node | |
| 1162 | */ | |
| 1163 | typedef void (*cx_tree_relink_func)( | |
| 1164 | void *node, | |
| 1165 | const void *old_parent, | |
| 1166 | const void *new_parent | |
| 1167 | ); | |
| 1168 | ||
| 1169 | /** | |
| 1170 | * Removes a node and re-links its children to its former parent. | |
| 1171 | * | |
| 1172 | * If the node is not part of the tree, the behavior is undefined. | |
| 1173 | * | |
| 440 | 1174 | * @note The destructor function, if any, will @em not be invoked. That means |
| 324 | 1175 | * you will need to free the removed node by yourself, eventually. |
| 1176 | * | |
| 1177 | * @param tree the tree | |
| 1178 | * @param node the node to remove (must not be the root node) | |
| 1179 | * @param relink_func optional callback to update the content of each re-linked | |
| 1180 | * node | |
| 440 | 1181 | * @return zero on success, non-zero if @p node is the root node of the tree |
| 324 | 1182 | */ |
| 440 | 1183 | cx_attr_nonnull_arg(1, 2) |
| 870 | 1184 | CX_EXPORT int cxTreeRemoveNode(CxTree *tree, void *node, cx_tree_relink_func relink_func); |
| 324 | 1185 | |
| 1186 | /** | |
| 845 | 1187 | * Removes a node and its subtree from the tree. |
| 324 | 1188 | * |
| 1189 | * If the node is not part of the tree, the behavior is undefined. | |
| 1190 | * | |
| 440 | 1191 | * @note The destructor function, if any, will @em not be invoked. That means |
| 324 | 1192 | * you will need to free the removed subtree by yourself, eventually. |
| 1193 | * | |
| 1194 | * @param tree the tree | |
| 1195 | * @param node the node to remove | |
| 1196 | */ | |
| 440 | 1197 | cx_attr_nonnull |
| 870 | 1198 | CX_EXPORT void cxTreeRemoveSubtree(CxTree *tree, void *node); |
| 324 | 1199 | |
| 440 | 1200 | /** |
| 1201 | * Destroys a node and re-links its children to its former parent. | |
| 1202 | * | |
| 1203 | * If the node is not part of the tree, the behavior is undefined. | |
| 1204 | * | |
| 1205 | * It is guaranteed that the simple destructor is invoked before | |
| 1206 | * the advanced destructor. | |
| 1207 | * | |
| 1208 | * @attention This function will not free the memory of the node with the | |
| 1209 | * tree's allocator, because that is usually done by the advanced destructor | |
| 1210 | * and would therefore result in a double-free. | |
| 1211 | * | |
| 1212 | * @param tree the tree | |
| 1213 | * @param node the node to destroy (must not be the root node) | |
| 1214 | * @param relink_func optional callback to update the content of each re-linked | |
| 1215 | * node | |
| 1216 | * @return zero on success, non-zero if @p node is the root node of the tree | |
| 1217 | */ | |
| 1218 | cx_attr_nonnull_arg(1, 2) | |
| 870 | 1219 | CX_EXPORT int cxTreeDestroyNode(CxTree *tree, void *node, cx_tree_relink_func relink_func); |
| 440 | 1220 | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1221 | #ifdef __cplusplus |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1222 | } // extern "C" |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1223 | #endif |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1224 | |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1225 | #endif //UCX_TREE_H |