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_right (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);
211 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_DESCENDANTS_INVALID))
213 if ((! GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID)) &&
214 (node->right != tree->nil && ! GTK_RBNODE_FLAG_SET (node->right, GTK_RBNODE_DESCENDANTS_INVALID)) &&
215 (node->left != tree->nil && ! GTK_RBNODE_FLAG_SET (node->left, GTK_RBNODE_DESCENDANTS_INVALID)) &&
216 (node->children && GTK_RBNODE_FLAG_SET (node->children->root, GTK_RBNODE_DESCENDANTS_INVALID)))
217 GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
219 if (GTK_RBNODE_FLAG_SET (right, GTK_RBNODE_DESCENDANTS_INVALID))
221 if ((! GTK_RBNODE_FLAG_SET (right, GTK_RBNODE_INVALID)) &&
222 (right->right != tree->nil && ! GTK_RBNODE_FLAG_SET (right->right, GTK_RBNODE_DESCENDANTS_INVALID)) &&
223 (right->left != tree->nil && ! GTK_RBNODE_FLAG_SET (right->left, GTK_RBNODE_DESCENDANTS_INVALID)) &&
224 (right->children && GTK_RBNODE_FLAG_SET (right->children->root, GTK_RBNODE_DESCENDANTS_INVALID)))
225 GTK_RBNODE_UNSET_FLAG (right, GTK_RBNODE_DESCENDANTS_INVALID);
230 _gtk_rbnode_rotate_right (GtkRBTree *tree,
233 gint node_height, left_height;
234 guint node_parity, left_parity;
235 GtkRBNode *left = node->left;
237 g_return_if_fail (node != tree->nil);
239 node_height = node->offset -
240 (node->left?node->left->offset:0) -
241 (node->right?node->right->offset:0) -
242 (node->children?node->children->root->offset:0);
243 left_height = left->offset -
244 (left->left?left->left->offset:0) -
245 (left->right?left->right->offset:0) -
246 (left->children?left->children->root->offset:0);
248 node_parity = node->parity -
249 (node->left?node->left->parity:0) -
250 (node->right?node->right->parity:0) -
251 (node->children?node->children->root->parity:0);
252 left_parity = left->parity -
253 (left->left?left->left->parity:0) -
254 (left->right?left->right->parity:0) -
255 (left->children?left->children->root->parity:0);
257 node->left = left->right;
258 if (left->right != tree->nil)
259 left->right->parent = node;
261 if (left != tree->nil)
262 left->parent = node->parent;
263 if (node->parent != tree->nil)
265 if (node == node->parent->right)
266 node->parent->right = left;
268 node->parent->left = left;
275 /* link node and left */
277 if (node != tree->nil)
280 node->count = 1 + (node->left?node->left->count:0) +
281 (node->right?node->right->count:0);
282 left->count = 1 + (left->left?left->left->count:0) +
283 (left->right?left->right->count:0);
285 node->offset = node_height +
286 (node->left?node->left->offset:0) +
287 (node->right?node->right->offset:0) +
288 (node->children?node->children->root->offset:0);
289 left->offset = left_height +
290 (left->left?left->left->offset:0) +
291 (left->right?left->right->offset:0) +
292 (left->children?left->children->root->offset:0);
294 node->parity = node_parity +
295 (node->left?node->left->parity:0) +
296 (node->right?node->right->parity:0) +
297 (node->children?node->children->root->parity:0);
298 left->parity = left_parity +
299 (left->left?left->left->parity:0) +
300 (left->right?left->right->parity:0) +
301 (left->children?left->children->root->parity:0);
303 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_DESCENDANTS_INVALID))
305 if ((! GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID)) &&
306 (node->right != tree->nil && ! GTK_RBNODE_FLAG_SET (node->right, GTK_RBNODE_DESCENDANTS_INVALID)) &&
307 (node->left != tree->nil && ! GTK_RBNODE_FLAG_SET (node->left, GTK_RBNODE_DESCENDANTS_INVALID)) &&
308 (node->children && GTK_RBNODE_FLAG_SET (node->children->root, GTK_RBNODE_DESCENDANTS_INVALID)))
309 GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
311 if (GTK_RBNODE_FLAG_SET (left, GTK_RBNODE_DESCENDANTS_INVALID))
313 if ((! GTK_RBNODE_FLAG_SET (left, GTK_RBNODE_INVALID)) &&
314 (left->right != tree->nil && ! GTK_RBNODE_FLAG_SET (left->right, GTK_RBNODE_DESCENDANTS_INVALID)) &&
315 (left->left != tree->nil && ! GTK_RBNODE_FLAG_SET (left->left, GTK_RBNODE_DESCENDANTS_INVALID)) &&
316 (left->children && GTK_RBNODE_FLAG_SET (left->children->root, GTK_RBNODE_DESCENDANTS_INVALID)))
317 GTK_RBNODE_UNSET_FLAG (left, GTK_RBNODE_DESCENDANTS_INVALID);
322 _gtk_rbtree_insert_fixup (GtkRBTree *tree,
326 /* check Red-Black properties */
327 while (node != tree->root && GTK_RBNODE_GET_COLOR (node->parent) == GTK_RBNODE_RED)
329 /* we have a violation */
330 if (node->parent == node->parent->parent->left)
332 GtkRBNode *y = node->parent->parent->right;
333 if (GTK_RBNODE_GET_COLOR (y) == GTK_RBNODE_RED)
335 /* uncle is GTK_RBNODE_RED */
336 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
337 GTK_RBNODE_SET_COLOR (y, GTK_RBNODE_BLACK);
338 GTK_RBNODE_SET_COLOR (node->parent->parent, GTK_RBNODE_RED);
339 node = node->parent->parent;
343 /* uncle is GTK_RBNODE_BLACK */
344 if (node == node->parent->right)
346 /* make node a left child */
348 _gtk_rbnode_rotate_left (tree, node);
351 /* recolor and rotate */
352 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
353 GTK_RBNODE_SET_COLOR (node->parent->parent, GTK_RBNODE_RED);
354 _gtk_rbnode_rotate_right(tree, node->parent->parent);
359 /* mirror image of above code */
360 GtkRBNode *y = node->parent->parent->left;
361 if (GTK_RBNODE_GET_COLOR (y) == GTK_RBNODE_RED)
363 /* uncle is GTK_RBNODE_RED */
364 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
365 GTK_RBNODE_SET_COLOR (y, GTK_RBNODE_BLACK);
366 GTK_RBNODE_SET_COLOR (node->parent->parent, GTK_RBNODE_RED);
367 node = node->parent->parent;
371 /* uncle is GTK_RBNODE_BLACK */
372 if (node == node->parent->left)
375 _gtk_rbnode_rotate_right (tree, node);
377 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
378 GTK_RBNODE_SET_COLOR (node->parent->parent, GTK_RBNODE_RED);
379 _gtk_rbnode_rotate_left (tree, node->parent->parent);
383 GTK_RBNODE_SET_COLOR (tree->root, GTK_RBNODE_BLACK);
387 _gtk_rbtree_remove_node_fixup (GtkRBTree *tree,
390 while (node != tree->root && GTK_RBNODE_GET_COLOR (node) == GTK_RBNODE_BLACK)
392 if (node == node->parent->left)
394 GtkRBNode *w = node->parent->right;
395 if (GTK_RBNODE_GET_COLOR (w) == GTK_RBNODE_RED)
397 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_BLACK);
398 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_RED);
399 _gtk_rbnode_rotate_left (tree, node->parent);
400 w = node->parent->right;
402 if (GTK_RBNODE_GET_COLOR (w->left) == GTK_RBNODE_BLACK && GTK_RBNODE_GET_COLOR (w->right) == GTK_RBNODE_BLACK)
404 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_RED);
409 if (GTK_RBNODE_GET_COLOR (w->right) == GTK_RBNODE_BLACK)
411 GTK_RBNODE_SET_COLOR (w->left, GTK_RBNODE_BLACK);
412 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_RED);
413 _gtk_rbnode_rotate_right (tree, w);
414 w = node->parent->right;
416 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_GET_COLOR (node->parent));
417 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
418 GTK_RBNODE_SET_COLOR (w->right, GTK_RBNODE_BLACK);
419 _gtk_rbnode_rotate_left (tree, node->parent);
425 GtkRBNode *w = node->parent->left;
426 if (GTK_RBNODE_GET_COLOR (w) == GTK_RBNODE_RED)
428 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_BLACK);
429 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_RED);
430 _gtk_rbnode_rotate_right (tree, node->parent);
431 w = node->parent->left;
433 if (GTK_RBNODE_GET_COLOR (w->right) == GTK_RBNODE_BLACK && GTK_RBNODE_GET_COLOR (w->left) == GTK_RBNODE_BLACK)
435 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_RED);
440 if (GTK_RBNODE_GET_COLOR (w->left) == GTK_RBNODE_BLACK)
442 GTK_RBNODE_SET_COLOR (w->right, GTK_RBNODE_BLACK);
443 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_RED);
444 _gtk_rbnode_rotate_left (tree, w);
445 w = node->parent->left;
447 GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_GET_COLOR (node->parent));
448 GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
449 GTK_RBNODE_SET_COLOR (w->left, GTK_RBNODE_BLACK);
450 _gtk_rbnode_rotate_right (tree, node->parent);
455 GTK_RBNODE_SET_COLOR (node, GTK_RBNODE_BLACK);
458 /* Public functions */
460 _gtk_rbnode_push_allocator (GAllocator *allocator)
462 G_LOCK (current_allocator);
463 _gtk_rbnode_validate_allocator ( allocator );
464 allocator->last = current_allocator;
465 current_allocator = allocator;
466 G_UNLOCK (current_allocator);
470 _gtk_rbnode_pop_allocator (void)
472 G_LOCK (current_allocator);
473 if (current_allocator)
475 GAllocator *allocator;
477 allocator = current_allocator;
478 current_allocator = allocator->last;
479 allocator->last = NULL;
480 allocator->is_unused = TRUE;
482 G_UNLOCK (current_allocator);
486 _gtk_rbtree_new (void)
490 retval = (GtkRBTree *) g_new (GtkRBTree, 1);
491 retval->parent_tree = NULL;
492 retval->parent_node = NULL;
494 retval->nil = g_new0 (GtkRBNode, 1);
495 retval->nil->left = NULL;
496 retval->nil->right = NULL;
497 retval->nil->parent = NULL;
498 retval->nil->flags = GTK_RBNODE_BLACK;
499 retval->nil->count = 0;
500 retval->nil->offset = 0;
501 retval->nil->parity = 0;
503 retval->root = retval->nil;
508 _gtk_rbtree_free_helper (GtkRBTree *tree,
513 _gtk_rbtree_free (node->children);
515 _gtk_rbnode_free (node);
519 _gtk_rbtree_free (GtkRBTree *tree)
521 _gtk_rbtree_traverse (tree,
524 _gtk_rbtree_free_helper,
527 if (tree->parent_node &&
528 tree->parent_node->children == tree)
529 tree->parent_node->children = NULL;
530 _gtk_rbnode_free (tree->nil);
535 _gtk_rbtree_remove (GtkRBTree *tree)
540 gint height = tree->root->offset;
542 if (gtk_debug_flags & GTK_DEBUG_TREE)
543 _gtk_rbtree_test (G_STRLOC, tree);
545 tmp_tree = tree->parent_tree;
546 tmp_node = tree->parent_node;
548 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
550 tmp_node->offset -= height;
552 /* If the removed tree was odd, flip all parents */
553 if (tree->root->parity)
554 tmp_node->parity = !tmp_node->parity;
556 tmp_node = tmp_node->parent;
557 if (tmp_node == tmp_tree->nil)
559 tmp_node = tmp_tree->parent_node;
560 tmp_tree = tmp_tree->parent_tree;
564 tmp_tree = tree->parent_tree;
565 tmp_node = tree->parent_node;
566 _gtk_rbtree_free (tree);
568 if (gtk_debug_flags & GTK_DEBUG_TREE)
569 _gtk_rbtree_test (G_STRLOC, tmp_tree);
574 _gtk_rbtree_insert_after (GtkRBTree *tree,
579 gboolean right = TRUE;
583 if (gtk_debug_flags & GTK_DEBUG_TREE)
584 _gtk_rbtree_test (G_STRLOC, tree);
586 if (current != NULL && current->right != tree->nil)
588 current = current->right;
589 while (current->left != tree->nil)
590 current = current->left;
595 node = _gtk_rbnode_new (tree, height);
596 node->parent = (current?current:tree->nil);
598 /* insert node in tree */
602 current->right = node;
604 current->left = node;
605 tmp_node = node->parent;
611 tmp_node = tree->parent_node;
612 tmp_tree = tree->parent_tree;
615 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
617 /* We only want to propagate the count if we are in the tree we
619 if (tmp_tree == tree)
622 tmp_node->parity += 1;
623 tmp_node->offset += height;
624 tmp_node = tmp_node->parent;
625 if (tmp_node == tmp_tree->nil)
627 tmp_node = tmp_tree->parent_node;
628 tmp_tree = tmp_tree->parent_tree;
631 _gtk_rbtree_insert_fixup (tree, node);
633 if (gtk_debug_flags & GTK_DEBUG_TREE)
634 _gtk_rbtree_test (G_STRLOC, tree);
640 _gtk_rbtree_insert_before (GtkRBTree *tree,
645 gboolean left = TRUE;
649 if (gtk_debug_flags & GTK_DEBUG_TREE)
650 _gtk_rbtree_test (G_STRLOC, tree);
652 if (current != NULL && current->left != tree->nil)
654 current = current->left;
655 while (current->right != tree->nil)
656 current = current->right;
661 node = _gtk_rbnode_new (tree, height);
662 node->parent = (current?current:tree->nil);
664 /* insert node in tree */
668 current->left = node;
670 current->right = node;
671 tmp_node = node->parent;
677 tmp_node = tree->parent_node;
678 tmp_tree = tree->parent_tree;
681 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
683 /* We only want to propagate the count if we are in the tree we
685 if (tmp_tree == tree)
688 tmp_node->parity += 1;
689 tmp_node->offset += height;
690 tmp_node = tmp_node->parent;
691 if (tmp_node == tmp_tree->nil)
693 tmp_node = tmp_tree->parent_node;
694 tmp_tree = tmp_tree->parent_tree;
697 _gtk_rbtree_insert_fixup (tree, node);
699 if (gtk_debug_flags & GTK_DEBUG_TREE)
700 _gtk_rbtree_test (G_STRLOC, tree);
706 _gtk_rbtree_find_count (GtkRBTree *tree,
712 while (node != tree->nil && (node->left->count + 1 != count))
714 if (node->left->count >= count)
718 count -= (node->left->count + 1);
722 if (node == tree->nil)
728 _gtk_rbtree_node_set_height (GtkRBTree *tree,
732 gint diff = height - GTK_RBNODE_GET_HEIGHT (node);
733 GtkRBNode *tmp_node = node;
734 GtkRBTree *tmp_tree = tree;
739 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
741 tmp_node->offset += diff;
742 tmp_node = tmp_node->parent;
743 if (tmp_node == tmp_tree->nil)
745 tmp_node = tmp_tree->parent_node;
746 tmp_tree = tmp_tree->parent_tree;
752 _gtk_rbtree_node_mark_invalid (GtkRBTree *tree,
755 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID))
758 GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_INVALID);
761 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_DESCENDANTS_INVALID))
763 GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
767 node = tree->parent_node;
768 tree = tree->parent_tree;
775 _gtk_rbtree_node_mark_valid (GtkRBTree *tree,
778 if (! GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID))
781 GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_INVALID);
784 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID) ||
785 (node->children && GTK_RBNODE_FLAG_SET (node->children->root, GTK_RBNODE_DESCENDANTS_INVALID)) ||
786 (node->left && GTK_RBNODE_FLAG_SET (node->left, GTK_RBNODE_DESCENDANTS_INVALID)) ||
787 (node->right && GTK_RBNODE_FLAG_SET (node->right, GTK_RBNODE_DESCENDANTS_INVALID)))
790 GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
794 node = tree->parent_node;
795 tree = tree->parent_tree;
801 typedef struct _GtkRBReorder
812 gtk_rbtree_reorder_sort_func (gconstpointer a,
815 return ((GtkRBReorder *) a)->order > ((GtkRBReorder *) b)->order;
819 gtk_rbtree_reorder_invert_func (gconstpointer a,
822 return ((GtkRBReorder *) a)->invert_order > ((GtkRBReorder *) b)->invert_order;
826 gtk_rbtree_reorder_fixup (GtkRBTree *tree,
829 if (node == tree->nil)
834 if (node->left != tree->nil)
836 gtk_rbtree_reorder_fixup (tree, node->left);
837 node->offset += node->left->offset;
838 node->parity += node->left->parity;
840 if (node->right != tree->nil)
842 gtk_rbtree_reorder_fixup (tree, node->right);
843 node->offset += node->right->offset;
844 node->parity += node->right->parity;
849 node->offset += node->children->root->offset;
850 node->parity += node->children->root->parity;
854 /* It basically pulls everything out of the tree, rearranges it, and puts it
855 * back together. Our strategy is to keep the old RBTree intact, and just
856 * rearrange the contents. When that is done, we go through and update the
857 * heights. There is probably a more elegant way to write this function. If
858 * anyone wants to spend the time writing it, patches will be accepted.
861 _gtk_rbtree_reorder (GtkRBTree *tree,
865 GtkRBReorder reorder;
870 g_return_if_fail (tree != NULL);
871 g_return_if_fail (length > 0);
872 g_return_if_fail (tree->root->count == length);
874 /* Sort the trees values in the new tree. */
875 array = g_array_sized_new (FALSE, FALSE, sizeof (GtkRBReorder), length);
876 for (i = 0; i < length; i++)
878 reorder.order = new_order[i];
879 reorder.invert_order = i;
880 g_array_append_val (array, reorder);
883 g_array_sort(array, gtk_rbtree_reorder_sort_func);
887 while (node && node->left != tree->nil)
890 for (i = 0; i < length; i++)
892 g_assert (node != tree->nil);
893 g_array_index (array, GtkRBReorder, i).children = node->children;
894 g_array_index (array, GtkRBReorder, i).flags = GTK_RBNODE_NON_COLORS & node->flags;
895 g_array_index (array, GtkRBReorder, i).height = GTK_RBNODE_GET_HEIGHT (node);
897 node = _gtk_rbtree_next (tree, node);
900 g_array_sort (array, gtk_rbtree_reorder_invert_func);
904 while (node && node->left != tree->nil)
907 /* Go through the tree and change the values to the new ones. */
908 for (i = 0; i < length; i++)
910 reorder = g_array_index (array, GtkRBReorder, i);
911 node->children = reorder.children;
913 node->children->parent_node = node;
914 node->flags = GTK_RBNODE_GET_COLOR (node) | reorder.flags;
915 /* We temporarily set the height to this. */
916 node->offset = reorder.height;
917 node = _gtk_rbtree_next (tree, node);
919 gtk_rbtree_reorder_fixup (tree, tree->root);
924 _gtk_rbtree_node_find_offset (GtkRBTree *tree,
931 g_assert (node->left);
933 retval = node->left->offset;
935 while (tree && node && node != tree->nil)
940 /* Add left branch, plus children, iff we came from the right */
941 if (node->right == last)
942 retval += node->offset - node->right->offset;
944 if (node == tree->nil)
946 node = tree->parent_node;
947 tree = tree->parent_tree;
949 /* Add the parent node, plus the left branch. */
951 retval += node->left->offset + GTK_RBNODE_GET_HEIGHT (node);
958 _gtk_rbtree_node_find_parity (GtkRBTree *tree,
965 g_assert (node->left);
967 retval = node->left->parity;
969 while (tree && node && node != tree->nil)
974 /* Add left branch, plus children, iff we came from the right */
975 if (node->right == last)
976 retval += node->parity - node->right->parity;
978 if (node == tree->nil)
980 node = tree->parent_node;
981 tree = tree->parent_tree;
983 /* Add the parent node, plus the left branch. */
985 retval += node->left->parity + 1; /* 1 == GTK_RBNODE_GET_PARITY() */
993 _gtk_rbtree_find_offset (GtkRBTree *tree,
995 GtkRBTree **new_tree,
996 GtkRBNode **new_node)
1007 tmp_node = tree->root;
1008 while (tmp_node != tree->nil &&
1009 (tmp_node->left->offset > height ||
1010 (tmp_node->offset - tmp_node->right->offset) < height))
1012 if (tmp_node->left->offset > height)
1013 tmp_node = tmp_node->left;
1016 height -= (tmp_node->offset - tmp_node->right->offset);
1017 tmp_node = tmp_node->right;
1020 if (tmp_node == tree->nil)
1026 if (tmp_node->children)
1028 if ((tmp_node->offset -
1029 tmp_node->right->offset -
1030 tmp_node->children->root->offset) > height)
1033 *new_node = tmp_node;
1034 return (height - tmp_node->left->offset);
1036 return _gtk_rbtree_find_offset (tmp_node->children,
1037 height - tmp_node->left->offset -
1039 tmp_node->left->offset -
1040 tmp_node->right->offset -
1041 tmp_node->children->root->offset),
1046 *new_node = tmp_node;
1047 return (height - tmp_node->left->offset);
1052 _gtk_rbtree_remove_node (GtkRBTree *tree,
1056 GtkRBTree *tmp_tree;
1057 GtkRBNode *tmp_node;
1059 g_return_if_fail (tree != NULL);
1060 g_return_if_fail (node != NULL);
1062 /* make sure we're deleting a node that's actually in the tree */
1063 for (x = node; x->parent != tree->nil; x = x->parent)
1065 g_return_if_fail (x == tree->root);
1067 if (gtk_debug_flags & GTK_DEBUG_TREE)
1068 _gtk_rbtree_test (G_STRLOC, tree);
1070 if (node->left == tree->nil || node->right == tree->nil)
1078 while (y->left != tree->nil)
1082 /* adjust count only beneath tree */
1083 for (x = y; x != tree->nil; x = x->parent)
1086 /* y->count = node->count; */
1088 /* offsets and parity adjust all the way up through parent trees */
1093 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
1095 /* tmp_node->offset -= y->offset; */
1096 tmp_node->parity -= (guint) 1; /* parity of y is always 1 */
1098 tmp_node = tmp_node->parent;
1099 if (tmp_node == tmp_tree->nil)
1101 tmp_node = tmp_tree->parent_node;
1102 tmp_tree = tmp_tree->parent_tree;
1106 /* x is y's only child */
1107 if (y->left != tree->nil)
1112 /* remove y from the parent chain */
1113 x->parent = y->parent;
1114 if (y->parent != tree->nil)
1115 if (y == y->parent->left)
1116 y->parent->left = x;
1118 y->parent->right = x;
1124 /* Copy the node over */
1125 if (GTK_RBNODE_GET_COLOR (node) == GTK_RBNODE_BLACK)
1126 node->flags = ((y->flags & (GTK_RBNODE_NON_COLORS)) | GTK_RBNODE_BLACK);
1128 node->flags = ((y->flags & (GTK_RBNODE_NON_COLORS)) | GTK_RBNODE_RED);
1129 node->children = y->children;
1132 if (GTK_RBNODE_GET_COLOR (y) == GTK_RBNODE_BLACK)
1133 _gtk_rbtree_remove_node_fixup (tree, x);
1135 _gtk_rbnode_free (y);
1137 if (gtk_debug_flags & GTK_DEBUG_TREE)
1138 _gtk_rbtree_test (G_STRLOC, tree);
1142 _gtk_rbtree_next (GtkRBTree *tree,
1145 g_return_val_if_fail (tree != NULL, NULL);
1146 g_return_val_if_fail (node != NULL, NULL);
1148 /* Case 1: the node's below us. */
1149 if (node->right != tree->nil)
1152 while (node->left != tree->nil)
1157 /* Case 2: it's an ancestor */
1158 while (node->parent != tree->nil)
1160 if (node->parent->right == node)
1161 node = node->parent;
1163 return (node->parent);
1166 /* Case 3: There is no next node */
1171 _gtk_rbtree_prev (GtkRBTree *tree,
1174 g_return_val_if_fail (tree != NULL, NULL);
1175 g_return_val_if_fail (node != NULL, NULL);
1177 /* Case 1: the node's below us. */
1178 if (node->left != tree->nil)
1181 while (node->right != tree->nil)
1186 /* Case 2: it's an ancestor */
1187 while (node->parent != tree->nil)
1189 if (node->parent->left == node)
1190 node = node->parent;
1192 return (node->parent);
1195 /* Case 3: There is no next node */
1200 _gtk_rbtree_next_full (GtkRBTree *tree,
1202 GtkRBTree **new_tree,
1203 GtkRBNode **new_node)
1205 g_return_if_fail (tree != NULL);
1206 g_return_if_fail (node != NULL);
1207 g_return_if_fail (new_tree != NULL);
1208 g_return_if_fail (new_node != NULL);
1212 *new_tree = node->children;
1213 *new_node = (*new_tree)->root;
1214 while ((*new_node)->left != (*new_tree)->nil)
1215 *new_node = (*new_node)->left;
1220 *new_node = _gtk_rbtree_next (tree, node);
1222 while ((*new_node == NULL) &&
1223 (*new_tree != NULL))
1225 *new_node = (*new_tree)->parent_node;
1226 *new_tree = (*new_tree)->parent_tree;
1228 *new_node = _gtk_rbtree_next (*new_tree, *new_node);
1233 _gtk_rbtree_prev_full (GtkRBTree *tree,
1235 GtkRBTree **new_tree,
1236 GtkRBNode **new_node)
1238 g_return_if_fail (tree != NULL);
1239 g_return_if_fail (node != NULL);
1240 g_return_if_fail (new_tree != NULL);
1241 g_return_if_fail (new_node != NULL);
1244 *new_node = _gtk_rbtree_prev (tree, node);
1246 if (*new_node == NULL)
1248 *new_node = (*new_tree)->parent_node;
1249 *new_tree = (*new_tree)->parent_tree;
1253 while ((*new_node)->children)
1255 *new_tree = (*new_node)->children;
1256 *new_node = (*new_tree)->root;
1257 while ((*new_node)->right != (*new_tree)->nil)
1258 *new_node = (*new_node)->right;
1264 _gtk_rbtree_get_depth (GtkRBTree *tree)
1266 GtkRBTree *tmp_tree;
1269 tmp_tree = tree->parent_tree;
1273 tmp_tree = tmp_tree->parent_tree;
1280 _gtk_rbtree_traverse_pre_order (GtkRBTree *tree,
1282 GtkRBTreeTraverseFunc func,
1285 if (node == tree->nil)
1288 (* func) (tree, node, data);
1289 _gtk_rbtree_traverse_pre_order (tree, node->left, func, data);
1290 _gtk_rbtree_traverse_pre_order (tree, node->right, func, data);
1294 _gtk_rbtree_traverse_post_order (GtkRBTree *tree,
1296 GtkRBTreeTraverseFunc func,
1299 if (node == tree->nil)
1302 _gtk_rbtree_traverse_post_order (tree, node->left, func, data);
1303 _gtk_rbtree_traverse_post_order (tree, node->right, func, data);
1304 (* func) (tree, node, data);
1308 _gtk_rbtree_traverse (GtkRBTree *tree,
1310 GTraverseType order,
1311 GtkRBTreeTraverseFunc func,
1314 g_return_if_fail (tree != NULL);
1315 g_return_if_fail (node != NULL);
1316 g_return_if_fail (func != NULL);
1317 g_return_if_fail (order <= G_LEVEL_ORDER);
1322 _gtk_rbtree_traverse_pre_order (tree, node, func, data);
1325 _gtk_rbtree_traverse_post_order (tree, node, func, data);
1330 g_warning ("unsupported traversal order.");
1336 _count_nodes (GtkRBTree *tree,
1340 if (node == tree->nil)
1343 g_assert (node->left);
1344 g_assert (node->right);
1346 res = (_count_nodes (tree, node->left) +
1347 _count_nodes (tree, node->right) + 1);
1349 if (res != node->count)
1350 g_print ("Tree failed\n");
1355 get_parity (GtkRBNode *node)
1357 guint child_total = 0;
1360 /* The parity of a node is node->parity minus
1361 * the parity of left, right, and children.
1363 * This is equivalent to saying that if left, right, children
1364 * sum to 0 parity, then node->parity is the parity of node,
1365 * and if left, right, children are odd parity, then
1366 * node->parity is the reverse of the node's parity.
1369 child_total += (guint) node->left->parity;
1370 child_total += (guint) node->right->parity;
1373 child_total += (guint) node->children->root->parity;
1375 rem = child_total % 2;
1378 return node->parity;
1380 return !node->parity;
1384 count_parity (GtkRBTree *tree,
1389 if (node == tree->nil)
1393 count_parity (tree, node->left) +
1394 count_parity (tree, node->right) +
1396 (node->children ? count_parity (node->children, node->children->root) : 0);
1398 res = res % (guint)2;
1400 if (res != node->parity)
1401 g_print ("parity incorrect for node\n");
1403 if (get_parity (node) != 1)
1404 g_error ("Node has incorrect parity %d", get_parity (node));
1410 _gtk_rbtree_test_height (GtkRBTree *tree,
1413 gint computed_offset = 0;
1415 /* This whole test is sort of a useless truism. */
1417 if (node->left != tree->nil)
1418 computed_offset += node->left->offset;
1420 if (node->right != tree->nil)
1421 computed_offset += node->right->offset;
1423 if (node->children && node->children->root != node->children->nil)
1424 computed_offset += node->children->root->offset;
1426 if (GTK_RBNODE_GET_HEIGHT (node) + computed_offset != node->offset)
1427 g_error ("node has broken offset\n");
1429 if (node->left != tree->nil)
1430 _gtk_rbtree_test_height (tree, node->left);
1432 if (node->right != tree->nil)
1433 _gtk_rbtree_test_height (tree, node->right);
1435 if (node->children && node->children->root != node->children->nil)
1436 _gtk_rbtree_test_height (node->children, node->children->root);
1440 _gtk_rbtree_test (const gchar *where,
1443 GtkRBTree *tmp_tree;
1445 /* Test the entire tree */
1447 while (tmp_tree->parent_tree)
1448 tmp_tree = tmp_tree->parent_tree;
1450 g_print ("%s: whole tree offset is %d\n", where, tmp_tree->root->offset);
1452 if (tmp_tree->root != tmp_tree->nil)
1454 g_assert ((_count_nodes (tmp_tree, tmp_tree->root->left) +
1455 _count_nodes (tmp_tree, tmp_tree->root->right) + 1) == tmp_tree->root->count);
1458 _gtk_rbtree_test_height (tmp_tree, tmp_tree->root);
1460 g_assert (count_parity (tmp_tree, tmp_tree->root) == tmp_tree->root->parity);