1*c0937300SAndre Przywara /*
2*c0937300SAndre Przywara Red Black Trees
3*c0937300SAndre Przywara (C) 1999 Andrea Arcangeli <andrea@suse.de>
4*c0937300SAndre Przywara (C) 2002 David Woodhouse <dwmw2@infradead.org>
5*c0937300SAndre Przywara (C) 2012 Michel Lespinasse <walken@google.com>
6*c0937300SAndre Przywara
7*c0937300SAndre Przywara This program is free software; you can redistribute it and/or modify
8*c0937300SAndre Przywara it under the terms of the GNU General Public License as published by
9*c0937300SAndre Przywara the Free Software Foundation; either version 2 of the License, or
10*c0937300SAndre Przywara (at your option) any later version.
11*c0937300SAndre Przywara
12*c0937300SAndre Przywara This program is distributed in the hope that it will be useful,
13*c0937300SAndre Przywara but WITHOUT ANY WARRANTY; without even the implied warranty of
14*c0937300SAndre Przywara MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*c0937300SAndre Przywara GNU General Public License for more details.
16*c0937300SAndre Przywara
17*c0937300SAndre Przywara You should have received a copy of the GNU General Public License
18*c0937300SAndre Przywara along with this program; if not, write to the Free Software
19*c0937300SAndre Przywara Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20*c0937300SAndre Przywara
21*c0937300SAndre Przywara linux/lib/rbtree.c
22*c0937300SAndre Przywara */
23*c0937300SAndre Przywara
24*c0937300SAndre Przywara #include <linux/rbtree_augmented.h>
25*c0937300SAndre Przywara
26*c0937300SAndre Przywara /*
27*c0937300SAndre Przywara * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
28*c0937300SAndre Przywara *
29*c0937300SAndre Przywara * 1) A node is either red or black
30*c0937300SAndre Przywara * 2) The root is black
31*c0937300SAndre Przywara * 3) All leaves (NULL) are black
32*c0937300SAndre Przywara * 4) Both children of every red node are black
33*c0937300SAndre Przywara * 5) Every simple path from root to leaves contains the same number
34*c0937300SAndre Przywara * of black nodes.
35*c0937300SAndre Przywara *
36*c0937300SAndre Przywara * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
37*c0937300SAndre Przywara * consecutive red nodes in a path and every red node is therefore followed by
38*c0937300SAndre Przywara * a black. So if B is the number of black nodes on every simple path (as per
39*c0937300SAndre Przywara * 5), then the longest possible path due to 4 is 2B.
40*c0937300SAndre Przywara *
41*c0937300SAndre Przywara * We shall indicate color with case, where black nodes are uppercase and red
42*c0937300SAndre Przywara * nodes will be lowercase. Unknown color nodes shall be drawn as red within
43*c0937300SAndre Przywara * parentheses and have some accompanying text comment.
44*c0937300SAndre Przywara */
45*c0937300SAndre Przywara
rb_set_black(struct rb_node * rb)46*c0937300SAndre Przywara static inline void rb_set_black(struct rb_node *rb)
47*c0937300SAndre Przywara {
48*c0937300SAndre Przywara rb->__rb_parent_color |= RB_BLACK;
49*c0937300SAndre Przywara }
50*c0937300SAndre Przywara
rb_red_parent(struct rb_node * red)51*c0937300SAndre Przywara static inline struct rb_node *rb_red_parent(struct rb_node *red)
52*c0937300SAndre Przywara {
53*c0937300SAndre Przywara return (struct rb_node *)red->__rb_parent_color;
54*c0937300SAndre Przywara }
55*c0937300SAndre Przywara
56*c0937300SAndre Przywara /*
57*c0937300SAndre Przywara * Helper function for rotations:
58*c0937300SAndre Przywara * - old's parent and color get assigned to new
59*c0937300SAndre Przywara * - old gets assigned new as a parent and 'color' as a color.
60*c0937300SAndre Przywara */
61*c0937300SAndre Przywara static inline void
__rb_rotate_set_parents(struct rb_node * old,struct rb_node * new,struct rb_root * root,int color)62*c0937300SAndre Przywara __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
63*c0937300SAndre Przywara struct rb_root *root, int color)
64*c0937300SAndre Przywara {
65*c0937300SAndre Przywara struct rb_node *parent = rb_parent(old);
66*c0937300SAndre Przywara new->__rb_parent_color = old->__rb_parent_color;
67*c0937300SAndre Przywara rb_set_parent_color(old, new, color);
68*c0937300SAndre Przywara __rb_change_child(old, new, parent, root);
69*c0937300SAndre Przywara }
70*c0937300SAndre Przywara
71*c0937300SAndre Przywara static __always_inline void
__rb_insert(struct rb_node * node,struct rb_root * root,void (* augment_rotate)(struct rb_node * old,struct rb_node * new))72*c0937300SAndre Przywara __rb_insert(struct rb_node *node, struct rb_root *root,
73*c0937300SAndre Przywara void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
74*c0937300SAndre Przywara {
75*c0937300SAndre Przywara struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
76*c0937300SAndre Przywara
77*c0937300SAndre Przywara while (true) {
78*c0937300SAndre Przywara /*
79*c0937300SAndre Przywara * Loop invariant: node is red
80*c0937300SAndre Przywara *
81*c0937300SAndre Przywara * If there is a black parent, we are done.
82*c0937300SAndre Przywara * Otherwise, take some corrective action as we don't
83*c0937300SAndre Przywara * want a red root or two consecutive red nodes.
84*c0937300SAndre Przywara */
85*c0937300SAndre Przywara if (!parent) {
86*c0937300SAndre Przywara rb_set_parent_color(node, NULL, RB_BLACK);
87*c0937300SAndre Przywara break;
88*c0937300SAndre Przywara } else if (rb_is_black(parent))
89*c0937300SAndre Przywara break;
90*c0937300SAndre Przywara
91*c0937300SAndre Przywara gparent = rb_red_parent(parent);
92*c0937300SAndre Przywara
93*c0937300SAndre Przywara tmp = gparent->rb_right;
94*c0937300SAndre Przywara if (parent != tmp) { /* parent == gparent->rb_left */
95*c0937300SAndre Przywara if (tmp && rb_is_red(tmp)) {
96*c0937300SAndre Przywara /*
97*c0937300SAndre Przywara * Case 1 - color flips
98*c0937300SAndre Przywara *
99*c0937300SAndre Przywara * G g
100*c0937300SAndre Przywara * / \ / \
101*c0937300SAndre Przywara * p u --> P U
102*c0937300SAndre Przywara * / /
103*c0937300SAndre Przywara * n n
104*c0937300SAndre Przywara *
105*c0937300SAndre Przywara * However, since g's parent might be red, and
106*c0937300SAndre Przywara * 4) does not allow this, we need to recurse
107*c0937300SAndre Przywara * at g.
108*c0937300SAndre Przywara */
109*c0937300SAndre Przywara rb_set_parent_color(tmp, gparent, RB_BLACK);
110*c0937300SAndre Przywara rb_set_parent_color(parent, gparent, RB_BLACK);
111*c0937300SAndre Przywara node = gparent;
112*c0937300SAndre Przywara parent = rb_parent(node);
113*c0937300SAndre Przywara rb_set_parent_color(node, parent, RB_RED);
114*c0937300SAndre Przywara continue;
115*c0937300SAndre Przywara }
116*c0937300SAndre Przywara
117*c0937300SAndre Przywara tmp = parent->rb_right;
118*c0937300SAndre Przywara if (node == tmp) {
119*c0937300SAndre Przywara /*
120*c0937300SAndre Przywara * Case 2 - left rotate at parent
121*c0937300SAndre Przywara *
122*c0937300SAndre Przywara * G G
123*c0937300SAndre Przywara * / \ / \
124*c0937300SAndre Przywara * p U --> n U
125*c0937300SAndre Przywara * \ /
126*c0937300SAndre Przywara * n p
127*c0937300SAndre Przywara *
128*c0937300SAndre Przywara * This still leaves us in violation of 4), the
129*c0937300SAndre Przywara * continuation into Case 3 will fix that.
130*c0937300SAndre Przywara */
131*c0937300SAndre Przywara parent->rb_right = tmp = node->rb_left;
132*c0937300SAndre Przywara node->rb_left = parent;
133*c0937300SAndre Przywara if (tmp)
134*c0937300SAndre Przywara rb_set_parent_color(tmp, parent,
135*c0937300SAndre Przywara RB_BLACK);
136*c0937300SAndre Przywara rb_set_parent_color(parent, node, RB_RED);
137*c0937300SAndre Przywara augment_rotate(parent, node);
138*c0937300SAndre Przywara parent = node;
139*c0937300SAndre Przywara tmp = node->rb_right;
140*c0937300SAndre Przywara }
141*c0937300SAndre Przywara
142*c0937300SAndre Przywara /*
143*c0937300SAndre Przywara * Case 3 - right rotate at gparent
144*c0937300SAndre Przywara *
145*c0937300SAndre Przywara * G P
146*c0937300SAndre Przywara * / \ / \
147*c0937300SAndre Przywara * p U --> n g
148*c0937300SAndre Przywara * / \
149*c0937300SAndre Przywara * n U
150*c0937300SAndre Przywara */
151*c0937300SAndre Przywara gparent->rb_left = tmp; /* == parent->rb_right */
152*c0937300SAndre Przywara parent->rb_right = gparent;
153*c0937300SAndre Przywara if (tmp)
154*c0937300SAndre Przywara rb_set_parent_color(tmp, gparent, RB_BLACK);
155*c0937300SAndre Przywara __rb_rotate_set_parents(gparent, parent, root, RB_RED);
156*c0937300SAndre Przywara augment_rotate(gparent, parent);
157*c0937300SAndre Przywara break;
158*c0937300SAndre Przywara } else {
159*c0937300SAndre Przywara tmp = gparent->rb_left;
160*c0937300SAndre Przywara if (tmp && rb_is_red(tmp)) {
161*c0937300SAndre Przywara /* Case 1 - color flips */
162*c0937300SAndre Przywara rb_set_parent_color(tmp, gparent, RB_BLACK);
163*c0937300SAndre Przywara rb_set_parent_color(parent, gparent, RB_BLACK);
164*c0937300SAndre Przywara node = gparent;
165*c0937300SAndre Przywara parent = rb_parent(node);
166*c0937300SAndre Przywara rb_set_parent_color(node, parent, RB_RED);
167*c0937300SAndre Przywara continue;
168*c0937300SAndre Przywara }
169*c0937300SAndre Przywara
170*c0937300SAndre Przywara tmp = parent->rb_left;
171*c0937300SAndre Przywara if (node == tmp) {
172*c0937300SAndre Przywara /* Case 2 - right rotate at parent */
173*c0937300SAndre Przywara parent->rb_left = tmp = node->rb_right;
174*c0937300SAndre Przywara node->rb_right = parent;
175*c0937300SAndre Przywara if (tmp)
176*c0937300SAndre Przywara rb_set_parent_color(tmp, parent,
177*c0937300SAndre Przywara RB_BLACK);
178*c0937300SAndre Przywara rb_set_parent_color(parent, node, RB_RED);
179*c0937300SAndre Przywara augment_rotate(parent, node);
180*c0937300SAndre Przywara parent = node;
181*c0937300SAndre Przywara tmp = node->rb_left;
182*c0937300SAndre Przywara }
183*c0937300SAndre Przywara
184*c0937300SAndre Przywara /* Case 3 - left rotate at gparent */
185*c0937300SAndre Przywara gparent->rb_right = tmp; /* == parent->rb_left */
186*c0937300SAndre Przywara parent->rb_left = gparent;
187*c0937300SAndre Przywara if (tmp)
188*c0937300SAndre Przywara rb_set_parent_color(tmp, gparent, RB_BLACK);
189*c0937300SAndre Przywara __rb_rotate_set_parents(gparent, parent, root, RB_RED);
190*c0937300SAndre Przywara augment_rotate(gparent, parent);
191*c0937300SAndre Przywara break;
192*c0937300SAndre Przywara }
193*c0937300SAndre Przywara }
194*c0937300SAndre Przywara }
195*c0937300SAndre Przywara
196*c0937300SAndre Przywara /*
197*c0937300SAndre Przywara * Inline version for rb_erase() use - we want to be able to inline
198*c0937300SAndre Przywara * and eliminate the dummy_rotate callback there
199*c0937300SAndre Przywara */
200*c0937300SAndre Przywara static __always_inline void
____rb_erase_color(struct rb_node * parent,struct rb_root * root,void (* augment_rotate)(struct rb_node * old,struct rb_node * new))201*c0937300SAndre Przywara ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
202*c0937300SAndre Przywara void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
203*c0937300SAndre Przywara {
204*c0937300SAndre Przywara struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
205*c0937300SAndre Przywara
206*c0937300SAndre Przywara while (true) {
207*c0937300SAndre Przywara /*
208*c0937300SAndre Przywara * Loop invariants:
209*c0937300SAndre Przywara * - node is black (or NULL on first iteration)
210*c0937300SAndre Przywara * - node is not the root (parent is not NULL)
211*c0937300SAndre Przywara * - All leaf paths going through parent and node have a
212*c0937300SAndre Przywara * black node count that is 1 lower than other leaf paths.
213*c0937300SAndre Przywara */
214*c0937300SAndre Przywara sibling = parent->rb_right;
215*c0937300SAndre Przywara if (node != sibling) { /* node == parent->rb_left */
216*c0937300SAndre Przywara if (rb_is_red(sibling)) {
217*c0937300SAndre Przywara /*
218*c0937300SAndre Przywara * Case 1 - left rotate at parent
219*c0937300SAndre Przywara *
220*c0937300SAndre Przywara * P S
221*c0937300SAndre Przywara * / \ / \
222*c0937300SAndre Przywara * N s --> p Sr
223*c0937300SAndre Przywara * / \ / \
224*c0937300SAndre Przywara * Sl Sr N Sl
225*c0937300SAndre Przywara */
226*c0937300SAndre Przywara parent->rb_right = tmp1 = sibling->rb_left;
227*c0937300SAndre Przywara sibling->rb_left = parent;
228*c0937300SAndre Przywara rb_set_parent_color(tmp1, parent, RB_BLACK);
229*c0937300SAndre Przywara __rb_rotate_set_parents(parent, sibling, root,
230*c0937300SAndre Przywara RB_RED);
231*c0937300SAndre Przywara augment_rotate(parent, sibling);
232*c0937300SAndre Przywara sibling = tmp1;
233*c0937300SAndre Przywara }
234*c0937300SAndre Przywara tmp1 = sibling->rb_right;
235*c0937300SAndre Przywara if (!tmp1 || rb_is_black(tmp1)) {
236*c0937300SAndre Przywara tmp2 = sibling->rb_left;
237*c0937300SAndre Przywara if (!tmp2 || rb_is_black(tmp2)) {
238*c0937300SAndre Przywara /*
239*c0937300SAndre Przywara * Case 2 - sibling color flip
240*c0937300SAndre Przywara * (p could be either color here)
241*c0937300SAndre Przywara *
242*c0937300SAndre Przywara * (p) (p)
243*c0937300SAndre Przywara * / \ / \
244*c0937300SAndre Przywara * N S --> N s
245*c0937300SAndre Przywara * / \ / \
246*c0937300SAndre Przywara * Sl Sr Sl Sr
247*c0937300SAndre Przywara *
248*c0937300SAndre Przywara * This leaves us violating 5) which
249*c0937300SAndre Przywara * can be fixed by flipping p to black
250*c0937300SAndre Przywara * if it was red, or by recursing at p.
251*c0937300SAndre Przywara * p is red when coming from Case 1.
252*c0937300SAndre Przywara */
253*c0937300SAndre Przywara rb_set_parent_color(sibling, parent,
254*c0937300SAndre Przywara RB_RED);
255*c0937300SAndre Przywara if (rb_is_red(parent))
256*c0937300SAndre Przywara rb_set_black(parent);
257*c0937300SAndre Przywara else {
258*c0937300SAndre Przywara node = parent;
259*c0937300SAndre Przywara parent = rb_parent(node);
260*c0937300SAndre Przywara if (parent)
261*c0937300SAndre Przywara continue;
262*c0937300SAndre Przywara }
263*c0937300SAndre Przywara break;
264*c0937300SAndre Przywara }
265*c0937300SAndre Przywara /*
266*c0937300SAndre Przywara * Case 3 - right rotate at sibling
267*c0937300SAndre Przywara * (p could be either color here)
268*c0937300SAndre Przywara *
269*c0937300SAndre Przywara * (p) (p)
270*c0937300SAndre Przywara * / \ / \
271*c0937300SAndre Przywara * N S --> N Sl
272*c0937300SAndre Przywara * / \ \
273*c0937300SAndre Przywara * sl Sr s
274*c0937300SAndre Przywara * \
275*c0937300SAndre Przywara * Sr
276*c0937300SAndre Przywara */
277*c0937300SAndre Przywara sibling->rb_left = tmp1 = tmp2->rb_right;
278*c0937300SAndre Przywara tmp2->rb_right = sibling;
279*c0937300SAndre Przywara parent->rb_right = tmp2;
280*c0937300SAndre Przywara if (tmp1)
281*c0937300SAndre Przywara rb_set_parent_color(tmp1, sibling,
282*c0937300SAndre Przywara RB_BLACK);
283*c0937300SAndre Przywara augment_rotate(sibling, tmp2);
284*c0937300SAndre Przywara tmp1 = sibling;
285*c0937300SAndre Przywara sibling = tmp2;
286*c0937300SAndre Przywara }
287*c0937300SAndre Przywara /*
288*c0937300SAndre Przywara * Case 4 - left rotate at parent + color flips
289*c0937300SAndre Przywara * (p and sl could be either color here.
290*c0937300SAndre Przywara * After rotation, p becomes black, s acquires
291*c0937300SAndre Przywara * p's color, and sl keeps its color)
292*c0937300SAndre Przywara *
293*c0937300SAndre Przywara * (p) (s)
294*c0937300SAndre Przywara * / \ / \
295*c0937300SAndre Przywara * N S --> P Sr
296*c0937300SAndre Przywara * / \ / \
297*c0937300SAndre Przywara * (sl) sr N (sl)
298*c0937300SAndre Przywara */
299*c0937300SAndre Przywara parent->rb_right = tmp2 = sibling->rb_left;
300*c0937300SAndre Przywara sibling->rb_left = parent;
301*c0937300SAndre Przywara rb_set_parent_color(tmp1, sibling, RB_BLACK);
302*c0937300SAndre Przywara if (tmp2)
303*c0937300SAndre Przywara rb_set_parent(tmp2, parent);
304*c0937300SAndre Przywara __rb_rotate_set_parents(parent, sibling, root,
305*c0937300SAndre Przywara RB_BLACK);
306*c0937300SAndre Przywara augment_rotate(parent, sibling);
307*c0937300SAndre Przywara break;
308*c0937300SAndre Przywara } else {
309*c0937300SAndre Przywara sibling = parent->rb_left;
310*c0937300SAndre Przywara if (rb_is_red(sibling)) {
311*c0937300SAndre Przywara /* Case 1 - right rotate at parent */
312*c0937300SAndre Przywara parent->rb_left = tmp1 = sibling->rb_right;
313*c0937300SAndre Przywara sibling->rb_right = parent;
314*c0937300SAndre Przywara rb_set_parent_color(tmp1, parent, RB_BLACK);
315*c0937300SAndre Przywara __rb_rotate_set_parents(parent, sibling, root,
316*c0937300SAndre Przywara RB_RED);
317*c0937300SAndre Przywara augment_rotate(parent, sibling);
318*c0937300SAndre Przywara sibling = tmp1;
319*c0937300SAndre Przywara }
320*c0937300SAndre Przywara tmp1 = sibling->rb_left;
321*c0937300SAndre Przywara if (!tmp1 || rb_is_black(tmp1)) {
322*c0937300SAndre Przywara tmp2 = sibling->rb_right;
323*c0937300SAndre Przywara if (!tmp2 || rb_is_black(tmp2)) {
324*c0937300SAndre Przywara /* Case 2 - sibling color flip */
325*c0937300SAndre Przywara rb_set_parent_color(sibling, parent,
326*c0937300SAndre Przywara RB_RED);
327*c0937300SAndre Przywara if (rb_is_red(parent))
328*c0937300SAndre Przywara rb_set_black(parent);
329*c0937300SAndre Przywara else {
330*c0937300SAndre Przywara node = parent;
331*c0937300SAndre Przywara parent = rb_parent(node);
332*c0937300SAndre Przywara if (parent)
333*c0937300SAndre Przywara continue;
334*c0937300SAndre Przywara }
335*c0937300SAndre Przywara break;
336*c0937300SAndre Przywara }
337*c0937300SAndre Przywara /* Case 3 - right rotate at sibling */
338*c0937300SAndre Przywara sibling->rb_right = tmp1 = tmp2->rb_left;
339*c0937300SAndre Przywara tmp2->rb_left = sibling;
340*c0937300SAndre Przywara parent->rb_left = tmp2;
341*c0937300SAndre Przywara if (tmp1)
342*c0937300SAndre Przywara rb_set_parent_color(tmp1, sibling,
343*c0937300SAndre Przywara RB_BLACK);
344*c0937300SAndre Przywara augment_rotate(sibling, tmp2);
345*c0937300SAndre Przywara tmp1 = sibling;
346*c0937300SAndre Przywara sibling = tmp2;
347*c0937300SAndre Przywara }
348*c0937300SAndre Przywara /* Case 4 - left rotate at parent + color flips */
349*c0937300SAndre Przywara parent->rb_left = tmp2 = sibling->rb_right;
350*c0937300SAndre Przywara sibling->rb_right = parent;
351*c0937300SAndre Przywara rb_set_parent_color(tmp1, sibling, RB_BLACK);
352*c0937300SAndre Przywara if (tmp2)
353*c0937300SAndre Przywara rb_set_parent(tmp2, parent);
354*c0937300SAndre Przywara __rb_rotate_set_parents(parent, sibling, root,
355*c0937300SAndre Przywara RB_BLACK);
356*c0937300SAndre Przywara augment_rotate(parent, sibling);
357*c0937300SAndre Przywara break;
358*c0937300SAndre Przywara }
359*c0937300SAndre Przywara }
360*c0937300SAndre Przywara }
361*c0937300SAndre Przywara
362*c0937300SAndre Przywara /* Non-inline version for rb_erase_augmented() use */
__rb_erase_color(struct rb_node * parent,struct rb_root * root,void (* augment_rotate)(struct rb_node * old,struct rb_node * new))363*c0937300SAndre Przywara void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
364*c0937300SAndre Przywara void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
365*c0937300SAndre Przywara {
366*c0937300SAndre Przywara ____rb_erase_color(parent, root, augment_rotate);
367*c0937300SAndre Przywara }
368*c0937300SAndre Przywara
369*c0937300SAndre Przywara /*
370*c0937300SAndre Przywara * Non-augmented rbtree manipulation functions.
371*c0937300SAndre Przywara *
372*c0937300SAndre Przywara * We use dummy augmented callbacks here, and have the compiler optimize them
373*c0937300SAndre Przywara * out of the rb_insert_color() and rb_erase() function definitions.
374*c0937300SAndre Przywara */
375*c0937300SAndre Przywara
dummy_propagate(struct rb_node * node,struct rb_node * stop)376*c0937300SAndre Przywara static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
dummy_copy(struct rb_node * old,struct rb_node * new)377*c0937300SAndre Przywara static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
dummy_rotate(struct rb_node * old,struct rb_node * new)378*c0937300SAndre Przywara static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
379*c0937300SAndre Przywara
380*c0937300SAndre Przywara static const struct rb_augment_callbacks dummy_callbacks = {
381*c0937300SAndre Przywara dummy_propagate, dummy_copy, dummy_rotate
382*c0937300SAndre Przywara };
383*c0937300SAndre Przywara
rb_insert_color(struct rb_node * node,struct rb_root * root)384*c0937300SAndre Przywara void rb_insert_color(struct rb_node *node, struct rb_root *root)
385*c0937300SAndre Przywara {
386*c0937300SAndre Przywara __rb_insert(node, root, dummy_rotate);
387*c0937300SAndre Przywara }
388*c0937300SAndre Przywara
rb_erase(struct rb_node * node,struct rb_root * root)389*c0937300SAndre Przywara void rb_erase(struct rb_node *node, struct rb_root *root)
390*c0937300SAndre Przywara {
391*c0937300SAndre Przywara struct rb_node *rebalance;
392*c0937300SAndre Przywara rebalance = __rb_erase_augmented(node, root, &dummy_callbacks);
393*c0937300SAndre Przywara if (rebalance)
394*c0937300SAndre Przywara ____rb_erase_color(rebalance, root, dummy_rotate);
395*c0937300SAndre Przywara }
396*c0937300SAndre Przywara
397*c0937300SAndre Przywara /*
398*c0937300SAndre Przywara * Augmented rbtree manipulation functions.
399*c0937300SAndre Przywara *
400*c0937300SAndre Przywara * This instantiates the same __always_inline functions as in the non-augmented
401*c0937300SAndre Przywara * case, but this time with user-defined callbacks.
402*c0937300SAndre Przywara */
403*c0937300SAndre Przywara
__rb_insert_augmented(struct rb_node * node,struct rb_root * root,void (* augment_rotate)(struct rb_node * old,struct rb_node * new))404*c0937300SAndre Przywara void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
405*c0937300SAndre Przywara void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
406*c0937300SAndre Przywara {
407*c0937300SAndre Przywara __rb_insert(node, root, augment_rotate);
408*c0937300SAndre Przywara }
409*c0937300SAndre Przywara
410*c0937300SAndre Przywara /*
411*c0937300SAndre Przywara * This function returns the first node (in sort order) of the tree.
412*c0937300SAndre Przywara */
rb_first(const struct rb_root * root)413*c0937300SAndre Przywara struct rb_node *rb_first(const struct rb_root *root)
414*c0937300SAndre Przywara {
415*c0937300SAndre Przywara struct rb_node *n;
416*c0937300SAndre Przywara
417*c0937300SAndre Przywara n = root->rb_node;
418*c0937300SAndre Przywara if (!n)
419*c0937300SAndre Przywara return NULL;
420*c0937300SAndre Przywara while (n->rb_left)
421*c0937300SAndre Przywara n = n->rb_left;
422*c0937300SAndre Przywara return n;
423*c0937300SAndre Przywara }
424*c0937300SAndre Przywara
rb_last(const struct rb_root * root)425*c0937300SAndre Przywara struct rb_node *rb_last(const struct rb_root *root)
426*c0937300SAndre Przywara {
427*c0937300SAndre Przywara struct rb_node *n;
428*c0937300SAndre Przywara
429*c0937300SAndre Przywara n = root->rb_node;
430*c0937300SAndre Przywara if (!n)
431*c0937300SAndre Przywara return NULL;
432*c0937300SAndre Przywara while (n->rb_right)
433*c0937300SAndre Przywara n = n->rb_right;
434*c0937300SAndre Przywara return n;
435*c0937300SAndre Przywara }
436*c0937300SAndre Przywara
rb_next(const struct rb_node * node)437*c0937300SAndre Przywara struct rb_node *rb_next(const struct rb_node *node)
438*c0937300SAndre Przywara {
439*c0937300SAndre Przywara struct rb_node *parent;
440*c0937300SAndre Przywara
441*c0937300SAndre Przywara if (RB_EMPTY_NODE(node))
442*c0937300SAndre Przywara return NULL;
443*c0937300SAndre Przywara
444*c0937300SAndre Przywara /*
445*c0937300SAndre Przywara * If we have a right-hand child, go down and then left as far
446*c0937300SAndre Przywara * as we can.
447*c0937300SAndre Przywara */
448*c0937300SAndre Przywara if (node->rb_right) {
449*c0937300SAndre Przywara node = node->rb_right;
450*c0937300SAndre Przywara while (node->rb_left)
451*c0937300SAndre Przywara node=node->rb_left;
452*c0937300SAndre Przywara return (struct rb_node *)node;
453*c0937300SAndre Przywara }
454*c0937300SAndre Przywara
455*c0937300SAndre Przywara /*
456*c0937300SAndre Przywara * No right-hand children. Everything down and left is smaller than us,
457*c0937300SAndre Przywara * so any 'next' node must be in the general direction of our parent.
458*c0937300SAndre Przywara * Go up the tree; any time the ancestor is a right-hand child of its
459*c0937300SAndre Przywara * parent, keep going up. First time it's a left-hand child of its
460*c0937300SAndre Przywara * parent, said parent is our 'next' node.
461*c0937300SAndre Przywara */
462*c0937300SAndre Przywara while ((parent = rb_parent(node)) && node == parent->rb_right)
463*c0937300SAndre Przywara node = parent;
464*c0937300SAndre Przywara
465*c0937300SAndre Przywara return parent;
466*c0937300SAndre Przywara }
467*c0937300SAndre Przywara
rb_prev(const struct rb_node * node)468*c0937300SAndre Przywara struct rb_node *rb_prev(const struct rb_node *node)
469*c0937300SAndre Przywara {
470*c0937300SAndre Przywara struct rb_node *parent;
471*c0937300SAndre Przywara
472*c0937300SAndre Przywara if (RB_EMPTY_NODE(node))
473*c0937300SAndre Przywara return NULL;
474*c0937300SAndre Przywara
475*c0937300SAndre Przywara /*
476*c0937300SAndre Przywara * If we have a left-hand child, go down and then right as far
477*c0937300SAndre Przywara * as we can.
478*c0937300SAndre Przywara */
479*c0937300SAndre Przywara if (node->rb_left) {
480*c0937300SAndre Przywara node = node->rb_left;
481*c0937300SAndre Przywara while (node->rb_right)
482*c0937300SAndre Przywara node=node->rb_right;
483*c0937300SAndre Przywara return (struct rb_node *)node;
484*c0937300SAndre Przywara }
485*c0937300SAndre Przywara
486*c0937300SAndre Przywara /*
487*c0937300SAndre Przywara * No left-hand children. Go up till we find an ancestor which
488*c0937300SAndre Przywara * is a right-hand child of its parent.
489*c0937300SAndre Przywara */
490*c0937300SAndre Przywara while ((parent = rb_parent(node)) && node == parent->rb_left)
491*c0937300SAndre Przywara node = parent;
492*c0937300SAndre Przywara
493*c0937300SAndre Przywara return parent;
494*c0937300SAndre Przywara }
495*c0937300SAndre Przywara
rb_replace_node(struct rb_node * victim,struct rb_node * new,struct rb_root * root)496*c0937300SAndre Przywara void rb_replace_node(struct rb_node *victim, struct rb_node *new,
497*c0937300SAndre Przywara struct rb_root *root)
498*c0937300SAndre Przywara {
499*c0937300SAndre Przywara struct rb_node *parent = rb_parent(victim);
500*c0937300SAndre Przywara
501*c0937300SAndre Przywara /* Set the surrounding nodes to point to the replacement */
502*c0937300SAndre Przywara __rb_change_child(victim, new, parent, root);
503*c0937300SAndre Przywara if (victim->rb_left)
504*c0937300SAndre Przywara rb_set_parent(victim->rb_left, new);
505*c0937300SAndre Przywara if (victim->rb_right)
506*c0937300SAndre Przywara rb_set_parent(victim->rb_right, new);
507*c0937300SAndre Przywara
508*c0937300SAndre Przywara /* Copy the pointers/colour from the victim to the replacement */
509*c0937300SAndre Przywara *new = *victim;
510*c0937300SAndre Przywara }
511*c0937300SAndre Przywara
rb_left_deepest_node(const struct rb_node * node)512*c0937300SAndre Przywara static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
513*c0937300SAndre Przywara {
514*c0937300SAndre Przywara for (;;) {
515*c0937300SAndre Przywara if (node->rb_left)
516*c0937300SAndre Przywara node = node->rb_left;
517*c0937300SAndre Przywara else if (node->rb_right)
518*c0937300SAndre Przywara node = node->rb_right;
519*c0937300SAndre Przywara else
520*c0937300SAndre Przywara return (struct rb_node *)node;
521*c0937300SAndre Przywara }
522*c0937300SAndre Przywara }
523*c0937300SAndre Przywara
rb_next_postorder(const struct rb_node * node)524*c0937300SAndre Przywara struct rb_node *rb_next_postorder(const struct rb_node *node)
525*c0937300SAndre Przywara {
526*c0937300SAndre Przywara const struct rb_node *parent;
527*c0937300SAndre Przywara if (!node)
528*c0937300SAndre Przywara return NULL;
529*c0937300SAndre Przywara parent = rb_parent(node);
530*c0937300SAndre Przywara
531*c0937300SAndre Przywara /* If we're sitting on node, we've already seen our children */
532*c0937300SAndre Przywara if (parent && node == parent->rb_left && parent->rb_right) {
533*c0937300SAndre Przywara /* If we are the parent's left node, go to the parent's right
534*c0937300SAndre Przywara * node then all the way down to the left */
535*c0937300SAndre Przywara return rb_left_deepest_node(parent->rb_right);
536*c0937300SAndre Przywara } else
537*c0937300SAndre Przywara /* Otherwise we are the parent's right node, and the parent
538*c0937300SAndre Przywara * should be next */
539*c0937300SAndre Przywara return (struct rb_node *)parent;
540*c0937300SAndre Przywara }
541*c0937300SAndre Przywara
rb_first_postorder(const struct rb_root * root)542*c0937300SAndre Przywara struct rb_node *rb_first_postorder(const struct rb_root *root)
543*c0937300SAndre Przywara {
544*c0937300SAndre Przywara if (!root->rb_node)
545*c0937300SAndre Przywara return NULL;
546*c0937300SAndre Przywara
547*c0937300SAndre Przywara return rb_left_deepest_node(root->rb_node);
548*c0937300SAndre Przywara }
549