blob: f4dc219bd98fae1505afa4b841215087ce7927fd [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>
Harald Weltee08da972017-11-13 01:00:26 +09005
6 SPDX-License-Identifier: GPL-2.0+
7
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +02008 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +020018 linux/lib/rbtree.c
19*/
20
21#include <osmocom/core/linuxrbtree.h>
22
23static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
24{
25 struct rb_node *right = node->rb_right;
26 struct rb_node *parent = rb_parent(node);
27
28 if ((node->rb_right = right->rb_left))
29 rb_set_parent(right->rb_left, node);
30 right->rb_left = node;
31
32 rb_set_parent(right, parent);
33
34 if (parent)
35 {
36 if (node == parent->rb_left)
37 parent->rb_left = right;
38 else
39 parent->rb_right = right;
40 }
41 else
42 root->rb_node = right;
43 rb_set_parent(node, right);
44}
45
46static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
47{
48 struct rb_node *left = node->rb_left;
49 struct rb_node *parent = rb_parent(node);
50
51 if ((node->rb_left = left->rb_right))
52 rb_set_parent(left->rb_right, node);
53 left->rb_right = node;
54
55 rb_set_parent(left, parent);
56
57 if (parent)
58 {
59 if (node == parent->rb_right)
60 parent->rb_right = left;
61 else
62 parent->rb_left = left;
63 }
64 else
65 root->rb_node = left;
66 rb_set_parent(node, left);
67}
68
69void rb_insert_color(struct rb_node *node, struct rb_root *root)
70{
71 struct rb_node *parent, *gparent;
72
73 while ((parent = rb_parent(node)) && rb_is_red(parent))
74 {
75 gparent = rb_parent(parent);
76
77 if (parent == gparent->rb_left)
78 {
79 {
80 register struct rb_node *uncle = gparent->rb_right;
81 if (uncle && rb_is_red(uncle))
82 {
83 rb_set_black(uncle);
84 rb_set_black(parent);
85 rb_set_red(gparent);
86 node = gparent;
87 continue;
88 }
89 }
90
91 if (parent->rb_right == node)
92 {
93 register struct rb_node *tmp;
94 __rb_rotate_left(parent, root);
95 tmp = parent;
96 parent = node;
97 node = tmp;
98 }
99
100 rb_set_black(parent);
101 rb_set_red(gparent);
102 __rb_rotate_right(gparent, root);
103 } else {
104 {
105 register struct rb_node *uncle = gparent->rb_left;
106 if (uncle && rb_is_red(uncle))
107 {
108 rb_set_black(uncle);
109 rb_set_black(parent);
110 rb_set_red(gparent);
111 node = gparent;
112 continue;
113 }
114 }
115
116 if (parent->rb_left == node)
117 {
118 register struct rb_node *tmp;
119 __rb_rotate_right(parent, root);
120 tmp = parent;
121 parent = node;
122 node = tmp;
123 }
124
125 rb_set_black(parent);
126 rb_set_red(gparent);
127 __rb_rotate_left(gparent, root);
128 }
129 }
130
131 rb_set_black(root->rb_node);
132}
133
134static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
135 struct rb_root *root)
136{
137 struct rb_node *other;
138
139 while ((!node || rb_is_black(node)) && node != root->rb_node)
140 {
141 if (parent->rb_left == node)
142 {
143 other = parent->rb_right;
144 if (rb_is_red(other))
145 {
146 rb_set_black(other);
147 rb_set_red(parent);
148 __rb_rotate_left(parent, root);
149 other = parent->rb_right;
150 }
151 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
152 (!other->rb_right || rb_is_black(other->rb_right)))
153 {
154 rb_set_red(other);
155 node = parent;
156 parent = rb_parent(node);
157 }
158 else
159 {
160 if (!other->rb_right || rb_is_black(other->rb_right))
161 {
Sylvain Munaut0395c6e2011-11-11 15:25:25 +0100162 rb_set_black(other->rb_left);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200163 rb_set_red(other);
164 __rb_rotate_right(other, root);
165 other = parent->rb_right;
166 }
167 rb_set_color(other, rb_color(parent));
168 rb_set_black(parent);
Sylvain Munaut0395c6e2011-11-11 15:25:25 +0100169 rb_set_black(other->rb_right);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200170 __rb_rotate_left(parent, root);
171 node = root->rb_node;
172 break;
173 }
174 }
175 else
176 {
177 other = parent->rb_left;
178 if (rb_is_red(other))
179 {
180 rb_set_black(other);
181 rb_set_red(parent);
182 __rb_rotate_right(parent, root);
183 other = parent->rb_left;
184 }
185 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
186 (!other->rb_right || rb_is_black(other->rb_right)))
187 {
188 rb_set_red(other);
189 node = parent;
190 parent = rb_parent(node);
191 }
192 else
193 {
194 if (!other->rb_left || rb_is_black(other->rb_left))
195 {
Sylvain Munaut0395c6e2011-11-11 15:25:25 +0100196 rb_set_black(other->rb_right);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200197 rb_set_red(other);
198 __rb_rotate_left(other, root);
199 other = parent->rb_left;
200 }
201 rb_set_color(other, rb_color(parent));
202 rb_set_black(parent);
Sylvain Munaut0395c6e2011-11-11 15:25:25 +0100203 rb_set_black(other->rb_left);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200204 __rb_rotate_right(parent, root);
205 node = root->rb_node;
206 break;
207 }
208 }
209 }
210 if (node)
211 rb_set_black(node);
212}
213
214void rb_erase(struct rb_node *node, struct rb_root *root)
215{
216 struct rb_node *child, *parent;
217 int color;
218
219 if (!node->rb_left)
220 child = node->rb_right;
221 else if (!node->rb_right)
222 child = node->rb_left;
223 else
224 {
225 struct rb_node *old = node, *left;
226
227 node = node->rb_right;
228 while ((left = node->rb_left) != NULL)
229 node = left;
Sylvain Munautb8c5bde2011-11-11 15:28:01 +0100230
231 if (rb_parent(old)) {
232 if (rb_parent(old)->rb_left == old)
233 rb_parent(old)->rb_left = node;
234 else
235 rb_parent(old)->rb_right = node;
236 } else
237 root->rb_node = node;
238
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200239 child = node->rb_right;
240 parent = rb_parent(node);
241 color = rb_color(node);
242
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200243 if (parent == old) {
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200244 parent = node;
Sylvain Munauta83cbe12011-11-11 15:29:59 +0100245 } else {
246 if (child)
247 rb_set_parent(child, parent);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200248 parent->rb_left = child;
Sylvain Munaut54187ba2011-11-11 15:31:01 +0100249
250 node->rb_right = old->rb_right;
251 rb_set_parent(old->rb_right, node);
Sylvain Munauta83cbe12011-11-11 15:29:59 +0100252 }
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200253
254 node->rb_parent_color = old->rb_parent_color;
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200255 node->rb_left = old->rb_left;
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200256 rb_set_parent(old->rb_left, node);
Sylvain Munaut54187ba2011-11-11 15:31:01 +0100257
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200258 goto color;
259 }
260
261 parent = rb_parent(node);
262 color = rb_color(node);
263
264 if (child)
265 rb_set_parent(child, parent);
266 if (parent)
267 {
268 if (parent->rb_left == node)
269 parent->rb_left = child;
270 else
271 parent->rb_right = child;
272 }
273 else
274 root->rb_node = child;
275
276 color:
277 if (color == RB_BLACK)
278 __rb_erase_color(child, parent, root);
279}
280
281/*
282 * This function returns the first node (in sort order) of the tree.
283 */
Sylvain Munaut2179f402011-11-11 15:32:57 +0100284struct rb_node *rb_first(const struct rb_root *root)
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200285{
286 struct rb_node *n;
287
288 n = root->rb_node;
289 if (!n)
290 return NULL;
291 while (n->rb_left)
292 n = n->rb_left;
293 return n;
294}
295
Sylvain Munaut2179f402011-11-11 15:32:57 +0100296struct rb_node *rb_last(const struct rb_root *root)
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200297{
298 struct rb_node *n;
299
300 n = root->rb_node;
301 if (!n)
302 return NULL;
303 while (n->rb_right)
304 n = n->rb_right;
305 return n;
306}
307
Sylvain Munaut2179f402011-11-11 15:32:57 +0100308struct rb_node *rb_next(const struct rb_node *node)
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200309{
310 struct rb_node *parent;
311
312 if (rb_parent(node) == node)
313 return NULL;
314
315 /* If we have a right-hand child, go down and then left as far
316 as we can. */
317 if (node->rb_right) {
318 node = node->rb_right;
319 while (node->rb_left)
320 node=node->rb_left;
Sylvain Munaut2179f402011-11-11 15:32:57 +0100321 return (struct rb_node *)node;
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200322 }
323
324 /* No right-hand children. Everything down and left is
325 smaller than us, so any 'next' node must be in the general
326 direction of our parent. Go up the tree; any time the
327 ancestor is a right-hand child of its parent, keep going
328 up. First time it's a left-hand child of its parent, said
329 parent is our 'next' node. */
330 while ((parent = rb_parent(node)) && node == parent->rb_right)
331 node = parent;
332
333 return parent;
334}
335
Sylvain Munaut2179f402011-11-11 15:32:57 +0100336struct rb_node *rb_prev(const struct rb_node *node)
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200337{
338 struct rb_node *parent;
339
340 if (rb_parent(node) == node)
341 return NULL;
342
343 /* If we have a left-hand child, go down and then right as far
344 as we can. */
345 if (node->rb_left) {
346 node = node->rb_left;
347 while (node->rb_right)
348 node=node->rb_right;
Sylvain Munaut2179f402011-11-11 15:32:57 +0100349 return (struct rb_node *)node;
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200350 }
351
352 /* No left-hand children. Go up till we find an ancestor which
353 is a right-hand child of its parent */
354 while ((parent = rb_parent(node)) && node == parent->rb_left)
355 node = parent;
356
357 return parent;
358}
359
360void rb_replace_node(struct rb_node *victim, struct rb_node *new,
361 struct rb_root *root)
362{
363 struct rb_node *parent = rb_parent(victim);
364
365 /* Set the surrounding nodes to point to the replacement */
366 if (parent) {
367 if (victim == parent->rb_left)
368 parent->rb_left = new;
369 else
370 parent->rb_right = new;
371 } else {
372 root->rb_node = new;
373 }
374 if (victim->rb_left)
375 rb_set_parent(victim->rb_left, new);
376 if (victim->rb_right)
377 rb_set_parent(victim->rb_right, new);
378
379 /* Copy the pointers/colour from the victim to the replacement */
380 *new = *victim;
381}