1b8d30947SMasatake YAMATO /*
2b8d30947SMasatake YAMATO * =============================================================================
3b8d30947SMasatake YAMATO *
4b8d30947SMasatake YAMATO * Filename: rbtree.c
5b8d30947SMasatake YAMATO *
6b8d30947SMasatake YAMATO * Description: rbtree(Red-Black tree) implementation adapted from linux
7b8d30947SMasatake YAMATO * kernel thus can be used in userspace c program.
8b8d30947SMasatake YAMATO *
9b8d30947SMasatake YAMATO * Created: 09/02/2012 11:38:12 PM
10b8d30947SMasatake YAMATO *
11b8d30947SMasatake YAMATO * Author: Fu Haiping (forhappy), haipingf@gmail.com
12b8d30947SMasatake YAMATO * Company: ICT ( Institute Of Computing Technology, CAS )
13b8d30947SMasatake YAMATO *
14b8d30947SMasatake YAMATO * =============================================================================
15b8d30947SMasatake YAMATO */
16b8d30947SMasatake YAMATO
17b8d30947SMasatake YAMATO /*
18b8d30947SMasatake YAMATO Red Black Trees
19b8d30947SMasatake YAMATO (C) 1999 Andrea Arcangeli <andrea@suse.de>
20b8d30947SMasatake YAMATO (C) 2002 David Woodhouse <dwmw2@infradead.org>
21b8d30947SMasatake YAMATO
22b8d30947SMasatake YAMATO This program is free software; you can redistribute it and/or modify
23b8d30947SMasatake YAMATO it under the terms of the GNU General Public License as published by
24b8d30947SMasatake YAMATO the Free Software Foundation; either version 2 of the License, or
25b8d30947SMasatake YAMATO (at your option) any later version.
26b8d30947SMasatake YAMATO
27b8d30947SMasatake YAMATO This program is distributed in the hope that it will be useful,
28b8d30947SMasatake YAMATO but WITHOUT ANY WARRANTY; without even the implied warranty of
29b8d30947SMasatake YAMATO MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30b8d30947SMasatake YAMATO GNU General Public License for more details.
31b8d30947SMasatake YAMATO
32b8d30947SMasatake YAMATO You should have received a copy of the GNU General Public License
33b8d30947SMasatake YAMATO along with this program; if not, write to the Free Software
34b8d30947SMasatake YAMATO Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35b8d30947SMasatake YAMATO
36b8d30947SMasatake YAMATO linux/lib/rbtree.c
37b8d30947SMasatake YAMATO */
38b8d30947SMasatake YAMATO
39*b85d98b5SMasatake YAMATO #include "general.h"
40b8d30947SMasatake YAMATO #include "rbtree.h"
41b8d30947SMasatake YAMATO
__rb_rotate_left(struct rb_node * node,struct rb_root * root)42b8d30947SMasatake YAMATO static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
43b8d30947SMasatake YAMATO {
44b8d30947SMasatake YAMATO struct rb_node *right = node->rb_right;
45b8d30947SMasatake YAMATO struct rb_node *parent = rb_parent(node);
46b8d30947SMasatake YAMATO
47b8d30947SMasatake YAMATO if ((node->rb_right = right->rb_left))
48b8d30947SMasatake YAMATO rb_set_parent(right->rb_left, node);
49b8d30947SMasatake YAMATO right->rb_left = node;
50b8d30947SMasatake YAMATO
51b8d30947SMasatake YAMATO rb_set_parent(right, parent);
52b8d30947SMasatake YAMATO
53b8d30947SMasatake YAMATO if (parent)
54b8d30947SMasatake YAMATO {
55b8d30947SMasatake YAMATO if (node == parent->rb_left)
56b8d30947SMasatake YAMATO parent->rb_left = right;
57b8d30947SMasatake YAMATO else
58b8d30947SMasatake YAMATO parent->rb_right = right;
59b8d30947SMasatake YAMATO }
60b8d30947SMasatake YAMATO else
61b8d30947SMasatake YAMATO root->rb_node = right;
62b8d30947SMasatake YAMATO rb_set_parent(node, right);
63b8d30947SMasatake YAMATO }
64b8d30947SMasatake YAMATO
__rb_rotate_right(struct rb_node * node,struct rb_root * root)65b8d30947SMasatake YAMATO static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
66b8d30947SMasatake YAMATO {
67b8d30947SMasatake YAMATO struct rb_node *left = node->rb_left;
68b8d30947SMasatake YAMATO struct rb_node *parent = rb_parent(node);
69b8d30947SMasatake YAMATO
70b8d30947SMasatake YAMATO if ((node->rb_left = left->rb_right))
71b8d30947SMasatake YAMATO rb_set_parent(left->rb_right, node);
72b8d30947SMasatake YAMATO left->rb_right = node;
73b8d30947SMasatake YAMATO
74b8d30947SMasatake YAMATO rb_set_parent(left, parent);
75b8d30947SMasatake YAMATO
76b8d30947SMasatake YAMATO if (parent)
77b8d30947SMasatake YAMATO {
78b8d30947SMasatake YAMATO if (node == parent->rb_right)
79b8d30947SMasatake YAMATO parent->rb_right = left;
80b8d30947SMasatake YAMATO else
81b8d30947SMasatake YAMATO parent->rb_left = left;
82b8d30947SMasatake YAMATO }
83b8d30947SMasatake YAMATO else
84b8d30947SMasatake YAMATO root->rb_node = left;
85b8d30947SMasatake YAMATO rb_set_parent(node, left);
86b8d30947SMasatake YAMATO }
87b8d30947SMasatake YAMATO
rb_insert_color(struct rb_node * node,struct rb_root * root)88b8d30947SMasatake YAMATO void rb_insert_color(struct rb_node *node, struct rb_root *root)
89b8d30947SMasatake YAMATO {
90b8d30947SMasatake YAMATO struct rb_node *parent, *gparent;
91b8d30947SMasatake YAMATO
92b8d30947SMasatake YAMATO while ((parent = rb_parent(node)) && rb_is_red(parent))
93b8d30947SMasatake YAMATO {
94b8d30947SMasatake YAMATO gparent = rb_parent(parent);
95b8d30947SMasatake YAMATO
96b8d30947SMasatake YAMATO if (parent == gparent->rb_left)
97b8d30947SMasatake YAMATO {
98b8d30947SMasatake YAMATO {
99b8d30947SMasatake YAMATO register struct rb_node *uncle = gparent->rb_right;
100b8d30947SMasatake YAMATO if (uncle && rb_is_red(uncle))
101b8d30947SMasatake YAMATO {
102b8d30947SMasatake YAMATO rb_set_black(uncle);
103b8d30947SMasatake YAMATO rb_set_black(parent);
104b8d30947SMasatake YAMATO rb_set_red(gparent);
105b8d30947SMasatake YAMATO node = gparent;
106b8d30947SMasatake YAMATO continue;
107b8d30947SMasatake YAMATO }
108b8d30947SMasatake YAMATO }
109b8d30947SMasatake YAMATO
110b8d30947SMasatake YAMATO if (parent->rb_right == node)
111b8d30947SMasatake YAMATO {
112b8d30947SMasatake YAMATO register struct rb_node *tmp;
113b8d30947SMasatake YAMATO __rb_rotate_left(parent, root);
114b8d30947SMasatake YAMATO tmp = parent;
115b8d30947SMasatake YAMATO parent = node;
116b8d30947SMasatake YAMATO node = tmp;
117b8d30947SMasatake YAMATO }
118b8d30947SMasatake YAMATO
119b8d30947SMasatake YAMATO rb_set_black(parent);
120b8d30947SMasatake YAMATO rb_set_red(gparent);
121b8d30947SMasatake YAMATO __rb_rotate_right(gparent, root);
122b8d30947SMasatake YAMATO } else {
123b8d30947SMasatake YAMATO {
124b8d30947SMasatake YAMATO register struct rb_node *uncle = gparent->rb_left;
125b8d30947SMasatake YAMATO if (uncle && rb_is_red(uncle))
126b8d30947SMasatake YAMATO {
127b8d30947SMasatake YAMATO rb_set_black(uncle);
128b8d30947SMasatake YAMATO rb_set_black(parent);
129b8d30947SMasatake YAMATO rb_set_red(gparent);
130b8d30947SMasatake YAMATO node = gparent;
131b8d30947SMasatake YAMATO continue;
132b8d30947SMasatake YAMATO }
133b8d30947SMasatake YAMATO }
134b8d30947SMasatake YAMATO
135b8d30947SMasatake YAMATO if (parent->rb_left == node)
136b8d30947SMasatake YAMATO {
137b8d30947SMasatake YAMATO register struct rb_node *tmp;
138b8d30947SMasatake YAMATO __rb_rotate_right(parent, root);
139b8d30947SMasatake YAMATO tmp = parent;
140b8d30947SMasatake YAMATO parent = node;
141b8d30947SMasatake YAMATO node = tmp;
142b8d30947SMasatake YAMATO }
143b8d30947SMasatake YAMATO
144b8d30947SMasatake YAMATO rb_set_black(parent);
145b8d30947SMasatake YAMATO rb_set_red(gparent);
146b8d30947SMasatake YAMATO __rb_rotate_left(gparent, root);
147b8d30947SMasatake YAMATO }
148b8d30947SMasatake YAMATO }
149b8d30947SMasatake YAMATO
150b8d30947SMasatake YAMATO rb_set_black(root->rb_node);
151b8d30947SMasatake YAMATO }
152b8d30947SMasatake YAMATO
__rb_erase_color(struct rb_node * node,struct rb_node * parent,struct rb_root * root)153b8d30947SMasatake YAMATO static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
154b8d30947SMasatake YAMATO struct rb_root *root)
155b8d30947SMasatake YAMATO {
156b8d30947SMasatake YAMATO struct rb_node *other;
157b8d30947SMasatake YAMATO
158b8d30947SMasatake YAMATO while ((!node || rb_is_black(node)) && node != root->rb_node)
159b8d30947SMasatake YAMATO {
160b8d30947SMasatake YAMATO if (parent->rb_left == node)
161b8d30947SMasatake YAMATO {
162b8d30947SMasatake YAMATO other = parent->rb_right;
163b8d30947SMasatake YAMATO if (rb_is_red(other))
164b8d30947SMasatake YAMATO {
165b8d30947SMasatake YAMATO rb_set_black(other);
166b8d30947SMasatake YAMATO rb_set_red(parent);
167b8d30947SMasatake YAMATO __rb_rotate_left(parent, root);
168b8d30947SMasatake YAMATO other = parent->rb_right;
169b8d30947SMasatake YAMATO }
170b8d30947SMasatake YAMATO if ((!other->rb_left || rb_is_black(other->rb_left)) &&
171b8d30947SMasatake YAMATO (!other->rb_right || rb_is_black(other->rb_right)))
172b8d30947SMasatake YAMATO {
173b8d30947SMasatake YAMATO rb_set_red(other);
174b8d30947SMasatake YAMATO node = parent;
175b8d30947SMasatake YAMATO parent = rb_parent(node);
176b8d30947SMasatake YAMATO }
177b8d30947SMasatake YAMATO else
178b8d30947SMasatake YAMATO {
179b8d30947SMasatake YAMATO if (!other->rb_right || rb_is_black(other->rb_right))
180b8d30947SMasatake YAMATO {
181b8d30947SMasatake YAMATO rb_set_black(other->rb_left);
182b8d30947SMasatake YAMATO rb_set_red(other);
183b8d30947SMasatake YAMATO __rb_rotate_right(other, root);
184b8d30947SMasatake YAMATO other = parent->rb_right;
185b8d30947SMasatake YAMATO }
186b8d30947SMasatake YAMATO rb_set_color(other, rb_color(parent));
187b8d30947SMasatake YAMATO rb_set_black(parent);
188b8d30947SMasatake YAMATO rb_set_black(other->rb_right);
189b8d30947SMasatake YAMATO __rb_rotate_left(parent, root);
190b8d30947SMasatake YAMATO node = root->rb_node;
191b8d30947SMasatake YAMATO break;
192b8d30947SMasatake YAMATO }
193b8d30947SMasatake YAMATO }
194b8d30947SMasatake YAMATO else
195b8d30947SMasatake YAMATO {
196b8d30947SMasatake YAMATO other = parent->rb_left;
197b8d30947SMasatake YAMATO if (rb_is_red(other))
198b8d30947SMasatake YAMATO {
199b8d30947SMasatake YAMATO rb_set_black(other);
200b8d30947SMasatake YAMATO rb_set_red(parent);
201b8d30947SMasatake YAMATO __rb_rotate_right(parent, root);
202b8d30947SMasatake YAMATO other = parent->rb_left;
203b8d30947SMasatake YAMATO }
204b8d30947SMasatake YAMATO if ((!other->rb_left || rb_is_black(other->rb_left)) &&
205b8d30947SMasatake YAMATO (!other->rb_right || rb_is_black(other->rb_right)))
206b8d30947SMasatake YAMATO {
207b8d30947SMasatake YAMATO rb_set_red(other);
208b8d30947SMasatake YAMATO node = parent;
209b8d30947SMasatake YAMATO parent = rb_parent(node);
210b8d30947SMasatake YAMATO }
211b8d30947SMasatake YAMATO else
212b8d30947SMasatake YAMATO {
213b8d30947SMasatake YAMATO if (!other->rb_left || rb_is_black(other->rb_left))
214b8d30947SMasatake YAMATO {
215b8d30947SMasatake YAMATO rb_set_black(other->rb_right);
216b8d30947SMasatake YAMATO rb_set_red(other);
217b8d30947SMasatake YAMATO __rb_rotate_left(other, root);
218b8d30947SMasatake YAMATO other = parent->rb_left;
219b8d30947SMasatake YAMATO }
220b8d30947SMasatake YAMATO rb_set_color(other, rb_color(parent));
221b8d30947SMasatake YAMATO rb_set_black(parent);
222b8d30947SMasatake YAMATO rb_set_black(other->rb_left);
223b8d30947SMasatake YAMATO __rb_rotate_right(parent, root);
224b8d30947SMasatake YAMATO node = root->rb_node;
225b8d30947SMasatake YAMATO break;
226b8d30947SMasatake YAMATO }
227b8d30947SMasatake YAMATO }
228b8d30947SMasatake YAMATO }
229b8d30947SMasatake YAMATO if (node)
230b8d30947SMasatake YAMATO rb_set_black(node);
231b8d30947SMasatake YAMATO }
232b8d30947SMasatake YAMATO
rb_erase(struct rb_node * node,struct rb_root * root)233b8d30947SMasatake YAMATO void rb_erase(struct rb_node *node, struct rb_root *root)
234b8d30947SMasatake YAMATO {
235b8d30947SMasatake YAMATO struct rb_node *child, *parent;
236b8d30947SMasatake YAMATO int color;
237b8d30947SMasatake YAMATO
238b8d30947SMasatake YAMATO if (!node->rb_left)
239b8d30947SMasatake YAMATO child = node->rb_right;
240b8d30947SMasatake YAMATO else if (!node->rb_right)
241b8d30947SMasatake YAMATO child = node->rb_left;
242b8d30947SMasatake YAMATO else
243b8d30947SMasatake YAMATO {
244b8d30947SMasatake YAMATO struct rb_node *old = node, *left;
245b8d30947SMasatake YAMATO
246b8d30947SMasatake YAMATO node = node->rb_right;
247b8d30947SMasatake YAMATO while ((left = node->rb_left) != NULL)
248b8d30947SMasatake YAMATO node = left;
249b8d30947SMasatake YAMATO
250b8d30947SMasatake YAMATO if (rb_parent(old)) {
251b8d30947SMasatake YAMATO if (rb_parent(old)->rb_left == old)
252b8d30947SMasatake YAMATO rb_parent(old)->rb_left = node;
253b8d30947SMasatake YAMATO else
254b8d30947SMasatake YAMATO rb_parent(old)->rb_right = node;
255b8d30947SMasatake YAMATO } else
256b8d30947SMasatake YAMATO root->rb_node = node;
257b8d30947SMasatake YAMATO
258b8d30947SMasatake YAMATO child = node->rb_right;
259b8d30947SMasatake YAMATO parent = rb_parent(node);
260b8d30947SMasatake YAMATO color = rb_color(node);
261b8d30947SMasatake YAMATO
262b8d30947SMasatake YAMATO if (parent == old) {
263b8d30947SMasatake YAMATO parent = node;
264b8d30947SMasatake YAMATO } else {
265b8d30947SMasatake YAMATO if (child)
266b8d30947SMasatake YAMATO rb_set_parent(child, parent);
267b8d30947SMasatake YAMATO parent->rb_left = child;
268b8d30947SMasatake YAMATO
269b8d30947SMasatake YAMATO node->rb_right = old->rb_right;
270b8d30947SMasatake YAMATO rb_set_parent(old->rb_right, node);
271b8d30947SMasatake YAMATO }
272b8d30947SMasatake YAMATO
273b8d30947SMasatake YAMATO node->rb_parent_color = old->rb_parent_color;
274b8d30947SMasatake YAMATO node->rb_left = old->rb_left;
275b8d30947SMasatake YAMATO rb_set_parent(old->rb_left, node);
276b8d30947SMasatake YAMATO
277b8d30947SMasatake YAMATO goto color;
278b8d30947SMasatake YAMATO }
279b8d30947SMasatake YAMATO
280b8d30947SMasatake YAMATO parent = rb_parent(node);
281b8d30947SMasatake YAMATO color = rb_color(node);
282b8d30947SMasatake YAMATO
283b8d30947SMasatake YAMATO if (child)
284b8d30947SMasatake YAMATO rb_set_parent(child, parent);
285b8d30947SMasatake YAMATO if (parent)
286b8d30947SMasatake YAMATO {
287b8d30947SMasatake YAMATO if (parent->rb_left == node)
288b8d30947SMasatake YAMATO parent->rb_left = child;
289b8d30947SMasatake YAMATO else
290b8d30947SMasatake YAMATO parent->rb_right = child;
291b8d30947SMasatake YAMATO }
292b8d30947SMasatake YAMATO else
293b8d30947SMasatake YAMATO root->rb_node = child;
294b8d30947SMasatake YAMATO
295b8d30947SMasatake YAMATO color:
296b8d30947SMasatake YAMATO if (color == RB_BLACK)
297b8d30947SMasatake YAMATO __rb_erase_color(child, parent, root);
298b8d30947SMasatake YAMATO }
299b8d30947SMasatake YAMATO
rb_augment_path(struct rb_node * node,rb_augment_f func,void * data)300b8d30947SMasatake YAMATO static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
301b8d30947SMasatake YAMATO {
302b8d30947SMasatake YAMATO struct rb_node *parent;
303b8d30947SMasatake YAMATO
304b8d30947SMasatake YAMATO up:
305b8d30947SMasatake YAMATO func(node, data);
306b8d30947SMasatake YAMATO parent = rb_parent(node);
307b8d30947SMasatake YAMATO if (!parent)
308b8d30947SMasatake YAMATO return;
309b8d30947SMasatake YAMATO
310b8d30947SMasatake YAMATO if (node == parent->rb_left && parent->rb_right)
311b8d30947SMasatake YAMATO func(parent->rb_right, data);
312b8d30947SMasatake YAMATO else if (parent->rb_left)
313b8d30947SMasatake YAMATO func(parent->rb_left, data);
314b8d30947SMasatake YAMATO
315b8d30947SMasatake YAMATO node = parent;
316b8d30947SMasatake YAMATO goto up;
317b8d30947SMasatake YAMATO }
318b8d30947SMasatake YAMATO
319b8d30947SMasatake YAMATO /*
320b8d30947SMasatake YAMATO * after inserting @node into the tree, update the tree to account for
321b8d30947SMasatake YAMATO * both the new entry and any damage done by rebalance
322b8d30947SMasatake YAMATO */
rb_augment_insert(struct rb_node * node,rb_augment_f func,void * data)323b8d30947SMasatake YAMATO void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
324b8d30947SMasatake YAMATO {
325b8d30947SMasatake YAMATO if (node->rb_left)
326b8d30947SMasatake YAMATO node = node->rb_left;
327b8d30947SMasatake YAMATO else if (node->rb_right)
328b8d30947SMasatake YAMATO node = node->rb_right;
329b8d30947SMasatake YAMATO
330b8d30947SMasatake YAMATO rb_augment_path(node, func, data);
331b8d30947SMasatake YAMATO }
332b8d30947SMasatake YAMATO
333b8d30947SMasatake YAMATO /*
334b8d30947SMasatake YAMATO * before removing the node, find the deepest node on the rebalance path
335b8d30947SMasatake YAMATO * that will still be there after @node gets removed
336b8d30947SMasatake YAMATO */
rb_augment_erase_begin(struct rb_node * node)337b8d30947SMasatake YAMATO struct rb_node *rb_augment_erase_begin(struct rb_node *node)
338b8d30947SMasatake YAMATO {
339b8d30947SMasatake YAMATO struct rb_node *deepest;
340b8d30947SMasatake YAMATO
341b8d30947SMasatake YAMATO if (!node->rb_right && !node->rb_left)
342b8d30947SMasatake YAMATO deepest = rb_parent(node);
343b8d30947SMasatake YAMATO else if (!node->rb_right)
344b8d30947SMasatake YAMATO deepest = node->rb_left;
345b8d30947SMasatake YAMATO else if (!node->rb_left)
346b8d30947SMasatake YAMATO deepest = node->rb_right;
347b8d30947SMasatake YAMATO else {
348b8d30947SMasatake YAMATO deepest = rb_next(node);
349b8d30947SMasatake YAMATO if (deepest->rb_right)
350b8d30947SMasatake YAMATO deepest = deepest->rb_right;
351b8d30947SMasatake YAMATO else if (rb_parent(deepest) != node)
352b8d30947SMasatake YAMATO deepest = rb_parent(deepest);
353b8d30947SMasatake YAMATO }
354b8d30947SMasatake YAMATO
355b8d30947SMasatake YAMATO return deepest;
356b8d30947SMasatake YAMATO }
357b8d30947SMasatake YAMATO
358b8d30947SMasatake YAMATO /*
359b8d30947SMasatake YAMATO * after removal, update the tree to account for the removed entry
360b8d30947SMasatake YAMATO * and any rebalance damage.
361b8d30947SMasatake YAMATO */
rb_augment_erase_end(struct rb_node * node,rb_augment_f func,void * data)362b8d30947SMasatake YAMATO void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
363b8d30947SMasatake YAMATO {
364b8d30947SMasatake YAMATO if (node)
365b8d30947SMasatake YAMATO rb_augment_path(node, func, data);
366b8d30947SMasatake YAMATO }
367b8d30947SMasatake YAMATO
368b8d30947SMasatake YAMATO /*
369b8d30947SMasatake YAMATO * This function returns the first node (in sort order) of the tree.
370b8d30947SMasatake YAMATO */
rb_first(const struct rb_root * root)371b8d30947SMasatake YAMATO struct rb_node *rb_first(const struct rb_root *root)
372b8d30947SMasatake YAMATO {
373b8d30947SMasatake YAMATO struct rb_node *n;
374b8d30947SMasatake YAMATO
375b8d30947SMasatake YAMATO n = root->rb_node;
376b8d30947SMasatake YAMATO if (!n)
377b8d30947SMasatake YAMATO return NULL;
378b8d30947SMasatake YAMATO while (n->rb_left)
379b8d30947SMasatake YAMATO n = n->rb_left;
380b8d30947SMasatake YAMATO return n;
381b8d30947SMasatake YAMATO }
382b8d30947SMasatake YAMATO
rb_last(const struct rb_root * root)383b8d30947SMasatake YAMATO struct rb_node *rb_last(const struct rb_root *root)
384b8d30947SMasatake YAMATO {
385b8d30947SMasatake YAMATO struct rb_node *n;
386b8d30947SMasatake YAMATO
387b8d30947SMasatake YAMATO n = root->rb_node;
388b8d30947SMasatake YAMATO if (!n)
389b8d30947SMasatake YAMATO return NULL;
390b8d30947SMasatake YAMATO while (n->rb_right)
391b8d30947SMasatake YAMATO n = n->rb_right;
392b8d30947SMasatake YAMATO return n;
393b8d30947SMasatake YAMATO }
394b8d30947SMasatake YAMATO
rb_next(const struct rb_node * node)395b8d30947SMasatake YAMATO struct rb_node *rb_next(const struct rb_node *node)
396b8d30947SMasatake YAMATO {
397b8d30947SMasatake YAMATO struct rb_node *parent;
398b8d30947SMasatake YAMATO
399b8d30947SMasatake YAMATO if (rb_parent(node) == node)
400b8d30947SMasatake YAMATO return NULL;
401b8d30947SMasatake YAMATO
402b8d30947SMasatake YAMATO /* If we have a right-hand child, go down and then left as far
403b8d30947SMasatake YAMATO as we can. */
404b8d30947SMasatake YAMATO if (node->rb_right) {
405b8d30947SMasatake YAMATO node = node->rb_right;
406b8d30947SMasatake YAMATO while (node->rb_left)
407b8d30947SMasatake YAMATO node=node->rb_left;
408b8d30947SMasatake YAMATO return (struct rb_node *)node;
409b8d30947SMasatake YAMATO }
410b8d30947SMasatake YAMATO
411b8d30947SMasatake YAMATO /* No right-hand children. Everything down and left is
412b8d30947SMasatake YAMATO smaller than us, so any 'next' node must be in the general
413b8d30947SMasatake YAMATO direction of our parent. Go up the tree; any time the
414b8d30947SMasatake YAMATO ancestor is a right-hand child of its parent, keep going
415b8d30947SMasatake YAMATO up. First time it's a left-hand child of its parent, said
416b8d30947SMasatake YAMATO parent is our 'next' node. */
417b8d30947SMasatake YAMATO while ((parent = rb_parent(node)) && node == parent->rb_right)
418b8d30947SMasatake YAMATO node = parent;
419b8d30947SMasatake YAMATO
420b8d30947SMasatake YAMATO return parent;
421b8d30947SMasatake YAMATO }
422b8d30947SMasatake YAMATO
rb_prev(const struct rb_node * node)423b8d30947SMasatake YAMATO struct rb_node *rb_prev(const struct rb_node *node)
424b8d30947SMasatake YAMATO {
425b8d30947SMasatake YAMATO struct rb_node *parent;
426b8d30947SMasatake YAMATO
427b8d30947SMasatake YAMATO if (rb_parent(node) == node)
428b8d30947SMasatake YAMATO return NULL;
429b8d30947SMasatake YAMATO
430b8d30947SMasatake YAMATO /* If we have a left-hand child, go down and then right as far
431b8d30947SMasatake YAMATO as we can. */
432b8d30947SMasatake YAMATO if (node->rb_left) {
433b8d30947SMasatake YAMATO node = node->rb_left;
434b8d30947SMasatake YAMATO while (node->rb_right)
435b8d30947SMasatake YAMATO node=node->rb_right;
436b8d30947SMasatake YAMATO return (struct rb_node *)node;
437b8d30947SMasatake YAMATO }
438b8d30947SMasatake YAMATO
439b8d30947SMasatake YAMATO /* No left-hand children. Go up till we find an ancestor which
440b8d30947SMasatake YAMATO is a right-hand child of its parent */
441b8d30947SMasatake YAMATO while ((parent = rb_parent(node)) && node == parent->rb_left)
442b8d30947SMasatake YAMATO node = parent;
443b8d30947SMasatake YAMATO
444b8d30947SMasatake YAMATO return parent;
445b8d30947SMasatake YAMATO }
446b8d30947SMasatake YAMATO
rb_replace_node(struct rb_node * victim,struct rb_node * new,struct rb_root * root)447b8d30947SMasatake YAMATO void rb_replace_node(struct rb_node *victim, struct rb_node *new,
448b8d30947SMasatake YAMATO struct rb_root *root)
449b8d30947SMasatake YAMATO {
450b8d30947SMasatake YAMATO struct rb_node *parent = rb_parent(victim);
451b8d30947SMasatake YAMATO
452b8d30947SMasatake YAMATO /* Set the surrounding nodes to point to the replacement */
453b8d30947SMasatake YAMATO if (parent) {
454b8d30947SMasatake YAMATO if (victim == parent->rb_left)
455b8d30947SMasatake YAMATO parent->rb_left = new;
456b8d30947SMasatake YAMATO else
457b8d30947SMasatake YAMATO parent->rb_right = new;
458b8d30947SMasatake YAMATO } else {
459b8d30947SMasatake YAMATO root->rb_node = new;
460b8d30947SMasatake YAMATO }
461b8d30947SMasatake YAMATO if (victim->rb_left)
462b8d30947SMasatake YAMATO rb_set_parent(victim->rb_left, new);
463b8d30947SMasatake YAMATO if (victim->rb_right)
464b8d30947SMasatake YAMATO rb_set_parent(victim->rb_right, new);
465b8d30947SMasatake YAMATO
466b8d30947SMasatake YAMATO /* Copy the pointers/colour from the victim to the replacement */
467b8d30947SMasatake YAMATO *new = *victim;
468b8d30947SMasatake YAMATO }
469