src/cx/tree.h

Sun, 03 Oct 2021 16:02:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Oct 2021 16:02:53 +0200
changeset 455
8168e16cd1e9
parent 453
bb144d08cd44
child 484
9e6900b1cf9d
permissions
-rw-r--r--

change test names

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

mercurial