blob: 211978f1ede58ad84fa367c1cb4f6f177069e771 [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
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
Jaroslav Škarvada2b82c1c2015-11-11 16:02:54 +010020 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 MA 02110-1301, USA
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +020022
23 linux/lib/rbtree.c
24*/
25
26#include <osmocom/core/linuxrbtree.h>
27
28static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
29{
30 struct rb_node *right = node->rb_right;
31 struct rb_node *parent = rb_parent(node);
32
33 if ((node->rb_right = right->rb_left))
34 rb_set_parent(right->rb_left, node);
35 right->rb_left = node;
36
37 rb_set_parent(right, parent);
38
39 if (parent)
40 {
41 if (node == parent->rb_left)
42 parent->rb_left = right;
43 else
44 parent->rb_right = right;
45 }
46 else
47 root->rb_node = right;
48 rb_set_parent(node, right);
49}
50
51static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
52{
53 struct rb_node *left = node->rb_left;
54 struct rb_node *parent = rb_parent(node);
55
56 if ((node->rb_left = left->rb_right))
57 rb_set_parent(left->rb_right, node);
58 left->rb_right = node;
59
60 rb_set_parent(left, parent);
61
62 if (parent)
63 {
64 if (node == parent->rb_right)
65 parent->rb_right = left;
66 else
67 parent->rb_left = left;
68 }
69 else
70 root->rb_node = left;
71 rb_set_parent(node, left);
72}
73
74void rb_insert_color(struct rb_node *node, struct rb_root *root)
75{
76 struct rb_node *parent, *gparent;
77
78 while ((parent = rb_parent(node)) && rb_is_red(parent))
79 {
80 gparent = rb_parent(parent);
81
82 if (parent == gparent->rb_left)
83 {
84 {
85 register struct rb_node *uncle = gparent->rb_right;
86 if (uncle && rb_is_red(uncle))
87 {
88 rb_set_black(uncle);
89 rb_set_black(parent);
90 rb_set_red(gparent);
91 node = gparent;
92 continue;
93 }
94 }
95
96 if (parent->rb_right == node)
97 {
98 register struct rb_node *tmp;
99 __rb_rotate_left(parent, root);
100 tmp = parent;
101 parent = node;
102 node = tmp;
103 }
104
105 rb_set_black(parent);
106 rb_set_red(gparent);
107 __rb_rotate_right(gparent, root);
108 } else {
109 {
110 register struct rb_node *uncle = gparent->rb_left;
111 if (uncle && rb_is_red(uncle))
112 {
113 rb_set_black(uncle);
114 rb_set_black(parent);
115 rb_set_red(gparent);
116 node = gparent;
117 continue;
118 }
119 }
120
121 if (parent->rb_left == node)
122 {
123 register struct rb_node *tmp;
124 __rb_rotate_right(parent, root);
125 tmp = parent;
126 parent = node;
127 node = tmp;
128 }
129
130 rb_set_black(parent);
131 rb_set_red(gparent);
132 __rb_rotate_left(gparent, root);
133 }
134 }
135
136 rb_set_black(root->rb_node);
137}
138
139static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
140 struct rb_root *root)
141{
142 struct rb_node *other;
143
144 while ((!node || rb_is_black(node)) && node != root->rb_node)
145 {
146 if (parent->rb_left == node)
147 {
148 other = parent->rb_right;
149 if (rb_is_red(other))
150 {
151 rb_set_black(other);
152 rb_set_red(parent);
153 __rb_rotate_left(parent, root);
154 other = parent->rb_right;
155 }
156 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
157 (!other->rb_right || rb_is_black(other->rb_right)))
158 {
159 rb_set_red(other);
160 node = parent;
161 parent = rb_parent(node);
162 }
163 else
164 {
165 if (!other->rb_right || rb_is_black(other->rb_right))
166 {
Sylvain Munaut0395c6e2011-11-11 15:25:25 +0100167 rb_set_black(other->rb_left);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200168 rb_set_red(other);
169 __rb_rotate_right(other, root);
170 other = parent->rb_right;
171 }
172 rb_set_color(other, rb_color(parent));
173 rb_set_black(parent);
Sylvain Munaut0395c6e2011-11-11 15:25:25 +0100174 rb_set_black(other->rb_right);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200175 __rb_rotate_left(parent, root);
176 node = root->rb_node;
177 break;
178 }
179 }
180 else
181 {
182 other = parent->rb_left;
183 if (rb_is_red(other))
184 {
185 rb_set_black(other);
186 rb_set_red(parent);
187 __rb_rotate_right(parent, root);
188 other = parent->rb_left;
189 }
190 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
191 (!other->rb_right || rb_is_black(other->rb_right)))
192 {
193 rb_set_red(other);
194 node = parent;
195 parent = rb_parent(node);
196 }
197 else
198 {
199 if (!other->rb_left || rb_is_black(other->rb_left))
200 {
Sylvain Munaut0395c6e2011-11-11 15:25:25 +0100201 rb_set_black(other->rb_right);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200202 rb_set_red(other);
203 __rb_rotate_left(other, root);
204 other = parent->rb_left;
205 }
206 rb_set_color(other, rb_color(parent));
207 rb_set_black(parent);
Sylvain Munaut0395c6e2011-11-11 15:25:25 +0100208 rb_set_black(other->rb_left);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200209 __rb_rotate_right(parent, root);
210 node = root->rb_node;
211 break;
212 }
213 }
214 }
215 if (node)
216 rb_set_black(node);
217}
218
219void rb_erase(struct rb_node *node, struct rb_root *root)
220{
221 struct rb_node *child, *parent;
222 int color;
223
224 if (!node->rb_left)
225 child = node->rb_right;
226 else if (!node->rb_right)
227 child = node->rb_left;
228 else
229 {
230 struct rb_node *old = node, *left;
231
232 node = node->rb_right;
233 while ((left = node->rb_left) != NULL)
234 node = left;
Sylvain Munautb8c5bde2011-11-11 15:28:01 +0100235
236 if (rb_parent(old)) {
237 if (rb_parent(old)->rb_left == old)
238 rb_parent(old)->rb_left = node;
239 else
240 rb_parent(old)->rb_right = node;
241 } else
242 root->rb_node = node;
243
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200244 child = node->rb_right;
245 parent = rb_parent(node);
246 color = rb_color(node);
247
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200248 if (parent == old) {
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200249 parent = node;
Sylvain Munauta83cbe12011-11-11 15:29:59 +0100250 } else {
251 if (child)
252 rb_set_parent(child, parent);
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200253 parent->rb_left = child;
Sylvain Munaut54187ba2011-11-11 15:31:01 +0100254
255 node->rb_right = old->rb_right;
256 rb_set_parent(old->rb_right, node);
Sylvain Munauta83cbe12011-11-11 15:29:59 +0100257 }
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200258
259 node->rb_parent_color = old->rb_parent_color;
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200260 node->rb_left = old->rb_left;
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200261 rb_set_parent(old->rb_left, node);
Sylvain Munaut54187ba2011-11-11 15:31:01 +0100262
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200263 goto color;
264 }
265
266 parent = rb_parent(node);
267 color = rb_color(node);
268
269 if (child)
270 rb_set_parent(child, parent);
271 if (parent)
272 {
273 if (parent->rb_left == node)
274 parent->rb_left = child;
275 else
276 parent->rb_right = child;
277 }
278 else
279 root->rb_node = child;
280
281 color:
282 if (color == RB_BLACK)
283 __rb_erase_color(child, parent, root);
284}
285
286/*
287 * This function returns the first node (in sort order) of the tree.
288 */
Sylvain Munaut2179f402011-11-11 15:32:57 +0100289struct rb_node *rb_first(const struct rb_root *root)
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200290{
291 struct rb_node *n;
292
293 n = root->rb_node;
294 if (!n)
295 return NULL;
296 while (n->rb_left)
297 n = n->rb_left;
298 return n;
299}
300
Sylvain Munaut2179f402011-11-11 15:32:57 +0100301struct rb_node *rb_last(const struct rb_root *root)
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200302{
303 struct rb_node *n;
304
305 n = root->rb_node;
306 if (!n)
307 return NULL;
308 while (n->rb_right)
309 n = n->rb_right;
310 return n;
311}
312
Sylvain Munaut2179f402011-11-11 15:32:57 +0100313struct rb_node *rb_next(const struct rb_node *node)
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200314{
315 struct rb_node *parent;
316
317 if (rb_parent(node) == node)
318 return NULL;
319
320 /* If we have a right-hand child, go down and then left as far
321 as we can. */
322 if (node->rb_right) {
323 node = node->rb_right;
324 while (node->rb_left)
325 node=node->rb_left;
Sylvain Munaut2179f402011-11-11 15:32:57 +0100326 return (struct rb_node *)node;
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200327 }
328
329 /* No right-hand children. Everything down and left is
330 smaller than us, so any 'next' node must be in the general
331 direction of our parent. Go up the tree; any time the
332 ancestor is a right-hand child of its parent, keep going
333 up. First time it's a left-hand child of its parent, said
334 parent is our 'next' node. */
335 while ((parent = rb_parent(node)) && node == parent->rb_right)
336 node = parent;
337
338 return parent;
339}
340
Sylvain Munaut2179f402011-11-11 15:32:57 +0100341struct rb_node *rb_prev(const struct rb_node *node)
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200342{
343 struct rb_node *parent;
344
345 if (rb_parent(node) == node)
346 return NULL;
347
348 /* If we have a left-hand child, go down and then right as far
349 as we can. */
350 if (node->rb_left) {
351 node = node->rb_left;
352 while (node->rb_right)
353 node=node->rb_right;
Sylvain Munaut2179f402011-11-11 15:32:57 +0100354 return (struct rb_node *)node;
Pablo Neira Ayusof74db0b2011-09-26 11:44:57 +0200355 }
356
357 /* No left-hand children. Go up till we find an ancestor which
358 is a right-hand child of its parent */
359 while ((parent = rb_parent(node)) && node == parent->rb_left)
360 node = parent;
361
362 return parent;
363}
364
365void rb_replace_node(struct rb_node *victim, struct rb_node *new,
366 struct rb_root *root)
367{
368 struct rb_node *parent = rb_parent(victim);
369
370 /* Set the surrounding nodes to point to the replacement */
371 if (parent) {
372 if (victim == parent->rb_left)
373 parent->rb_left = new;
374 else
375 parent->rb_right = new;
376 } else {
377 root->rb_node = new;
378 }
379 if (victim->rb_left)
380 rb_set_parent(victim->rb_left, new);
381 if (victim->rb_right)
382 rb_set_parent(victim->rb_right, new);
383
384 /* Copy the pointers/colour from the victim to the replacement */
385 *new = *victim;
386}