Thu, 15 Feb 2024 21:54:43 +0100
add cx_tree_search() - relates to #165
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@816 | 41 | #ifdef __cplusplus |
universe@816 | 42 | extern "C" { |
universe@816 | 43 | #endif |
universe@816 | 44 | |
universe@816 | 45 | |
universe@822 | 46 | /** |
universe@822 | 47 | * Links a node to a (new) parent. |
universe@822 | 48 | * |
universe@822 | 49 | * If the node has already a parent, it is unlinked, first. |
universe@826 | 50 | * If the parent has children already, the node is prepended to the list |
universe@826 | 51 | * of all currently existing children. |
universe@822 | 52 | * |
universe@822 | 53 | * @param parent the parent node |
universe@822 | 54 | * @param node the node that shall be linked |
universe@822 | 55 | * @param loc_parent offset in the node struct for the parent pointer |
universe@822 | 56 | * @param loc_children offset in the node struct for the children linked list |
universe@822 | 57 | * @param loc_prev offset in the node struct for the prev pointer |
universe@822 | 58 | * @param loc_next offset in the node struct for the next pointer |
universe@822 | 59 | * @see cx_tree_unlink() |
universe@822 | 60 | */ |
universe@816 | 61 | __attribute__((__nonnull__)) |
universe@816 | 62 | void cx_tree_link( |
universe@816 | 63 | void * restrict parent, |
universe@816 | 64 | void * restrict node, |
universe@816 | 65 | ptrdiff_t loc_parent, |
universe@816 | 66 | ptrdiff_t loc_children, |
universe@816 | 67 | ptrdiff_t loc_prev, |
universe@816 | 68 | ptrdiff_t loc_next |
universe@816 | 69 | ); |
universe@816 | 70 | |
universe@822 | 71 | /** |
universe@822 | 72 | * Unlinks a node from its parent. |
universe@822 | 73 | * |
universe@822 | 74 | * If the node has no parent, this function does nothing. |
universe@822 | 75 | * |
universe@822 | 76 | * @param node the node that shall be unlinked from its parent |
universe@822 | 77 | * @param loc_parent offset in the node struct for the parent pointer |
universe@822 | 78 | * @param loc_children offset in the node struct for the children linked list |
universe@822 | 79 | * @param loc_prev offset in the node struct for the prev pointer |
universe@822 | 80 | * @param loc_next offset in the node struct for the next pointer |
universe@822 | 81 | * @see cx_tree_link() |
universe@822 | 82 | */ |
universe@816 | 83 | __attribute__((__nonnull__)) |
universe@816 | 84 | void cx_tree_unlink( |
universe@816 | 85 | void *node, |
universe@816 | 86 | ptrdiff_t loc_parent, |
universe@816 | 87 | ptrdiff_t loc_children, |
universe@816 | 88 | ptrdiff_t loc_prev, |
universe@816 | 89 | ptrdiff_t loc_next |
universe@816 | 90 | ); |
universe@816 | 91 | |
universe@823 | 92 | /** |
universe@823 | 93 | * Function pointer for a search function. |
universe@823 | 94 | * |
universe@823 | 95 | * A function of this kind shall check if the specified \p node |
universe@823 | 96 | * contains the given \p data or if one of the children might contain |
universe@823 | 97 | * the data. |
universe@823 | 98 | * |
universe@826 | 99 | * The function should use the returned integer to indicate how close the |
universe@826 | 100 | * match is, where a negative number means that it does not match at all. |
universe@826 | 101 | * |
universe@823 | 102 | * For example if a tree stores file path information, a node that is |
universe@823 | 103 | * describing a parent directory of a filename that is searched, shall |
universe@826 | 104 | * return a positive number to indicate that a child node might contain the |
universe@826 | 105 | * searched item. On the other hand, if the node denotes a path that is not a |
universe@826 | 106 | * prefix of the searched filename, the function would return -1 to indicate |
universe@826 | 107 | * that * the search does not need to be continued in that branch. |
universe@823 | 108 | * |
universe@823 | 109 | * @param node the node that is currently investigated |
universe@823 | 110 | * @param data the data that is searched for |
universe@823 | 111 | * |
universe@823 | 112 | * @return 0 if the node contains the data, |
universe@826 | 113 | * positive if one of the children might contain the data, |
universe@826 | 114 | * negative if neither the node, nor the children contains the data |
universe@823 | 115 | */ |
universe@824 | 116 | typedef int (*cx_tree_search_func)(void const *node, void const* data); |
universe@823 | 117 | |
universe@826 | 118 | |
universe@826 | 119 | /** |
universe@826 | 120 | * Searches for data in a tree. |
universe@826 | 121 | * |
universe@826 | 122 | * When the data cannot be found exactly, the search function might return a |
universe@826 | 123 | * closest result which might be a good starting point for adding a new node |
universe@826 | 124 | * to the tree. |
universe@826 | 125 | * |
universe@826 | 126 | * Depending on the tree structure it is not necessarily guaranteed that the |
universe@826 | 127 | * "closest" match is uniquely defined. This function will search for a node |
universe@826 | 128 | * with the best match according to the \p sfunc (meaning: the return value of |
universe@826 | 129 | * \p sfunc which is closest to zero). If that is also ambiguous, an arbitrary |
universe@826 | 130 | * node matching the criteria is returned. |
universe@826 | 131 | * |
universe@826 | 132 | * @param root the root node |
universe@826 | 133 | * @param data the data to search for |
universe@826 | 134 | * @param sfunc the search function |
universe@826 | 135 | * @param result where the result shall be stored |
universe@826 | 136 | * @param loc_children offset in the node struct for the children linked list |
universe@826 | 137 | * @param loc_next offset in the node struct for the next pointer |
universe@826 | 138 | * @return zero if the node was found exactly, positive if a node was found that |
universe@826 | 139 | * could contain the node (but doesn't right now), negative if the tree does not |
universe@826 | 140 | * contain any node that might be related to the searched data |
universe@826 | 141 | */ |
universe@826 | 142 | __attribute__((__nonnull__)) |
universe@826 | 143 | int cx_tree_search( |
universe@826 | 144 | void const *root, |
universe@826 | 145 | void const *data, |
universe@826 | 146 | cx_tree_search_func sfunc, |
universe@826 | 147 | void **result, |
universe@826 | 148 | ptrdiff_t loc_children, |
universe@826 | 149 | ptrdiff_t loc_next |
universe@826 | 150 | ); |
universe@826 | 151 | |
universe@816 | 152 | #ifdef __cplusplus |
universe@816 | 153 | } // extern "C" |
universe@816 | 154 | #endif |
universe@816 | 155 | |
universe@816 | 156 | #endif //UCX_TREE_H |