3 (C) 1999 Andrea Arcangeli <andrea@suse.de>
4 (C) 2002 David Woodhouse <dwmw2@infradead.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
20 static void __rb_rotate_left(struct rb_node
*node
, struct rb_root
*root
)
22 struct rb_node
*right
= node
->rb_right
;
23 struct rb_node
*parent
= rb_parent(node
);
25 if ((node
->rb_right
= right
->rb_left
))
26 rb_set_parent(right
->rb_left
, node
);
27 right
->rb_left
= node
;
29 rb_set_parent(right
, parent
);
33 if (node
== parent
->rb_left
)
34 parent
->rb_left
= right
;
36 parent
->rb_right
= right
;
39 root
->rb_node
= right
;
40 rb_set_parent(node
, right
);
43 static void __rb_rotate_right(struct rb_node
*node
, struct rb_root
*root
)
45 struct rb_node
*left
= node
->rb_left
;
46 struct rb_node
*parent
= rb_parent(node
);
48 if ((node
->rb_left
= left
->rb_right
))
49 rb_set_parent(left
->rb_right
, node
);
50 left
->rb_right
= node
;
52 rb_set_parent(left
, parent
);
56 if (node
== parent
->rb_right
)
57 parent
->rb_right
= left
;
59 parent
->rb_left
= left
;
63 rb_set_parent(node
, left
);
66 void rb_insert_color(struct rb_node
*node
, struct rb_root
*root
)
68 struct rb_node
*parent
, *gparent
;
70 while ((parent
= rb_parent(node
)) && rb_is_red(parent
))
72 gparent
= rb_parent(parent
);
74 if (parent
== gparent
->rb_left
)
77 register struct rb_node
*uncle
= gparent
->rb_right
;
78 if (uncle
&& rb_is_red(uncle
))
88 if (parent
->rb_right
== node
)
90 register struct rb_node
*tmp
;
91 __rb_rotate_left(parent
, root
);
99 __rb_rotate_right(gparent
, root
);
102 register struct rb_node
*uncle
= gparent
->rb_left
;
103 if (uncle
&& rb_is_red(uncle
))
106 rb_set_black(parent
);
113 if (parent
->rb_left
== node
)
115 register struct rb_node
*tmp
;
116 __rb_rotate_right(parent
, root
);
122 rb_set_black(parent
);
124 __rb_rotate_left(gparent
, root
);
128 rb_set_black(root
->rb_node
);
131 static void __rb_erase_color(struct rb_node
*node
, struct rb_node
*parent
,
132 struct rb_root
*root
)
134 struct rb_node
*other
;
136 while ((!node
|| rb_is_black(node
)) && node
!= root
->rb_node
)
138 if (parent
->rb_left
== node
)
140 other
= parent
->rb_right
;
141 if (rb_is_red(other
))
145 __rb_rotate_left(parent
, root
);
146 other
= parent
->rb_right
;
148 if ((!other
->rb_left
|| rb_is_black(other
->rb_left
)) &&
149 (!other
->rb_right
|| rb_is_black(other
->rb_right
)))
153 parent
= rb_parent(node
);
157 if (!other
->rb_right
|| rb_is_black(other
->rb_right
))
159 struct rb_node
*o_left
;
160 if ((o_left
= other
->rb_left
))
161 rb_set_black(o_left
);
163 __rb_rotate_right(other
, root
);
164 other
= parent
->rb_right
;
166 rb_set_color(other
, rb_color(parent
));
167 rb_set_black(parent
);
169 rb_set_black(other
->rb_right
);
170 __rb_rotate_left(parent
, root
);
171 node
= root
->rb_node
;
177 other
= parent
->rb_left
;
178 if (rb_is_red(other
))
182 __rb_rotate_right(parent
, root
);
183 other
= parent
->rb_left
;
185 if ((!other
->rb_left
|| rb_is_black(other
->rb_left
)) &&
186 (!other
->rb_right
|| rb_is_black(other
->rb_right
)))
190 parent
= rb_parent(node
);
194 if (!other
->rb_left
|| rb_is_black(other
->rb_left
))
196 register struct rb_node
*o_right
;
197 if ((o_right
= other
->rb_right
))
198 rb_set_black(o_right
);
200 __rb_rotate_left(other
, root
);
201 other
= parent
->rb_left
;
203 rb_set_color(other
, rb_color(parent
));
204 rb_set_black(parent
);
206 rb_set_black(other
->rb_left
);
207 __rb_rotate_right(parent
, root
);
208 node
= root
->rb_node
;
217 void rb_erase(struct rb_node
*node
, struct rb_root
*root
)
219 struct rb_node
*child
, *parent
;
223 child
= node
->rb_right
;
224 else if (!node
->rb_right
)
225 child
= node
->rb_left
;
228 struct rb_node
*old
= node
, *left
;
230 node
= node
->rb_right
;
231 while ((left
= node
->rb_left
) != NULL
)
233 child
= node
->rb_right
;
234 parent
= rb_parent(node
);
235 color
= rb_color(node
);
238 rb_set_parent(child
, parent
);
240 parent
->rb_right
= child
;
243 parent
->rb_left
= child
;
245 node
->rb_parent_color
= old
->rb_parent_color
;
246 node
->rb_right
= old
->rb_right
;
247 node
->rb_left
= old
->rb_left
;
251 if (rb_parent(old
)->rb_left
== old
)
252 rb_parent(old
)->rb_left
= node
;
254 rb_parent(old
)->rb_right
= node
;
256 root
->rb_node
= node
;
258 rb_set_parent(old
->rb_left
, node
);
260 rb_set_parent(old
->rb_right
, node
);
264 parent
= rb_parent(node
);
265 color
= rb_color(node
);
268 rb_set_parent(child
, parent
);
271 if (parent
->rb_left
== node
)
272 parent
->rb_left
= child
;
274 parent
->rb_right
= child
;
277 root
->rb_node
= child
;
280 if (color
== RB_BLACK
)
281 __rb_erase_color(child
, parent
, root
);
285 * This function returns the first node (in sort order) of the tree.
287 struct rb_node
*rb_first(struct rb_root
*root
)
299 struct rb_node
*rb_last(struct rb_root
*root
)
311 struct rb_node
*rb_next(struct rb_node
*node
)
313 struct rb_node
*parent
;
315 if (rb_parent(node
) == node
)
318 /* If we have a right-hand child, go down and then left as far
320 if (node
->rb_right
) {
321 node
= node
->rb_right
;
322 while (node
->rb_left
)
327 /* No right-hand children. Everything down and left is
328 smaller than us, so any 'next' node must be in the general
329 direction of our parent. Go up the tree; any time the
330 ancestor is a right-hand child of its parent, keep going
331 up. First time it's a left-hand child of its parent, said
332 parent is our 'next' node. */
333 while ((parent
= rb_parent(node
)) && node
== parent
->rb_right
)
339 struct rb_node
*rb_prev(struct rb_node
*node
)
341 struct rb_node
*parent
;
343 if (rb_parent(node
) == node
)
346 /* If we have a left-hand child, go down and then right as far
349 node
= node
->rb_left
;
350 while (node
->rb_right
)
355 /* No left-hand children. Go up till we find an ancestor which
356 is a right-hand child of its parent */
357 while ((parent
= rb_parent(node
)) && node
== parent
->rb_left
)
363 void rb_replace_node(struct rb_node
*victim
, struct rb_node
*newp
,
364 struct rb_root
*root
)
366 struct rb_node
*parent
= rb_parent(victim
);
368 /* Set the surrounding nodes to point to the replacement */
370 if (victim
== parent
->rb_left
)
371 parent
->rb_left
= newp
;
373 parent
->rb_right
= newp
;
375 root
->rb_node
= newp
;
378 rb_set_parent(victim
->rb_left
, newp
);
379 if (victim
->rb_right
)
380 rb_set_parent(victim
->rb_right
, newp
);
382 /* Copy the pointers/colour from the victim to the replacement */