579
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1
|
/*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4
|
* Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6
|
* Redistribution and use in source and binary forms, with or without
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7
|
* modification, are permitted provided that the following conditions are met:
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9
|
* 1. Redistributions of source code must retain the above copyright
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10
|
* notice, this list of conditions and the following disclaimer.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12
|
* 2. Redistributions in binary form must reproduce the above copyright
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13
|
* notice, this list of conditions and the following disclaimer in the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14
|
* documentation and/or other materials provided with the distribution.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
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
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26
|
* POSSIBILITY OF SUCH DAMAGE.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29
|
* @file tree.h
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30
|
* @brief Interface for tree implementations.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31
|
* @author Mike Becker
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32
|
* @author Olaf Wintermann
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33
|
* @copyright 2-Clause BSD License
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36
|
#ifndef UCX_TREE_H
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37
|
#define UCX_TREE_H
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39
|
#include "common.h"
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41
|
#include "collection.h"
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43
|
#ifdef __cplusplus
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44
|
extern "C" {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45
|
#endif
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48
|
* A depth-first tree iterator.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50
|
* This iterator is not position-aware in a strict sense, as it does not assume
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51
|
* a particular order of elements in the tree. However, the iterator keeps track
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52
|
* of the number of nodes it has passed in a counter variable.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53
|
* Each node, regardless of the number of passes, is counted only once.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55
|
* @note Objects that are pointed to by an iterator are mutable through that
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56
|
* iterator. However, if the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57
|
* underlying data structure is mutated by other means than this iterator (e.g.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58
|
* elements added or removed), the iterator becomes invalid (regardless of what
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59
|
* cxIteratorValid() returns).
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61
|
* @see CxIterator
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63
|
typedef struct cx_tree_iterator_s {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65
|
* Base members.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67
|
CX_ITERATOR_BASE;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69
|
* Indicates whether the subtree below the current node shall be skipped.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71
|
bool skip;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73
|
* Set to true, when the iterator shall visit a node again
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74
|
* when all it's children have been processed.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76
|
bool visit_on_exit;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78
|
* True, if this iterator is currently leaving the node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80
|
bool exiting;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82
|
* Offset in the node struct for the children linked list.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84
|
ptrdiff_t loc_children;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86
|
* Offset in the node struct for the next pointer.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
88
|
ptrdiff_t loc_next;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90
|
* The total number of distinct nodes that have been passed so far.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92
|
size_t counter;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94
|
* The currently observed node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96
|
* This is the same what cxIteratorCurrent() would return.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98
|
void *node;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100
|
* Stores a copy of the next pointer of the visited node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101
|
* Allows freeing a node on exit without corrupting the iteration.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103
|
void *node_next;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105
|
* Internal stack.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106
|
* Will be automatically freed once the iterator becomes invalid.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
107
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
108
|
* If you want to discard the iterator before, you need to manually
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
109
|
* call cxTreeIteratorDispose().
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
110
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
111
|
void **stack;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
112
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113
|
* Internal capacity of the stack.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115
|
size_t stack_capacity;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116
|
union {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118
|
* Internal stack size.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120
|
size_t stack_size;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
122
|
* The current depth in the tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
123
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
124
|
size_t depth;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
125
|
};
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
126
|
} CxTreeIterator;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
127
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
128
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
129
|
* An element in a visitor queue.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
130
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131
|
struct cx_tree_visitor_queue_s {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
132
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133
|
* The tree node to visit.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
134
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
135
|
void *node;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
136
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
137
|
* The depth of the node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
138
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139
|
size_t depth;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141
|
* The next element in the queue or @c NULL.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
142
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143
|
struct cx_tree_visitor_queue_s *next;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
144
|
};
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
145
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
146
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147
|
* A breadth-first tree iterator.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
148
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
149
|
* This iterator needs to maintain a visitor queue that will be automatically
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150
|
* freed once the iterator becomes invalid.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
151
|
* If you want to discard the iterator before, you MUST manually call
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
152
|
* cxTreeVisitorDispose().
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
153
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
154
|
* This iterator is not position-aware in a strict sense, as it does not assume
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
155
|
* a particular order of elements in the tree. However, the iterator keeps track
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
156
|
* of the number of nodes it has passed in a counter variable.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
157
|
* Each node, regardless of the number of passes, is counted only once.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
158
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159
|
* @note Objects that are pointed to by an iterator are mutable through that
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
160
|
* iterator. However, if the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
161
|
* underlying data structure is mutated by other means than this iterator (e.g.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162
|
* elements added or removed), the iterator becomes invalid (regardless of what
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163
|
* cxIteratorValid() returns).
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
164
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
165
|
* @see CxIterator
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
166
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
167
|
typedef struct cx_tree_visitor_s {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
169
|
* Base members.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
170
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
171
|
CX_ITERATOR_BASE;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173
|
* Indicates whether the subtree below the current node shall be skipped.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
174
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
175
|
bool skip;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
176
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
177
|
* Offset in the node struct for the children linked list.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179
|
ptrdiff_t loc_children;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181
|
* Offset in the node struct for the next pointer.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
182
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
183
|
ptrdiff_t loc_next;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
184
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
185
|
* The total number of distinct nodes that have been passed so far.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
186
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187
|
size_t counter;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189
|
* The currently observed node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191
|
* This is the same what cxIteratorCurrent() would return.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
193
|
void *node;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
194
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
195
|
* The current depth in the tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
196
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
197
|
size_t depth;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
198
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
199
|
* The next element in the visitor queue.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
200
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
201
|
struct cx_tree_visitor_queue_s *queue_next;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
202
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203
|
* The last element in the visitor queue.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
204
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
205
|
struct cx_tree_visitor_queue_s *queue_last;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
206
|
} CxTreeVisitor;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
207
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
209
|
* Releases internal memory of the given tree iterator.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210
|
* @param iter the iterator
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
211
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
212
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
213
|
static inline void cxTreeIteratorDispose(CxTreeIterator *iter) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
214
|
free(iter->stack);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
215
|
iter->stack = NULL;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
216
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
217
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
218
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
219
|
* Releases internal memory of the given tree visitor.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
220
|
* @param visitor the visitor
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
221
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
222
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
223
|
static inline void cxTreeVisitorDispose(CxTreeVisitor *visitor) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
224
|
struct cx_tree_visitor_queue_s *q = visitor->queue_next;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
225
|
while (q != NULL) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
226
|
struct cx_tree_visitor_queue_s *next = q->next;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
227
|
free(q);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
228
|
q = next;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
229
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
230
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
231
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
232
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
233
|
* Advises the iterator to skip the subtree below the current node and
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
234
|
* also continues the current loop.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
235
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
236
|
* @param iterator (@c CxTreeIterator) the iterator
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
237
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
238
|
#define cxTreeIteratorContinue(iterator) (iterator).skip = true; continue
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
239
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
240
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
241
|
* Advises the visitor to skip the subtree below the current node and
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
242
|
* also continues the current loop.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
243
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
244
|
* @param visitor (@c CxTreeVisitor) the visitor
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
245
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
246
|
#define cxTreeVisitorContinue(visitor) cxTreeIteratorContinue(visitor)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
247
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
248
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
249
|
* Links a node to a (new) parent.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
250
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
251
|
* If the node has already a parent, it is unlinked, first.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
252
|
* If the parent has children already, the node is @em appended to the list
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
253
|
* of all currently existing children.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
254
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
255
|
* @param parent the parent node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
256
|
* @param node the node that shall be linked
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
257
|
* @param loc_parent offset in the node struct for the parent pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
258
|
* @param loc_children offset in the node struct for the children linked list
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
259
|
* @param loc_last_child optional offset in the node struct for the pointer to
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
260
|
* the last child in the linked list (negative if there is no such pointer)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
261
|
* @param loc_prev optional offset in the node struct for the prev pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
262
|
* @param loc_next offset in the node struct for the next pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
263
|
* @see cx_tree_unlink()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
264
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
265
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
266
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
267
|
void cx_tree_link(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
268
|
void *parent,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
269
|
void *node,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
270
|
ptrdiff_t loc_parent,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
271
|
ptrdiff_t loc_children,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
272
|
ptrdiff_t loc_last_child,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
273
|
ptrdiff_t loc_prev,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
274
|
ptrdiff_t loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
275
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
276
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
277
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
278
|
* Unlinks a node from its parent.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
279
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
280
|
* If the node has no parent, this function does nothing.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
281
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
282
|
* @param node the node that shall be unlinked from its parent
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
283
|
* @param loc_parent offset in the node struct for the parent pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
284
|
* @param loc_children offset in the node struct for the children linked list
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
285
|
* @param loc_last_child optional offset in the node struct for the pointer to
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
286
|
* the last child in the linked list (negative if there is no such pointer)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
287
|
* @param loc_prev optional offset in the node struct for the prev pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
288
|
* @param loc_next offset in the node struct for the next pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
289
|
* @see cx_tree_link()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
290
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
291
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
292
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
293
|
void cx_tree_unlink(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
294
|
void *node,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
295
|
ptrdiff_t loc_parent,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
296
|
ptrdiff_t loc_children,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
297
|
ptrdiff_t loc_last_child,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
298
|
ptrdiff_t loc_prev,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
299
|
ptrdiff_t loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
300
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
301
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
302
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
303
|
* Macro that can be used instead of the magic value for infinite search depth.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
304
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
305
|
#define CX_TREE_SEARCH_INFINITE_DEPTH 0
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
306
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
307
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
308
|
* Function pointer for a search function.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
309
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
310
|
* A function of this kind shall check if the specified @p node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
311
|
* contains the given @p data or if one of the children might contain
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
312
|
* the data.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
313
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
314
|
* The function should use the returned integer to indicate how close the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
315
|
* match is, where a negative number means that it does not match at all.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
316
|
* Zero means exact match and a positive number is an implementation defined
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
317
|
* measure for the distance to an exact match.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
318
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
319
|
* For example if a tree stores file path information, a node that is
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
320
|
* describing a parent directory of a filename that is searched, shall
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
321
|
* return a positive number to indicate that a child node might contain the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
322
|
* searched item. On the other hand, if the node denotes a path that is not a
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
323
|
* prefix of the searched filename, the function would return -1 to indicate
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
324
|
* that the search does not need to be continued in that branch.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
325
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
326
|
* @param node the node that is currently investigated
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
327
|
* @param data the data that is searched for
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
328
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
329
|
* @return 0 if the node contains the data,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
330
|
* positive if one of the children might contain the data,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
331
|
* negative if neither the node, nor the children contains the data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
332
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
333
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
334
|
typedef int (*cx_tree_search_data_func)(const void *node, const void *data);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
335
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
336
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
337
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
338
|
* Function pointer for a search function.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
339
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
340
|
* A function of this kind shall check if the specified @p node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
341
|
* contains the same @p data as @p new_node or if one of the children might
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
342
|
* contain the data.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
343
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
344
|
* The function should use the returned integer to indicate how close the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
345
|
* match is, where a negative number means that it does not match at all.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
346
|
* Zero means exact match and a positive number is an implementation defined
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
347
|
* measure for the distance to an exact match.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
348
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
349
|
* For example if a tree stores file path information, a node that is
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
350
|
* describing a parent directory of a filename that is searched, shall
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
351
|
* return a positive number to indicate that a child node might contain the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
352
|
* searched item. On the other hand, if the node denotes a path that is not a
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
353
|
* prefix of the searched filename, the function would return -1 to indicate
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
354
|
* that the search does not need to be continued in that branch.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
355
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
356
|
* @param node the node that is currently investigated
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
357
|
* @param new_node a new node with the information which is searched
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
358
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
359
|
* @return 0 if @p node contains the same data as @p new_node,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
360
|
* positive if one of the children might contain the data,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
361
|
* negative if neither the node, nor the children contains the data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
362
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
363
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
364
|
typedef int (*cx_tree_search_func)(const void *node, const void *new_node);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
365
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
366
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
367
|
* Searches for data in a tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
368
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
369
|
* When the data cannot be found exactly, the search function might return a
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
370
|
* closest result which might be a good starting point for adding a new node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
371
|
* to the tree (see also #cx_tree_add()).
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
372
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
373
|
* Depending on the tree structure it is not necessarily guaranteed that the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
374
|
* "closest" match is uniquely defined. This function will search for a node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
375
|
* with the best match according to the @p sfunc (meaning: the return value of
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
376
|
* @p sfunc which is closest to zero). If that is also ambiguous, an arbitrary
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
377
|
* node matching the criteria is returned.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
378
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
379
|
* @param root the root node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
380
|
* @param depth the maximum depth (zero=indefinite, one=just root)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
381
|
* @param data the data to search for
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
382
|
* @param sfunc the search function
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
383
|
* @param result where the result shall be stored
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
384
|
* @param loc_children offset in the node struct for the children linked list
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
385
|
* @param loc_next offset in the node struct for the next pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
386
|
* @return zero if the node was found exactly, positive if a node was found that
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
387
|
* could contain the node (but doesn't right now), negative if the tree does not
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
388
|
* contain any node that might be related to the searched data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
389
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
390
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
391
|
cx_attr_access_w(5)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
392
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
393
|
int cx_tree_search_data(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
394
|
const void *root,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
395
|
size_t depth,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
396
|
const void *data,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
397
|
cx_tree_search_data_func sfunc,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
398
|
void **result,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
399
|
ptrdiff_t loc_children,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
400
|
ptrdiff_t loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
401
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
402
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
403
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
404
|
* Searches for a node in a tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
405
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
406
|
* When no node with the same data can be found, the search function might
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
407
|
* return a closest result which might be a good starting point for adding the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
408
|
* new node to the tree (see also #cx_tree_add()).
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
409
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
410
|
* Depending on the tree structure it is not necessarily guaranteed that the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
411
|
* "closest" match is uniquely defined. This function will search for a node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
412
|
* with the best match according to the @p sfunc (meaning: the return value of
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
413
|
* @p sfunc which is closest to zero). If that is also ambiguous, an arbitrary
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
414
|
* node matching the criteria is returned.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
415
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
416
|
* @param root the root node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
417
|
* @param depth the maximum depth (zero=indefinite, one=just root)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
418
|
* @param node the node to search for
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
419
|
* @param sfunc the search function
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
420
|
* @param result where the result shall be stored
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
421
|
* @param loc_children offset in the node struct for the children linked list
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
422
|
* @param loc_next offset in the node struct for the next pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
423
|
* @return zero if the node was found exactly, positive if a node was found that
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
424
|
* could contain the node (but doesn't right now), negative if the tree does not
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
425
|
* contain any node that might be related to the searched data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
426
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
427
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
428
|
cx_attr_access_w(5)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
429
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
430
|
int cx_tree_search(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
431
|
const void *root,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
432
|
size_t depth,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
433
|
const void *node,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
434
|
cx_tree_search_func sfunc,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
435
|
void **result,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
436
|
ptrdiff_t loc_children,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
437
|
ptrdiff_t loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
438
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
439
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
440
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
441
|
* Creates a depth-first iterator for a tree with the specified root node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
442
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
443
|
* @note A tree iterator needs to maintain a stack of visited nodes, which is
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
444
|
* allocated using stdlib malloc().
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
445
|
* When the iterator becomes invalid, this memory is automatically released.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
446
|
* However, if you wish to cancel the iteration before the iterator becomes
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
447
|
* invalid by itself, you MUST call cxTreeIteratorDispose() manually to release
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
448
|
* the memory.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
449
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
450
|
* @remark The returned iterator does not support cxIteratorFlagRemoval().
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
451
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
452
|
* @param root the root node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
453
|
* @param visit_on_exit set to true, when the iterator shall visit a node again
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
454
|
* after processing all children
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
455
|
* @param loc_children offset in the node struct for the children linked list
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
456
|
* @param loc_next offset in the node struct for the next pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
457
|
* @return the new tree iterator
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
458
|
* @see cxTreeIteratorDispose()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
459
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
460
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
461
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
462
|
CxTreeIterator cx_tree_iterator(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
463
|
void *root,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
464
|
bool visit_on_exit,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
465
|
ptrdiff_t loc_children,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
466
|
ptrdiff_t loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
467
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
468
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
469
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
470
|
* Creates a breadth-first iterator for a tree with the specified root node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
471
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
472
|
* @note A tree visitor needs to maintain a queue of to be visited nodes, which
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
473
|
* is allocated using stdlib malloc().
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
474
|
* When the visitor becomes invalid, this memory is automatically released.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
475
|
* However, if you wish to cancel the iteration before the visitor becomes
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
476
|
* invalid by itself, you MUST call cxTreeVisitorDispose() manually to release
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
477
|
* the memory.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
478
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
479
|
* @remark The returned iterator does not support cxIteratorFlagRemoval().
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
480
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
481
|
* @param root the root node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
482
|
* @param loc_children offset in the node struct for the children linked list
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
483
|
* @param loc_next offset in the node struct for the next pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
484
|
* @return the new tree visitor
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
485
|
* @see cxTreeVisitorDispose()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
486
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
487
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
488
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
489
|
CxTreeVisitor cx_tree_visitor(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
490
|
void *root,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
491
|
ptrdiff_t loc_children,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
492
|
ptrdiff_t loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
493
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
494
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
495
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
496
|
* Describes a function that creates a tree node from the specified data.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
497
|
* The first argument points to the data the node shall contain and
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
498
|
* the second argument may be used for additional data (e.g. an allocator).
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
499
|
* Functions of this type shall either return a new pointer to a newly
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
500
|
* created node or @c NULL when allocation fails.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
501
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
502
|
* @note the function may leave the node pointers in the struct uninitialized.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
503
|
* The caller is responsible to set them according to the intended use case.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
504
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
505
|
cx_attr_nonnull_arg(1)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
506
|
typedef void *(*cx_tree_node_create_func)(const void *, void *);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
507
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
508
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
509
|
* The local search depth for a new subtree when adding multiple elements.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
510
|
* The default value is 3.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
511
|
* This variable is used by #cx_tree_add_array() and #cx_tree_add_iter() to
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
512
|
* implement optimized insertion of multiple elements into a tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
513
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
514
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
515
|
extern unsigned int cx_tree_add_look_around_depth;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
516
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
517
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
518
|
* Adds multiple elements efficiently to a tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
519
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
520
|
* Once an element cannot be added to the tree, this function returns, leaving
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
521
|
* the iterator in a valid state pointing to the element that could not be
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
522
|
* added.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
523
|
* Also, the pointer of the created node will be stored to @p failed.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
524
|
* The integer returned by this function denotes the number of elements obtained
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
525
|
* from the @p iter that have been successfully processed.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
526
|
* When all elements could be processed, a @c NULL pointer will be written to
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
527
|
* @p failed.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
528
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
529
|
* The advantage of this function compared to multiple invocations of
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
530
|
* #cx_tree_add() is that the search for the insert locations is not always
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
531
|
* started from the root node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
532
|
* Instead, the function checks #cx_tree_add_look_around_depth many parent nodes
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
533
|
* of the current insert location before starting from the root node again.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
534
|
* When the variable is set to zero, only the last found location is checked
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
535
|
* again.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
536
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
537
|
* Refer to the documentation of #cx_tree_add() for more details.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
538
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
539
|
* @param iter a pointer to an arbitrary iterator
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
540
|
* @param num the maximum number of elements to obtain from the iterator
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
541
|
* @param sfunc a search function
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
542
|
* @param cfunc a node creation function
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
543
|
* @param cdata optional additional data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
544
|
* @param root the root node of the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
545
|
* @param failed location where the pointer to a failed node shall be stored
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
546
|
* @param loc_parent offset in the node struct for the parent pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
547
|
* @param loc_children offset in the node struct for the children linked list
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
548
|
* @param loc_last_child optional offset in the node struct for the pointer to
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
549
|
* the last child in the linked list (negative if there is no such pointer)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
550
|
* @param loc_prev optional offset in the node struct for the prev pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
551
|
* @param loc_next offset in the node struct for the next pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
552
|
* @return the number of nodes created and added
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
553
|
* @see cx_tree_add()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
554
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
555
|
cx_attr_nonnull_arg(1, 3, 4, 6, 7)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
556
|
cx_attr_access_w(6)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
557
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
558
|
size_t cx_tree_add_iter(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
559
|
struct cx_iterator_base_s *iter,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
560
|
size_t num,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
561
|
cx_tree_search_func sfunc,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
562
|
cx_tree_node_create_func cfunc,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
563
|
void *cdata,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
564
|
void **failed,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
565
|
void *root,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
566
|
ptrdiff_t loc_parent,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
567
|
ptrdiff_t loc_children,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
568
|
ptrdiff_t loc_last_child,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
569
|
ptrdiff_t loc_prev,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
570
|
ptrdiff_t loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
571
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
572
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
573
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
574
|
* Adds multiple elements efficiently to a tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
575
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
576
|
* Once an element cannot be added to the tree, this function returns, storing
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
577
|
* the pointer of the created node to @p failed.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
578
|
* The integer returned by this function denotes the number of elements from
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
579
|
* the @p src array that have been successfully processed.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
580
|
* When all elements could be processed, a @c NULL pointer will be written to
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
581
|
* @p failed.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
582
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
583
|
* The advantage of this function compared to multiple invocations of
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
584
|
* #cx_tree_add() is that the search for the insert locations is not always
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
585
|
* started from the root node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
586
|
* Instead, the function checks #cx_tree_add_look_around_depth many parent nodes
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
587
|
* of the current insert location before starting from the root node again.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
588
|
* When the variable is set to zero, only the last found location is checked
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
589
|
* again.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
590
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
591
|
* Refer to the documentation of #cx_tree_add() for more details.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
592
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
593
|
* @param src a pointer to the source data array
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
594
|
* @param num the number of elements in the @p src array
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
595
|
* @param elem_size the size of each element in the @p src array
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
596
|
* @param sfunc a search function
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
597
|
* @param cfunc a node creation function
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
598
|
* @param cdata optional additional data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
599
|
* @param failed location where the pointer to a failed node shall be stored
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
600
|
* @param root the root node of the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
601
|
* @param loc_parent offset in the node struct for the parent pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
602
|
* @param loc_children offset in the node struct for the children linked list
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
603
|
* @param loc_last_child optional offset in the node struct for the pointer to
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
604
|
* the last child in the linked list (negative if there is no such pointer)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
605
|
* @param loc_prev optional offset in the node struct for the prev pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
606
|
* @param loc_next offset in the node struct for the next pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
607
|
* @return the number of array elements successfully processed
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
608
|
* @see cx_tree_add()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
609
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
610
|
cx_attr_nonnull_arg(1, 4, 5, 7, 8)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
611
|
cx_attr_access_w(7)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
612
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
613
|
size_t cx_tree_add_array(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
614
|
const void *src,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
615
|
size_t num,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
616
|
size_t elem_size,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
617
|
cx_tree_search_func sfunc,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
618
|
cx_tree_node_create_func cfunc,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
619
|
void *cdata,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
620
|
void **failed,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
621
|
void *root,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
622
|
ptrdiff_t loc_parent,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
623
|
ptrdiff_t loc_children,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
624
|
ptrdiff_t loc_last_child,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
625
|
ptrdiff_t loc_prev,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
626
|
ptrdiff_t loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
627
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
628
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
629
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
630
|
* Adds data to a tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
631
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
632
|
* An adequate location where to add the new tree node is searched with the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
633
|
* specified @p sfunc.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
634
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
635
|
* When a location is found, the @p cfunc will be invoked with @p cdata.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
636
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
637
|
* The node returned by @p cfunc will be linked into the tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
638
|
* When @p sfunc returned a positive integer, the new node will be linked as a
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
639
|
* child. The other children (now siblings of the new node) are then checked
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
640
|
* with @p sfunc, whether they could be children of the new node and re-linked
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
641
|
* accordingly.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
642
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
643
|
* When @p sfunc returned zero and the found node has a parent, the new
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
644
|
* node will be added as sibling - otherwise, the new node will be added
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
645
|
* as a child.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
646
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
647
|
* When @p sfunc returned a negative value, the new node will not be added to
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
648
|
* the tree and this function returns a non-zero value.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
649
|
* The caller should check if @p cnode contains a node pointer and deal with the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
650
|
* node that could not be added.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
651
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
652
|
* This function also returns a non-zero value when @p cfunc tries to allocate
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
653
|
* a new node but fails to do so. In that case, the pointer stored to @p cnode
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
654
|
* will be @c NULL.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
655
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
656
|
* Multiple elements can be added more efficiently with
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
657
|
* #cx_tree_add_array() or #cx_tree_add_iter().
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
658
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
659
|
* @param src a pointer to the data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
660
|
* @param sfunc a search function
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
661
|
* @param cfunc a node creation function
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
662
|
* @param cdata optional additional data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
663
|
* @param cnode the location where a pointer to the new node is stored
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
664
|
* @param root the root node of the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
665
|
* @param loc_parent offset in the node struct for the parent pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
666
|
* @param loc_children offset in the node struct for the children linked list
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
667
|
* @param loc_last_child optional offset in the node struct for the pointer to
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
668
|
* the last child in the linked list (negative if there is no such pointer)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
669
|
* @param loc_prev optional offset in the node struct for the prev pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
670
|
* @param loc_next offset in the node struct for the next pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
671
|
* @return zero when a new node was created and added to the tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
672
|
* non-zero otherwise
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
673
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
674
|
cx_attr_nonnull_arg(1, 2, 3, 5, 6)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
675
|
cx_attr_access_w(5)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
676
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
677
|
int cx_tree_add(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
678
|
const void *src,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
679
|
cx_tree_search_func sfunc,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
680
|
cx_tree_node_create_func cfunc,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
681
|
void *cdata,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
682
|
void **cnode,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
683
|
void *root,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
684
|
ptrdiff_t loc_parent,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
685
|
ptrdiff_t loc_children,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
686
|
ptrdiff_t loc_last_child,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
687
|
ptrdiff_t loc_prev,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
688
|
ptrdiff_t loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
689
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
690
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
691
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
692
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
693
|
* Tree class type.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
694
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
695
|
typedef struct cx_tree_class_s cx_tree_class;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
696
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
697
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
698
|
* Base structure that can be used for tree nodes in a #CxTree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
699
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
700
|
struct cx_tree_node_base_s {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
701
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
702
|
* Pointer to the parent.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
703
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
704
|
struct cx_tree_node_base_s *parent;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
705
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
706
|
* Pointer to the first child.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
707
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
708
|
struct cx_tree_node_base_s *children;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
709
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
710
|
* Pointer to the last child.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
711
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
712
|
struct cx_tree_node_base_s *last_child;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
713
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
714
|
* Pointer to the previous sibling.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
715
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
716
|
struct cx_tree_node_base_s *prev;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
717
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
718
|
* Pointer to the next sibling.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
719
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
720
|
struct cx_tree_node_base_s *next;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
721
|
};
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
722
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
723
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
724
|
* Structure for holding the base data of a tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
725
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
726
|
struct cx_tree_s {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
727
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
728
|
* The tree class definition.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
729
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
730
|
const cx_tree_class *cl;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
731
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
732
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
733
|
* Allocator to allocate new nodes.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
734
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
735
|
const CxAllocator *allocator;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
736
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
737
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
738
|
* A pointer to the root node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
739
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
740
|
* Will be @c NULL when @c size is 0.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
741
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
742
|
void *root;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
743
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
744
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
745
|
* A function to create new nodes.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
746
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
747
|
* Invocations to this function will receive a pointer to this tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
748
|
* structure as second argument.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
749
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
750
|
* Nodes MAY use #cx_tree_node_base_s as base layout, but do not need to.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
751
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
752
|
cx_tree_node_create_func node_create;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
753
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
754
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
755
|
* An optional simple destructor for the tree nodes.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
756
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
757
|
cx_destructor_func simple_destructor;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
758
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
759
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
760
|
* An optional advanced destructor for the tree nodes.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
761
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
762
|
cx_destructor_func2 advanced_destructor;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
763
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
764
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
765
|
* The pointer to additional data that is passed to the advanced destructor.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
766
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
767
|
void *destructor_data;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
768
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
769
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
770
|
* A function to compare two nodes.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
771
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
772
|
cx_tree_search_func search;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
773
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
774
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
775
|
* A function to compare a node with data.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
776
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
777
|
cx_tree_search_data_func search_data;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
778
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
779
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
780
|
* The number of currently stored elements.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
781
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
782
|
size_t size;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
783
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
784
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
785
|
* Offset in the node struct for the parent pointer.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
786
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
787
|
ptrdiff_t loc_parent;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
788
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
789
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
790
|
* Offset in the node struct for the children linked list.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
791
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
792
|
ptrdiff_t loc_children;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
793
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
794
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
795
|
* Optional offset in the node struct for the pointer to the last child
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
796
|
* in the linked list (negative if there is no such pointer).
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
797
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
798
|
ptrdiff_t loc_last_child;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
799
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
800
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
801
|
* Offset in the node struct for the previous sibling pointer.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
802
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
803
|
ptrdiff_t loc_prev;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
804
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
805
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
806
|
* Offset in the node struct for the next sibling pointer.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
807
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
808
|
ptrdiff_t loc_next;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
809
|
};
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
810
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
811
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
812
|
* Macro to roll out the #cx_tree_node_base_s structure with a custom
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
813
|
* node type.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
814
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
815
|
* Must be used as first member in your custom tree struct.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
816
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
817
|
* @param type the data type for the nodes
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
818
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
819
|
#define CX_TREE_NODE_BASE(type) \
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
820
|
type *parent; \
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
821
|
type *children;\
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
822
|
type *last_child;\
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
823
|
type *prev;\
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
824
|
type *next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
825
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
826
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
827
|
* Macro for specifying the layout of a base node tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
828
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
829
|
* When your tree uses #CX_TREE_NODE_BASE, you can use this
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
830
|
* macro in all tree functions that expect the layout parameters
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
831
|
* @c loc_parent, @c loc_children, @c loc_last_child, @c loc_prev,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
832
|
* and @c loc_next.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
833
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
834
|
#define cx_tree_node_base_layout \
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
835
|
offsetof(struct cx_tree_node_base_s, parent),\
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
836
|
offsetof(struct cx_tree_node_base_s, children),\
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
837
|
offsetof(struct cx_tree_node_base_s, last_child),\
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
838
|
offsetof(struct cx_tree_node_base_s, prev), \
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
839
|
offsetof(struct cx_tree_node_base_s, next)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
840
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
841
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
842
|
* The class definition for arbitrary trees.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
843
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
844
|
struct cx_tree_class_s {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
845
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
846
|
* Member function for inserting a single element.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
847
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
848
|
* Implementations SHALL NOT simply invoke @p insert_many as this comes
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
849
|
* with too much overhead.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
850
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
851
|
int (*insert_element)(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
852
|
struct cx_tree_s *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
853
|
const void *data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
854
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
855
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
856
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
857
|
* Member function for inserting multiple elements.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
858
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
859
|
* Implementations SHALL avoid to perform a full search in the tree for
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
860
|
* every element even though the source data MAY be unsorted.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
861
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
862
|
size_t (*insert_many)(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
863
|
struct cx_tree_s *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
864
|
struct cx_iterator_base_s *iter,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
865
|
size_t n
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
866
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
867
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
868
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
869
|
* Member function for finding a node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
870
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
871
|
void *(*find)(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
872
|
struct cx_tree_s *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
873
|
const void *subtree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
874
|
const void *data,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
875
|
size_t depth
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
876
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
877
|
};
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
878
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
879
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
880
|
* Common type for all tree implementations.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
881
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
882
|
typedef struct cx_tree_s CxTree;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
883
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
884
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
885
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
886
|
* Destroys a node and it's subtree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
887
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
888
|
* It is guaranteed that the simple destructor is invoked before
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
889
|
* the advanced destructor, starting with the leaf nodes of the subtree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
890
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
891
|
* When this function is invoked on the root node of the tree, it destroys the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
892
|
* tree contents, but - in contrast to #cxTreeFree() - not the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
893
|
* structure, leaving an empty tree behind.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
894
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
895
|
* @note The destructor function, if any, will @em not be invoked. That means
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
896
|
* you will need to free the removed subtree by yourself, eventually.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
897
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
898
|
* @attention This function will not free the memory of the nodes with the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
899
|
* tree's allocator, because that is usually done by the advanced destructor
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
900
|
* and would therefore result in a double-free.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
901
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
902
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
903
|
* @param node the node to remove
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
904
|
* @see cxTreeFree()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
905
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
906
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
907
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
908
|
void cxTreeDestroySubtree(CxTree *tree, void *node);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
909
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
910
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
911
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
912
|
* Destroys the tree contents.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
913
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
914
|
* It is guaranteed that the simple destructor is invoked before
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
915
|
* the advanced destructor, starting with the leaf nodes of the subtree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
916
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
917
|
* This is a convenience macro for invoking #cxTreeDestroySubtree() on the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
918
|
* root node of the tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
919
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
920
|
* @attention Be careful when calling this function when no destructor function
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
921
|
* is registered that actually frees the memory of nodes. In that case you will
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
922
|
* need a reference to the (former) root node of the tree somewhere or
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
923
|
* otherwise you will be leaking memory.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
924
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
925
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
926
|
* @see cxTreeDestroySubtree()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
927
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
928
|
#define cxTreeClear(tree) cxTreeDestroySubtree(tree, tree->root)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
929
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
930
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
931
|
* Deallocates the tree structure.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
932
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
933
|
* The destructor functions are invoked for each node, starting with the leaf
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
934
|
* nodes.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
935
|
* It is guaranteed that for each node the simple destructor is invoked before
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
936
|
* the advanced destructor.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
937
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
938
|
* @attention This function will only invoke the destructor functions
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
939
|
* on the nodes.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
940
|
* It will NOT additionally free the nodes with the tree's allocator, because
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
941
|
* that would cause a double-free in most scenarios where the advanced
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
942
|
* destructor is already freeing the memory.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
943
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
944
|
* @param tree the tree to free
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
945
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
946
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
947
|
void cxTreeFree(CxTree *tree);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
948
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
949
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
950
|
* Creates a new tree structure based on the specified layout.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
951
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
952
|
* The specified @p allocator will be used for creating the tree struct
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
953
|
* and SHALL be used by @p create_func to allocate memory for the nodes.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
954
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
955
|
* @note This function will also register an advanced destructor which
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
956
|
* will free the nodes with the allocator's free() method.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
957
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
958
|
* @param allocator the allocator that shall be used
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
959
|
* (if @c NULL, a default stdlib allocator will be used)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
960
|
* @param create_func a function that creates new nodes
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
961
|
* @param search_func a function that compares two nodes
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
962
|
* @param search_data_func a function that compares a node with data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
963
|
* @param loc_parent offset in the node struct for the parent pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
964
|
* @param loc_children offset in the node struct for the children linked list
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
965
|
* @param loc_last_child optional offset in the node struct for the pointer to
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
966
|
* the last child in the linked list (negative if there is no such pointer)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
967
|
* @param loc_prev optional offset in the node struct for the prev pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
968
|
* @param loc_next offset in the node struct for the next pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
969
|
* @return the new tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
970
|
* @see cxTreeCreateSimple()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
971
|
* @see cxTreeCreateWrapped()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
972
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
973
|
cx_attr_nonnull_arg(2, 3, 4)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
974
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
975
|
cx_attr_malloc
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
976
|
cx_attr_dealloc(cxTreeFree, 1)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
977
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
978
|
CxTree *cxTreeCreate(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
979
|
const CxAllocator *allocator,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
980
|
cx_tree_node_create_func create_func,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
981
|
cx_tree_search_func search_func,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
982
|
cx_tree_search_data_func search_data_func,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
983
|
ptrdiff_t loc_parent,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
984
|
ptrdiff_t loc_children,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
985
|
ptrdiff_t loc_last_child,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
986
|
ptrdiff_t loc_prev,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
987
|
ptrdiff_t loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
988
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
989
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
990
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
991
|
* Creates a new tree structure based on a default layout.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
992
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
993
|
* Nodes created by @p create_func MUST contain #cx_tree_node_base_s as first
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
994
|
* member (or at least respect the default offsets specified in the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
995
|
* struct) and they MUST be allocated with the specified allocator.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
996
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
997
|
* @note This function will also register an advanced destructor which
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
998
|
* will free the nodes with the allocator's free() method.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
999
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1000
|
* @param allocator (@c CxAllocator*) the allocator that shall be used
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1001
|
* @param create_func (@c cx_tree_node_create_func) a function that creates new nodes
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1002
|
* @param search_func (@c cx_tree_search_func) a function that compares two nodes
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1003
|
* @param search_data_func (@c cx_tree_search_data_func) a function that compares a node with data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1004
|
* @return (@c CxTree*) the new tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1005
|
* @see cxTreeCreate()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1006
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1007
|
#define cxTreeCreateSimple(\
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1008
|
allocator, create_func, search_func, search_data_func \
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1009
|
) cxTreeCreate(allocator, create_func, search_func, search_data_func, \
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1010
|
cx_tree_node_base_layout)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1011
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1012
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1013
|
* Creates a new tree structure based on an existing tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1014
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1015
|
* The specified @p allocator will be used for creating the tree struct.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1016
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1017
|
* @attention This function will create an incompletely defined tree structure
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1018
|
* where neither the create function, the search function, nor a destructor
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1019
|
* will be set. If you wish to use any of this functionality for the wrapped
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1020
|
* tree, you need to specify those functions afterwards.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1021
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1022
|
* @param allocator the allocator that was used for nodes of the wrapped tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1023
|
* (if @c NULL, a default stdlib allocator is assumed)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1024
|
* @param root the root node of the tree that shall be wrapped
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1025
|
* @param loc_parent offset in the node struct for the parent pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1026
|
* @param loc_children offset in the node struct for the children linked list
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1027
|
* @param loc_last_child optional offset in the node struct for the pointer to
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1028
|
* the last child in the linked list (negative if there is no such pointer)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1029
|
* @param loc_prev optional offset in the node struct for the prev pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1030
|
* @param loc_next offset in the node struct for the next pointer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1031
|
* @return the new tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1032
|
* @see cxTreeCreate()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1033
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1034
|
cx_attr_nonnull_arg(2)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1035
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1036
|
cx_attr_malloc
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1037
|
cx_attr_dealloc(cxTreeFree, 1)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1038
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1039
|
CxTree *cxTreeCreateWrapped(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1040
|
const CxAllocator *allocator,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1041
|
void *root,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1042
|
ptrdiff_t loc_parent,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1043
|
ptrdiff_t loc_children,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1044
|
ptrdiff_t loc_last_child,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1045
|
ptrdiff_t loc_prev,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1046
|
ptrdiff_t loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1047
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1048
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1049
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1050
|
* Inserts data into the tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1051
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1052
|
* @remark For this function to work, the tree needs specified search and
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1053
|
* create functions, which might not be available for wrapped trees
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1054
|
* (see #cxTreeCreateWrapped()).
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1055
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1056
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1057
|
* @param data the data to insert
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1058
|
* @retval zero success
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1059
|
* @retval non-zero failure
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1060
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1061
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1062
|
static inline int cxTreeInsert(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1063
|
CxTree *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1064
|
const void *data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1065
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1066
|
return tree->cl->insert_element(tree, data);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1067
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1068
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1069
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1070
|
* Inserts elements provided by an iterator efficiently into the tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1071
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1072
|
* @remark For this function to work, the tree needs specified search and
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1073
|
* create functions, which might not be available for wrapped trees
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1074
|
* (see #cxTreeCreateWrapped()).
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1075
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1076
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1077
|
* @param iter the iterator providing the elements
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1078
|
* @param n the maximum number of elements to insert
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1079
|
* @return the number of elements that could be successfully inserted
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1080
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1081
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1082
|
static inline size_t cxTreeInsertIter(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1083
|
CxTree *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1084
|
CxIteratorBase *iter,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1085
|
size_t n
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1086
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1087
|
return tree->cl->insert_many(tree, iter, n);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1088
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1089
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1090
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1091
|
* Inserts an array of data efficiently into the tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1092
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1093
|
* @remark For this function to work, the tree needs specified search and
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1094
|
* create functions, which might not be available for wrapped trees
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1095
|
* (see #cxTreeCreateWrapped()).
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1096
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1097
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1098
|
* @param data the array of data to insert
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1099
|
* @param elem_size the size of each element in the array
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1100
|
* @param n the number of elements in the array
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1101
|
* @return the number of elements that could be successfully inserted
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1102
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1103
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1104
|
static inline size_t cxTreeInsertArray(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1105
|
CxTree *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1106
|
const void *data,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1107
|
size_t elem_size,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1108
|
size_t n
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1109
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1110
|
if (n == 0) return 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1111
|
if (n == 1) return 0 == cxTreeInsert(tree, data) ? 1 : 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1112
|
CxIterator iter = cxIterator(data, elem_size, n);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1113
|
return cxTreeInsertIter(tree, cxIteratorRef(iter), n);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1114
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1115
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1116
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1117
|
* Searches the data in the specified tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1118
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1119
|
* @remark For this function to work, the tree needs a specified @c search_data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1120
|
* function, which might not be available wrapped trees
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1121
|
* (see #cxTreeCreateWrapped()).
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1122
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1123
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1124
|
* @param data the data to search for
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1125
|
* @return the first matching node, or @c NULL when the data cannot be found
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1126
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1127
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1128
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1129
|
static inline void *cxTreeFind(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1130
|
CxTree *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1131
|
const void *data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1132
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1133
|
return tree->cl->find(tree, tree->root, data, 0);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1134
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1135
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1136
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1137
|
* Searches the data in the specified subtree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1138
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1139
|
* When @p max_depth is zero, the depth is not limited.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1140
|
* The @p subtree_root itself is on depth 1 and its children have depth 2.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1141
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1142
|
* @note When @p subtree_root is not part of the @p tree, the behavior is
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1143
|
* undefined.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1144
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1145
|
* @remark For this function to work, the tree needs a specified @c search_data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1146
|
* function, which might not be the case for wrapped trees
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1147
|
* (see #cxTreeCreateWrapped()).
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1148
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1149
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1150
|
* @param data the data to search for
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1151
|
* @param subtree_root the node where to start
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1152
|
* @param max_depth the maximum search depth
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1153
|
* @return the first matching node, or @c NULL when the data cannot be found
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1154
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1155
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1156
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1157
|
static inline void *cxTreeFindInSubtree(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1158
|
CxTree *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1159
|
const void *data,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1160
|
void *subtree_root,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1161
|
size_t max_depth
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1162
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1163
|
return tree->cl->find(tree, subtree_root, data, max_depth);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1164
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1165
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1166
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1167
|
* Determines the size of the specified subtree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1168
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1169
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1170
|
* @param subtree_root the root node of the subtree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1171
|
* @return the number of nodes in the specified subtree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1172
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1173
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1174
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1175
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1176
|
size_t cxTreeSubtreeSize(CxTree *tree, void *subtree_root);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1177
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1178
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1179
|
* Determines the depth of the specified subtree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1180
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1181
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1182
|
* @param subtree_root the root node of the subtree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1183
|
* @return the tree depth including the @p subtree_root
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1184
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1185
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1186
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1187
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1188
|
size_t cxTreeSubtreeDepth(CxTree *tree, void *subtree_root);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1189
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1190
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1191
|
* Determines the depth of the entire tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1192
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1193
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1194
|
* @return the tree depth, counting the root as one
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1195
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1196
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1197
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1198
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1199
|
size_t cxTreeDepth(CxTree *tree);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1200
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1201
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1202
|
* Creates a depth-first iterator for the specified tree starting in @p node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1203
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1204
|
* If the node is not part of the tree, the behavior is undefined.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1205
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1206
|
* @param tree the tree to iterate
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1207
|
* @param node the node where to start
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1208
|
* @param visit_on_exit true, if the iterator shall visit a node again when
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1209
|
* leaving the subtree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1210
|
* @return a tree iterator (depth-first)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1211
|
* @see cxTreeVisit()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1212
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1213
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1214
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1215
|
static inline CxTreeIterator cxTreeIterateSubtree(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1216
|
CxTree *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1217
|
void *node,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1218
|
bool visit_on_exit
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1219
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1220
|
return cx_tree_iterator(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1221
|
node, visit_on_exit,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1222
|
tree->loc_children, tree->loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1223
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1224
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1225
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1226
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1227
|
* Creates a breadth-first iterator for the specified tree starting in @p node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1228
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1229
|
* If the node is not part of the tree, the behavior is undefined.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1230
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1231
|
* @param tree the tree to iterate
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1232
|
* @param node the node where to start
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1233
|
* @return a tree visitor (a.k.a. breadth-first iterator)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1234
|
* @see cxTreeIterate()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1235
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1236
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1237
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1238
|
static inline CxTreeVisitor cxTreeVisitSubtree(CxTree *tree, void *node) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1239
|
return cx_tree_visitor(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1240
|
node, tree->loc_children, tree->loc_next
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1241
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1242
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1243
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1244
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1245
|
* Creates a depth-first iterator for the specified tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1246
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1247
|
* @param tree the tree to iterate
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1248
|
* @param visit_on_exit true, if the iterator shall visit a node again when
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1249
|
* leaving the subtree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1250
|
* @return a tree iterator (depth-first)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1251
|
* @see cxTreeVisit()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1252
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1253
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1254
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1255
|
static inline CxTreeIterator cxTreeIterate(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1256
|
CxTree *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1257
|
bool visit_on_exit
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1258
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1259
|
return cxTreeIterateSubtree(tree, tree->root, visit_on_exit);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1260
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1261
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1262
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1263
|
* Creates a breadth-first iterator for the specified tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1264
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1265
|
* @param tree the tree to iterate
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1266
|
* @return a tree visitor (a.k.a. breadth-first iterator)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1267
|
* @see cxTreeIterate()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1268
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1269
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1270
|
cx_attr_nodiscard
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1271
|
static inline CxTreeVisitor cxTreeVisit(CxTree *tree) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1272
|
return cxTreeVisitSubtree(tree, tree->root);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1273
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1274
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1275
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1276
|
* Sets the (new) parent of the specified child.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1277
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1278
|
* If the @p child is not already member of the tree, this function behaves
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1279
|
* as #cxTreeAddChildNode().
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1280
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1281
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1282
|
* @param parent the (new) parent of the child
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1283
|
* @param child the node to add
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1284
|
* @see cxTreeAddChildNode()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1285
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1286
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1287
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1288
|
void cxTreeSetParent(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1289
|
CxTree *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1290
|
void *parent,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1291
|
void *child
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1292
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1293
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1294
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1295
|
* Adds a new node to the tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1296
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1297
|
* If the @p child is already member of the tree, the behavior is undefined.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1298
|
* Use #cxTreeSetParent() if you want to move a subtree to another location.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1299
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1300
|
* @attention The node may be externally created, but MUST obey the same rules
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1301
|
* as if it was created by the tree itself with #cxTreeAddChild() (e.g. use
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1302
|
* the same allocator).
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1303
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1304
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1305
|
* @param parent the parent of the node to add
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1306
|
* @param child the node to add
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1307
|
* @see cxTreeSetParent()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1308
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1309
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1310
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1311
|
void cxTreeAddChildNode(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1312
|
CxTree *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1313
|
void *parent,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1314
|
void *child
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1315
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1316
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1317
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1318
|
* Creates a new node and adds it to the tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1319
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1320
|
* With this function you can decide where exactly the new node shall be added.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1321
|
* If you specified an appropriate search function, you may want to consider
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1322
|
* leaving this task to the tree by using #cxTreeInsert().
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1323
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1324
|
* Be aware that adding nodes at arbitrary locations in the tree might cause
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1325
|
* wrong or undesired results when subsequently invoking #cxTreeInsert() and
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1326
|
* the invariant imposed by the search function does not hold any longer.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1327
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1328
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1329
|
* @param parent the parent node of the new node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1330
|
* @param data the data that will be submitted to the create function
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1331
|
* @return zero when the new node was created, non-zero on allocation failure
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1332
|
* @see cxTreeInsert()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1333
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1334
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1335
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1336
|
int cxTreeAddChild(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1337
|
CxTree *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1338
|
void *parent,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1339
|
const void *data
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1340
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1341
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1342
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1343
|
* A function that is invoked when a node needs to be re-linked to a new parent.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1344
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1345
|
* When a node is re-linked, sometimes the contents need to be updated.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1346
|
* This callback is invoked by #cxTreeRemoveNode() and #cxTreeDestroyNode()
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1347
|
* so that those updates can be applied when re-linking the children of the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1348
|
* removed node.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1349
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1350
|
* @param node the affected node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1351
|
* @param old_parent the old parent of the node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1352
|
* @param new_parent the new parent of the node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1353
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1354
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1355
|
typedef void (*cx_tree_relink_func)(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1356
|
void *node,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1357
|
const void *old_parent,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1358
|
const void *new_parent
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1359
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1360
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1361
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1362
|
* Removes a node and re-links its children to its former parent.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1363
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1364
|
* If the node is not part of the tree, the behavior is undefined.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1365
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1366
|
* @note The destructor function, if any, will @em not be invoked. That means
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1367
|
* you will need to free the removed node by yourself, eventually.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1368
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1369
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1370
|
* @param node the node to remove (must not be the root node)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1371
|
* @param relink_func optional callback to update the content of each re-linked
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1372
|
* node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1373
|
* @return zero on success, non-zero if @p node is the root node of the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1374
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1375
|
cx_attr_nonnull_arg(1, 2)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1376
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1377
|
int cxTreeRemoveNode(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1378
|
CxTree *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1379
|
void *node,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1380
|
cx_tree_relink_func relink_func
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1381
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1382
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1383
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1384
|
* Removes a node and it's subtree from the tree.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1385
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1386
|
* If the node is not part of the tree, the behavior is undefined.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1387
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1388
|
* @note The destructor function, if any, will @em not be invoked. That means
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1389
|
* you will need to free the removed subtree by yourself, eventually.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1390
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1391
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1392
|
* @param node the node to remove
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1393
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1394
|
cx_attr_nonnull
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1395
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1396
|
void cxTreeRemoveSubtree(CxTree *tree, void *node);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1397
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1398
|
/**
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1399
|
* Destroys a node and re-links its children to its former parent.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1400
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1401
|
* If the node is not part of the tree, the behavior is undefined.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1402
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1403
|
* It is guaranteed that the simple destructor is invoked before
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1404
|
* the advanced destructor.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1405
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1406
|
* @attention This function will not free the memory of the node with the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1407
|
* tree's allocator, because that is usually done by the advanced destructor
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1408
|
* and would therefore result in a double-free.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1409
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1410
|
* @param tree the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1411
|
* @param node the node to destroy (must not be the root node)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1412
|
* @param relink_func optional callback to update the content of each re-linked
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1413
|
* node
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1414
|
* @return zero on success, non-zero if @p node is the root node of the tree
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1415
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1416
|
cx_attr_nonnull_arg(1, 2)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1417
|
cx_attr_export
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1418
|
int cxTreeDestroyNode(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1419
|
CxTree *tree,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1420
|
void *node,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1421
|
cx_tree_relink_func relink_func
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1422
|
);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1423
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1424
|
#ifdef __cplusplus
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1425
|
} // extern "C"
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1426
|
#endif
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1427
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1428
|
#endif //UCX_TREE_H
|