diff -r a10c3e127050 -r bb144d08cd44 src/cx/tree.h --- a/src/cx/tree.h Sun Oct 03 13:07:48 2021 +0200 +++ b/src/cx/tree.h Sun Oct 03 14:06:57 2021 +0200 @@ -25,6 +25,14 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ +/** + * \file tree.h + * \brief Interface for tree implementations. + * \author Olaf Wintermann + * \author Mike Becker + * \version 3.0 + * \copyright 2-Clause BSD License + */ #ifndef UCX_TREE_H #define UCX_TREE_H @@ -36,23 +44,93 @@ #ifdef __cplusplus extern "C" { #endif - -void* cx_tree_last(void *node, ptrdiff_t loc_next); - -int cx_tree_add_node(void *node, ptrdiff_t loc_parent, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node); -int cx_tree_add_child_node( - void *parent, - ptrdiff_t loc_parent, - ptrdiff_t loc_prev, - ptrdiff_t loc_next, - void **children_begin, - void **children_end, - void *new_node); +/** + * Adds a sibling to the current tree node. + * + * In case your struct does not have a \p prev or a \p parent pointer, + * specify a negative location. The location of the \p next pointer is + * mandatory. + * + * \attention Do not use this function to add siblings in a tree where the + * nodes store a pointer to the last sibling because that would not be modified by this function. + * + * \remark If yo do not provide a location to the parent pointer, a call to this function is + * effectively the same as a call to cx_linked_list_add(). + * + * @param node a pointer to the node + * @param loc_prev the location of a \c prev pointer within your node struct + * @param loc_next the location of a \c next pointer within your node struct + * @param loc_parent the location of a \c parent pointer within your node struct + * @param new_node the new node that shall be added as a sibling + */ +void cx_tree_add_sibling(void *node, + ptrdiff_t loc_prev, ptrdiff_t loc_next, + ptrdiff_t loc_parent, + void *new_node) +__attribute__((__nonnull__)); + +/** + * Adds a node to the list of children. + * + * \par Example with a full structure + * A full tree node structure may look like this: + * \code + * typedef struct MyTreeNode MyTreeNode; + * struct MyTreeNode { + * MyTreeNode* parent; + * MyTreeNode* first_child; + * MyTreeNode* last_child; + * MyTreeNode* prev_sibling; + * MyTreeNode* next_sibling; + * // ...contents... + * } + * \endcode + * Adding a new child to a node with the above structure can be performed with the following call: + * \code + * MyTreeNode *node, *child; // given + * cx_tree_add_child(&node->first_child, &node->last_child, + * offsetof(MyTreeNode, prev_sibling), offsetof(MyTreeNode, next_sibling), + * child, offsetof(MyTreeNode, parent), node); + * \endcode + * + * \par Example with a reduced structure + * The minimal reasonable structure with parent pointer looks like this: + * \code + * typedef struct MyTreeNode MyTreeNode; + * struct MyTreeNode { + * MyTreeNode* parent; + * MyTreeNode* children; + * MyTreeNode* next_sibling; + * // ...contents... + * } + * \endcode + * This simplifies the function call to: + * \code + * MyTreeNode *node, *child; // given + * cx_tree_add_child(&node->children, NULL, -1, offsetof(MyTreeNode, next_sibling), + * child, offsetof(MyTreeNode, parent), node); + * \endcode + * + * \remark If your tree structure does not possess a parent pointer, a call to this function is + * effectively the same as a call to cx_linked_list_add(). + * + * @param children_begin a pointer to the begin node pointer (if your list has one) + * @param children_end a pointer to the end node pointer (if your list has one) + * @param loc_prev the location of a \c prev pointer within your node struct + * @param loc_next the location of a \c next pointer within your node struct + * @param new_node a pointer to the node that shall be appended + * @param loc_parent the location of a \c parent pointer within your node struct + * @param parent the parent node + */ +void cx_tree_add_child(void **children_begin, void **children_end, + ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node, + ptrdiff_t loc_parent, void *parent) +__attribute__((__nonnull__ (5))); #ifdef __cplusplus -} +} /* extern "C" */ #endif #endif /* UCX_TREE_H */