src/cx/tree.h

Sun, 23 Oct 2022 13:32:46 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 23 Oct 2022 13:32:46 +0200
changeset 593
ea9b41b5ebbc
parent 484
9e6900b1cf9d
child 628
1e2be40f0cb5
permissions
-rw-r--r--

explicitly cast int to char

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * 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

#include "common.h"
#include "allocator.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * 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 */

mercurial