src/cx/tree.h

Sun, 03 Oct 2021 14:06:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Oct 2021 14:06:57 +0200
changeset 453
bb144d08cd44
parent 442
310019ddfe4e
child 484
9e6900b1cf9d
permissions
-rw-r--r--

add some documentation and changes some signatures

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

mercurial