messages_ctdb: Use message_hdr_[get/put]
[Samba.git] / lib / util / rbtree.c
blobf6868cab5de5de01cf001addfde26a0dec559d62
1 /*
2 Red Black Trees
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
20 linux/lib/rbtree.c
23 #include "includes.h"
24 #include "rbtree.h"
26 #define RB_RED 0
27 #define RB_BLACK 1
29 #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
30 #define rb_color(r) ((r)->rb_parent_color & 1)
31 #define rb_is_red(r) (!rb_color(r))
32 #define rb_is_black(r) rb_color(r)
33 #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
34 #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
36 static void rb_set_parent(struct rb_node *rb, struct rb_node *p)
38 rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
40 static void rb_set_color(struct rb_node *rb, int color)
42 rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
45 #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
46 #define RB_EMPTY_NODE(node) (rb_parent(node) == node)
47 #define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
49 static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
51 struct rb_node *right = node->rb_right;
52 struct rb_node *parent = rb_parent(node);
54 if ((node->rb_right = right->rb_left))
55 rb_set_parent(right->rb_left, node);
56 right->rb_left = node;
58 rb_set_parent(right, parent);
60 if (parent)
62 if (node == parent->rb_left)
63 parent->rb_left = right;
64 else
65 parent->rb_right = right;
67 else
68 root->rb_node = right;
69 rb_set_parent(node, right);
72 static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
74 struct rb_node *left = node->rb_left;
75 struct rb_node *parent = rb_parent(node);
77 if ((node->rb_left = left->rb_right))
78 rb_set_parent(left->rb_right, node);
79 left->rb_right = node;
81 rb_set_parent(left, parent);
83 if (parent)
85 if (node == parent->rb_right)
86 parent->rb_right = left;
87 else
88 parent->rb_left = left;
90 else
91 root->rb_node = left;
92 rb_set_parent(node, left);
95 void rb_insert_color(struct rb_node *node, struct rb_root *root)
97 struct rb_node *parent, *gparent;
99 while ((parent = rb_parent(node)) && rb_is_red(parent))
101 gparent = rb_parent(parent);
103 if (parent == gparent->rb_left)
106 register struct rb_node *uncle = gparent->rb_right;
107 if (uncle && rb_is_red(uncle))
109 rb_set_black(uncle);
110 rb_set_black(parent);
111 rb_set_red(gparent);
112 node = gparent;
113 continue;
117 if (parent->rb_right == node)
119 register struct rb_node *tmp;
120 __rb_rotate_left(parent, root);
121 tmp = parent;
122 parent = node;
123 node = tmp;
126 rb_set_black(parent);
127 rb_set_red(gparent);
128 __rb_rotate_right(gparent, root);
129 } else {
131 register struct rb_node *uncle = gparent->rb_left;
132 if (uncle && rb_is_red(uncle))
134 rb_set_black(uncle);
135 rb_set_black(parent);
136 rb_set_red(gparent);
137 node = gparent;
138 continue;
142 if (parent->rb_left == node)
144 register struct rb_node *tmp;
145 __rb_rotate_right(parent, root);
146 tmp = parent;
147 parent = node;
148 node = tmp;
151 rb_set_black(parent);
152 rb_set_red(gparent);
153 __rb_rotate_left(gparent, root);
157 rb_set_black(root->rb_node);
160 static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
161 struct rb_root *root)
163 struct rb_node *other;
165 while ((!node || rb_is_black(node)) && node != root->rb_node)
167 if (parent->rb_left == node)
169 other = parent->rb_right;
170 if (rb_is_red(other))
172 rb_set_black(other);
173 rb_set_red(parent);
174 __rb_rotate_left(parent, root);
175 other = parent->rb_right;
177 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
178 (!other->rb_right || rb_is_black(other->rb_right)))
180 rb_set_red(other);
181 node = parent;
182 parent = rb_parent(node);
184 else
186 if (!other->rb_right || rb_is_black(other->rb_right))
188 struct rb_node *o_left;
189 if ((o_left = other->rb_left))
190 rb_set_black(o_left);
191 rb_set_red(other);
192 __rb_rotate_right(other, root);
193 other = parent->rb_right;
195 rb_set_color(other, rb_color(parent));
196 rb_set_black(parent);
197 if (other->rb_right)
198 rb_set_black(other->rb_right);
199 __rb_rotate_left(parent, root);
200 node = root->rb_node;
201 break;
204 else
206 other = parent->rb_left;
207 if (rb_is_red(other))
209 rb_set_black(other);
210 rb_set_red(parent);
211 __rb_rotate_right(parent, root);
212 other = parent->rb_left;
214 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
215 (!other->rb_right || rb_is_black(other->rb_right)))
217 rb_set_red(other);
218 node = parent;
219 parent = rb_parent(node);
221 else
223 if (!other->rb_left || rb_is_black(other->rb_left))
225 register struct rb_node *o_right;
226 if ((o_right = other->rb_right))
227 rb_set_black(o_right);
228 rb_set_red(other);
229 __rb_rotate_left(other, root);
230 other = parent->rb_left;
232 rb_set_color(other, rb_color(parent));
233 rb_set_black(parent);
234 if (other->rb_left)
235 rb_set_black(other->rb_left);
236 __rb_rotate_right(parent, root);
237 node = root->rb_node;
238 break;
242 if (node)
243 rb_set_black(node);
246 void rb_erase(struct rb_node *node, struct rb_root *root)
248 struct rb_node *child, *parent;
249 int color;
251 if (!node->rb_left)
252 child = node->rb_right;
253 else if (!node->rb_right)
254 child = node->rb_left;
255 else
257 struct rb_node *old = node, *left;
259 node = node->rb_right;
260 while ((left = node->rb_left) != NULL)
261 node = left;
262 child = node->rb_right;
263 parent = rb_parent(node);
264 color = rb_color(node);
266 if (child)
267 rb_set_parent(child, parent);
268 if (parent == old) {
269 parent->rb_right = child;
270 parent = node;
271 } else
272 parent->rb_left = child;
274 node->rb_parent_color = old->rb_parent_color;
275 node->rb_right = old->rb_right;
276 node->rb_left = old->rb_left;
278 if (rb_parent(old))
280 if (rb_parent(old)->rb_left == old)
281 rb_parent(old)->rb_left = node;
282 else
283 rb_parent(old)->rb_right = node;
284 } else
285 root->rb_node = node;
287 rb_set_parent(old->rb_left, node);
288 if (old->rb_right)
289 rb_set_parent(old->rb_right, node);
290 goto color;
293 parent = rb_parent(node);
294 color = rb_color(node);
296 if (child)
297 rb_set_parent(child, parent);
298 if (parent)
300 if (parent->rb_left == node)
301 parent->rb_left = child;
302 else
303 parent->rb_right = child;
305 else
306 root->rb_node = child;
308 color:
309 if (color == RB_BLACK)
310 __rb_erase_color(child, parent, root);
314 * This function returns the first node (in sort order) of the tree.
316 struct rb_node *rb_first(struct rb_root *root)
318 struct rb_node *n;
320 n = root->rb_node;
321 if (!n)
322 return NULL;
323 while (n->rb_left)
324 n = n->rb_left;
325 return n;
328 struct rb_node *rb_last(struct rb_root *root)
330 struct rb_node *n;
332 n = root->rb_node;
333 if (!n)
334 return NULL;
335 while (n->rb_right)
336 n = n->rb_right;
337 return n;
340 struct rb_node *rb_next(struct rb_node *node)
342 struct rb_node *parent;
344 if (rb_parent(node) == node)
345 return NULL;
347 /* If we have a right-hand child, go down and then left as far
348 as we can. */
349 if (node->rb_right) {
350 node = node->rb_right;
351 while (node->rb_left)
352 node=node->rb_left;
353 return node;
356 /* No right-hand children. Everything down and left is
357 smaller than us, so any 'next' node must be in the general
358 direction of our parent. Go up the tree; any time the
359 ancestor is a right-hand child of its parent, keep going
360 up. First time it's a left-hand child of its parent, said
361 parent is our 'next' node. */
362 while ((parent = rb_parent(node)) && node == parent->rb_right)
363 node = parent;
365 return parent;
368 struct rb_node *rb_prev(struct rb_node *node)
370 struct rb_node *parent;
372 if (rb_parent(node) == node)
373 return NULL;
375 /* If we have a left-hand child, go down and then right as far
376 as we can. */
377 if (node->rb_left) {
378 node = node->rb_left;
379 while (node->rb_right)
380 node=node->rb_right;
381 return node;
384 /* No left-hand children. Go up till we find an ancestor which
385 is a right-hand child of its parent */
386 while ((parent = rb_parent(node)) && node == parent->rb_left)
387 node = parent;
389 return parent;
392 void rb_replace_node(struct rb_node *victim, struct rb_node *new_node,
393 struct rb_root *root)
395 struct rb_node *parent = rb_parent(victim);
397 /* Set the surrounding nodes to point to the replacement */
398 if (parent) {
399 if (victim == parent->rb_left)
400 parent->rb_left = new_node;
401 else
402 parent->rb_right = new_node;
403 } else {
404 root->rb_node = new_node;
406 if (victim->rb_left)
407 rb_set_parent(victim->rb_left, new_node);
408 if (victim->rb_right)
409 rb_set_parent(victim->rb_right, new_node);
411 /* Copy the pointers/colour from the victim to the replacement */
412 *new_node = *victim;
415 void rb_link_node(struct rb_node * node, struct rb_node * parent,
416 struct rb_node ** rb_link)
418 node->rb_parent_color = (unsigned long )parent;
419 node->rb_left = node->rb_right = NULL;
421 *rb_link = node;