2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "gtkrbtree.h"
23 static void _gtk_rbnode_validate_allocator (GAllocator *allocator);
24 static GtkRBNode *_gtk_rbnode_new (GtkRBTree *tree,
26 static void _gtk_rbnode_free (GtkRBNode *node);
27 static void _gtk_rbnode_rotate_left (GtkRBTree *tree,
29 static void _gtk_rbnode_rotate_left (GtkRBTree *tree,
31 static void _gtk_rbtree_insert_fixup (GtkRBTree *tree,
33 static void _gtk_rbtree_remove_node_fixup (GtkRBTree *tree,
35 static gint _count_nodes (GtkRBTree *tree,
41 struct _GAllocator /* from gmem.c */
49 GtkRBNode *free_nodes; /* implementation specific */
53 G_LOCK_DEFINE_STATIC (current_allocator);
54 static GAllocator *current_allocator = NULL;
56 /* HOLDS: current_allocator_lock */
58 _gtk_rbnode_validate_allocator (GAllocator *allocator)
60 g_return_if_fail (allocator != NULL);
61 g_return_if_fail (allocator->is_unused == TRUE);
63 if (allocator->type != G_ALLOCATOR_NODE)
65 allocator->type = G_ALLOCATOR_NODE;
66 if (allocator->mem_chunk)
68 g_mem_chunk_destroy (allocator->mem_chunk);
69 allocator->mem_chunk = NULL;
73 if (!allocator->mem_chunk)
75 allocator->mem_chunk = g_mem_chunk_new (allocator->name,
77 sizeof (GtkRBNode) * allocator->n_preallocs,
79 allocator->free_nodes = NULL;
82 allocator->is_unused = FALSE;
86 _gtk_rbnode_new (GtkRBTree *tree,
91 G_LOCK (current_allocator);
92 if (!current_allocator)
94 GAllocator *allocator = g_allocator_new ("GTK+ default GtkRBNode allocator",
96 _gtk_rbnode_validate_allocator (allocator);
97 allocator->last = NULL;
98 current_allocator = allocator;
100 if (!current_allocator->free_nodes)
101 node = g_chunk_new (GtkRBNode, current_allocator->mem_chunk);
104 node = current_allocator->free_nodes;
105 current_allocator->free_nodes = node->left;
107 G_UNLOCK (current_allocator);
109 node->left = tree->nil;
110 node->right = tree->nil;
111 node->parent = tree->nil;
112 node->flags = GTK_RBNODE_RED;
115 node->children = NULL;
116 node->offset = height;
121 _gtk_rbnode_free (GtkRBNode *node)
123 G_LOCK (current_allocator);
124 node->left = current_allocator->free_nodes;
125 current_allocator->free_nodes = node;
126 if (gtk_debug_flags & GTK_DEBUG_TREE)
128 /* unfortunately node->left has to continue to point to
131 node->right = (gpointer) 0xdeadbeef;
132 node->parent = (gpointer) 0xdeadbeef;
133 node->offset = 56789;
137 G_UNLOCK (current_allocator);
141 _gtk_rbnode_rotate_left (GtkRBTree *tree,
144 gint node_height, right_height;
145 guint node_parity, right_parity;
146 GtkRBNode *right = node->right;
148 g_return_if_fail (node != tree->nil);
150 node_height = node->offset -
151 (node->left?node->left->offset:0) -
152 (node->right?node->right->offset:0) -
153 (node->children?node->children->root->offset:0);
154 right_height = right->offset -
155 (right->left?right->left->offset:0) -
156 (right->right?right->right->offset:0) -
157 (right->children?right->children->root->offset:0);
159 node_parity = node->parity -
160 (node->left?node->left->parity:0) -
161 (node->right?node->right->parity:0) -
162 (node->children?node->children->root->parity:0);
163 right_parity = right->parity -
164 (right->left?right->left->parity:0) -
165 (right->right?right->right->parity:0) -
166 (right->children?right->children->root->parity:0);
168 node->right = right->left;
169 if (right->left != tree->nil)
170 right->left->parent = node;
172 if (right != tree->nil)
173 right->parent = node->parent;
174 if (node->parent != tree->nil)
176 if (node == node->parent->left)
177 node->parent->left = right;
179 node->parent->right = right;
185 if (node != tree->nil)
186 node->parent = right;
188 node->count = 1 + (node->left?node->left->count:0) +
189 (node->right?node->right->count:0);
190 right->count = 1 + (right->left?right->left->count:0) +
191 (right->right?right->right->count:0);
193 node->offset = node_height +
194 (node->left?node->left->offset:0) +
195 (node->right?node->right->offset:0) +
196 (node->children?node->children->root->offset:0);
197 right->offset = right_height +
198 (right->left?right->left->offset:0) +
199 (right->right?right->right->offset:0) +
200 (right->children?right->children->root->offset:0);
202 node->parity = node_parity +
203 (node->left?node->left->parity:0) +
204 (node->right?node->right->parity:0) +
205 (node->children?node->children->root->parity:0);
206 right->parity = right_parity +
207 (right->left?right->left->parity:0) +
208 (right->right?right->right->parity:0) +
209 (right->children?right->children->root->parity:0);
213 _gtk_rbnode_rotate_right (GtkRBTree *tree,
216 gint node_height, left_height;
217 guint node_parity, left_parity;
218 GtkRBNode *left = node->left;
220 g_return_if_fail (node != tree->nil);
222 node_height = node->offset -
223 (node->left?node->left->offset:0) -
224 (node->right?node->right->offset:0) -
225 (node->children?node->children->root->offset:0);
226 left_height = left->offset -
227 (left->left?left->left->offset:0) -
228 (left->right?left->right->offset:0) -
229 (left->children?left->children->root->offset:0);
231 node_parity = node->parity -
232 (node->left?node->left->parity:0) -
233 (node->right?node->right->parity:0) -
234 (node->children?node->children->root->parity:0);
235 left_parity = left->parity -
236 (left->left?left->left->parity:0) -
237 (left->right?left->right->parity:0) -
238 (left->children?left->children->root->parity:0);
240 node->left = left->right;
241 if (left->right != tree->nil)
242 left->right->parent = node;
244 if (left != tree->nil)
245 left->parent = node->parent;
246 if (node->parent != tree->nil)
248 if (node == node->parent->right)
249 node->parent->right = left;
251 node->parent->left = left;
258 /* link node and left */
260 if (node != tree->nil)
263 node->count = 1 + (node->left?node->left->count:0) +
264 (node->right?node->right->count:0);
265 left->count = 1 + (left->left?left->left->count:0) +
266 (left->right?left->right->count:0);
268 node->offset = node_height +
269 (node->left?node->left->offset:0) +
270 (node->right?node->right->offset:0) +
271 (node->children?node->children->root->offset:0);
272 left->offset = left_height +
273 (left->left?left->left->offset:0) +
274 (left->right?left->right->offset:0) +
275 (left->children?left->children->root->offset:0);
277 node->parity = node_parity +
278 (node->left?node->left->parity:0) +
279 (node->right?node->right->parity:0) +
280 (node->children?node->children->root->parity:0);
281 left->parity = left_parity +
282 (left->left?left->left->parity:0) +
283 (left->right?left->right->parity:0) +
284 (left->children?left->children->root->parity:0);
288 _gtk_rbtree_insert_fixup (GtkRBTree *tree,
292 /* check Red-Black properties */
293 while (node != tree->root && GTK_RBNODE_GET_COLOR (node->parent) == GTK_RBNODE_RED)
295 /* we have a violation */
296 if (node->parent == node->parent->parent->left)
298 GtkRBNode *y = node->parent->parent->right;
299 if (GTK_RBNODE_GET_COLOR (y) == GTK_RBNODE_RED)
301 /* uncle is GTK_RBNODE_RED */
302 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
303 GTK_RBNODE_SET_COLOR (y, GTK_RBNODE_BLACK);
304 GTK_RBNODE_SET_COLOR (node->parent->parent, GTK_RBNODE_RED);
305 node = node->parent->parent;
309 /* uncle is GTK_RBNODE_BLACK */
310 if (node == node->parent->right)
312 /* make node a left child */
314 _gtk_rbnode_rotate_left (tree, node);
317 /* recolor and rotate */
318 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
319 GTK_RBNODE_SET_COLOR (node->parent->parent, GTK_RBNODE_RED);
320 _gtk_rbnode_rotate_right(tree, node->parent->parent);
325 /* mirror image of above code */
326 GtkRBNode *y = node->parent->parent->left;
327 if (GTK_RBNODE_GET_COLOR (y) == GTK_RBNODE_RED)
329 /* uncle is GTK_RBNODE_RED */
330 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
331 GTK_RBNODE_SET_COLOR (y, GTK_RBNODE_BLACK);
332 GTK_RBNODE_SET_COLOR (node->parent->parent, GTK_RBNODE_RED);
333 node = node->parent->parent;
337 /* uncle is GTK_RBNODE_BLACK */
338 if (node == node->parent->left)
341 _gtk_rbnode_rotate_right (tree, node);
343 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
344 GTK_RBNODE_SET_COLOR (node->parent->parent, GTK_RBNODE_RED);
345 _gtk_rbnode_rotate_left (tree, node->parent->parent);
349 GTK_RBNODE_SET_COLOR (tree->root, GTK_RBNODE_BLACK);
353 _gtk_rbtree_remove_node_fixup (GtkRBTree *tree,
356 while (node != tree->root && GTK_RBNODE_GET_COLOR (node) == GTK_RBNODE_BLACK)
358 if (node == node->parent->left)
360 GtkRBNode *w = node->parent->right;
361 if (GTK_RBNODE_GET_COLOR (w) == GTK_RBNODE_RED)
363 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_BLACK);
364 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_RED);
365 _gtk_rbnode_rotate_left (tree, node->parent);
366 w = node->parent->right;
368 if (GTK_RBNODE_GET_COLOR (w->left) == GTK_RBNODE_BLACK && GTK_RBNODE_GET_COLOR (w->right) == GTK_RBNODE_BLACK)
370 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_RED);
375 if (GTK_RBNODE_GET_COLOR (w->right) == GTK_RBNODE_BLACK)
377 GTK_RBNODE_SET_COLOR (w->left, GTK_RBNODE_BLACK);
378 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_RED);
379 _gtk_rbnode_rotate_right (tree, w);
380 w = node->parent->right;
382 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_GET_COLOR (node->parent));
383 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
384 GTK_RBNODE_SET_COLOR (w->right, GTK_RBNODE_BLACK);
385 _gtk_rbnode_rotate_left (tree, node->parent);
391 GtkRBNode *w = node->parent->left;
392 if (GTK_RBNODE_GET_COLOR (w) == GTK_RBNODE_RED)
394 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_BLACK);
395 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_RED);
396 _gtk_rbnode_rotate_right (tree, node->parent);
397 w = node->parent->left;
399 if (GTK_RBNODE_GET_COLOR (w->right) == GTK_RBNODE_BLACK && GTK_RBNODE_GET_COLOR (w->left) == GTK_RBNODE_BLACK)
401 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_RED);
406 if (GTK_RBNODE_GET_COLOR (w->left) == GTK_RBNODE_BLACK)
408 GTK_RBNODE_SET_COLOR (w->right, GTK_RBNODE_BLACK);
409 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_RED);
410 _gtk_rbnode_rotate_left (tree, w);
411 w = node->parent->left;
413 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_GET_COLOR (node->parent));
414 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
415 GTK_RBNODE_SET_COLOR (w->left, GTK_RBNODE_BLACK);
416 _gtk_rbnode_rotate_right (tree, node->parent);
421 GTK_RBNODE_SET_COLOR (node, GTK_RBNODE_BLACK);
424 /* Public functions */
426 _gtk_rbnode_push_allocator (GAllocator *allocator)
428 G_LOCK (current_allocator);
429 _gtk_rbnode_validate_allocator ( allocator );
430 allocator->last = current_allocator;
431 current_allocator = allocator;
432 G_UNLOCK (current_allocator);
436 _gtk_rbnode_pop_allocator (void)
438 G_LOCK (current_allocator);
439 if (current_allocator)
441 GAllocator *allocator;
443 allocator = current_allocator;
444 current_allocator = allocator->last;
445 allocator->last = NULL;
446 allocator->is_unused = TRUE;
448 G_UNLOCK (current_allocator);
452 _gtk_rbtree_new (void)
456 retval = (GtkRBTree *) g_new (GtkRBTree, 1);
457 retval->parent_tree = NULL;
458 retval->parent_node = NULL;
460 retval->nil = g_new0 (GtkRBNode, 1);
461 retval->nil->left = NULL;
462 retval->nil->right = NULL;
463 retval->nil->parent = NULL;
464 retval->nil->flags = GTK_RBNODE_BLACK;
465 retval->nil->count = 0;
466 retval->nil->offset = 0;
467 retval->nil->parity = 0;
469 retval->root = retval->nil;
474 _gtk_rbtree_free_helper (GtkRBTree *tree,
479 _gtk_rbtree_free (node->children);
481 _gtk_rbnode_free (node);
485 _gtk_rbtree_free (GtkRBTree *tree)
487 _gtk_rbtree_traverse (tree,
490 _gtk_rbtree_free_helper,
493 if (tree->parent_node &&
494 tree->parent_node->children == tree)
495 tree->parent_node->children = NULL;
496 _gtk_rbnode_free (tree->nil);
501 _gtk_rbtree_remove (GtkRBTree *tree)
506 gint height = tree->root->offset;
508 if (gtk_debug_flags & GTK_DEBUG_TREE)
509 _gtk_rbtree_test (G_STRLOC, tree);
511 tmp_tree = tree->parent_tree;
512 tmp_node = tree->parent_node;
514 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
516 tmp_node->offset -= height;
518 /* If the removed tree was odd, flip all parents */
519 if (tree->root->parity)
520 tmp_node->parity = !tmp_node->parity;
522 tmp_node = tmp_node->parent;
523 if (tmp_node == tmp_tree->nil)
525 tmp_node = tmp_tree->parent_node;
526 tmp_tree = tmp_tree->parent_tree;
530 tmp_tree = tree->parent_tree;
531 tmp_node = tree->parent_node;
532 _gtk_rbtree_free (tree);
534 if (gtk_debug_flags & GTK_DEBUG_TREE)
535 _gtk_rbtree_test (G_STRLOC, tmp_tree);
540 _gtk_rbtree_insert_after (GtkRBTree *tree,
545 gboolean right = TRUE;
549 if (gtk_debug_flags & GTK_DEBUG_TREE)
550 _gtk_rbtree_test (G_STRLOC, tree);
552 if (current != NULL && current->right != tree->nil)
554 current = current->right;
555 while (current->left != tree->nil)
556 current = current->left;
561 node = _gtk_rbnode_new (tree, height);
562 node->parent = (current?current:tree->nil);
564 /* insert node in tree */
568 current->right = node;
570 current->left = node;
571 tmp_node = node->parent;
577 tmp_node = tree->parent_node;
578 tmp_tree = tree->parent_tree;
581 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
583 /* We only want to propagate the count if we are in the tree we
585 if (tmp_tree == tree)
588 tmp_node->parity += 1;
589 tmp_node->offset += height;
590 tmp_node = tmp_node->parent;
591 if (tmp_node == tmp_tree->nil)
593 tmp_node = tmp_tree->parent_node;
594 tmp_tree = tmp_tree->parent_tree;
597 _gtk_rbtree_insert_fixup (tree, node);
599 if (gtk_debug_flags & GTK_DEBUG_TREE)
600 _gtk_rbtree_test (G_STRLOC, tree);
606 _gtk_rbtree_insert_before (GtkRBTree *tree,
611 gboolean left = TRUE;
615 if (gtk_debug_flags & GTK_DEBUG_TREE)
616 _gtk_rbtree_test (G_STRLOC, tree);
618 if (current != NULL && current->left != tree->nil)
620 current = current->left;
621 while (current->right != tree->nil)
622 current = current->right;
627 node = _gtk_rbnode_new (tree, height);
628 node->parent = (current?current:tree->nil);
630 /* insert node in tree */
634 current->left = node;
636 current->right = node;
637 tmp_node = node->parent;
643 tmp_node = tree->parent_node;
644 tmp_tree = tree->parent_tree;
647 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
649 /* We only want to propagate the count if we are in the tree we
651 if (tmp_tree == tree)
654 tmp_node->parity += 1;
655 tmp_node->offset += height;
656 tmp_node = tmp_node->parent;
657 if (tmp_node == tmp_tree->nil)
659 tmp_node = tmp_tree->parent_node;
660 tmp_tree = tmp_tree->parent_tree;
663 _gtk_rbtree_insert_fixup (tree, node);
665 if (gtk_debug_flags & GTK_DEBUG_TREE)
666 _gtk_rbtree_test (G_STRLOC, tree);
672 _gtk_rbtree_find_count (GtkRBTree *tree,
678 while (node != tree->nil && (node->left->count + 1 != count))
680 if (node->left->count >= count)
684 count -= (node->left->count + 1);
688 if (node == tree->nil)
694 _gtk_rbtree_node_set_height (GtkRBTree *tree,
698 gint diff = height - GTK_RBNODE_GET_HEIGHT (node);
699 GtkRBNode *tmp_node = node;
700 GtkRBTree *tmp_tree = tree;
705 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
707 tmp_node->offset += diff;
708 tmp_node = tmp_node->parent;
709 if (tmp_node == tmp_tree->nil)
711 tmp_node = tmp_tree->parent_node;
712 tmp_tree = tmp_tree->parent_tree;
717 typedef struct _GtkRBReorder
727 gtk_rbtree_reorder_sort_func (gconstpointer a,
730 return ((GtkRBReorder *) a)->order > ((GtkRBReorder *) b)->order;
734 gtk_rbtree_reorder_invert_func (gconstpointer a,
737 return ((GtkRBReorder *) a)->invert_order > ((GtkRBReorder *) b)->invert_order;
741 gtk_rbtree_reorder_fixup (GtkRBTree *tree,
744 if (node == tree->nil)
747 if (node->left != tree->nil)
749 gtk_rbtree_reorder_fixup (tree, node->left);
750 node->offset += node->left->offset;
752 if (node->right != tree->nil)
754 gtk_rbtree_reorder_fixup (tree, node->right);
755 node->offset += node->right->offset;
759 node->offset += node->children->root->offset;
762 /* It basically pulls everything out of the tree, rearranges it, and puts it
763 * back together. Our strategy is to keep the old RBTree intact, and just
764 * rearrange the contents. When that is done, we go through and update the
765 * heights. There is probably a more elegant way to write this function. If
766 * anyone wants to spend the time writing it, patches will be accepted.
770 _gtk_rbtree_reorder (GtkRBTree *tree,
774 GtkRBReorder reorder;
779 g_return_if_fail (tree != NULL);
780 g_return_if_fail (length > 0);
781 g_return_if_fail (tree->root->count == length);
783 /* Sort the trees values in the new tree. */
784 array = g_array_sized_new (FALSE, FALSE, sizeof (GtkRBReorder), length);
785 for (i = 0; i < length; i++)
787 reorder.order = new_order[i];
788 reorder.invert_order = i;
789 g_array_append_val (array, reorder);
792 g_array_sort(array, gtk_rbtree_reorder_sort_func);
796 while (node && node->left != tree->nil)
799 for (i = 0; i < length; i++)
801 g_assert (node != tree->nil);
802 g_array_index (array, GtkRBReorder, i).children = node->children;
803 g_array_index (array, GtkRBReorder, i).flags = GTK_RBNODE_NON_COLORS & node->flags;
804 g_array_index (array, GtkRBReorder, i).height = GTK_RBNODE_GET_HEIGHT (node);
806 node = _gtk_rbtree_next (tree, node);
809 g_array_sort (array, gtk_rbtree_reorder_invert_func);
813 while (node && node->left != tree->nil)
816 /* Go through the tree and change the values to the new ones. */
817 for (i = 0; i < length; i++)
819 reorder = g_array_index (array, GtkRBReorder, i);
820 node->children = reorder.children;
822 node->children->parent_node = node;
823 node->flags = GTK_RBNODE_GET_COLOR (node) | reorder.flags;
824 /* We temporarily set the height to this. */
825 node->offset = reorder.height;
826 node = _gtk_rbtree_next (tree, node);
828 gtk_rbtree_reorder_fixup (tree, tree->root);
833 _gtk_rbtree_node_find_offset (GtkRBTree *tree,
840 g_assert (node->left);
842 retval = node->left->offset;
844 while (tree && node && node != tree->nil)
849 /* Add left branch, plus children, iff we came from the right */
850 if (node->right == last)
851 retval += node->offset - node->right->offset;
853 if (node == tree->nil)
855 node = tree->parent_node;
856 tree = tree->parent_tree;
858 /* Add the parent node, plus the left branch. */
860 retval += node->left->offset + GTK_RBNODE_GET_HEIGHT (node);
867 _gtk_rbtree_node_find_parity (GtkRBTree *tree,
874 g_assert (node->left);
876 retval = node->left->parity;
878 while (tree && node && node != tree->nil)
883 /* Add left branch, plus children, iff we came from the right */
884 if (node->right == last)
885 retval += node->parity - node->right->parity;
887 if (node == tree->nil)
889 node = tree->parent_node;
890 tree = tree->parent_tree;
892 /* Add the parent node, plus the left branch. */
894 retval += node->left->parity + 1; /* 1 == GTK_RBNODE_GET_PARITY() */
902 _gtk_rbtree_find_offset (GtkRBTree *tree,
904 GtkRBTree **new_tree,
905 GtkRBNode **new_node)
916 tmp_node = tree->root;
917 while (tmp_node != tree->nil &&
918 (tmp_node->left->offset > height ||
919 (tmp_node->offset - tmp_node->right->offset) < height))
921 if (tmp_node->left->offset > height)
922 tmp_node = tmp_node->left;
925 height -= (tmp_node->offset - tmp_node->right->offset);
926 tmp_node = tmp_node->right;
929 if (tmp_node == tree->nil)
935 if (tmp_node->children)
937 if ((tmp_node->offset -
938 tmp_node->right->offset -
939 tmp_node->children->root->offset) > height)
942 *new_node = tmp_node;
943 return (height - tmp_node->left->offset);
945 return _gtk_rbtree_find_offset (tmp_node->children,
946 height - tmp_node->left->offset -
948 tmp_node->left->offset -
949 tmp_node->right->offset -
950 tmp_node->children->root->offset),
955 *new_node = tmp_node;
956 return (height - tmp_node->left->offset);
961 _gtk_rbtree_remove_node (GtkRBTree *tree,
968 g_return_if_fail (tree != NULL);
969 g_return_if_fail (node != NULL);
971 /* make sure we're deleting a node that's actually in the tree */
972 for (x = node; x->parent != tree->nil; x = x->parent)
974 g_return_if_fail (x == tree->root);
976 if (gtk_debug_flags & GTK_DEBUG_TREE)
977 _gtk_rbtree_test (G_STRLOC, tree);
979 if (node->left == tree->nil || node->right == tree->nil)
987 while (y->left != tree->nil)
991 /* adjust count only beneath tree */
992 for (x = y; x != tree->nil; x = x->parent)
995 /* y->count = node->count; */
997 /* offsets and parity adjust all the way up through parent trees */
1002 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
1004 /* tmp_node->offset -= y->offset; */
1005 tmp_node->parity -= (guint) 1; /* parity of y is always 1 */
1007 tmp_node = tmp_node->parent;
1008 if (tmp_node == tmp_tree->nil)
1010 tmp_node = tmp_tree->parent_node;
1011 tmp_tree = tmp_tree->parent_tree;
1015 /* x is y's only child */
1016 if (y->left != tree->nil)
1021 /* remove y from the parent chain */
1022 x->parent = y->parent;
1023 if (y->parent != tree->nil)
1024 if (y == y->parent->left)
1025 y->parent->left = x;
1027 y->parent->right = x;
1033 /* Copy the node over */
1034 if (GTK_RBNODE_GET_COLOR (node) == GTK_RBNODE_BLACK)
1035 node->flags = ((y->flags & (GTK_RBNODE_NON_COLORS)) | GTK_RBNODE_BLACK);
1037 node->flags = ((y->flags & (GTK_RBNODE_NON_COLORS)) | GTK_RBNODE_RED);
1038 node->children = y->children;
1041 if (GTK_RBNODE_GET_COLOR (y) == GTK_RBNODE_BLACK)
1042 _gtk_rbtree_remove_node_fixup (tree, x);
1044 _gtk_rbnode_free (y);
1046 if (gtk_debug_flags & GTK_DEBUG_TREE)
1047 _gtk_rbtree_test (G_STRLOC, tree);
1051 _gtk_rbtree_next (GtkRBTree *tree,
1054 g_return_val_if_fail (tree != NULL, NULL);
1055 g_return_val_if_fail (node != NULL, NULL);
1057 /* Case 1: the node's below us. */
1058 if (node->right != tree->nil)
1061 while (node->left != tree->nil)
1066 /* Case 2: it's an ancestor */
1067 while (node->parent != tree->nil)
1069 if (node->parent->right == node)
1070 node = node->parent;
1072 return (node->parent);
1075 /* Case 3: There is no next node */
1080 _gtk_rbtree_prev (GtkRBTree *tree,
1083 g_return_val_if_fail (tree != NULL, NULL);
1084 g_return_val_if_fail (node != NULL, NULL);
1086 /* Case 1: the node's below us. */
1087 if (node->left != tree->nil)
1090 while (node->right != tree->nil)
1095 /* Case 2: it's an ancestor */
1096 while (node->parent != tree->nil)
1098 if (node->parent->left == node)
1099 node = node->parent;
1101 return (node->parent);
1104 /* Case 3: There is no next node */
1109 _gtk_rbtree_next_full (GtkRBTree *tree,
1111 GtkRBTree **new_tree,
1112 GtkRBNode **new_node)
1114 g_return_if_fail (tree != NULL);
1115 g_return_if_fail (node != NULL);
1116 g_return_if_fail (new_tree != NULL);
1117 g_return_if_fail (new_node != NULL);
1121 *new_tree = node->children;
1122 *new_node = (*new_tree)->root;
1123 while ((*new_node)->left != (*new_tree)->nil)
1124 *new_node = (*new_node)->left;
1129 *new_node = _gtk_rbtree_next (tree, node);
1131 while ((*new_node == NULL) &&
1132 (*new_tree != NULL))
1134 *new_node = (*new_tree)->parent_node;
1135 *new_tree = (*new_tree)->parent_tree;
1137 *new_node = _gtk_rbtree_next (*new_tree, *new_node);
1142 _gtk_rbtree_prev_full (GtkRBTree *tree,
1144 GtkRBTree **new_tree,
1145 GtkRBNode **new_node)
1147 g_return_if_fail (tree != NULL);
1148 g_return_if_fail (node != NULL);
1149 g_return_if_fail (new_tree != NULL);
1150 g_return_if_fail (new_node != NULL);
1153 *new_node = _gtk_rbtree_prev (tree, node);
1155 if (*new_node == NULL)
1157 *new_node = (*new_tree)->parent_node;
1158 *new_tree = (*new_tree)->parent_tree;
1162 while ((*new_node)->children)
1164 *new_tree = (*new_node)->children;
1165 *new_node = (*new_tree)->root;
1166 while ((*new_node)->right != (*new_tree)->nil)
1167 *new_node = (*new_node)->right;
1173 _gtk_rbtree_get_depth (GtkRBTree *tree)
1175 GtkRBTree *tmp_tree;
1178 tmp_tree = tree->parent_tree;
1182 tmp_tree = tmp_tree->parent_tree;
1189 _gtk_rbtree_traverse_pre_order (GtkRBTree *tree,
1191 GtkRBTreeTraverseFunc func,
1194 if (node == tree->nil)
1197 (* func) (tree, node, data);
1198 _gtk_rbtree_traverse_pre_order (tree, node->left, func, data);
1199 _gtk_rbtree_traverse_pre_order (tree, node->right, func, data);
1203 _gtk_rbtree_traverse_post_order (GtkRBTree *tree,
1205 GtkRBTreeTraverseFunc func,
1208 if (node == tree->nil)
1211 _gtk_rbtree_traverse_post_order (tree, node->left, func, data);
1212 _gtk_rbtree_traverse_post_order (tree, node->right, func, data);
1213 (* func) (tree, node, data);
1217 _gtk_rbtree_traverse (GtkRBTree *tree,
1219 GTraverseType order,
1220 GtkRBTreeTraverseFunc func,
1223 g_return_if_fail (tree != NULL);
1224 g_return_if_fail (node != NULL);
1225 g_return_if_fail (func != NULL);
1226 g_return_if_fail (order <= G_LEVEL_ORDER);
1231 _gtk_rbtree_traverse_pre_order (tree, node, func, data);
1234 _gtk_rbtree_traverse_post_order (tree, node, func, data);
1239 g_warning ("unsupported traversal order.");
1245 _count_nodes (GtkRBTree *tree,
1249 if (node == tree->nil)
1252 g_assert (node->left);
1253 g_assert (node->right);
1255 res = (_count_nodes (tree, node->left) +
1256 _count_nodes (tree, node->right) + 1);
1258 if (res != node->count)
1259 g_print ("Tree failed\n");
1264 get_parity (GtkRBNode *node)
1266 guint child_total = 0;
1269 /* The parity of a node is node->parity minus
1270 * the parity of left, right, and children.
1272 * This is equivalent to saying that if left, right, children
1273 * sum to 0 parity, then node->parity is the parity of node,
1274 * and if left, right, children are odd parity, then
1275 * node->parity is the reverse of the node's parity.
1278 child_total += (guint) node->left->parity;
1279 child_total += (guint) node->right->parity;
1282 child_total += (guint) node->children->root->parity;
1284 rem = child_total % 2;
1287 return node->parity;
1289 return !node->parity;
1293 count_parity (GtkRBTree *tree,
1298 if (node == tree->nil)
1302 count_parity (tree, node->left) +
1303 count_parity (tree, node->right) +
1305 (node->children ? count_parity (node->children, node->children->root) : 0);
1307 res = res % (guint)2;
1309 if (res != node->parity)
1310 g_print ("parity incorrect for node\n");
1312 if (get_parity (node) != 1)
1313 g_error ("Node has incorrect parity %d", get_parity (node));
1319 _gtk_rbtree_test_height (GtkRBTree *tree,
1322 gint computed_offset = 0;
1324 /* This whole test is sort of a useless truism. */
1326 if (node->left != tree->nil)
1327 computed_offset += node->left->offset;
1329 if (node->right != tree->nil)
1330 computed_offset += node->right->offset;
1332 if (node->children && node->children->root != node->children->nil)
1333 computed_offset += node->children->root->offset;
1335 if (GTK_RBNODE_GET_HEIGHT (node) + computed_offset != node->offset)
1336 g_error ("node has broken offset\n");
1338 if (node->left != tree->nil)
1339 _gtk_rbtree_test_height (tree, node->left);
1341 if (node->right != tree->nil)
1342 _gtk_rbtree_test_height (tree, node->right);
1344 if (node->children && node->children->root != node->children->nil)
1345 _gtk_rbtree_test_height (node->children, node->children->root);
1349 _gtk_rbtree_test (const gchar *where,
1352 GtkRBTree *tmp_tree;
1354 /* Test the entire tree */
1356 while (tmp_tree->parent_tree)
1357 tmp_tree = tmp_tree->parent_tree;
1359 g_print ("%s: whole tree offset is %d\n", where, tmp_tree->root->offset);
1361 if (tmp_tree->root != tmp_tree->nil)
1363 g_assert ((_count_nodes (tmp_tree, tmp_tree->root->left) +
1364 _count_nodes (tmp_tree, tmp_tree->root->right) + 1) == tmp_tree->root->count);
1367 _gtk_rbtree_test_height (tmp_tree, tmp_tree->root);
1369 g_assert (count_parity (tmp_tree, tmp_tree->root) == tmp_tree->root->parity);