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;
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;
718 _gtk_rbtree_node_find_offset (GtkRBTree *tree,
725 g_assert (node->left);
727 retval = node->left->offset;
729 while (tree && node && node != tree->nil)
734 /* Add left branch, plus children, iff we came from the right */
735 if (node->right == last)
736 retval += node->offset - node->right->offset;
738 if (node == tree->nil)
740 node = tree->parent_node;
741 tree = tree->parent_tree;
743 /* Add the parent node, plus the left branch. */
745 retval += node->left->offset + GTK_RBNODE_GET_HEIGHT (node);
752 _gtk_rbtree_node_find_parity (GtkRBTree *tree,
759 g_assert (node->left);
761 retval = node->left->parity;
763 while (tree && node && node != tree->nil)
768 /* Add left branch, plus children, iff we came from the right */
769 if (node->right == last)
770 retval += node->parity - node->right->parity;
772 if (node == tree->nil)
774 node = tree->parent_node;
775 tree = tree->parent_tree;
777 /* Add the parent node, plus the left branch. */
779 retval += node->left->parity + 1; /* 1 == GTK_RBNODE_GET_PARITY() */
787 _gtk_rbtree_find_offset (GtkRBTree *tree,
789 GtkRBTree **new_tree,
790 GtkRBNode **new_node)
801 tmp_node = tree->root;
802 while (tmp_node != tree->nil &&
803 (tmp_node->left->offset > height ||
804 (tmp_node->offset - tmp_node->right->offset) < height))
806 if (tmp_node->left->offset > height)
807 tmp_node = tmp_node->left;
810 height -= (tmp_node->offset - tmp_node->right->offset);
811 tmp_node = tmp_node->right;
814 if (tmp_node == tree->nil)
820 if (tmp_node->children)
822 if ((tmp_node->offset -
823 tmp_node->right->offset -
824 tmp_node->children->root->offset) > height)
827 *new_node = tmp_node;
828 return (height - tmp_node->left->offset);
830 return _gtk_rbtree_find_offset (tmp_node->children,
831 height - tmp_node->left->offset -
833 tmp_node->left->offset -
834 tmp_node->right->offset -
835 tmp_node->children->root->offset),
840 *new_node = tmp_node;
841 return (height - tmp_node->left->offset);
846 _gtk_rbtree_remove_node (GtkRBTree *tree,
853 g_return_if_fail (tree != NULL);
854 g_return_if_fail (node != NULL);
856 /* make sure we're deleting a node that's actually in the tree */
857 for (x = node; x->parent != tree->nil; x = x->parent)
859 g_return_if_fail (x == tree->root);
861 if (gtk_debug_flags & GTK_DEBUG_TREE)
862 _gtk_rbtree_test (G_STRLOC, tree);
864 if (node->left == tree->nil || node->right == tree->nil)
872 while (y->left != tree->nil)
876 /* adjust count only beneath tree */
877 for (x = y; x != tree->nil; x = x->parent)
880 /* y->count = node->count; */
882 /* offsets and parity adjust all the way up through parent trees */
887 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
889 /* tmp_node->offset -= y->offset; */
890 tmp_node->parity -= (guint) 1; /* parity of y is always 1 */
892 tmp_node = tmp_node->parent;
893 if (tmp_node == tmp_tree->nil)
895 tmp_node = tmp_tree->parent_node;
896 tmp_tree = tmp_tree->parent_tree;
900 /* x is y's only child */
901 if (y->left != tree->nil)
906 /* remove y from the parent chain */
907 x->parent = y->parent;
908 if (y->parent != tree->nil)
909 if (y == y->parent->left)
912 y->parent->right = x;
918 /* Copy the node over */
919 if (GTK_RBNODE_GET_COLOR (node) == GTK_RBNODE_BLACK)
920 node->flags = ((y->flags & (GTK_RBNODE_NON_COLORS)) | GTK_RBNODE_BLACK);
922 node->flags = ((y->flags & (GTK_RBNODE_NON_COLORS)) | GTK_RBNODE_RED);
923 node->children = y->children;
926 if (GTK_RBNODE_GET_COLOR (y) == GTK_RBNODE_BLACK)
927 _gtk_rbtree_remove_node_fixup (tree, x);
929 _gtk_rbnode_free (y);
931 if (gtk_debug_flags & GTK_DEBUG_TREE)
932 _gtk_rbtree_test (G_STRLOC, tree);
936 _gtk_rbtree_next (GtkRBTree *tree,
939 g_return_val_if_fail (tree != NULL, NULL);
940 g_return_val_if_fail (node != NULL, NULL);
942 /* Case 1: the node's below us. */
943 if (node->right != tree->nil)
946 while (node->left != tree->nil)
951 /* Case 2: it's an ancestor */
952 while (node->parent != tree->nil)
954 if (node->parent->right == node)
957 return (node->parent);
960 /* Case 3: There is no next node */
965 _gtk_rbtree_prev (GtkRBTree *tree,
968 g_return_val_if_fail (tree != NULL, NULL);
969 g_return_val_if_fail (node != NULL, NULL);
971 /* Case 1: the node's below us. */
972 if (node->left != tree->nil)
975 while (node->right != tree->nil)
980 /* Case 2: it's an ancestor */
981 while (node->parent != tree->nil)
983 if (node->parent->left == node)
986 return (node->parent);
989 /* Case 3: There is no next node */
994 _gtk_rbtree_next_full (GtkRBTree *tree,
996 GtkRBTree **new_tree,
997 GtkRBNode **new_node)
999 g_return_if_fail (tree != NULL);
1000 g_return_if_fail (node != NULL);
1001 g_return_if_fail (new_tree != NULL);
1002 g_return_if_fail (new_node != NULL);
1006 *new_tree = node->children;
1007 *new_node = (*new_tree)->root;
1008 while ((*new_node)->left != (*new_tree)->nil)
1009 *new_node = (*new_node)->left;
1014 *new_node = _gtk_rbtree_next (tree, node);
1016 while ((*new_node == NULL) &&
1017 (*new_tree != NULL))
1019 *new_node = (*new_tree)->parent_node;
1020 *new_tree = (*new_tree)->parent_tree;
1022 *new_node = _gtk_rbtree_next (*new_tree, *new_node);
1027 _gtk_rbtree_prev_full (GtkRBTree *tree,
1029 GtkRBTree **new_tree,
1030 GtkRBNode **new_node)
1032 g_return_if_fail (tree != NULL);
1033 g_return_if_fail (node != NULL);
1034 g_return_if_fail (new_tree != NULL);
1035 g_return_if_fail (new_node != NULL);
1038 *new_node = _gtk_rbtree_prev (tree, node);
1040 if (*new_node == NULL)
1042 *new_node = (*new_tree)->parent_node;
1043 *new_tree = (*new_tree)->parent_tree;
1047 while ((*new_node)->children)
1049 *new_tree = (*new_node)->children;
1050 *new_node = (*new_tree)->root;
1051 while ((*new_node)->right != (*new_tree)->nil)
1052 *new_node = (*new_node)->right;
1058 _gtk_rbtree_get_depth (GtkRBTree *tree)
1060 GtkRBTree *tmp_tree;
1063 tmp_tree = tree->parent_tree;
1067 tmp_tree = tmp_tree->parent_tree;
1074 _gtk_rbtree_traverse_pre_order (GtkRBTree *tree,
1076 GtkRBTreeTraverseFunc func,
1079 if (node == tree->nil)
1082 (* func) (tree, node, data);
1083 _gtk_rbtree_traverse_pre_order (tree, node->left, func, data);
1084 _gtk_rbtree_traverse_pre_order (tree, node->right, func, data);
1088 _gtk_rbtree_traverse_post_order (GtkRBTree *tree,
1090 GtkRBTreeTraverseFunc func,
1093 if (node == tree->nil)
1096 _gtk_rbtree_traverse_post_order (tree, node->left, func, data);
1097 _gtk_rbtree_traverse_post_order (tree, node->right, func, data);
1098 (* func) (tree, node, data);
1102 _gtk_rbtree_traverse (GtkRBTree *tree,
1104 GTraverseType order,
1105 GtkRBTreeTraverseFunc func,
1108 g_return_if_fail (tree != NULL);
1109 g_return_if_fail (node != NULL);
1110 g_return_if_fail (func != NULL);
1111 g_return_if_fail (order <= G_LEVEL_ORDER);
1116 _gtk_rbtree_traverse_pre_order (tree, node, func, data);
1119 _gtk_rbtree_traverse_post_order (tree, node, func, data);
1124 g_warning ("unsupported traversal order.");
1130 _count_nodes (GtkRBTree *tree,
1134 if (node == tree->nil)
1137 g_assert (node->left);
1138 g_assert (node->right);
1140 res = (_count_nodes (tree, node->left) +
1141 _count_nodes (tree, node->right) + 1);
1143 if (res != node->count)
1144 g_print ("Tree failed\n");
1149 get_parity (GtkRBNode *node)
1151 guint child_total = 0;
1154 /* The parity of a node is node->parity minus
1155 * the parity of left, right, and children.
1157 * This is equivalent to saying that if left, right, children
1158 * sum to 0 parity, then node->parity is the parity of node,
1159 * and if left, right, children are odd parity, then
1160 * node->parity is the reverse of the node's parity.
1163 child_total += (guint) node->left->parity;
1164 child_total += (guint) node->right->parity;
1167 child_total += (guint) node->children->root->parity;
1169 rem = child_total % 2;
1172 return node->parity;
1174 return !node->parity;
1178 count_parity (GtkRBTree *tree,
1183 if (node == tree->nil)
1187 count_parity (tree, node->left) +
1188 count_parity (tree, node->right) +
1190 (node->children ? count_parity (node->children, node->children->root) : 0);
1192 res = res % (guint)2;
1194 if (res != node->parity)
1195 g_print ("parity incorrect for node\n");
1197 if (get_parity (node) != 1)
1198 g_error ("Node has incorrect parity %d", get_parity (node));
1204 _gtk_rbtree_test_height (GtkRBTree *tree,
1207 gint computed_offset = 0;
1209 /* This whole test is sort of a useless truism. */
1211 if (node->left != tree->nil)
1212 computed_offset += node->left->offset;
1214 if (node->right != tree->nil)
1215 computed_offset += node->right->offset;
1217 if (node->children && node->children->root != node->children->nil)
1218 computed_offset += node->children->root->offset;
1220 if (GTK_RBNODE_GET_HEIGHT (node) + computed_offset != node->offset)
1221 g_error ("node has broken offset\n");
1223 if (node->left != tree->nil)
1224 _gtk_rbtree_test_height (tree, node->left);
1226 if (node->right != tree->nil)
1227 _gtk_rbtree_test_height (tree, node->right);
1229 if (node->children && node->children->root != node->children->nil)
1230 _gtk_rbtree_test_height (node->children, node->children->root);
1234 _gtk_rbtree_test (const gchar *where,
1237 GtkRBTree *tmp_tree;
1239 /* Test the entire tree */
1241 while (tmp_tree->parent_tree)
1242 tmp_tree = tmp_tree->parent_tree;
1244 g_print ("%s: whole tree offset is %d\n", where, tmp_tree->root->offset);
1246 if (tmp_tree->root != tmp_tree->nil)
1248 g_assert ((_count_nodes (tmp_tree, tmp_tree->root->left) +
1249 _count_nodes (tmp_tree, tmp_tree->root->right) + 1) == tmp_tree->root->count);
1252 _gtk_rbtree_test_height (tmp_tree, tmp_tree->root);
1254 g_assert (count_parity (tmp_tree, tmp_tree->root) == tmp_tree->root->parity);