src/cx/tree.h

Sun, 18 Feb 2024 13:16:38 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 18 Feb 2024 13:16:38 +0100
changeset 832
97df2e4c68fb
parent 830
c4dae6fe6d5b
child 833
5c926801f052
permissions
-rw-r--r--

make cx_array_simple_add() automatically take the address of the element

816
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 /**
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 * \file tree.h
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 * \brief Interface for tree implementations.
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 * \author Mike Becker
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 * \author Olaf Wintermann
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 * \copyright 2-Clause BSD License
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 */
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 #ifndef UCX_TREE_H
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 #define UCX_TREE_H
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 #include "common.h"
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40
827
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
41 #include "iterator.h"
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
42
816
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 #ifdef __cplusplus
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 extern "C" {
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 #endif
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46
827
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
47 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
48 * When entering a node.
830
c4dae6fe6d5b commit complicated stuff before simplifying it
Mike Becker <universe@uap-core.de>
parents: 828
diff changeset
49 *
c4dae6fe6d5b commit complicated stuff before simplifying it
Mike Becker <universe@uap-core.de>
parents: 828
diff changeset
50 * When this is the first sibling, source is the parent, otherwise it is the previous child.
827
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
51 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
52 #define CX_TREE_ITERATOR_ENTER 0x1
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
53 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
54 * When advancing to the next child.
830
c4dae6fe6d5b commit complicated stuff before simplifying it
Mike Becker <universe@uap-core.de>
parents: 828
diff changeset
55 *
c4dae6fe6d5b commit complicated stuff before simplifying it
Mike Becker <universe@uap-core.de>
parents: 828
diff changeset
56 * The visited node is the next child and the source is the previous child.
c4dae6fe6d5b commit complicated stuff before simplifying it
Mike Becker <universe@uap-core.de>
parents: 828
diff changeset
57 * This pass is triggered after exiting the previous child and before entering the next child.
827
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
58 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
59 #define CX_TREE_ITERATOR_NEXT_CHILD 0x2
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
60 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
61 * When exiting the node.
830
c4dae6fe6d5b commit complicated stuff before simplifying it
Mike Becker <universe@uap-core.de>
parents: 828
diff changeset
62 *
c4dae6fe6d5b commit complicated stuff before simplifying it
Mike Becker <universe@uap-core.de>
parents: 828
diff changeset
63 * The visited node is the node being exited and source is the previously entered node
c4dae6fe6d5b commit complicated stuff before simplifying it
Mike Becker <universe@uap-core.de>
parents: 828
diff changeset
64 * (usually the last child of the exited node, unless it has no children, in which case it is the node itself).
c4dae6fe6d5b commit complicated stuff before simplifying it
Mike Becker <universe@uap-core.de>
parents: 828
diff changeset
65 * When advancing to the next child in a list of siblings, the previous child is exited, first.
827
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
66 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
67 #define CX_TREE_ITERATOR_EXIT 0x4
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
68
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
69 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
70 * Tree iterator.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
71 *
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
72 * This iterator is not position-aware in a strict sense, as it does not assume a particular order of elements in the
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
73 * tree. However, the iterator keeps track of the number of nodes it has passed in a counter variable.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
74 * Each node, regardless of the number of passes, is counted only once.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
75 *
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
76 * @note Objects that are pointed to by an iterator are mutable through that iterator. However, if the
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
77 * iterator is based on a collection and the underlying collection is mutated by other means than this iterator
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
78 * (e.g. elements added or removed), the iterator becomes invalid (regardless of what cxIteratorValid() returns)
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
79 * and MUST be re-obtained from the collection.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
80 *
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
81 * @see CxIterator
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
82 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
83 typedef struct cx_tree_iterator_s {
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
84 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
85 * The base properties of this iterator.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
86 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
87 struct cx_iterator_base_s base;
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
88 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
89 * The passes that are requested by this iterator.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
90 * A combination of the flags #CX_TREE_ITERATOR_ENTER, #CX_TREE_ITERATOR_NEXT_CHILD, #CX_TREE_ITERATOR_EXIT.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
91 *
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
92 * Changing the value after beginning the iteration is unspecified.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
93 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
94 uint8_t requested_passes;
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
95 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
96 * The current pass.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
97 *
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
98 * @see CX_TREE_ITERATOR_ENTER
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
99 * @see CX_TREE_ITERATOR_NEXT_CHILD
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
100 * @see CX_TREE_ITERATOR_EXIT
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
101 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
102 uint8_t current_pass;
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
103 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
104 * Offset in the node struct for the children linked list.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
105 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
106 off_t loc_children;
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
107 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
108 * Offset in the node struct for the next pointer.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
109 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
110 off_t loc_next;
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
111 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
112 * The total number of distinct nodes that have been passed so far.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
113 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
114 size_t counter;
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
115 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
116 * The currently observed node.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
117 *
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
118 * This is the same what cxIteratorCurrent() would return.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
119 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
120 void *node;
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
121 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
122 * The node where we came from.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
123 *
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
124 * - When entering the root node, this is \c NULL.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
125 * - When entering another node, this is the node's parent.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
126 * - When advancing to the next child, this is the previous child.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
127 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
128 void *source;
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
129 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
130 * Internal stack.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
131 * Will be automatically freed once the iterator becomes invalid.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
132 *
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
133 * If you want to discard the iterator before, you need to manually
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
134 * call cxTreeIteratorDispose().
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
135 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
136 void **stack;
828
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
137 /**
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
138 * Internal capacity of the stack.
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
139 */
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
140 size_t stack_capacity;
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
141 /**
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
142 * Current depth.
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
143 */
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
144 size_t depth;
827
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
145 } CxTreeIterator;
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
146
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
147 /**
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
148 * Releases internal memory of the given tree iterator.
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
149 * @param iter the iterator
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
150 */
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
151 static inline void cxTreeIteratorDispose(CxTreeIterator *iter) {
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
152 free(iter->stack);
13b40a598d16 first draft of a tree iterator
Mike Becker <universe@uap-core.de>
parents: 826
diff changeset
153 }
816
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154
822
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
155 /**
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
156 * Links a node to a (new) parent.
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
157 *
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
158 * If the node has already a parent, it is unlinked, first.
826
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
159 * If the parent has children already, the node is prepended to the list
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
160 * of all currently existing children.
822
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
161 *
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
162 * @param parent the parent node
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
163 * @param node the node that shall be linked
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
164 * @param loc_parent offset in the node struct for the parent pointer
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
165 * @param loc_children offset in the node struct for the children linked list
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
166 * @param loc_prev offset in the node struct for the prev pointer
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
167 * @param loc_next offset in the node struct for the next pointer
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
168 * @see cx_tree_unlink()
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
169 */
816
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 __attribute__((__nonnull__))
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 void cx_tree_link(
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 void * restrict parent,
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 void * restrict node,
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 ptrdiff_t loc_parent,
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 ptrdiff_t loc_children,
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 ptrdiff_t loc_prev,
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 ptrdiff_t loc_next
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 );
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179
822
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
180 /**
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
181 * Unlinks a node from its parent.
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
182 *
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
183 * If the node has no parent, this function does nothing.
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
184 *
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
185 * @param node the node that shall be unlinked from its parent
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
186 * @param loc_parent offset in the node struct for the parent pointer
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
187 * @param loc_children offset in the node struct for the children linked list
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
188 * @param loc_prev offset in the node struct for the prev pointer
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
189 * @param loc_next offset in the node struct for the next pointer
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
190 * @see cx_tree_link()
e2243453127f add code documentation for tree functions
Mike Becker <universe@uap-core.de>
parents: 816
diff changeset
191 */
816
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 __attribute__((__nonnull__))
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 void cx_tree_unlink(
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 void *node,
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 ptrdiff_t loc_parent,
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 ptrdiff_t loc_children,
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 ptrdiff_t loc_prev,
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 ptrdiff_t loc_next
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 );
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200
823
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
201 /**
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
202 * Function pointer for a search function.
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
203 *
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
204 * A function of this kind shall check if the specified \p node
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
205 * contains the given \p data or if one of the children might contain
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
206 * the data.
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
207 *
826
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
208 * The function should use the returned integer to indicate how close the
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
209 * match is, where a negative number means that it does not match at all.
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
210 *
823
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
211 * For example if a tree stores file path information, a node that is
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
212 * describing a parent directory of a filename that is searched, shall
826
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
213 * return a positive number to indicate that a child node might contain the
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
214 * searched item. On the other hand, if the node denotes a path that is not a
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
215 * prefix of the searched filename, the function would return -1 to indicate
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
216 * that * the search does not need to be continued in that branch.
823
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
217 *
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
218 * @param node the node that is currently investigated
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
219 * @param data the data that is searched for
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
220 *
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
221 * @return 0 if the node contains the data,
826
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
222 * positive if one of the children might contain the data,
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
223 * negative if neither the node, nor the children contains the data
823
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
224 */
824
a939872c284d fix missing typedef
Mike Becker <universe@uap-core.de>
parents: 823
diff changeset
225 typedef int (*cx_tree_search_func)(void const *node, void const* data);
823
f4faa7f73cb8 declare cx_tree_search_func function pointer
Mike Becker <universe@uap-core.de>
parents: 822
diff changeset
226
826
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
227
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
228 /**
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
229 * Searches for data in a tree.
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
230 *
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
231 * When the data cannot be found exactly, the search function might return a
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
232 * closest result which might be a good starting point for adding a new node
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
233 * to the tree.
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
234 *
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
235 * Depending on the tree structure it is not necessarily guaranteed that the
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
236 * "closest" match is uniquely defined. This function will search for a node
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
237 * with the best match according to the \p sfunc (meaning: the return value of
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
238 * \p sfunc which is closest to zero). If that is also ambiguous, an arbitrary
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
239 * node matching the criteria is returned.
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
240 *
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
241 * @param root the root node
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
242 * @param data the data to search for
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
243 * @param sfunc the search function
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
244 * @param result where the result shall be stored
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
245 * @param loc_children offset in the node struct for the children linked list
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
246 * @param loc_next offset in the node struct for the next pointer
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
247 * @return zero if the node was found exactly, positive if a node was found that
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
248 * could contain the node (but doesn't right now), negative if the tree does not
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
249 * contain any node that might be related to the searched data
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
250 */
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
251 __attribute__((__nonnull__))
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
252 int cx_tree_search(
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
253 void const *root,
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
254 void const *data,
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
255 cx_tree_search_func sfunc,
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
256 void **result,
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
257 ptrdiff_t loc_children,
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
258 ptrdiff_t loc_next
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
259 );
21840975d541 add cx_tree_search() - relates to #165
Mike Becker <universe@uap-core.de>
parents: 824
diff changeset
260
828
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
261 /**
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
262 * Creates an iterator for a tree with the specified root node.
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
263 *
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
264 * The \p passes argument is supposed to be a combination of the flags
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
265 * #CX_TREE_ITERATOR_ENTER, #CX_TREE_ITERATOR_NEXT_CHILD, and #CX_TREE_ITERATOR_EXIT.
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
266 * Alternatively, the integer 1 is equivalent to just specifying #CX_TREE_ITERATOR_ENTER
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
267 * which will cause the iterator to pass every node only once (when entering the node).
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
268 *
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
269 * When #CX_TREE_ITERATOR_EXIT is set, the iterator will visit a parent node again,
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
270 * when \em every of it's children has been visited (including the case when the node does not have any children).
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
271 *
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
272 * When #CX_TREE_ITERATOR_NEXT_CHILD is set, the iterator will visit a parent node again,
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
273 * when advancing from one child to the next.
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
274 *
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
275 * @note A tree iterator needs to maintain a stack of visited nodes, which is allocated using stdlib malloc().
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
276 * When the iterator becomes invalid, this memory is automatically released. However, if you wish to cancel the
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
277 * iteration before the iterator becomes invalid by itself, you MUST call cxTreeIteratorDispose() manually to release
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
278 * the memory.
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
279 *
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
280 * @remark At the moment, the returned iterator does not support cxIteratorFlagRemoval().
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
281 *
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
282 * @param root the root node
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
283 * @param passes the passes this iterator shall perform
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
284 * @param loc_children offset in the node struct for the children linked list
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
285 * @param loc_next offset in the node struct for the next pointer
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
286 * @return the new tree iterator
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
287 * @see cxTreeIteratorDispose()
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
288 */
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
289 __attribute__((__nonnull__))
830
c4dae6fe6d5b commit complicated stuff before simplifying it
Mike Becker <universe@uap-core.de>
parents: 828
diff changeset
290 CxTreeIterator cx_tree_iterator(
c4dae6fe6d5b commit complicated stuff before simplifying it
Mike Becker <universe@uap-core.de>
parents: 828
diff changeset
291 void *root,
828
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
292 int passes,
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
293 ptrdiff_t loc_children,
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
294 ptrdiff_t loc_next
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
295 );
88fa3381206d improve tree iterator struct and add signature for a function that can create an iterator
Mike Becker <universe@uap-core.de>
parents: 827
diff changeset
296
816
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
297 #ifdef __cplusplus
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
298 } // extern "C"
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
299 #endif
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
300
425234b05dff add first basic low level tree functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
301 #endif //UCX_TREE_H

mercurial