blob: f0ebb8c5f7b757c6fca182987fc866199d1f333e [file] [log] [blame]
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +02001/*
2 Red Black Trees
3 (C) 1999 Andrea Arcangeli <andrea@suse.de>
4 (C) 2002 David Woodhouse <dwmw2@infradead.org>
5
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.
10
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.
15
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
Jaroslav Škarvada2b82c1c2015-11-11 16:02:54 +010018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 MA 02110-1301, USA
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +020020
21 linux/lib/rbtree.c
22*/
23
24#include <osmocom/core/linuxrbtree.h>
25
26static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
27{
28 struct rb_node *right = node->rb_right;
29 struct rb_node *parent = rb_parent(node);
30
31 if ((node->rb_right = right->rb_left))
32 rb_set_parent(right->rb_left, node);
33 right->rb_left = node;
34
35 rb_set_parent(right, parent);
36
37 if (parent)
38 {
39 if (node == parent->rb_left)
40 parent->rb_left = right;
41 else
42 parent->rb_right = right;
43 }
44 else
45 root->rb_node = right;
46 rb_set_parent(node, right);
47}
48
49static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
50{
51 struct rb_node *left = node->rb_left;
52 struct rb_node *parent = rb_parent(node);
53
54 if ((node->rb_left = left->rb_right))
55 rb_set_parent(left->rb_right, node);
56 left->rb_right = node;
57
58 rb_set_parent(left, parent);
59
60 if (parent)
61 {
62 if (node == parent->rb_right)
63 parent->rb_right = left;
64 else
65 parent->rb_left = left;
66 }
67 else
68 root->rb_node = left;
69 rb_set_parent(node, left);
70}
71
72void rb_insert_color(struct rb_node *node, struct rb_root *root)
73{
74 struct rb_node *parent, *gparent;
75
76 while ((parent = rb_parent(node)) && rb_is_red(parent))
77 {
78 gparent = rb_parent(parent);
79
80 if (parent == gparent->rb_left)
81 {
82 {
83 register struct rb_node *uncle = gparent->rb_right;
84 if (uncle && rb_is_red(uncle))
85 {
86 rb_set_black(uncle);
87 rb_set_black(parent);
88 rb_set_red(gparent);
89 node = gparent;
90 continue;
91 }
92 }
93
94 if (parent->rb_right == node)
95 {
96 register struct rb_node *tmp;
97 __rb_rotate_left(parent, root);
98 tmp = parent;
99 parent = node;
100 node = tmp;
101 }
102
103 rb_set_black(parent);
104 rb_set_red(gparent);
105 __rb_rotate_right(gparent, root);
106 } else {
107 {
108 register struct rb_node *uncle = gparent->rb_left;
109 if (uncle && rb_is_red(uncle))
110 {
111 rb_set_black(uncle);
112 rb_set_black(parent);
113 rb_set_red(gparent);
114 node = gparent;
115 continue;
116 }
117 }
118
119 if (parent->rb_left == node)
120 {
121 register struct rb_node *tmp;
122 __rb_rotate_right(parent, root);
123 tmp = parent;
124 parent = node;
125 node = tmp;
126 }
127
128 rb_set_black(parent);
129 rb_set_red(gparent);
130 __rb_rotate_left(gparent, root);
131 }
132 }
133
134 rb_set_black(root->rb_node);
135}
136
137static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
138 struct rb_root *root)
139{
140 struct rb_node *other;
141
142 while ((!node || rb_is_black(node)) && node != root->rb_node)
143 {
144 if (parent->rb_left == node)
145 {
146 other = parent->rb_right;
147 if (rb_is_red(other))
148 {
149 rb_set_black(other);
150 rb_set_red(parent);
151 __rb_rotate_left(parent, root);
152 other = parent->rb_right;
153 }
154 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
155 (!other->rb_right || rb_is_black(other->rb_right)))
156 {
157 rb_set_red(other);
158 node = parent;
159 parent = rb_parent(node);
160 }
161 else
162 {
163 if (!other->rb_right || rb_is_black(other->rb_right))
164 {
Sylvain Munaut0395c6e2011-11-11 15:25:25 +0100165 rb_set_black(other->rb_left);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200166 rb_set_red(other);
167 __rb_rotate_right(other, root);
168 other = parent->rb_right;
169 }
170 rb_set_color(other, rb_color(parent));
171 rb_set_black(parent);
Sylvain Munaut0395c6e2011-11-11 15:25:25 +0100172 rb_set_black(other->rb_right);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200173 __rb_rotate_left(parent, root);
174 node = root->rb_node;
175 break;
176 }
177 }
178 else
179 {
180 other = parent->rb_left;
181 if (rb_is_red(other))
182 {
183 rb_set_black(other);
184 rb_set_red(parent);
185 __rb_rotate_right(parent, root);
186 other = parent->rb_left;
187 }
188 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
189 (!other->rb_right || rb_is_black(other->rb_right)))
190 {
191 rb_set_red(other);
192 node = parent;
193 parent = rb_parent(node);
194 }
195 else
196 {
197 if (!other->rb_left || rb_is_black(other->rb_left))
198 {
Sylvain Munaut0395c6e2011-11-11 15:25:25 +0100199 rb_set_black(other->rb_right);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200200 rb_set_red(other);
201 __rb_rotate_left(other, root);
202 other = parent->rb_left;
203 }
204 rb_set_color(other, rb_color(parent));
205 rb_set_black(parent);
Sylvain Munaut0395c6e2011-11-11 15:25:25 +0100206 rb_set_black(other->rb_left);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200207 __rb_rotate_right(parent, root);
208 node = root->rb_node;
209 break;
210 }
211 }
212 }
213 if (node)
214 rb_set_black(node);
215}
216
217void rb_erase(struct rb_node *node, struct rb_root *root)
218{
219 struct rb_node *child, *parent;
220 int color;
221
222 if (!node->rb_left)
223 child = node->rb_right;
224 else if (!node->rb_right)
225 child = node->rb_left;
226 else
227 {
228 struct rb_node *old = node, *left;
229
230 node = node->rb_right;
231 while ((left = node->rb_left) != NULL)
232 node = left;
Sylvain Munautb8c5bde2011-11-11 15:28:01 +0100233
234 if (rb_parent(old)) {
235 if (rb_parent(old)->rb_left == old)
236 rb_parent(old)->rb_left = node;
237 else
238 rb_parent(old)->rb_right = node;
239 } else
240 root->rb_node = node;
241
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200242 child = node->rb_right;
243 parent = rb_parent(node);
244 color = rb_color(node);
245
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200246 if (parent == old) {
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200247 parent = node;
Sylvain Munauta83cbe12011-11-11 15:29:59 +0100248 } else {
249 if (child)
250 rb_set_parent(child, parent);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200251 parent->rb_left = child;
Sylvain Munaut54187ba2011-11-11 15:31:01 +0100252
253 node->rb_right = old->rb_right;
254 rb_set_parent(old->rb_right, node);
Sylvain Munauta83cbe12011-11-11 15:29:59 +0100255 }
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200256
257 node->rb_parent_color = old->rb_parent_color;
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200258 node->rb_left = old->rb_left;
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200259 rb_set_parent(old->rb_left, node);
Sylvain Munaut54187ba2011-11-11 15:31:01 +0100260
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200261 goto color;
262 }
263
264 parent = rb_parent(node);
265 color = rb_color(node);
266
267 if (child)
268 rb_set_parent(child, parent);
269 if (parent)
270 {
271 if (parent->rb_left == node)
272 parent->rb_left = child;
273 else
274 parent->rb_right = child;
275 }
276 else
277 root->rb_node = child;
278
279 color:
280 if (color == RB_BLACK)
281 __rb_erase_color(child, parent, root);
282}
283
284/*
285 * This function returns the first node (in sort order) of the tree.
286 */
Sylvain Munaut2179f402011-11-11 15:32:57 +0100287struct rb_node *rb_first(const struct rb_root *root)
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200288{
289 struct rb_node *n;
290
291 n = root->rb_node;
292 if (!n)
293 return NULL;
294 while (n->rb_left)
295 n = n->rb_left;
296 return n;
297}
298
Sylvain Munaut2179f402011-11-11 15:32:57 +0100299struct rb_node *rb_last(const struct rb_root *root)
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200300{
301 struct rb_node *n;
302
303 n = root->rb_node;
304 if (!n)
305 return NULL;
306 while (n->rb_right)
307 n = n->rb_right;
308 return n;
309}
310
Sylvain Munaut2179f402011-11-11 15:32:57 +0100311struct rb_node *rb_next(const struct rb_node *node)
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200312{
313 struct rb_node *parent;
314
315 if (rb_parent(node) == node)
316 return NULL;
317
318 /* If we have a right-hand child, go down and then left as far
319 as we can. */
320 if (node->rb_right) {
321 node = node->rb_right;
322 while (node->rb_left)
323 node=node->rb_left;
Sylvain Munaut2179f402011-11-11 15:32:57 +0100324 return (struct rb_node *)node;
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200325 }
326
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)
334 node = parent;
335
336 return parent;
337}
338
Sylvain Munaut2179f402011-11-11 15:32:57 +0100339struct rb_node *rb_prev(const struct rb_node *node)
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200340{
341 struct rb_node *parent;
342
343 if (rb_parent(node) == node)
344 return NULL;
345
346 /* If we have a left-hand child, go down and then right as far
347 as we can. */
348 if (node->rb_left) {
349 node = node->rb_left;
350 while (node->rb_right)
351 node=node->rb_right;
Sylvain Munaut2179f402011-11-11 15:32:57 +0100352 return (struct rb_node *)node;
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200353 }
354
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)
358 node = parent;
359
360 return parent;
361}
362
363void rb_replace_node(struct rb_node *victim, struct rb_node *new,
364 struct rb_root *root)
365{
366 struct rb_node *parent = rb_parent(victim);
367
368 /* Set the surrounding nodes to point to the replacement */
369 if (parent) {
370 if (victim == parent->rb_left)
371 parent->rb_left = new;
372 else
373 parent->rb_right = new;
374 } else {
375 root->rb_node = new;
376 }
377 if (victim->rb_left)
378 rb_set_parent(victim->rb_left, new);
379 if (victim->rb_right)
380 rb_set_parent(victim->rb_right, new);
381
382 /* Copy the pointers/colour from the victim to the replacement */
383 *new = *victim;
384}