src/cx/tree.h

Sun, 18 Feb 2024 13:38:42 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 18 Feb 2024 13:38:42 +0100
changeset 833
5c926801f052
parent 830
c4dae6fe6d5b
child 835
13068743197f
permissions
-rw-r--r--

vastly simplify tree iterators and add test for creating them

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 * Tree iterator.
universe@827 49 *
universe@827 50 * This iterator is not position-aware in a strict sense, as it does not assume a particular order of elements in the
universe@827 51 * tree. However, the iterator keeps track of the number of nodes it has passed in a counter variable.
universe@827 52 * Each node, regardless of the number of passes, is counted only once.
universe@827 53 *
universe@827 54 * @note Objects that are pointed to by an iterator are mutable through that iterator. However, if the
universe@833 55 * underlying data structure is mutated by other means than this iterator (e.g. elements added or removed),
universe@833 56 * the iterator becomes invalid (regardless of what cxIteratorValid() returns).
universe@827 57 *
universe@827 58 * @see CxIterator
universe@827 59 */
universe@827 60 typedef struct cx_tree_iterator_s {
universe@827 61 /**
universe@827 62 * The base properties of this iterator.
universe@827 63 */
universe@827 64 struct cx_iterator_base_s base;
universe@827 65 /**
universe@833 66 * Set to true, when the iterator shall visit a node again
universe@833 67 * when all it's children have been processed.
universe@827 68 */
universe@833 69 bool visit_on_exit;
universe@827 70 /**
universe@833 71 * True, if this iterator is currently leaving the node.
universe@827 72 */
universe@833 73 bool exiting;
universe@827 74 /**
universe@827 75 * Offset in the node struct for the children linked list.
universe@827 76 */
universe@827 77 off_t loc_children;
universe@827 78 /**
universe@827 79 * Offset in the node struct for the next pointer.
universe@827 80 */
universe@827 81 off_t loc_next;
universe@827 82 /**
universe@827 83 * The total number of distinct nodes that have been passed so far.
universe@827 84 */
universe@827 85 size_t counter;
universe@827 86 /**
universe@827 87 * The currently observed node.
universe@827 88 *
universe@827 89 * This is the same what cxIteratorCurrent() would return.
universe@827 90 */
universe@827 91 void *node;
universe@827 92 /**
universe@827 93 * Internal stack.
universe@827 94 * Will be automatically freed once the iterator becomes invalid.
universe@827 95 *
universe@827 96 * If you want to discard the iterator before, you need to manually
universe@827 97 * call cxTreeIteratorDispose().
universe@827 98 */
universe@827 99 void **stack;
universe@828 100 /**
universe@828 101 * Internal capacity of the stack.
universe@828 102 */
universe@828 103 size_t stack_capacity;
universe@833 104 union {
universe@833 105 /**
universe@833 106 * Internal stack size.
universe@833 107 */
universe@833 108 size_t stack_size;
universe@833 109 /**
universe@833 110 * The current depth in the tree.
universe@833 111 */
universe@833 112 size_t depth;
universe@833 113 };
universe@827 114 } CxTreeIterator;
universe@827 115
universe@827 116 /**
universe@827 117 * Releases internal memory of the given tree iterator.
universe@827 118 * @param iter the iterator
universe@827 119 */
universe@827 120 static inline void cxTreeIteratorDispose(CxTreeIterator *iter) {
universe@827 121 free(iter->stack);
universe@827 122 }
universe@816 123
universe@822 124 /**
universe@822 125 * Links a node to a (new) parent.
universe@822 126 *
universe@822 127 * If the node has already a parent, it is unlinked, first.
universe@826 128 * If the parent has children already, the node is prepended to the list
universe@826 129 * of all currently existing children.
universe@822 130 *
universe@822 131 * @param parent the parent node
universe@822 132 * @param node the node that shall be linked
universe@822 133 * @param loc_parent offset in the node struct for the parent pointer
universe@822 134 * @param loc_children offset in the node struct for the children linked list
universe@822 135 * @param loc_prev offset in the node struct for the prev pointer
universe@822 136 * @param loc_next offset in the node struct for the next pointer
universe@822 137 * @see cx_tree_unlink()
universe@822 138 */
universe@816 139 __attribute__((__nonnull__))
universe@816 140 void cx_tree_link(
universe@816 141 void * restrict parent,
universe@816 142 void * restrict node,
universe@816 143 ptrdiff_t loc_parent,
universe@816 144 ptrdiff_t loc_children,
universe@816 145 ptrdiff_t loc_prev,
universe@816 146 ptrdiff_t loc_next
universe@816 147 );
universe@816 148
universe@822 149 /**
universe@822 150 * Unlinks a node from its parent.
universe@822 151 *
universe@822 152 * If the node has no parent, this function does nothing.
universe@822 153 *
universe@822 154 * @param node the node that shall be unlinked from its parent
universe@822 155 * @param loc_parent offset in the node struct for the parent pointer
universe@822 156 * @param loc_children offset in the node struct for the children linked list
universe@822 157 * @param loc_prev offset in the node struct for the prev pointer
universe@822 158 * @param loc_next offset in the node struct for the next pointer
universe@822 159 * @see cx_tree_link()
universe@822 160 */
universe@816 161 __attribute__((__nonnull__))
universe@816 162 void cx_tree_unlink(
universe@816 163 void *node,
universe@816 164 ptrdiff_t loc_parent,
universe@816 165 ptrdiff_t loc_children,
universe@816 166 ptrdiff_t loc_prev,
universe@816 167 ptrdiff_t loc_next
universe@816 168 );
universe@816 169
universe@823 170 /**
universe@823 171 * Function pointer for a search function.
universe@823 172 *
universe@823 173 * A function of this kind shall check if the specified \p node
universe@823 174 * contains the given \p data or if one of the children might contain
universe@823 175 * the data.
universe@823 176 *
universe@826 177 * The function should use the returned integer to indicate how close the
universe@826 178 * match is, where a negative number means that it does not match at all.
universe@826 179 *
universe@823 180 * For example if a tree stores file path information, a node that is
universe@823 181 * describing a parent directory of a filename that is searched, shall
universe@826 182 * return a positive number to indicate that a child node might contain the
universe@826 183 * searched item. On the other hand, if the node denotes a path that is not a
universe@826 184 * prefix of the searched filename, the function would return -1 to indicate
universe@826 185 * that * the search does not need to be continued in that branch.
universe@823 186 *
universe@823 187 * @param node the node that is currently investigated
universe@823 188 * @param data the data that is searched for
universe@823 189 *
universe@823 190 * @return 0 if the node contains the data,
universe@826 191 * positive if one of the children might contain the data,
universe@826 192 * negative if neither the node, nor the children contains the data
universe@823 193 */
universe@824 194 typedef int (*cx_tree_search_func)(void const *node, void const* data);
universe@823 195
universe@826 196
universe@826 197 /**
universe@826 198 * Searches for data in a tree.
universe@826 199 *
universe@826 200 * When the data cannot be found exactly, the search function might return a
universe@826 201 * closest result which might be a good starting point for adding a new node
universe@826 202 * to the tree.
universe@826 203 *
universe@826 204 * Depending on the tree structure it is not necessarily guaranteed that the
universe@826 205 * "closest" match is uniquely defined. This function will search for a node
universe@826 206 * with the best match according to the \p sfunc (meaning: the return value of
universe@826 207 * \p sfunc which is closest to zero). If that is also ambiguous, an arbitrary
universe@826 208 * node matching the criteria is returned.
universe@826 209 *
universe@826 210 * @param root the root node
universe@826 211 * @param data the data to search for
universe@826 212 * @param sfunc the search function
universe@826 213 * @param result where the result shall be stored
universe@826 214 * @param loc_children offset in the node struct for the children linked list
universe@826 215 * @param loc_next offset in the node struct for the next pointer
universe@826 216 * @return zero if the node was found exactly, positive if a node was found that
universe@826 217 * could contain the node (but doesn't right now), negative if the tree does not
universe@826 218 * contain any node that might be related to the searched data
universe@826 219 */
universe@826 220 __attribute__((__nonnull__))
universe@826 221 int cx_tree_search(
universe@826 222 void const *root,
universe@826 223 void const *data,
universe@826 224 cx_tree_search_func sfunc,
universe@826 225 void **result,
universe@826 226 ptrdiff_t loc_children,
universe@826 227 ptrdiff_t loc_next
universe@826 228 );
universe@826 229
universe@828 230 /**
universe@828 231 * Creates an iterator for a tree with the specified root node.
universe@828 232 *
universe@828 233 * @note A tree iterator needs to maintain a stack of visited nodes, which is allocated using stdlib malloc().
universe@828 234 * When the iterator becomes invalid, this memory is automatically released. However, if you wish to cancel the
universe@828 235 * iteration before the iterator becomes invalid by itself, you MUST call cxTreeIteratorDispose() manually to release
universe@828 236 * the memory.
universe@828 237 *
universe@828 238 * @remark At the moment, the returned iterator does not support cxIteratorFlagRemoval().
universe@828 239 *
universe@828 240 * @param root the root node
universe@833 241 * @param visit_on_exit set to true, when the iterator shall visit a node again after processing all children
universe@828 242 * @param loc_children offset in the node struct for the children linked list
universe@828 243 * @param loc_next offset in the node struct for the next pointer
universe@828 244 * @return the new tree iterator
universe@828 245 * @see cxTreeIteratorDispose()
universe@828 246 */
universe@828 247 __attribute__((__nonnull__))
universe@830 248 CxTreeIterator cx_tree_iterator(
universe@830 249 void *root,
universe@833 250 bool visit_on_exit,
universe@828 251 ptrdiff_t loc_children,
universe@828 252 ptrdiff_t loc_next
universe@828 253 );
universe@828 254
universe@816 255 #ifdef __cplusplus
universe@816 256 } // extern "C"
universe@816 257 #endif
universe@816 258
universe@816 259 #endif //UCX_TREE_H

mercurial