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
30 #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
31 #define rb_color(r) ((r)->rb_parent_color & 1)
32 #define rb_is_red(r) (!rb_color(r))
33 #define rb_is_black(r) rb_color(r)
34 #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
35 #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
37 static void rb_set_parent(struct rb_node
*rb
, struct rb_node
*p
)
39 rb
->rb_parent_color
= (rb
->rb_parent_color
& 3) | (unsigned long)p
;
41 static void rb_set_color(struct rb_node
*rb
, int color
)
43 rb
->rb_parent_color
= (rb
->rb_parent_color
& ~1) | color
;
46 #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
47 #define RB_EMPTY_NODE(node) (rb_parent(node) == node)
48 #define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
50 static void __rb_rotate_left(struct rb_node
*node
, struct rb_root
*root
)
52 struct rb_node
*right
= node
->rb_right
;
53 struct rb_node
*parent
= rb_parent(node
);
55 if ((node
->rb_right
= right
->rb_left
))
56 rb_set_parent(right
->rb_left
, node
);
57 right
->rb_left
= node
;
59 rb_set_parent(right
, parent
);
63 if (node
== parent
->rb_left
)
64 parent
->rb_left
= right
;
66 parent
->rb_right
= right
;
69 root
->rb_node
= right
;
70 rb_set_parent(node
, right
);
73 static void __rb_rotate_right(struct rb_node
*node
, struct rb_root
*root
)
75 struct rb_node
*left
= node
->rb_left
;
76 struct rb_node
*parent
= rb_parent(node
);
78 if ((node
->rb_left
= left
->rb_right
))
79 rb_set_parent(left
->rb_right
, node
);
80 left
->rb_right
= node
;
82 rb_set_parent(left
, parent
);
86 if (node
== parent
->rb_right
)
87 parent
->rb_right
= left
;
89 parent
->rb_left
= left
;
93 rb_set_parent(node
, left
);
96 void rb_insert_color(struct rb_node
*node
, struct rb_root
*root
)
98 struct rb_node
*parent
, *gparent
;
100 while ((parent
= rb_parent(node
)) && rb_is_red(parent
))
102 gparent
= rb_parent(parent
);
104 if (parent
== gparent
->rb_left
)
107 register struct rb_node
*uncle
= gparent
->rb_right
;
108 if (uncle
&& rb_is_red(uncle
))
111 rb_set_black(parent
);
118 if (parent
->rb_right
== node
)
120 register struct rb_node
*tmp
;
121 __rb_rotate_left(parent
, root
);
127 rb_set_black(parent
);
129 __rb_rotate_right(gparent
, root
);
132 register struct rb_node
*uncle
= gparent
->rb_left
;
133 if (uncle
&& rb_is_red(uncle
))
136 rb_set_black(parent
);
143 if (parent
->rb_left
== node
)
145 register struct rb_node
*tmp
;
146 __rb_rotate_right(parent
, root
);
152 rb_set_black(parent
);
154 __rb_rotate_left(gparent
, root
);
158 rb_set_black(root
->rb_node
);
161 static void __rb_erase_color(struct rb_node
*node
, struct rb_node
*parent
,
162 struct rb_root
*root
)
164 struct rb_node
*other
;
166 while ((!node
|| rb_is_black(node
)) && node
!= root
->rb_node
)
168 if (parent
->rb_left
== node
)
170 other
= parent
->rb_right
;
172 /* we should never get here */
173 smb_panic("corrupted rb tree");
174 /* satisfy static checkers */
177 if (rb_is_red(other
))
181 __rb_rotate_left(parent
, root
);
182 other
= parent
->rb_right
;
184 if ((!other
->rb_left
|| rb_is_black(other
->rb_left
)) &&
185 (!other
->rb_right
|| rb_is_black(other
->rb_right
)))
189 parent
= rb_parent(node
);
193 if (!other
->rb_right
|| rb_is_black(other
->rb_right
))
195 struct rb_node
*o_left
;
196 if ((o_left
= other
->rb_left
))
197 rb_set_black(o_left
);
199 __rb_rotate_right(other
, root
);
200 other
= parent
->rb_right
;
202 rb_set_color(other
, rb_color(parent
));
203 rb_set_black(parent
);
205 rb_set_black(other
->rb_right
);
206 __rb_rotate_left(parent
, root
);
207 node
= root
->rb_node
;
213 other
= parent
->rb_left
;
214 if (rb_is_red(other
))
218 __rb_rotate_right(parent
, root
);
219 other
= parent
->rb_left
;
221 if ((!other
->rb_left
|| rb_is_black(other
->rb_left
)) &&
222 (!other
->rb_right
|| rb_is_black(other
->rb_right
)))
226 parent
= rb_parent(node
);
230 if (!other
->rb_left
|| rb_is_black(other
->rb_left
))
232 register struct rb_node
*o_right
;
233 if ((o_right
= other
->rb_right
))
234 rb_set_black(o_right
);
236 __rb_rotate_left(other
, root
);
237 other
= parent
->rb_left
;
239 rb_set_color(other
, rb_color(parent
));
240 rb_set_black(parent
);
242 rb_set_black(other
->rb_left
);
243 __rb_rotate_right(parent
, root
);
244 node
= root
->rb_node
;
253 void rb_erase(struct rb_node
*node
, struct rb_root
*root
)
255 struct rb_node
*child
, *parent
;
259 child
= node
->rb_right
;
260 else if (!node
->rb_right
)
261 child
= node
->rb_left
;
264 struct rb_node
*old
= node
, *left
;
266 node
= node
->rb_right
;
267 while ((left
= node
->rb_left
) != NULL
)
269 child
= node
->rb_right
;
270 parent
= rb_parent(node
);
271 color
= rb_color(node
);
274 rb_set_parent(child
, parent
);
276 parent
->rb_right
= child
;
279 parent
->rb_left
= child
;
281 node
->rb_parent_color
= old
->rb_parent_color
;
282 node
->rb_right
= old
->rb_right
;
283 node
->rb_left
= old
->rb_left
;
287 if (rb_parent(old
)->rb_left
== old
)
288 rb_parent(old
)->rb_left
= node
;
290 rb_parent(old
)->rb_right
= node
;
292 root
->rb_node
= node
;
294 rb_set_parent(old
->rb_left
, node
);
296 rb_set_parent(old
->rb_right
, node
);
300 parent
= rb_parent(node
);
301 color
= rb_color(node
);
304 rb_set_parent(child
, parent
);
307 if (parent
->rb_left
== node
)
308 parent
->rb_left
= child
;
310 parent
->rb_right
= child
;
313 root
->rb_node
= child
;
316 if (color
== RB_BLACK
)
317 __rb_erase_color(child
, parent
, root
);
321 * This function returns the first node (in sort order) of the tree.
323 struct rb_node
*rb_first(struct rb_root
*root
)
335 struct rb_node
*rb_last(struct rb_root
*root
)
347 struct rb_node
*rb_next(struct rb_node
*node
)
349 struct rb_node
*parent
;
351 if (rb_parent(node
) == node
)
354 /* If we have a right-hand child, go down and then left as far
356 if (node
->rb_right
) {
357 node
= node
->rb_right
;
358 while (node
->rb_left
)
363 /* No right-hand children. Everything down and left is
364 smaller than us, so any 'next' node must be in the general
365 direction of our parent. Go up the tree; any time the
366 ancestor is a right-hand child of its parent, keep going
367 up. First time it's a left-hand child of its parent, said
368 parent is our 'next' node. */
369 while ((parent
= rb_parent(node
)) && node
== parent
->rb_right
)
375 struct rb_node
*rb_prev(struct rb_node
*node
)
377 struct rb_node
*parent
;
379 if (rb_parent(node
) == node
)
382 /* If we have a left-hand child, go down and then right as far
385 node
= node
->rb_left
;
386 while (node
->rb_right
)
391 /* No left-hand children. Go up till we find an ancestor which
392 is a right-hand child of its parent */
393 while ((parent
= rb_parent(node
)) && node
== parent
->rb_left
)
399 void rb_replace_node(struct rb_node
*victim
, struct rb_node
*new_node
,
400 struct rb_root
*root
)
402 struct rb_node
*parent
= rb_parent(victim
);
404 /* Set the surrounding nodes to point to the replacement */
406 if (victim
== parent
->rb_left
)
407 parent
->rb_left
= new_node
;
409 parent
->rb_right
= new_node
;
411 root
->rb_node
= new_node
;
414 rb_set_parent(victim
->rb_left
, new_node
);
415 if (victim
->rb_right
)
416 rb_set_parent(victim
->rb_right
, new_node
);
418 /* Copy the pointers/colour from the victim to the replacement */
422 void rb_link_node(struct rb_node
* node
, struct rb_node
* parent
,
423 struct rb_node
** rb_link
)
425 node
->rb_parent_color
= (unsigned long )parent
;
426 node
->rb_left
= node
->rb_right
= NULL
;