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
23 #include <linux/rbtree.h>
24 #include <linux/module.h>
26 static void __rb_rotate_left(struct rb_node
*node
, struct rb_root
*root
)
28 struct rb_node
*right
= node
->rb_right
;
30 if ((node
->rb_right
= right
->rb_left
))
31 right
->rb_left
->rb_parent
= node
;
32 right
->rb_left
= node
;
34 if ((right
->rb_parent
= node
->rb_parent
))
36 if (node
== node
->rb_parent
->rb_left
)
37 node
->rb_parent
->rb_left
= right
;
39 node
->rb_parent
->rb_right
= right
;
42 root
->rb_node
= right
;
43 node
->rb_parent
= right
;
46 static void __rb_rotate_right(struct rb_node
*node
, struct rb_root
*root
)
48 struct rb_node
*left
= node
->rb_left
;
50 if ((node
->rb_left
= left
->rb_right
))
51 left
->rb_right
->rb_parent
= node
;
52 left
->rb_right
= node
;
54 if ((left
->rb_parent
= node
->rb_parent
))
56 if (node
== node
->rb_parent
->rb_right
)
57 node
->rb_parent
->rb_right
= left
;
59 node
->rb_parent
->rb_left
= left
;
63 node
->rb_parent
= left
;
66 void rb_insert_color(struct rb_node
*node
, struct rb_root
*root
)
68 struct rb_node
*parent
, *gparent
;
70 while ((parent
= node
->rb_parent
) && parent
->rb_color
== RB_RED
)
72 gparent
= parent
->rb_parent
;
74 if (parent
== gparent
->rb_left
)
77 register struct rb_node
*uncle
= gparent
->rb_right
;
78 if (uncle
&& uncle
->rb_color
== RB_RED
)
80 uncle
->rb_color
= RB_BLACK
;
81 parent
->rb_color
= RB_BLACK
;
82 gparent
->rb_color
= RB_RED
;
88 if (parent
->rb_right
== node
)
90 register struct rb_node
*tmp
;
91 __rb_rotate_left(parent
, root
);
97 parent
->rb_color
= RB_BLACK
;
98 gparent
->rb_color
= RB_RED
;
99 __rb_rotate_right(gparent
, root
);
102 register struct rb_node
*uncle
= gparent
->rb_left
;
103 if (uncle
&& uncle
->rb_color
== RB_RED
)
105 uncle
->rb_color
= RB_BLACK
;
106 parent
->rb_color
= RB_BLACK
;
107 gparent
->rb_color
= RB_RED
;
113 if (parent
->rb_left
== node
)
115 register struct rb_node
*tmp
;
116 __rb_rotate_right(parent
, root
);
122 parent
->rb_color
= RB_BLACK
;
123 gparent
->rb_color
= RB_RED
;
124 __rb_rotate_left(gparent
, root
);
128 root
->rb_node
->rb_color
= RB_BLACK
;
130 EXPORT_SYMBOL(rb_insert_color
);
132 static void __rb_erase_color(struct rb_node
*node
, struct rb_node
*parent
,
133 struct rb_root
*root
)
135 struct rb_node
*other
;
137 while ((!node
|| node
->rb_color
== RB_BLACK
) && node
!= root
->rb_node
)
139 if (parent
->rb_left
== node
)
141 other
= parent
->rb_right
;
142 if (other
->rb_color
== RB_RED
)
144 other
->rb_color
= RB_BLACK
;
145 parent
->rb_color
= RB_RED
;
146 __rb_rotate_left(parent
, root
);
147 other
= parent
->rb_right
;
149 if ((!other
->rb_left
||
150 other
->rb_left
->rb_color
== RB_BLACK
)
151 && (!other
->rb_right
||
152 other
->rb_right
->rb_color
== RB_BLACK
))
154 other
->rb_color
= RB_RED
;
156 parent
= node
->rb_parent
;
160 if (!other
->rb_right
||
161 other
->rb_right
->rb_color
== RB_BLACK
)
163 register struct rb_node
*o_left
;
164 if ((o_left
= other
->rb_left
))
165 o_left
->rb_color
= RB_BLACK
;
166 other
->rb_color
= RB_RED
;
167 __rb_rotate_right(other
, root
);
168 other
= parent
->rb_right
;
170 other
->rb_color
= parent
->rb_color
;
171 parent
->rb_color
= RB_BLACK
;
173 other
->rb_right
->rb_color
= RB_BLACK
;
174 __rb_rotate_left(parent
, root
);
175 node
= root
->rb_node
;
181 other
= parent
->rb_left
;
182 if (other
->rb_color
== RB_RED
)
184 other
->rb_color
= RB_BLACK
;
185 parent
->rb_color
= RB_RED
;
186 __rb_rotate_right(parent
, root
);
187 other
= parent
->rb_left
;
189 if ((!other
->rb_left
||
190 other
->rb_left
->rb_color
== RB_BLACK
)
191 && (!other
->rb_right
||
192 other
->rb_right
->rb_color
== RB_BLACK
))
194 other
->rb_color
= RB_RED
;
196 parent
= node
->rb_parent
;
200 if (!other
->rb_left
||
201 other
->rb_left
->rb_color
== RB_BLACK
)
203 register struct rb_node
*o_right
;
204 if ((o_right
= other
->rb_right
))
205 o_right
->rb_color
= RB_BLACK
;
206 other
->rb_color
= RB_RED
;
207 __rb_rotate_left(other
, root
);
208 other
= parent
->rb_left
;
210 other
->rb_color
= parent
->rb_color
;
211 parent
->rb_color
= RB_BLACK
;
213 other
->rb_left
->rb_color
= RB_BLACK
;
214 __rb_rotate_right(parent
, root
);
215 node
= root
->rb_node
;
221 node
->rb_color
= RB_BLACK
;
224 void rb_erase(struct rb_node
*node
, struct rb_root
*root
)
226 struct rb_node
*child
, *parent
;
230 child
= node
->rb_right
;
231 else if (!node
->rb_right
)
232 child
= node
->rb_left
;
235 struct rb_node
*old
= node
, *left
;
237 node
= node
->rb_right
;
238 while ((left
= node
->rb_left
))
240 child
= node
->rb_right
;
241 parent
= node
->rb_parent
;
242 color
= node
->rb_color
;
245 child
->rb_parent
= parent
;
248 if (parent
->rb_left
== node
)
249 parent
->rb_left
= child
;
251 parent
->rb_right
= child
;
254 root
->rb_node
= child
;
256 if (node
->rb_parent
== old
)
258 node
->rb_parent
= old
->rb_parent
;
259 node
->rb_color
= old
->rb_color
;
260 node
->rb_right
= old
->rb_right
;
261 node
->rb_left
= old
->rb_left
;
265 if (old
->rb_parent
->rb_left
== old
)
266 old
->rb_parent
->rb_left
= node
;
268 old
->rb_parent
->rb_right
= node
;
270 root
->rb_node
= node
;
272 old
->rb_left
->rb_parent
= node
;
274 old
->rb_right
->rb_parent
= node
;
278 parent
= node
->rb_parent
;
279 color
= node
->rb_color
;
282 child
->rb_parent
= parent
;
285 if (parent
->rb_left
== node
)
286 parent
->rb_left
= child
;
288 parent
->rb_right
= child
;
291 root
->rb_node
= child
;
294 if (color
== RB_BLACK
)
295 __rb_erase_color(child
, parent
, root
);
297 EXPORT_SYMBOL(rb_erase
);
300 * This function returns the first node (in sort order) of the tree.
302 struct rb_node
*rb_first(struct rb_root
*root
)
313 EXPORT_SYMBOL(rb_first
);
315 struct rb_node
*rb_next(struct rb_node
*node
)
317 /* If we have a right-hand child, go down and then left as far
319 if (node
->rb_right
) {
320 node
= node
->rb_right
;
321 while (node
->rb_left
)
326 /* No right-hand children. Everything down and left is
327 smaller than us, so any 'next' node must be in the general
328 direction of our parent. Go up the tree; any time the
329 ancestor is a right-hand child of its parent, keep going
330 up. First time it's a left-hand child of its parent, said
331 parent is our 'next' node. */
332 while (node
->rb_parent
&& node
== node
->rb_parent
->rb_right
)
333 node
= node
->rb_parent
;
335 return node
->rb_parent
;
337 EXPORT_SYMBOL(rb_next
);
339 struct rb_node
*rb_prev(struct rb_node
*node
)
341 /* If we have a left-hand child, go down and then right as far
344 node
= node
->rb_left
;
345 while (node
->rb_right
)
350 /* No left-hand children. Go up till we find an ancestor which
351 is a right-hand child of its parent */
352 while (node
->rb_parent
&& node
== node
->rb_parent
->rb_left
)
353 node
= node
->rb_parent
;
355 return node
->rb_parent
;
357 EXPORT_SYMBOL(rb_prev
);
359 void rb_replace_node(struct rb_node
*victim
, struct rb_node
*new,
360 struct rb_root
*root
)
362 struct rb_node
*parent
= victim
->rb_parent
;
364 /* Set the surrounding nodes to point to the replacement */
366 if (victim
== parent
->rb_left
)
367 parent
->rb_left
= new;
369 parent
->rb_right
= new;
374 victim
->rb_left
->rb_parent
= new;
375 if (victim
->rb_right
)
376 victim
->rb_right
->rb_parent
= new;
378 /* Copy the pointers/colour from the victim to the replacement */
381 EXPORT_SYMBOL(rb_replace_node
);