1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker * Copyright © 2017 Faith Ekstrand
3*61046927SAndroid Build Coastguard Worker *
4*61046927SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a
5*61046927SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"),
6*61046927SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation
7*61046927SAndroid Build Coastguard Worker * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*61046927SAndroid Build Coastguard Worker * and/or sell copies of the Software, and to permit persons to whom the
9*61046927SAndroid Build Coastguard Worker * Software is furnished to do so, subject to the following conditions:
10*61046927SAndroid Build Coastguard Worker *
11*61046927SAndroid Build Coastguard Worker * The above copyright notice and this permission notice shall be included in
12*61046927SAndroid Build Coastguard Worker * all copies or substantial portions of the Software.
13*61046927SAndroid Build Coastguard Worker *
14*61046927SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*61046927SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*61046927SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17*61046927SAndroid Build Coastguard Worker * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18*61046927SAndroid Build Coastguard Worker * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19*61046927SAndroid Build Coastguard Worker * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20*61046927SAndroid Build Coastguard Worker * DEALINGS IN THE SOFTWARE.
21*61046927SAndroid Build Coastguard Worker */
22*61046927SAndroid Build Coastguard Worker
23*61046927SAndroid Build Coastguard Worker #ifndef RB_TREE_H
24*61046927SAndroid Build Coastguard Worker #define RB_TREE_H
25*61046927SAndroid Build Coastguard Worker
26*61046927SAndroid Build Coastguard Worker #include <stdbool.h>
27*61046927SAndroid Build Coastguard Worker #include <stddef.h>
28*61046927SAndroid Build Coastguard Worker #include <stdint.h>
29*61046927SAndroid Build Coastguard Worker #include <stdlib.h>
30*61046927SAndroid Build Coastguard Worker
31*61046927SAndroid Build Coastguard Worker #ifdef __cplusplus
32*61046927SAndroid Build Coastguard Worker extern "C" {
33*61046927SAndroid Build Coastguard Worker #endif
34*61046927SAndroid Build Coastguard Worker
35*61046927SAndroid Build Coastguard Worker /** A red-black tree node
36*61046927SAndroid Build Coastguard Worker *
37*61046927SAndroid Build Coastguard Worker * This struct represents a node in the red-black tree. This struct should
38*61046927SAndroid Build Coastguard Worker * be embedded as a member in whatever structure you wish to put in the
39*61046927SAndroid Build Coastguard Worker * tree.
40*61046927SAndroid Build Coastguard Worker */
41*61046927SAndroid Build Coastguard Worker struct rb_node {
42*61046927SAndroid Build Coastguard Worker /** Parent and color of this node
43*61046927SAndroid Build Coastguard Worker *
44*61046927SAndroid Build Coastguard Worker * The least significant bit represents the color and is set to 1 for
45*61046927SAndroid Build Coastguard Worker * black and 0 for red. The other bits are the pointer to the parent
46*61046927SAndroid Build Coastguard Worker * and that pointer can be retrieved by masking off the bottom bit and
47*61046927SAndroid Build Coastguard Worker * casting to a pointer.
48*61046927SAndroid Build Coastguard Worker */
49*61046927SAndroid Build Coastguard Worker uintptr_t parent;
50*61046927SAndroid Build Coastguard Worker
51*61046927SAndroid Build Coastguard Worker /** Left child of this node, null for a leaf */
52*61046927SAndroid Build Coastguard Worker struct rb_node *left;
53*61046927SAndroid Build Coastguard Worker
54*61046927SAndroid Build Coastguard Worker /** Right child of this node, null for a leaf */
55*61046927SAndroid Build Coastguard Worker struct rb_node *right;
56*61046927SAndroid Build Coastguard Worker };
57*61046927SAndroid Build Coastguard Worker
58*61046927SAndroid Build Coastguard Worker /** Return the parent node of the given node or NULL if it is the root */
59*61046927SAndroid Build Coastguard Worker static inline struct rb_node *
rb_node_parent(struct rb_node * n)60*61046927SAndroid Build Coastguard Worker rb_node_parent(struct rb_node *n)
61*61046927SAndroid Build Coastguard Worker {
62*61046927SAndroid Build Coastguard Worker return (struct rb_node *)(n->parent & ~(uintptr_t)1);
63*61046927SAndroid Build Coastguard Worker }
64*61046927SAndroid Build Coastguard Worker
65*61046927SAndroid Build Coastguard Worker /** A red-black tree
66*61046927SAndroid Build Coastguard Worker *
67*61046927SAndroid Build Coastguard Worker * This struct represents the red-black tree itself. It is just a pointer
68*61046927SAndroid Build Coastguard Worker * to the root node with no other metadata.
69*61046927SAndroid Build Coastguard Worker */
70*61046927SAndroid Build Coastguard Worker struct rb_tree {
71*61046927SAndroid Build Coastguard Worker struct rb_node *root;
72*61046927SAndroid Build Coastguard Worker };
73*61046927SAndroid Build Coastguard Worker
74*61046927SAndroid Build Coastguard Worker /** Initialize a red-black tree */
75*61046927SAndroid Build Coastguard Worker static inline void
rb_tree_init(struct rb_tree * T)76*61046927SAndroid Build Coastguard Worker rb_tree_init(struct rb_tree *T)
77*61046927SAndroid Build Coastguard Worker {
78*61046927SAndroid Build Coastguard Worker T->root = NULL;
79*61046927SAndroid Build Coastguard Worker }
80*61046927SAndroid Build Coastguard Worker
81*61046927SAndroid Build Coastguard Worker
82*61046927SAndroid Build Coastguard Worker /** Returns true if the red-black tree is empty */
83*61046927SAndroid Build Coastguard Worker static inline bool
rb_tree_is_empty(const struct rb_tree * T)84*61046927SAndroid Build Coastguard Worker rb_tree_is_empty(const struct rb_tree *T)
85*61046927SAndroid Build Coastguard Worker {
86*61046927SAndroid Build Coastguard Worker return T->root == NULL;
87*61046927SAndroid Build Coastguard Worker }
88*61046927SAndroid Build Coastguard Worker
89*61046927SAndroid Build Coastguard Worker /** Get the first (left-most) node in the tree or NULL */
90*61046927SAndroid Build Coastguard Worker struct rb_node *rb_tree_first(struct rb_tree *T);
91*61046927SAndroid Build Coastguard Worker
92*61046927SAndroid Build Coastguard Worker /** Get the last (right-most) node in the tree or NULL */
93*61046927SAndroid Build Coastguard Worker struct rb_node *rb_tree_last(struct rb_tree *T);
94*61046927SAndroid Build Coastguard Worker
95*61046927SAndroid Build Coastguard Worker /** Get the next node (to the right) in the tree or NULL */
96*61046927SAndroid Build Coastguard Worker struct rb_node *rb_node_next(struct rb_node *node);
97*61046927SAndroid Build Coastguard Worker
98*61046927SAndroid Build Coastguard Worker /** Get the next previous (to the left) in the tree or NULL */
99*61046927SAndroid Build Coastguard Worker struct rb_node *rb_node_prev(struct rb_node *node);
100*61046927SAndroid Build Coastguard Worker
101*61046927SAndroid Build Coastguard Worker #ifdef __cplusplus
102*61046927SAndroid Build Coastguard Worker /* This macro will not work correctly if `t' uses virtual inheritance. */
103*61046927SAndroid Build Coastguard Worker #define rb_tree_offsetof(t, f, p) \
104*61046927SAndroid Build Coastguard Worker (((char *) &((t *) p)->f) - ((char *) p))
105*61046927SAndroid Build Coastguard Worker #else
106*61046927SAndroid Build Coastguard Worker #define rb_tree_offsetof(t, f, p) offsetof(t, f)
107*61046927SAndroid Build Coastguard Worker #endif
108*61046927SAndroid Build Coastguard Worker
109*61046927SAndroid Build Coastguard Worker /** Retrieve the data structure containing a node
110*61046927SAndroid Build Coastguard Worker *
111*61046927SAndroid Build Coastguard Worker * \param type The type of the containing data structure
112*61046927SAndroid Build Coastguard Worker *
113*61046927SAndroid Build Coastguard Worker * \param node A pointer to a rb_node
114*61046927SAndroid Build Coastguard Worker *
115*61046927SAndroid Build Coastguard Worker * \param field The rb_node field in the containing data structure
116*61046927SAndroid Build Coastguard Worker */
117*61046927SAndroid Build Coastguard Worker #define rb_node_data(type, node, field) \
118*61046927SAndroid Build Coastguard Worker ((type *)(((char *)(node)) - rb_tree_offsetof(type, field, node)))
119*61046927SAndroid Build Coastguard Worker
120*61046927SAndroid Build Coastguard Worker /** Insert a node into a possibly augmented tree at a particular location
121*61046927SAndroid Build Coastguard Worker *
122*61046927SAndroid Build Coastguard Worker * This function should probably not be used directly as it relies on the
123*61046927SAndroid Build Coastguard Worker * caller to ensure that the parent node is correct. Use rb_tree_insert
124*61046927SAndroid Build Coastguard Worker * instead.
125*61046927SAndroid Build Coastguard Worker *
126*61046927SAndroid Build Coastguard Worker * If \p update is non-NULL, it will be called for the node being inserted as
127*61046927SAndroid Build Coastguard Worker * well as any nodes which have their children changed and all of their
128*61046927SAndroid Build Coastguard Worker * ancestors. The intent is that each node may contain some augmented data
129*61046927SAndroid Build Coastguard Worker * which is calculated recursively from the node itself and its children, and
130*61046927SAndroid Build Coastguard Worker * \p update should recalculate that data. It's assumed that the function used
131*61046927SAndroid Build Coastguard Worker * to calculate the node data is associative in order to avoid calling it
132*61046927SAndroid Build Coastguard Worker * redundantly after rebalancing the tree.
133*61046927SAndroid Build Coastguard Worker *
134*61046927SAndroid Build Coastguard Worker * \param T The red-black tree into which to insert the new node
135*61046927SAndroid Build Coastguard Worker *
136*61046927SAndroid Build Coastguard Worker * \param parent The node in the tree that will be the parent of the
137*61046927SAndroid Build Coastguard Worker * newly inserted node
138*61046927SAndroid Build Coastguard Worker *
139*61046927SAndroid Build Coastguard Worker * \param node The node to insert
140*61046927SAndroid Build Coastguard Worker *
141*61046927SAndroid Build Coastguard Worker * \param insert_left If true, the new node will be the left child of
142*61046927SAndroid Build Coastguard Worker * \p parent, otherwise it will be the right child
143*61046927SAndroid Build Coastguard Worker *
144*61046927SAndroid Build Coastguard Worker * \param update The optional function used to calculate per-node data
145*61046927SAndroid Build Coastguard Worker */
146*61046927SAndroid Build Coastguard Worker void rb_augmented_tree_insert_at(struct rb_tree *T, struct rb_node *parent,
147*61046927SAndroid Build Coastguard Worker struct rb_node *node, bool insert_left,
148*61046927SAndroid Build Coastguard Worker void (*update)(struct rb_node *));
149*61046927SAndroid Build Coastguard Worker
150*61046927SAndroid Build Coastguard Worker /** Insert a node into a tree at a particular location
151*61046927SAndroid Build Coastguard Worker *
152*61046927SAndroid Build Coastguard Worker * This function should probably not be used directly as it relies on the
153*61046927SAndroid Build Coastguard Worker * caller to ensure that the parent node is correct. Use rb_tree_insert
154*61046927SAndroid Build Coastguard Worker * instead.
155*61046927SAndroid Build Coastguard Worker *
156*61046927SAndroid Build Coastguard Worker * \param T The red-black tree into which to insert the new node
157*61046927SAndroid Build Coastguard Worker *
158*61046927SAndroid Build Coastguard Worker * \param parent The node in the tree that will be the parent of the
159*61046927SAndroid Build Coastguard Worker * newly inserted node
160*61046927SAndroid Build Coastguard Worker *
161*61046927SAndroid Build Coastguard Worker * \param node The node to insert
162*61046927SAndroid Build Coastguard Worker *
163*61046927SAndroid Build Coastguard Worker * \param insert_left If true, the new node will be the left child of
164*61046927SAndroid Build Coastguard Worker * \p parent, otherwise it will be the right child
165*61046927SAndroid Build Coastguard Worker */
166*61046927SAndroid Build Coastguard Worker static inline void
rb_tree_insert_at(struct rb_tree * T,struct rb_node * parent,struct rb_node * node,bool insert_left)167*61046927SAndroid Build Coastguard Worker rb_tree_insert_at(struct rb_tree *T, struct rb_node *parent,
168*61046927SAndroid Build Coastguard Worker struct rb_node *node, bool insert_left)
169*61046927SAndroid Build Coastguard Worker {
170*61046927SAndroid Build Coastguard Worker rb_augmented_tree_insert_at(T, parent, node, insert_left, NULL);
171*61046927SAndroid Build Coastguard Worker }
172*61046927SAndroid Build Coastguard Worker
173*61046927SAndroid Build Coastguard Worker /** Insert a node into a possibly augmented tree
174*61046927SAndroid Build Coastguard Worker *
175*61046927SAndroid Build Coastguard Worker * \param T The red-black tree into which to insert the new node
176*61046927SAndroid Build Coastguard Worker *
177*61046927SAndroid Build Coastguard Worker * \param node The node to insert
178*61046927SAndroid Build Coastguard Worker *
179*61046927SAndroid Build Coastguard Worker * \param cmp A comparison function to use to order the nodes.
180*61046927SAndroid Build Coastguard Worker *
181*61046927SAndroid Build Coastguard Worker * \param update Same meaning as in rb_augmented_tree_insert_at()
182*61046927SAndroid Build Coastguard Worker */
183*61046927SAndroid Build Coastguard Worker static inline void
rb_augmented_tree_insert(struct rb_tree * T,struct rb_node * node,int (* cmp)(const struct rb_node *,const struct rb_node *),void (* update)(struct rb_node *))184*61046927SAndroid Build Coastguard Worker rb_augmented_tree_insert(struct rb_tree *T, struct rb_node *node,
185*61046927SAndroid Build Coastguard Worker int (*cmp)(const struct rb_node *, const struct rb_node *),
186*61046927SAndroid Build Coastguard Worker void (*update)(struct rb_node *))
187*61046927SAndroid Build Coastguard Worker {
188*61046927SAndroid Build Coastguard Worker /* This function is declared inline in the hopes that the compiler can
189*61046927SAndroid Build Coastguard Worker * optimize away the comparison function pointer call.
190*61046927SAndroid Build Coastguard Worker */
191*61046927SAndroid Build Coastguard Worker struct rb_node *y = NULL;
192*61046927SAndroid Build Coastguard Worker struct rb_node *x = T->root;
193*61046927SAndroid Build Coastguard Worker bool left = false;
194*61046927SAndroid Build Coastguard Worker while (x != NULL) {
195*61046927SAndroid Build Coastguard Worker y = x;
196*61046927SAndroid Build Coastguard Worker left = cmp(x, node) < 0;
197*61046927SAndroid Build Coastguard Worker if (left)
198*61046927SAndroid Build Coastguard Worker x = x->left;
199*61046927SAndroid Build Coastguard Worker else
200*61046927SAndroid Build Coastguard Worker x = x->right;
201*61046927SAndroid Build Coastguard Worker }
202*61046927SAndroid Build Coastguard Worker
203*61046927SAndroid Build Coastguard Worker rb_augmented_tree_insert_at(T, y, node, left, update);
204*61046927SAndroid Build Coastguard Worker }
205*61046927SAndroid Build Coastguard Worker
206*61046927SAndroid Build Coastguard Worker /** Insert a node into a tree
207*61046927SAndroid Build Coastguard Worker *
208*61046927SAndroid Build Coastguard Worker * \param T The red-black tree into which to insert the new node
209*61046927SAndroid Build Coastguard Worker *
210*61046927SAndroid Build Coastguard Worker * \param node The node to insert
211*61046927SAndroid Build Coastguard Worker *
212*61046927SAndroid Build Coastguard Worker * \param cmp A comparison function to use to order the nodes.
213*61046927SAndroid Build Coastguard Worker */
214*61046927SAndroid Build Coastguard Worker static inline void
rb_tree_insert(struct rb_tree * T,struct rb_node * node,int (* cmp)(const struct rb_node *,const struct rb_node *))215*61046927SAndroid Build Coastguard Worker rb_tree_insert(struct rb_tree *T, struct rb_node *node,
216*61046927SAndroid Build Coastguard Worker int (*cmp)(const struct rb_node *, const struct rb_node *))
217*61046927SAndroid Build Coastguard Worker {
218*61046927SAndroid Build Coastguard Worker rb_augmented_tree_insert(T, node, cmp, NULL);
219*61046927SAndroid Build Coastguard Worker }
220*61046927SAndroid Build Coastguard Worker
221*61046927SAndroid Build Coastguard Worker /** Remove a node from a possibly augmented tree
222*61046927SAndroid Build Coastguard Worker *
223*61046927SAndroid Build Coastguard Worker * \param T The red-black tree from which to remove the node
224*61046927SAndroid Build Coastguard Worker *
225*61046927SAndroid Build Coastguard Worker * \param node The node to remove
226*61046927SAndroid Build Coastguard Worker *
227*61046927SAndroid Build Coastguard Worker * \param update Same meaning as in rb_agumented_tree_insert_at()
228*61046927SAndroid Build Coastguard Worker *
229*61046927SAndroid Build Coastguard Worker */
230*61046927SAndroid Build Coastguard Worker void rb_augmented_tree_remove(struct rb_tree *T, struct rb_node *z,
231*61046927SAndroid Build Coastguard Worker void (*update)(struct rb_node *));
232*61046927SAndroid Build Coastguard Worker
233*61046927SAndroid Build Coastguard Worker /** Remove a node from a tree
234*61046927SAndroid Build Coastguard Worker *
235*61046927SAndroid Build Coastguard Worker * \param T The red-black tree from which to remove the node
236*61046927SAndroid Build Coastguard Worker *
237*61046927SAndroid Build Coastguard Worker * \param node The node to remove
238*61046927SAndroid Build Coastguard Worker */
239*61046927SAndroid Build Coastguard Worker static inline void
rb_tree_remove(struct rb_tree * T,struct rb_node * z)240*61046927SAndroid Build Coastguard Worker rb_tree_remove(struct rb_tree *T, struct rb_node *z)
241*61046927SAndroid Build Coastguard Worker {
242*61046927SAndroid Build Coastguard Worker rb_augmented_tree_remove(T, z, NULL);
243*61046927SAndroid Build Coastguard Worker }
244*61046927SAndroid Build Coastguard Worker
245*61046927SAndroid Build Coastguard Worker /** Search the tree for a node
246*61046927SAndroid Build Coastguard Worker *
247*61046927SAndroid Build Coastguard Worker * If a node with a matching key exists, the first matching node found will
248*61046927SAndroid Build Coastguard Worker * be returned. If no matching node exists, NULL is returned.
249*61046927SAndroid Build Coastguard Worker *
250*61046927SAndroid Build Coastguard Worker * \param T The red-black tree to search
251*61046927SAndroid Build Coastguard Worker *
252*61046927SAndroid Build Coastguard Worker * \param key The key to search for
253*61046927SAndroid Build Coastguard Worker *
254*61046927SAndroid Build Coastguard Worker * \param cmp A comparison function to use to order the nodes
255*61046927SAndroid Build Coastguard Worker */
256*61046927SAndroid Build Coastguard Worker static inline struct rb_node *
rb_tree_search(struct rb_tree * T,const void * key,int (* cmp)(const struct rb_node *,const void *))257*61046927SAndroid Build Coastguard Worker rb_tree_search(struct rb_tree *T, const void *key,
258*61046927SAndroid Build Coastguard Worker int (*cmp)(const struct rb_node *, const void *))
259*61046927SAndroid Build Coastguard Worker {
260*61046927SAndroid Build Coastguard Worker /* This function is declared inline in the hopes that the compiler can
261*61046927SAndroid Build Coastguard Worker * optimize away the comparison function pointer call.
262*61046927SAndroid Build Coastguard Worker */
263*61046927SAndroid Build Coastguard Worker struct rb_node *x = T->root;
264*61046927SAndroid Build Coastguard Worker while (x != NULL) {
265*61046927SAndroid Build Coastguard Worker int c = cmp(x, key);
266*61046927SAndroid Build Coastguard Worker if (c < 0) {
267*61046927SAndroid Build Coastguard Worker x = x->left;
268*61046927SAndroid Build Coastguard Worker } else if (c > 0) {
269*61046927SAndroid Build Coastguard Worker x = x->right;
270*61046927SAndroid Build Coastguard Worker } else {
271*61046927SAndroid Build Coastguard Worker /* x is the first *encountered* node matching the key. There may
272*61046927SAndroid Build Coastguard Worker * be other nodes in the left subtree that also match the key.
273*61046927SAndroid Build Coastguard Worker */
274*61046927SAndroid Build Coastguard Worker while (true) {
275*61046927SAndroid Build Coastguard Worker struct rb_node *prev = rb_node_prev(x);
276*61046927SAndroid Build Coastguard Worker
277*61046927SAndroid Build Coastguard Worker if (prev == NULL || cmp(prev, key) != 0)
278*61046927SAndroid Build Coastguard Worker return x;
279*61046927SAndroid Build Coastguard Worker
280*61046927SAndroid Build Coastguard Worker x = prev;
281*61046927SAndroid Build Coastguard Worker }
282*61046927SAndroid Build Coastguard Worker }
283*61046927SAndroid Build Coastguard Worker }
284*61046927SAndroid Build Coastguard Worker
285*61046927SAndroid Build Coastguard Worker return x;
286*61046927SAndroid Build Coastguard Worker }
287*61046927SAndroid Build Coastguard Worker
288*61046927SAndroid Build Coastguard Worker /** Sloppily search the tree for a node
289*61046927SAndroid Build Coastguard Worker *
290*61046927SAndroid Build Coastguard Worker * This function searches the tree for a given node. If a node with a
291*61046927SAndroid Build Coastguard Worker * matching key exists, that first encountered matching node found (there may
292*61046927SAndroid Build Coastguard Worker * be other matching nodes in the left subtree) will be returned. If no node
293*61046927SAndroid Build Coastguard Worker * with an exactly matching key exists, the node returned will be either the
294*61046927SAndroid Build Coastguard Worker * right-most node comparing less than \p key or the right-most node comparing
295*61046927SAndroid Build Coastguard Worker * greater than \p key. If the tree is empty, NULL is returned.
296*61046927SAndroid Build Coastguard Worker *
297*61046927SAndroid Build Coastguard Worker * \param T The red-black tree to search
298*61046927SAndroid Build Coastguard Worker *
299*61046927SAndroid Build Coastguard Worker * \param key The key to search for
300*61046927SAndroid Build Coastguard Worker *
301*61046927SAndroid Build Coastguard Worker * \param cmp A comparison function to use to order the nodes
302*61046927SAndroid Build Coastguard Worker */
303*61046927SAndroid Build Coastguard Worker static inline struct rb_node *
rb_tree_search_sloppy(struct rb_tree * T,const void * key,int (* cmp)(const struct rb_node *,const void *))304*61046927SAndroid Build Coastguard Worker rb_tree_search_sloppy(struct rb_tree *T, const void *key,
305*61046927SAndroid Build Coastguard Worker int (*cmp)(const struct rb_node *, const void *))
306*61046927SAndroid Build Coastguard Worker {
307*61046927SAndroid Build Coastguard Worker /* This function is declared inline in the hopes that the compiler can
308*61046927SAndroid Build Coastguard Worker * optimize away the comparison function pointer call.
309*61046927SAndroid Build Coastguard Worker */
310*61046927SAndroid Build Coastguard Worker struct rb_node *y = NULL;
311*61046927SAndroid Build Coastguard Worker struct rb_node *x = T->root;
312*61046927SAndroid Build Coastguard Worker while (x != NULL) {
313*61046927SAndroid Build Coastguard Worker y = x;
314*61046927SAndroid Build Coastguard Worker int c = cmp(x, key);
315*61046927SAndroid Build Coastguard Worker if (c < 0)
316*61046927SAndroid Build Coastguard Worker x = x->left;
317*61046927SAndroid Build Coastguard Worker else if (c > 0)
318*61046927SAndroid Build Coastguard Worker x = x->right;
319*61046927SAndroid Build Coastguard Worker else
320*61046927SAndroid Build Coastguard Worker return x;
321*61046927SAndroid Build Coastguard Worker }
322*61046927SAndroid Build Coastguard Worker
323*61046927SAndroid Build Coastguard Worker return y;
324*61046927SAndroid Build Coastguard Worker }
325*61046927SAndroid Build Coastguard Worker
326*61046927SAndroid Build Coastguard Worker #define rb_node_next_or_null(n) ((n) == NULL ? NULL : rb_node_next(n))
327*61046927SAndroid Build Coastguard Worker #define rb_node_prev_or_null(n) ((n) == NULL ? NULL : rb_node_prev(n))
328*61046927SAndroid Build Coastguard Worker
329*61046927SAndroid Build Coastguard Worker /** Iterate over the nodes in the tree
330*61046927SAndroid Build Coastguard Worker *
331*61046927SAndroid Build Coastguard Worker * \param type The type of the containing data structure
332*61046927SAndroid Build Coastguard Worker *
333*61046927SAndroid Build Coastguard Worker * \param node The variable name for current node in the iteration;
334*61046927SAndroid Build Coastguard Worker * this will be declared as a pointer to \p type
335*61046927SAndroid Build Coastguard Worker *
336*61046927SAndroid Build Coastguard Worker * \param T The red-black tree
337*61046927SAndroid Build Coastguard Worker *
338*61046927SAndroid Build Coastguard Worker * \param field The rb_node field in containing data structure
339*61046927SAndroid Build Coastguard Worker */
340*61046927SAndroid Build Coastguard Worker #define rb_tree_foreach(type, iter, T, field) \
341*61046927SAndroid Build Coastguard Worker for (type *iter, *__node = (type *)rb_tree_first(T); \
342*61046927SAndroid Build Coastguard Worker __node != NULL && \
343*61046927SAndroid Build Coastguard Worker (iter = rb_node_data(type, (struct rb_node *)__node, field), true); \
344*61046927SAndroid Build Coastguard Worker __node = (type *)rb_node_next((struct rb_node *)__node))
345*61046927SAndroid Build Coastguard Worker
346*61046927SAndroid Build Coastguard Worker /** Iterate over the nodes in the tree, allowing the current node to be freed
347*61046927SAndroid Build Coastguard Worker *
348*61046927SAndroid Build Coastguard Worker * \param type The type of the containing data structure
349*61046927SAndroid Build Coastguard Worker *
350*61046927SAndroid Build Coastguard Worker * \param node The variable name for current node in the iteration;
351*61046927SAndroid Build Coastguard Worker * this will be declared as a pointer to \p type
352*61046927SAndroid Build Coastguard Worker *
353*61046927SAndroid Build Coastguard Worker * \param T The red-black tree
354*61046927SAndroid Build Coastguard Worker *
355*61046927SAndroid Build Coastguard Worker * \param field The rb_node field in containing data structure
356*61046927SAndroid Build Coastguard Worker */
357*61046927SAndroid Build Coastguard Worker #define rb_tree_foreach_safe(type, iter, T, field) \
358*61046927SAndroid Build Coastguard Worker for (type *iter, \
359*61046927SAndroid Build Coastguard Worker *__node = (type *)rb_tree_first(T), \
360*61046927SAndroid Build Coastguard Worker *__next = (type *)rb_node_next_or_null((struct rb_node *)__node); \
361*61046927SAndroid Build Coastguard Worker __node != NULL && \
362*61046927SAndroid Build Coastguard Worker (iter = rb_node_data(type, (struct rb_node *)__node, field), true); \
363*61046927SAndroid Build Coastguard Worker __node = __next, \
364*61046927SAndroid Build Coastguard Worker __next = (type *)rb_node_next_or_null((struct rb_node *)__node))
365*61046927SAndroid Build Coastguard Worker
366*61046927SAndroid Build Coastguard Worker /** Iterate over the nodes in the tree in reverse
367*61046927SAndroid Build Coastguard Worker *
368*61046927SAndroid Build Coastguard Worker * \param type The type of the containing data structure
369*61046927SAndroid Build Coastguard Worker *
370*61046927SAndroid Build Coastguard Worker * \param node The variable name for current node in the iteration;
371*61046927SAndroid Build Coastguard Worker * this will be declared as a pointer to \p type
372*61046927SAndroid Build Coastguard Worker *
373*61046927SAndroid Build Coastguard Worker * \param T The red-black tree
374*61046927SAndroid Build Coastguard Worker *
375*61046927SAndroid Build Coastguard Worker * \param field The rb_node field in containing data structure
376*61046927SAndroid Build Coastguard Worker */
377*61046927SAndroid Build Coastguard Worker #define rb_tree_foreach_rev(type, iter, T, field) \
378*61046927SAndroid Build Coastguard Worker for (type *iter, *__node = (type *)rb_tree_last(T); \
379*61046927SAndroid Build Coastguard Worker __node != NULL && \
380*61046927SAndroid Build Coastguard Worker (iter = rb_node_data(type, (struct rb_node *)__node, field), true); \
381*61046927SAndroid Build Coastguard Worker __node = (type *)rb_node_prev((struct rb_node *)__node))
382*61046927SAndroid Build Coastguard Worker
383*61046927SAndroid Build Coastguard Worker /** Iterate over the nodes in the tree in reverse, allowing the current node to be freed
384*61046927SAndroid Build Coastguard Worker *
385*61046927SAndroid Build Coastguard Worker * \param type The type of the containing data structure
386*61046927SAndroid Build Coastguard Worker *
387*61046927SAndroid Build Coastguard Worker * \param node The variable name for current node in the iteration;
388*61046927SAndroid Build Coastguard Worker * this will be declared as a pointer to \p type
389*61046927SAndroid Build Coastguard Worker *
390*61046927SAndroid Build Coastguard Worker * \param T The red-black tree
391*61046927SAndroid Build Coastguard Worker *
392*61046927SAndroid Build Coastguard Worker * \param field The rb_node field in containing data structure
393*61046927SAndroid Build Coastguard Worker */
394*61046927SAndroid Build Coastguard Worker #define rb_tree_foreach_rev_safe(type, iter, T, field) \
395*61046927SAndroid Build Coastguard Worker for (type *iter, \
396*61046927SAndroid Build Coastguard Worker *__node = (type *)rb_tree_last(T), \
397*61046927SAndroid Build Coastguard Worker *__prev = (type *)rb_node_prev_or_null((struct rb_node *)__node); \
398*61046927SAndroid Build Coastguard Worker __node != NULL && \
399*61046927SAndroid Build Coastguard Worker (iter = rb_node_data(type, (struct rb_node *)__node, field), true); \
400*61046927SAndroid Build Coastguard Worker __node = __prev, \
401*61046927SAndroid Build Coastguard Worker __prev = (type *)rb_node_prev_or_null((struct rb_node *)__node))
402*61046927SAndroid Build Coastguard Worker
403*61046927SAndroid Build Coastguard Worker /** Unsigned interval
404*61046927SAndroid Build Coastguard Worker *
405*61046927SAndroid Build Coastguard Worker * Intervals are closed by convention.
406*61046927SAndroid Build Coastguard Worker */
407*61046927SAndroid Build Coastguard Worker struct uinterval {
408*61046927SAndroid Build Coastguard Worker unsigned start, end;
409*61046927SAndroid Build Coastguard Worker };
410*61046927SAndroid Build Coastguard Worker
411*61046927SAndroid Build Coastguard Worker struct uinterval_node {
412*61046927SAndroid Build Coastguard Worker struct rb_node node;
413*61046927SAndroid Build Coastguard Worker
414*61046927SAndroid Build Coastguard Worker /* Must be filled in before inserting */
415*61046927SAndroid Build Coastguard Worker struct uinterval interval;
416*61046927SAndroid Build Coastguard Worker
417*61046927SAndroid Build Coastguard Worker /* Managed internally by the tree */
418*61046927SAndroid Build Coastguard Worker unsigned max_end;
419*61046927SAndroid Build Coastguard Worker };
420*61046927SAndroid Build Coastguard Worker
421*61046927SAndroid Build Coastguard Worker /** Insert a node into an unsigned interval tree. */
422*61046927SAndroid Build Coastguard Worker void uinterval_tree_insert(struct rb_tree *tree, struct uinterval_node *node);
423*61046927SAndroid Build Coastguard Worker
424*61046927SAndroid Build Coastguard Worker /** Remove a node from an unsigned interval tree. */
425*61046927SAndroid Build Coastguard Worker void uinterval_tree_remove(struct rb_tree *tree, struct uinterval_node *node);
426*61046927SAndroid Build Coastguard Worker
427*61046927SAndroid Build Coastguard Worker /** Get the first node intersecting the given interval. */
428*61046927SAndroid Build Coastguard Worker struct uinterval_node *uinterval_tree_first(struct rb_tree *tree,
429*61046927SAndroid Build Coastguard Worker struct uinterval interval);
430*61046927SAndroid Build Coastguard Worker
431*61046927SAndroid Build Coastguard Worker /** Get the next node after \p node intersecting the given interval. */
432*61046927SAndroid Build Coastguard Worker struct uinterval_node *uinterval_node_next(struct uinterval_node *node,
433*61046927SAndroid Build Coastguard Worker struct uinterval interval);
434*61046927SAndroid Build Coastguard Worker
435*61046927SAndroid Build Coastguard Worker /** Iterate over the nodes in the tree intersecting the given interval
436*61046927SAndroid Build Coastguard Worker *
437*61046927SAndroid Build Coastguard Worker * The iteration itself should take O(k log n) time, where k is the number of
438*61046927SAndroid Build Coastguard Worker * iterations of the loop and n is the size of the tree.
439*61046927SAndroid Build Coastguard Worker *
440*61046927SAndroid Build Coastguard Worker * \param type The type of the containing data structure
441*61046927SAndroid Build Coastguard Worker *
442*61046927SAndroid Build Coastguard Worker * \param node The variable name for current node in the iteration;
443*61046927SAndroid Build Coastguard Worker * this will be declared as a pointer to \p type
444*61046927SAndroid Build Coastguard Worker *
445*61046927SAndroid Build Coastguard Worker * \param interval The interval to be tested against.
446*61046927SAndroid Build Coastguard Worker *
447*61046927SAndroid Build Coastguard Worker * \param T The red-black tree
448*61046927SAndroid Build Coastguard Worker *
449*61046927SAndroid Build Coastguard Worker * \param field The uinterval_node field in containing data structure
450*61046927SAndroid Build Coastguard Worker */
451*61046927SAndroid Build Coastguard Worker #define uinterval_tree_foreach(type, iter, interval, T, field) \
452*61046927SAndroid Build Coastguard Worker for (type *iter, *__node = (type *)uinterval_tree_first(T, interval); \
453*61046927SAndroid Build Coastguard Worker __node != NULL && \
454*61046927SAndroid Build Coastguard Worker (iter = rb_node_data(type, (struct uinterval_node *)__node, field), true); \
455*61046927SAndroid Build Coastguard Worker __node = (type *)uinterval_node_next((struct uinterval_node *)__node, interval))
456*61046927SAndroid Build Coastguard Worker
457*61046927SAndroid Build Coastguard Worker /** Validate a red-black tree
458*61046927SAndroid Build Coastguard Worker *
459*61046927SAndroid Build Coastguard Worker * This function walks the tree and validates that this is a valid red-
460*61046927SAndroid Build Coastguard Worker * black tree. If anything is wrong, it will assert-fail.
461*61046927SAndroid Build Coastguard Worker */
462*61046927SAndroid Build Coastguard Worker void rb_tree_validate(struct rb_tree *T);
463*61046927SAndroid Build Coastguard Worker
464*61046927SAndroid Build Coastguard Worker #ifdef __cplusplus
465*61046927SAndroid Build Coastguard Worker } /* extern C */
466*61046927SAndroid Build Coastguard Worker #endif
467*61046927SAndroid Build Coastguard Worker
468*61046927SAndroid Build Coastguard Worker #endif /* RB_TREE_H */
469