Implement check for invalid playlist entries.
[paraslash.git] / rbtree.c
1 /*
2 Red Black Trees
3 (C) 1999 Andrea Arcangeli <andrea@suse.de>
4 (C) 2002 David Woodhouse <dwmw2@infradead.org>
5 (C) 2007 Andre Noll <maan@systemlinux.org>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
21 linux/lib/rbtree.c
22 */
23
24 #include "stddef.h"
25 #include "rbtree.h"
26
27 static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
28 {
29 struct rb_node *right = node->rb_right;
30 struct rb_node *parent = rb_parent(node);
31
32 if ((node->rb_right = right->rb_left))
33 rb_set_parent(right->rb_left, node);
34 right->rb_left = node;
35
36 rb_set_parent(right, parent);
37
38 if (parent)
39 {
40 if (node == parent->rb_left)
41 parent->rb_left = right;
42 else
43 parent->rb_right = right;
44 }
45 else
46 root->rb_node = right;
47 rb_set_parent(node, right);
48 right->size = node->size;
49 node->size = 1;
50 if (node->rb_right)
51 node->size += node->rb_right->size;
52 if (node->rb_left)
53 node->size += node->rb_left->size;
54 }
55
56 static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
57 {
58 struct rb_node *left = node->rb_left;
59 struct rb_node *parent = rb_parent(node);
60
61 if ((node->rb_left = left->rb_right))
62 rb_set_parent(left->rb_right, node);
63 left->rb_right = node;
64
65 rb_set_parent(left, parent);
66
67 if (parent)
68 {
69 if (node == parent->rb_right)
70 parent->rb_right = left;
71 else
72 parent->rb_left = left;
73 }
74 else
75 root->rb_node = left;
76 rb_set_parent(node, left);
77 left->size = node->size;
78 node->size = 1;
79 if (node->rb_right)
80 node->size += node->rb_right->size;
81 if (node->rb_left)
82 node->size += node->rb_left->size;
83 }
84
85 void rb_insert_color(struct rb_node *node, struct rb_root *root)
86 {
87 struct rb_node *parent, *gparent;
88
89 while ((parent = rb_parent(node)) && rb_is_red(parent))
90 {
91 gparent = rb_parent(parent);
92
93 if (parent == gparent->rb_left)
94 {
95 {
96 register struct rb_node *uncle = gparent->rb_right;
97 if (uncle && rb_is_red(uncle))
98 {
99 rb_set_black(uncle);
100 rb_set_black(parent);
101 rb_set_red(gparent);
102 node = gparent;
103 continue;
104 }
105 }
106
107 if (parent->rb_right == node)
108 {
109 register struct rb_node *tmp;
110 __rb_rotate_left(parent, root);
111 tmp = parent;
112 parent = node;
113 node = tmp;
114 }
115
116 rb_set_black(parent);
117 rb_set_red(gparent);
118 __rb_rotate_right(gparent, root);
119 } else {
120 {
121 register struct rb_node *uncle = gparent->rb_left;
122 if (uncle && rb_is_red(uncle))
123 {
124 rb_set_black(uncle);
125 rb_set_black(parent);
126 rb_set_red(gparent);
127 node = gparent;
128 continue;
129 }
130 }
131
132 if (parent->rb_left == node)
133 {
134 register struct rb_node *tmp;
135 __rb_rotate_right(parent, root);
136 tmp = parent;
137 parent = node;
138 node = tmp;
139 }
140
141 rb_set_black(parent);
142 rb_set_red(gparent);
143 __rb_rotate_left(gparent, root);
144 }
145 }
146
147 rb_set_black(root->rb_node);
148 }
149
150 static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
151 struct rb_root *root)
152 {
153 struct rb_node *other;
154
155 while ((!node || rb_is_black(node)) && node != root->rb_node)
156 {
157 if (parent->rb_left == node)
158 {
159 other = parent->rb_right;
160 if (rb_is_red(other))
161 {
162 rb_set_black(other);
163 rb_set_red(parent);
164 __rb_rotate_left(parent, root);
165 other = parent->rb_right;
166 }
167 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
168 (!other->rb_right || rb_is_black(other->rb_right)))
169 {
170 rb_set_red(other);
171 node = parent;
172 parent = rb_parent(node);
173 }
174 else
175 {
176 if (!other->rb_right || rb_is_black(other->rb_right))
177 {
178 struct rb_node *o_left;
179 if ((o_left = other->rb_left))
180 rb_set_black(o_left);
181 rb_set_red(other);
182 __rb_rotate_right(other, root);
183 other = parent->rb_right;
184 }
185 rb_set_color(other, rb_color(parent));
186 rb_set_black(parent);
187 if (other->rb_right)
188 rb_set_black(other->rb_right);
189 __rb_rotate_left(parent, root);
190 node = root->rb_node;
191 break;
192 }
193 }
194 else
195 {
196 other = parent->rb_left;
197 if (rb_is_red(other))
198 {
199 rb_set_black(other);
200 rb_set_red(parent);
201 __rb_rotate_right(parent, root);
202 other = parent->rb_left;
203 }
204 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
205 (!other->rb_right || rb_is_black(other->rb_right)))
206 {
207 rb_set_red(other);
208 node = parent;
209 parent = rb_parent(node);
210 }
211 else
212 {
213 if (!other->rb_left || rb_is_black(other->rb_left))
214 {
215 register struct rb_node *o_right;
216 if ((o_right = other->rb_right))
217 rb_set_black(o_right);
218 rb_set_red(other);
219 __rb_rotate_left(other, root);
220 other = parent->rb_left;
221 }
222 rb_set_color(other, rb_color(parent));
223 rb_set_black(parent);
224 if (other->rb_left)
225 rb_set_black(other->rb_left);
226 __rb_rotate_right(parent, root);
227 node = root->rb_node;
228 break;
229 }
230 }
231 }
232 if (node)
233 rb_set_black(node);
234 }
235
236 void rb_erase(struct rb_node *node, struct rb_root *root)
237 {
238 struct rb_node *child, *parent;
239 int color;
240
241 if (!node->rb_left)
242 child = node->rb_right;
243 else if (!node->rb_right)
244 child = node->rb_left;
245 else
246 {
247 struct rb_node *old = node, *left;
248
249 node = node->rb_right;
250 while ((left = node->rb_left) != NULL)
251 node = left;
252 child = node->rb_right;
253 parent = rb_parent(node);
254 color = rb_color(node);
255
256 if (child)
257 rb_set_parent(child, parent);
258 if (parent == old) {
259 parent->rb_right = child;
260 parent = node;
261 } else
262 parent->rb_left = child;
263
264 node->rb_parent_color = old->rb_parent_color;
265 node->rb_right = old->rb_right;
266 node->rb_left = old->rb_left;
267 node->size = old->size;
268
269 if (rb_parent(old))
270 {
271 if (rb_parent(old)->rb_left == old)
272 rb_parent(old)->rb_left = node;
273 else
274 rb_parent(old)->rb_right = node;
275 } else
276 root->rb_node = node;
277
278 rb_set_parent(old->rb_left, node);
279 if (old->rb_right)
280 rb_set_parent(old->rb_right, node);
281 goto color;
282 }
283
284 parent = rb_parent(node);
285 color = rb_color(node);
286
287 if (child)
288 rb_set_parent(child, parent);
289 if (parent)
290 {
291 if (parent->rb_left == node)
292 parent->rb_left = child;
293 else
294 parent->rb_right = child;
295 }
296 else
297 root->rb_node = child;
298
299 color:
300 if (color == RB_BLACK)
301 __rb_erase_color(child, parent, root);
302 }
303
304 /*
305 * This function returns the first node (in sort order) of the tree.
306 */
307 struct rb_node *rb_first(struct rb_root *root)
308 {
309 struct rb_node *n;
310
311 n = root->rb_node;
312 if (!n)
313 return NULL;
314 while (n->rb_left)
315 n = n->rb_left;
316 return n;
317 }
318
319 struct rb_node *rb_last(struct rb_root *root)
320 {
321 struct rb_node *n;
322
323 n = root->rb_node;
324 if (!n)
325 return NULL;
326 while (n->rb_right)
327 n = n->rb_right;
328 return n;
329 }
330
331 struct rb_node *rb_next(struct rb_node *node)
332 {
333 struct rb_node *parent;
334
335 if (rb_parent(node) == node)
336 return NULL;
337
338 /* If we have a right-hand child, go down and then left as far
339 as we can. */
340 if (node->rb_right) {
341 node = node->rb_right;
342 while (node->rb_left)
343 node=node->rb_left;
344 return node;
345 }
346
347 /* No right-hand children. Everything down and left is
348 smaller than us, so any 'next' node must be in the general
349 direction of our parent. Go up the tree; any time the
350 ancestor is a right-hand child of its parent, keep going
351 up. First time it's a left-hand child of its parent, said
352 parent is our 'next' node. */
353 while ((parent = rb_parent(node)) && node == parent->rb_right)
354 node = parent;
355
356 return parent;
357 }
358
359 struct rb_node *rb_prev(struct rb_node *node)
360 {
361 struct rb_node *parent;
362
363 if (rb_parent(node) == node)
364 return NULL;
365
366 /* If we have a left-hand child, go down and then right as far
367 as we can. */
368 if (node->rb_left) {
369 node = node->rb_left;
370 while (node->rb_right)
371 node=node->rb_right;
372 return node;
373 }
374
375 /* No left-hand children. Go up till we find an ancestor which
376 is a right-hand child of its parent */
377 while ((parent = rb_parent(node)) && node == parent->rb_left)
378 node = parent;
379
380 return parent;
381 }
382
383 void rb_replace_node(struct rb_node *victim, struct rb_node *new,
384 struct rb_root *root)
385 {
386 struct rb_node *parent = rb_parent(victim);
387
388 /* Set the surrounding nodes to point to the replacement */
389 if (parent) {
390 if (victim == parent->rb_left)
391 parent->rb_left = new;
392 else
393 parent->rb_right = new;
394 } else {
395 root->rb_node = new;
396 }
397 if (victim->rb_left)
398 rb_set_parent(victim->rb_left, new);
399 if (victim->rb_right)
400 rb_set_parent(victim->rb_right, new);
401
402 /* Copy the pointers/colour from the victim to the replacement */
403 *new = *victim;
404 }
405
406 /**
407 * Get the n-th node (in sort order) of the tree.
408 *
409 * \param node The root of the subtree to consider.
410 * \param n The order statistic to compute.
411 *
412 * \return Pointer to the \a n th greatest node on success, \p NULL on errors.
413 */
414 struct rb_node *rb_nth(struct rb_node *node, unsigned n)
415 {
416 unsigned size = 1;
417
418 if (!node)
419 return NULL;
420 if (node->rb_left)
421 size += node->rb_left->size;
422 if (n == size)
423 return node;
424 if (n < size)
425 return rb_nth(node->rb_left, n);
426 return rb_nth(node->rb_right, n - size);
427 }
428
429 /**
430 * Get the rank of a node in O(log n) time.
431 *
432 * \param node The node to get the rank of.
433 * \param rank Result pointer.
434 *
435 * \return Positive on success, -1 on errors.
436 */
437 int rb_rank(struct rb_node *node, unsigned *rank)
438 {
439 *rank = 1;
440 struct rb_node *parent;
441
442 if (!node)
443 return -1;
444 if (node->rb_left)
445 *rank += node->rb_left->size;
446 while ((parent = rb_parent(node))) {
447 if (node == parent->rb_right) {
448 (*rank)++;
449 if (parent->rb_left)
450 *rank += parent->rb_left->size;
451 }
452 node = parent;
453 }
454 return 1;
455 }