UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 /** 29 * \file tree.h 30 * \brief Interface for tree implementations. 31 * \author Olaf Wintermann 32 * \author Mike Becker 33 * \version 3.0 34 * \copyright 2-Clause BSD License 35 */ 36 37 #ifndef UCX_TREE_H 38 #define UCX_TREE_H 39 40 #include "common.h" 41 #include "allocator.h" 42 43 #ifdef __cplusplus 44 extern "C" { 45 #endif 46 47 /** 48 * Adds a sibling to the current tree node. 49 * 50 * In case your struct does not have a \p prev or a \p parent pointer, 51 * specify a negative location. The location of the \p next pointer is 52 * mandatory. 53 * 54 * \attention Do not use this function to add siblings in a tree where the 55 * nodes store a pointer to the last sibling because that would not be modified by this function. 56 * 57 * \remark If yo do not provide a location to the parent pointer, a call to this function is 58 * effectively the same as a call to cx_linked_list_add(). 59 * 60 * @param node a pointer to the node 61 * @param loc_prev the location of a \c prev pointer within your node struct 62 * @param loc_next the location of a \c next pointer within your node struct 63 * @param loc_parent the location of a \c parent pointer within your node struct 64 * @param new_node the new node that shall be added as a sibling 65 */ 66 void cx_tree_add_sibling(void *node, 67 ptrdiff_t loc_prev, ptrdiff_t loc_next, 68 ptrdiff_t loc_parent, 69 void *new_node) 70 __attribute__((__nonnull__)); 71 72 /** 73 * Adds a node to the list of children. 74 * 75 * \par Example with a full structure 76 * A full tree node structure may look like this: 77 * \code 78 * typedef struct MyTreeNode MyTreeNode; 79 * struct MyTreeNode { 80 * MyTreeNode* parent; 81 * MyTreeNode* first_child; 82 * MyTreeNode* last_child; 83 * MyTreeNode* prev_sibling; 84 * MyTreeNode* next_sibling; 85 * // ...contents... 86 * } 87 * \endcode 88 * Adding a new child to a node with the above structure can be performed with the following call: 89 * \code 90 * MyTreeNode *node, *child; // given 91 * cx_tree_add_child(&node->first_child, &node->last_child, 92 * offsetof(MyTreeNode, prev_sibling), offsetof(MyTreeNode, next_sibling), 93 * child, offsetof(MyTreeNode, parent), node); 94 * \endcode 95 * 96 * \par Example with a reduced structure 97 * The minimal reasonable structure with parent pointer looks like this: 98 * \code 99 * typedef struct MyTreeNode MyTreeNode; 100 * struct MyTreeNode { 101 * MyTreeNode* parent; 102 * MyTreeNode* children; 103 * MyTreeNode* next_sibling; 104 * // ...contents... 105 * } 106 * \endcode 107 * This simplifies the function call to: 108 * \code 109 * MyTreeNode *node, *child; // given 110 * cx_tree_add_child(&node->children, NULL, -1, offsetof(MyTreeNode, next_sibling), 111 * child, offsetof(MyTreeNode, parent), node); 112 * \endcode 113 * 114 * \remark If your tree structure does not possess a parent pointer, a call to this function is 115 * effectively the same as a call to cx_linked_list_add(). 116 * 117 * @param children_begin a pointer to the begin node pointer (if your list has one) 118 * @param children_end a pointer to the end node pointer (if your list has one) 119 * @param loc_prev the location of a \c prev pointer within your node struct 120 * @param loc_next the location of a \c next pointer within your node struct 121 * @param new_node a pointer to the node that shall be appended 122 * @param loc_parent the location of a \c parent pointer within your node struct 123 * @param parent the parent node 124 125 void cx_tree_add_child(void **children_begin, void **children_end, 126 ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node, 127 ptrdiff_t loc_parent, void *parent) 128 __attribute__((__nonnull__ (5))); 129 130 131 #ifdef __cplusplus 132 } // extern "C" 133 #endif 134 135 #endif // UCX_TREE_H 136 137