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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
30 #define rb_color(r) ((r)->rb_parent_color & 1)
31 #define rb_is_red(r) (!rb_color(r))
32 #define rb_is_black(r) rb_color(r)
33 #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
34 #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
36 static void rb_set_parent(struct rb_node
*rb
, struct rb_node
*p
)
38 rb
->rb_parent_color
= (rb
->rb_parent_color
& 3) | (unsigned long)p
;
40 static void rb_set_color(struct rb_node
*rb
, int color
)
42 rb
->rb_parent_color
= (rb
->rb_parent_color
& ~1) | color
;
45 #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
46 #define RB_EMPTY_NODE(node) (rb_parent(node) == node)
47 #define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
49 static void __rb_rotate_left(struct rb_node
*node
, struct rb_root
*root
)
51 struct rb_node
*right
= node
->rb_right
;
52 struct rb_node
*parent
= rb_parent(node
);
54 if ((node
->rb_right
= right
->rb_left
))
55 rb_set_parent(right
->rb_left
, node
);
56 right
->rb_left
= node
;
58 rb_set_parent(right
, parent
);
62 if (node
== parent
->rb_left
)
63 parent
->rb_left
= right
;
65 parent
->rb_right
= right
;
68 root
->rb_node
= right
;
69 rb_set_parent(node
, right
);
72 static void __rb_rotate_right(struct rb_node
*node
, struct rb_root
*root
)
74 struct rb_node
*left
= node
->rb_left
;
75 struct rb_node
*parent
= rb_parent(node
);
77 if ((node
->rb_left
= left
->rb_right
))
78 rb_set_parent(left
->rb_right
, node
);
79 left
->rb_right
= node
;
81 rb_set_parent(left
, parent
);
85 if (node
== parent
->rb_right
)
86 parent
->rb_right
= left
;
88 parent
->rb_left
= left
;
92 rb_set_parent(node
, left
);
95 void rb_insert_color(struct rb_node
*node
, struct rb_root
*root
)
97 struct rb_node
*parent
, *gparent
;
99 while ((parent
= rb_parent(node
)) && rb_is_red(parent
))
101 gparent
= rb_parent(parent
);
103 if (parent
== gparent
->rb_left
)
106 register struct rb_node
*uncle
= gparent
->rb_right
;
107 if (uncle
&& rb_is_red(uncle
))
110 rb_set_black(parent
);
117 if (parent
->rb_right
== node
)
119 register struct rb_node
*tmp
;
120 __rb_rotate_left(parent
, root
);
126 rb_set_black(parent
);
128 __rb_rotate_right(gparent
, root
);
131 register struct rb_node
*uncle
= gparent
->rb_left
;
132 if (uncle
&& rb_is_red(uncle
))
135 rb_set_black(parent
);
142 if (parent
->rb_left
== node
)
144 register struct rb_node
*tmp
;
145 __rb_rotate_right(parent
, root
);
151 rb_set_black(parent
);
153 __rb_rotate_left(gparent
, root
);
157 rb_set_black(root
->rb_node
);
160 static void __rb_erase_color(struct rb_node
*node
, struct rb_node
*parent
,
161 struct rb_root
*root
)
163 struct rb_node
*other
;
165 while ((!node
|| rb_is_black(node
)) && node
!= root
->rb_node
)
167 if (parent
->rb_left
== node
)
169 other
= parent
->rb_right
;
170 if (rb_is_red(other
))
174 __rb_rotate_left(parent
, root
);
175 other
= parent
->rb_right
;
177 if ((!other
->rb_left
|| rb_is_black(other
->rb_left
)) &&
178 (!other
->rb_right
|| rb_is_black(other
->rb_right
)))
182 parent
= rb_parent(node
);
186 if (!other
->rb_right
|| rb_is_black(other
->rb_right
))
188 struct rb_node
*o_left
;
189 if ((o_left
= other
->rb_left
))
190 rb_set_black(o_left
);
192 __rb_rotate_right(other
, root
);
193 other
= parent
->rb_right
;
195 rb_set_color(other
, rb_color(parent
));
196 rb_set_black(parent
);
198 rb_set_black(other
->rb_right
);
199 __rb_rotate_left(parent
, root
);
200 node
= root
->rb_node
;
206 other
= parent
->rb_left
;
207 if (rb_is_red(other
))
211 __rb_rotate_right(parent
, root
);
212 other
= parent
->rb_left
;
214 if ((!other
->rb_left
|| rb_is_black(other
->rb_left
)) &&
215 (!other
->rb_right
|| rb_is_black(other
->rb_right
)))
219 parent
= rb_parent(node
);
223 if (!other
->rb_left
|| rb_is_black(other
->rb_left
))
225 register struct rb_node
*o_right
;
226 if ((o_right
= other
->rb_right
))
227 rb_set_black(o_right
);
229 __rb_rotate_left(other
, root
);
230 other
= parent
->rb_left
;
232 rb_set_color(other
, rb_color(parent
));
233 rb_set_black(parent
);
235 rb_set_black(other
->rb_left
);
236 __rb_rotate_right(parent
, root
);
237 node
= root
->rb_node
;
246 void rb_erase(struct rb_node
*node
, struct rb_root
*root
)
248 struct rb_node
*child
, *parent
;
252 child
= node
->rb_right
;
253 else if (!node
->rb_right
)
254 child
= node
->rb_left
;
257 struct rb_node
*old
= node
, *left
;
259 node
= node
->rb_right
;
260 while ((left
= node
->rb_left
) != NULL
)
262 child
= node
->rb_right
;
263 parent
= rb_parent(node
);
264 color
= rb_color(node
);
267 rb_set_parent(child
, parent
);
269 parent
->rb_right
= child
;
272 parent
->rb_left
= child
;
274 node
->rb_parent_color
= old
->rb_parent_color
;
275 node
->rb_right
= old
->rb_right
;
276 node
->rb_left
= old
->rb_left
;
280 if (rb_parent(old
)->rb_left
== old
)
281 rb_parent(old
)->rb_left
= node
;
283 rb_parent(old
)->rb_right
= node
;
285 root
->rb_node
= node
;
287 rb_set_parent(old
->rb_left
, node
);
289 rb_set_parent(old
->rb_right
, node
);
293 parent
= rb_parent(node
);
294 color
= rb_color(node
);
297 rb_set_parent(child
, parent
);
300 if (parent
->rb_left
== node
)
301 parent
->rb_left
= child
;
303 parent
->rb_right
= child
;
306 root
->rb_node
= child
;
309 if (color
== RB_BLACK
)
310 __rb_erase_color(child
, parent
, root
);
314 * This function returns the first node (in sort order) of the tree.
316 struct rb_node
*rb_first(struct rb_root
*root
)
328 struct rb_node
*rb_last(struct rb_root
*root
)
340 struct rb_node
*rb_next(struct rb_node
*node
)
342 struct rb_node
*parent
;
344 if (rb_parent(node
) == node
)
347 /* If we have a right-hand child, go down and then left as far
349 if (node
->rb_right
) {
350 node
= node
->rb_right
;
351 while (node
->rb_left
)
356 /* No right-hand children. Everything down and left is
357 smaller than us, so any 'next' node must be in the general
358 direction of our parent. Go up the tree; any time the
359 ancestor is a right-hand child of its parent, keep going
360 up. First time it's a left-hand child of its parent, said
361 parent is our 'next' node. */
362 while ((parent
= rb_parent(node
)) && node
== parent
->rb_right
)
368 struct rb_node
*rb_prev(struct rb_node
*node
)
370 struct rb_node
*parent
;
372 if (rb_parent(node
) == node
)
375 /* If we have a left-hand child, go down and then right as far
378 node
= node
->rb_left
;
379 while (node
->rb_right
)
384 /* No left-hand children. Go up till we find an ancestor which
385 is a right-hand child of its parent */
386 while ((parent
= rb_parent(node
)) && node
== parent
->rb_left
)
392 void rb_replace_node(struct rb_node
*victim
, struct rb_node
*new_node
,
393 struct rb_root
*root
)
395 struct rb_node
*parent
= rb_parent(victim
);
397 /* Set the surrounding nodes to point to the replacement */
399 if (victim
== parent
->rb_left
)
400 parent
->rb_left
= new_node
;
402 parent
->rb_right
= new_node
;
404 root
->rb_node
= new_node
;
407 rb_set_parent(victim
->rb_left
, new_node
);
408 if (victim
->rb_right
)
409 rb_set_parent(victim
->rb_right
, new_node
);
411 /* Copy the pointers/colour from the victim to the replacement */
415 void rb_link_node(struct rb_node
* node
, struct rb_node
* parent
,
416 struct rb_node
** rb_link
)
418 node
->rb_parent_color
= (unsigned long )parent
;
419 node
->rb_left
= node
->rb_right
= NULL
;