Sun, 18 Feb 2024 12:24:04 +0100
commit complicated stuff before simplifying it
relates to #371
universe@816 | 1 | /* |
universe@816 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@816 | 3 | * |
universe@816 | 4 | * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved. |
universe@816 | 5 | * |
universe@816 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@816 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@816 | 8 | * |
universe@816 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@816 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@816 | 11 | * |
universe@816 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@816 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@816 | 14 | * documentation and/or other materials provided with the distribution. |
universe@816 | 15 | * |
universe@816 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@816 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@816 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@816 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@816 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@816 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@816 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@816 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@816 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@816 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@816 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@816 | 27 | */ |
universe@816 | 28 | /** |
universe@816 | 29 | * \file tree.h |
universe@816 | 30 | * \brief Interface for tree implementations. |
universe@816 | 31 | * \author Mike Becker |
universe@816 | 32 | * \author Olaf Wintermann |
universe@816 | 33 | * \copyright 2-Clause BSD License |
universe@816 | 34 | */ |
universe@816 | 35 | |
universe@816 | 36 | #ifndef UCX_TREE_H |
universe@816 | 37 | #define UCX_TREE_H |
universe@816 | 38 | |
universe@816 | 39 | #include "common.h" |
universe@816 | 40 | |
universe@827 | 41 | #include "iterator.h" |
universe@827 | 42 | |
universe@816 | 43 | #ifdef __cplusplus |
universe@816 | 44 | extern "C" { |
universe@816 | 45 | #endif |
universe@816 | 46 | |
universe@827 | 47 | /** |
universe@827 | 48 | * When entering a node. |
universe@830 | 49 | * |
universe@830 | 50 | * When this is the first sibling, source is the parent, otherwise it is the previous child. |
universe@827 | 51 | */ |
universe@827 | 52 | #define CX_TREE_ITERATOR_ENTER 0x1 |
universe@827 | 53 | /** |
universe@827 | 54 | * When advancing to the next child. |
universe@830 | 55 | * |
universe@830 | 56 | * The visited node is the next child and the source is the previous child. |
universe@830 | 57 | * This pass is triggered after exiting the previous child and before entering the next child. |
universe@827 | 58 | */ |
universe@827 | 59 | #define CX_TREE_ITERATOR_NEXT_CHILD 0x2 |
universe@827 | 60 | /** |
universe@827 | 61 | * When exiting the node. |
universe@830 | 62 | * |
universe@830 | 63 | * The visited node is the node being exited and source is the previously entered node |
universe@830 | 64 | * (usually the last child of the exited node, unless it has no children, in which case it is the node itself). |
universe@830 | 65 | * When advancing to the next child in a list of siblings, the previous child is exited, first. |
universe@827 | 66 | */ |
universe@827 | 67 | #define CX_TREE_ITERATOR_EXIT 0x4 |
universe@827 | 68 | |
universe@827 | 69 | /** |
universe@827 | 70 | * Tree iterator. |
universe@827 | 71 | * |
universe@827 | 72 | * This iterator is not position-aware in a strict sense, as it does not assume a particular order of elements in the |
universe@827 | 73 | * tree. However, the iterator keeps track of the number of nodes it has passed in a counter variable. |
universe@827 | 74 | * Each node, regardless of the number of passes, is counted only once. |
universe@827 | 75 | * |
universe@827 | 76 | * @note Objects that are pointed to by an iterator are mutable through that iterator. However, if the |
universe@827 | 77 | * iterator is based on a collection and the underlying collection is mutated by other means than this iterator |
universe@827 | 78 | * (e.g. elements added or removed), the iterator becomes invalid (regardless of what cxIteratorValid() returns) |
universe@827 | 79 | * and MUST be re-obtained from the collection. |
universe@827 | 80 | * |
universe@827 | 81 | * @see CxIterator |
universe@827 | 82 | */ |
universe@827 | 83 | typedef struct cx_tree_iterator_s { |
universe@827 | 84 | /** |
universe@827 | 85 | * The base properties of this iterator. |
universe@827 | 86 | */ |
universe@827 | 87 | struct cx_iterator_base_s base; |
universe@827 | 88 | /** |
universe@827 | 89 | * The passes that are requested by this iterator. |
universe@827 | 90 | * A combination of the flags #CX_TREE_ITERATOR_ENTER, #CX_TREE_ITERATOR_NEXT_CHILD, #CX_TREE_ITERATOR_EXIT. |
universe@827 | 91 | * |
universe@827 | 92 | * Changing the value after beginning the iteration is unspecified. |
universe@827 | 93 | */ |
universe@827 | 94 | uint8_t requested_passes; |
universe@827 | 95 | /** |
universe@827 | 96 | * The current pass. |
universe@827 | 97 | * |
universe@827 | 98 | * @see CX_TREE_ITERATOR_ENTER |
universe@827 | 99 | * @see CX_TREE_ITERATOR_NEXT_CHILD |
universe@827 | 100 | * @see CX_TREE_ITERATOR_EXIT |
universe@827 | 101 | */ |
universe@827 | 102 | uint8_t current_pass; |
universe@827 | 103 | /** |
universe@827 | 104 | * Offset in the node struct for the children linked list. |
universe@827 | 105 | */ |
universe@827 | 106 | off_t loc_children; |
universe@827 | 107 | /** |
universe@827 | 108 | * Offset in the node struct for the next pointer. |
universe@827 | 109 | */ |
universe@827 | 110 | off_t loc_next; |
universe@827 | 111 | /** |
universe@827 | 112 | * The total number of distinct nodes that have been passed so far. |
universe@827 | 113 | */ |
universe@827 | 114 | size_t counter; |
universe@827 | 115 | /** |
universe@827 | 116 | * The currently observed node. |
universe@827 | 117 | * |
universe@827 | 118 | * This is the same what cxIteratorCurrent() would return. |
universe@827 | 119 | */ |
universe@827 | 120 | void *node; |
universe@827 | 121 | /** |
universe@827 | 122 | * The node where we came from. |
universe@827 | 123 | * |
universe@827 | 124 | * - When entering the root node, this is \c NULL. |
universe@827 | 125 | * - When entering another node, this is the node's parent. |
universe@827 | 126 | * - When advancing to the next child, this is the previous child. |
universe@827 | 127 | */ |
universe@827 | 128 | void *source; |
universe@827 | 129 | /** |
universe@827 | 130 | * Internal stack. |
universe@827 | 131 | * Will be automatically freed once the iterator becomes invalid. |
universe@827 | 132 | * |
universe@827 | 133 | * If you want to discard the iterator before, you need to manually |
universe@827 | 134 | * call cxTreeIteratorDispose(). |
universe@827 | 135 | */ |
universe@827 | 136 | void **stack; |
universe@828 | 137 | /** |
universe@828 | 138 | * Internal capacity of the stack. |
universe@828 | 139 | */ |
universe@828 | 140 | size_t stack_capacity; |
universe@828 | 141 | /** |
universe@828 | 142 | * Current depth. |
universe@828 | 143 | */ |
universe@828 | 144 | size_t depth; |
universe@827 | 145 | } CxTreeIterator; |
universe@827 | 146 | |
universe@827 | 147 | /** |
universe@827 | 148 | * Releases internal memory of the given tree iterator. |
universe@827 | 149 | * @param iter the iterator |
universe@827 | 150 | */ |
universe@827 | 151 | static inline void cxTreeIteratorDispose(CxTreeIterator *iter) { |
universe@827 | 152 | free(iter->stack); |
universe@827 | 153 | } |
universe@816 | 154 | |
universe@822 | 155 | /** |
universe@822 | 156 | * Links a node to a (new) parent. |
universe@822 | 157 | * |
universe@822 | 158 | * If the node has already a parent, it is unlinked, first. |
universe@826 | 159 | * If the parent has children already, the node is prepended to the list |
universe@826 | 160 | * of all currently existing children. |
universe@822 | 161 | * |
universe@822 | 162 | * @param parent the parent node |
universe@822 | 163 | * @param node the node that shall be linked |
universe@822 | 164 | * @param loc_parent offset in the node struct for the parent pointer |
universe@822 | 165 | * @param loc_children offset in the node struct for the children linked list |
universe@822 | 166 | * @param loc_prev offset in the node struct for the prev pointer |
universe@822 | 167 | * @param loc_next offset in the node struct for the next pointer |
universe@822 | 168 | * @see cx_tree_unlink() |
universe@822 | 169 | */ |
universe@816 | 170 | __attribute__((__nonnull__)) |
universe@816 | 171 | void cx_tree_link( |
universe@816 | 172 | void * restrict parent, |
universe@816 | 173 | void * restrict node, |
universe@816 | 174 | ptrdiff_t loc_parent, |
universe@816 | 175 | ptrdiff_t loc_children, |
universe@816 | 176 | ptrdiff_t loc_prev, |
universe@816 | 177 | ptrdiff_t loc_next |
universe@816 | 178 | ); |
universe@816 | 179 | |
universe@822 | 180 | /** |
universe@822 | 181 | * Unlinks a node from its parent. |
universe@822 | 182 | * |
universe@822 | 183 | * If the node has no parent, this function does nothing. |
universe@822 | 184 | * |
universe@822 | 185 | * @param node the node that shall be unlinked from its parent |
universe@822 | 186 | * @param loc_parent offset in the node struct for the parent pointer |
universe@822 | 187 | * @param loc_children offset in the node struct for the children linked list |
universe@822 | 188 | * @param loc_prev offset in the node struct for the prev pointer |
universe@822 | 189 | * @param loc_next offset in the node struct for the next pointer |
universe@822 | 190 | * @see cx_tree_link() |
universe@822 | 191 | */ |
universe@816 | 192 | __attribute__((__nonnull__)) |
universe@816 | 193 | void cx_tree_unlink( |
universe@816 | 194 | void *node, |
universe@816 | 195 | ptrdiff_t loc_parent, |
universe@816 | 196 | ptrdiff_t loc_children, |
universe@816 | 197 | ptrdiff_t loc_prev, |
universe@816 | 198 | ptrdiff_t loc_next |
universe@816 | 199 | ); |
universe@816 | 200 | |
universe@823 | 201 | /** |
universe@823 | 202 | * Function pointer for a search function. |
universe@823 | 203 | * |
universe@823 | 204 | * A function of this kind shall check if the specified \p node |
universe@823 | 205 | * contains the given \p data or if one of the children might contain |
universe@823 | 206 | * the data. |
universe@823 | 207 | * |
universe@826 | 208 | * The function should use the returned integer to indicate how close the |
universe@826 | 209 | * match is, where a negative number means that it does not match at all. |
universe@826 | 210 | * |
universe@823 | 211 | * For example if a tree stores file path information, a node that is |
universe@823 | 212 | * describing a parent directory of a filename that is searched, shall |
universe@826 | 213 | * return a positive number to indicate that a child node might contain the |
universe@826 | 214 | * searched item. On the other hand, if the node denotes a path that is not a |
universe@826 | 215 | * prefix of the searched filename, the function would return -1 to indicate |
universe@826 | 216 | * that * the search does not need to be continued in that branch. |
universe@823 | 217 | * |
universe@823 | 218 | * @param node the node that is currently investigated |
universe@823 | 219 | * @param data the data that is searched for |
universe@823 | 220 | * |
universe@823 | 221 | * @return 0 if the node contains the data, |
universe@826 | 222 | * positive if one of the children might contain the data, |
universe@826 | 223 | * negative if neither the node, nor the children contains the data |
universe@823 | 224 | */ |
universe@824 | 225 | typedef int (*cx_tree_search_func)(void const *node, void const* data); |
universe@823 | 226 | |
universe@826 | 227 | |
universe@826 | 228 | /** |
universe@826 | 229 | * Searches for data in a tree. |
universe@826 | 230 | * |
universe@826 | 231 | * When the data cannot be found exactly, the search function might return a |
universe@826 | 232 | * closest result which might be a good starting point for adding a new node |
universe@826 | 233 | * to the tree. |
universe@826 | 234 | * |
universe@826 | 235 | * Depending on the tree structure it is not necessarily guaranteed that the |
universe@826 | 236 | * "closest" match is uniquely defined. This function will search for a node |
universe@826 | 237 | * with the best match according to the \p sfunc (meaning: the return value of |
universe@826 | 238 | * \p sfunc which is closest to zero). If that is also ambiguous, an arbitrary |
universe@826 | 239 | * node matching the criteria is returned. |
universe@826 | 240 | * |
universe@826 | 241 | * @param root the root node |
universe@826 | 242 | * @param data the data to search for |
universe@826 | 243 | * @param sfunc the search function |
universe@826 | 244 | * @param result where the result shall be stored |
universe@826 | 245 | * @param loc_children offset in the node struct for the children linked list |
universe@826 | 246 | * @param loc_next offset in the node struct for the next pointer |
universe@826 | 247 | * @return zero if the node was found exactly, positive if a node was found that |
universe@826 | 248 | * could contain the node (but doesn't right now), negative if the tree does not |
universe@826 | 249 | * contain any node that might be related to the searched data |
universe@826 | 250 | */ |
universe@826 | 251 | __attribute__((__nonnull__)) |
universe@826 | 252 | int cx_tree_search( |
universe@826 | 253 | void const *root, |
universe@826 | 254 | void const *data, |
universe@826 | 255 | cx_tree_search_func sfunc, |
universe@826 | 256 | void **result, |
universe@826 | 257 | ptrdiff_t loc_children, |
universe@826 | 258 | ptrdiff_t loc_next |
universe@826 | 259 | ); |
universe@826 | 260 | |
universe@828 | 261 | /** |
universe@828 | 262 | * Creates an iterator for a tree with the specified root node. |
universe@828 | 263 | * |
universe@828 | 264 | * The \p passes argument is supposed to be a combination of the flags |
universe@828 | 265 | * #CX_TREE_ITERATOR_ENTER, #CX_TREE_ITERATOR_NEXT_CHILD, and #CX_TREE_ITERATOR_EXIT. |
universe@828 | 266 | * Alternatively, the integer 1 is equivalent to just specifying #CX_TREE_ITERATOR_ENTER |
universe@828 | 267 | * which will cause the iterator to pass every node only once (when entering the node). |
universe@828 | 268 | * |
universe@828 | 269 | * When #CX_TREE_ITERATOR_EXIT is set, the iterator will visit a parent node again, |
universe@828 | 270 | * when \em every of it's children has been visited (including the case when the node does not have any children). |
universe@828 | 271 | * |
universe@828 | 272 | * When #CX_TREE_ITERATOR_NEXT_CHILD is set, the iterator will visit a parent node again, |
universe@828 | 273 | * when advancing from one child to the next. |
universe@828 | 274 | * |
universe@828 | 275 | * @note A tree iterator needs to maintain a stack of visited nodes, which is allocated using stdlib malloc(). |
universe@828 | 276 | * When the iterator becomes invalid, this memory is automatically released. However, if you wish to cancel the |
universe@828 | 277 | * iteration before the iterator becomes invalid by itself, you MUST call cxTreeIteratorDispose() manually to release |
universe@828 | 278 | * the memory. |
universe@828 | 279 | * |
universe@828 | 280 | * @remark At the moment, the returned iterator does not support cxIteratorFlagRemoval(). |
universe@828 | 281 | * |
universe@828 | 282 | * @param root the root node |
universe@828 | 283 | * @param passes the passes this iterator shall perform |
universe@828 | 284 | * @param loc_children offset in the node struct for the children linked list |
universe@828 | 285 | * @param loc_next offset in the node struct for the next pointer |
universe@828 | 286 | * @return the new tree iterator |
universe@828 | 287 | * @see cxTreeIteratorDispose() |
universe@828 | 288 | */ |
universe@828 | 289 | __attribute__((__nonnull__)) |
universe@830 | 290 | CxTreeIterator cx_tree_iterator( |
universe@830 | 291 | void *root, |
universe@828 | 292 | int passes, |
universe@828 | 293 | ptrdiff_t loc_children, |
universe@828 | 294 | ptrdiff_t loc_next |
universe@828 | 295 | ); |
universe@828 | 296 | |
universe@816 | 297 | #ifdef __cplusplus |
universe@816 | 298 | } // extern "C" |
universe@816 | 299 | #endif |
universe@816 | 300 | |
universe@816 | 301 | #endif //UCX_TREE_H |