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,
580 gboolean right = TRUE;
584 if (gtk_debug_flags & GTK_DEBUG_TREE)
585 _gtk_rbtree_test (G_STRLOC, tree);
587 if (current != NULL && current->right != tree->nil)
589 current = current->right;
590 while (current->left != tree->nil)
591 current = current->left;
596 node = _gtk_rbnode_new (tree, height);
597 node->parent = (current?current:tree->nil);
599 /* insert node in tree */
603 current->right = node;
605 current->left = node;
606 tmp_node = node->parent;
612 tmp_node = tree->parent_node;
613 tmp_tree = tree->parent_tree;
616 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
618 /* We only want to propagate the count if we are in the tree we
620 if (tmp_tree == tree)
623 tmp_node->parity += 1;
624 tmp_node->offset += height;
625 tmp_node = tmp_node->parent;
626 if (tmp_node == tmp_tree->nil)
628 tmp_node = tmp_tree->parent_node;
629 tmp_tree = tmp_tree->parent_tree;
632 _gtk_rbtree_insert_fixup (tree, node);
634 if (gtk_debug_flags & GTK_DEBUG_TREE)
635 _gtk_rbtree_test (G_STRLOC, tree);
638 _gtk_rbtree_node_mark_valid (tree, node);
640 _gtk_rbtree_node_mark_invalid (tree, node);
646 _gtk_rbtree_insert_before (GtkRBTree *tree,
652 gboolean left = TRUE;
656 if (gtk_debug_flags & GTK_DEBUG_TREE)
657 _gtk_rbtree_test (G_STRLOC, tree);
659 if (current != NULL && current->left != tree->nil)
661 current = current->left;
662 while (current->right != tree->nil)
663 current = current->right;
668 node = _gtk_rbnode_new (tree, height);
669 node->parent = (current?current:tree->nil);
671 /* insert node in tree */
675 current->left = node;
677 current->right = node;
678 tmp_node = node->parent;
684 tmp_node = tree->parent_node;
685 tmp_tree = tree->parent_tree;
688 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
690 /* We only want to propagate the count if we are in the tree we
692 if (tmp_tree == tree)
695 tmp_node->parity += 1;
696 tmp_node->offset += height;
697 tmp_node = tmp_node->parent;
698 if (tmp_node == tmp_tree->nil)
700 tmp_node = tmp_tree->parent_node;
701 tmp_tree = tmp_tree->parent_tree;
704 _gtk_rbtree_insert_fixup (tree, node);
706 if (gtk_debug_flags & GTK_DEBUG_TREE)
707 _gtk_rbtree_test (G_STRLOC, tree);
710 _gtk_rbtree_node_mark_valid (tree, node);
712 _gtk_rbtree_node_mark_invalid (tree, node);
718 _gtk_rbtree_find_count (GtkRBTree *tree,
724 while (node != tree->nil && (node->left->count + 1 != count))
726 if (node->left->count >= count)
730 count -= (node->left->count + 1);
734 if (node == tree->nil)
740 _gtk_rbtree_node_set_height (GtkRBTree *tree,
744 gint diff = height - GTK_RBNODE_GET_HEIGHT (node);
745 GtkRBNode *tmp_node = node;
746 GtkRBTree *tmp_tree = tree;
751 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
753 tmp_node->offset += diff;
754 tmp_node = tmp_node->parent;
755 if (tmp_node == tmp_tree->nil)
757 tmp_node = tmp_tree->parent_node;
758 tmp_tree = tmp_tree->parent_tree;
764 _gtk_rbtree_node_mark_invalid (GtkRBTree *tree,
767 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID))
770 GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_INVALID);
773 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_DESCENDANTS_INVALID))
775 GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
779 node = tree->parent_node;
780 tree = tree->parent_tree;
787 _gtk_rbtree_node_mark_valid (GtkRBTree *tree,
790 if ((!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID)) &&
791 (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_COLUMN_INVALID)))
794 GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_INVALID);
795 GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_COLUMN_INVALID);
799 if ((GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID)) ||
800 (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_COLUMN_INVALID)) ||
801 (node->children && GTK_RBNODE_FLAG_SET (node->children->root, GTK_RBNODE_DESCENDANTS_INVALID)) ||
802 (node->left != tree->nil && GTK_RBNODE_FLAG_SET (node->left, GTK_RBNODE_DESCENDANTS_INVALID)) ||
803 (node->right != tree->nil && GTK_RBNODE_FLAG_SET (node->right, GTK_RBNODE_DESCENDANTS_INVALID)))
806 GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
810 node = tree->parent_node;
811 tree = tree->parent_tree;
818 /* Assume tree is the root node as it doesn't set DESCENDANTS_INVALID above.
821 _gtk_rbtree_column_invalid (GtkRBTree *tree)
830 while (node->left != tree->nil)
835 if (! (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID)))
836 GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_COLUMN_INVALID);
837 GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
840 _gtk_rbtree_column_invalid (node->children);
842 while ((node = _gtk_rbtree_next (tree, node)) != NULL);
845 typedef struct _GtkRBReorder
856 gtk_rbtree_reorder_sort_func (gconstpointer a,
859 return ((GtkRBReorder *) a)->order > ((GtkRBReorder *) b)->order;
863 gtk_rbtree_reorder_invert_func (gconstpointer a,
866 return ((GtkRBReorder *) a)->invert_order > ((GtkRBReorder *) b)->invert_order;
870 gtk_rbtree_reorder_fixup (GtkRBTree *tree,
873 if (node == tree->nil)
878 if (node->left != tree->nil)
880 gtk_rbtree_reorder_fixup (tree, node->left);
881 node->offset += node->left->offset;
882 node->parity += node->left->parity;
884 if (node->right != tree->nil)
886 gtk_rbtree_reorder_fixup (tree, node->right);
887 node->offset += node->right->offset;
888 node->parity += node->right->parity;
893 node->offset += node->children->root->offset;
894 node->parity += node->children->root->parity;
897 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID) ||
898 (node->right != tree->nil && GTK_RBNODE_FLAG_SET (node->right, GTK_RBNODE_DESCENDANTS_INVALID)) ||
899 (node->left != tree->nil && GTK_RBNODE_FLAG_SET (node->left, GTK_RBNODE_DESCENDANTS_INVALID)) ||
900 (node->children && GTK_RBNODE_FLAG_SET (node->children->root, GTK_RBNODE_DESCENDANTS_INVALID)))
901 GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
903 GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
906 /* It basically pulls everything out of the tree, rearranges it, and puts it
907 * back together. Our strategy is to keep the old RBTree intact, and just
908 * rearrange the contents. When that is done, we go through and update the
909 * heights. There is probably a more elegant way to write this function. If
910 * anyone wants to spend the time writing it, patches will be accepted.
913 _gtk_rbtree_reorder (GtkRBTree *tree,
917 GtkRBReorder reorder;
922 g_return_if_fail (tree != NULL);
923 g_return_if_fail (length > 0);
924 g_return_if_fail (tree->root->count == length);
926 /* Sort the trees values in the new tree. */
927 array = g_array_sized_new (FALSE, FALSE, sizeof (GtkRBReorder), length);
928 for (i = 0; i < length; i++)
930 reorder.order = new_order[i];
931 reorder.invert_order = i;
932 g_array_append_val (array, reorder);
935 g_array_sort(array, gtk_rbtree_reorder_sort_func);
939 while (node && node->left != tree->nil)
942 for (i = 0; i < length; i++)
944 g_assert (node != tree->nil);
945 g_array_index (array, GtkRBReorder, i).children = node->children;
946 g_array_index (array, GtkRBReorder, i).flags = GTK_RBNODE_NON_COLORS & node->flags;
947 g_array_index (array, GtkRBReorder, i).height = GTK_RBNODE_GET_HEIGHT (node);
949 node = _gtk_rbtree_next (tree, node);
952 g_array_sort (array, gtk_rbtree_reorder_invert_func);
956 while (node && node->left != tree->nil)
959 /* Go through the tree and change the values to the new ones. */
960 for (i = 0; i < length; i++)
962 reorder = g_array_index (array, GtkRBReorder, i);
963 node->children = reorder.children;
965 node->children->parent_node = node;
966 node->flags = GTK_RBNODE_GET_COLOR (node) | reorder.flags;
967 /* We temporarily set the height to this. */
968 node->offset = reorder.height;
969 node = _gtk_rbtree_next (tree, node);
971 gtk_rbtree_reorder_fixup (tree, tree->root);
976 _gtk_rbtree_node_find_offset (GtkRBTree *tree,
983 g_assert (node->left);
985 retval = node->left->offset;
987 while (tree && node && node != tree->nil)
992 /* Add left branch, plus children, iff we came from the right */
993 if (node->right == last)
994 retval += node->offset - node->right->offset;
996 if (node == tree->nil)
998 node = tree->parent_node;
999 tree = tree->parent_tree;
1001 /* Add the parent node, plus the left branch. */
1003 retval += node->left->offset + GTK_RBNODE_GET_HEIGHT (node);
1010 _gtk_rbtree_node_find_parity (GtkRBTree *tree,
1017 g_assert (node->left);
1019 retval = node->left->parity;
1021 while (tree && node && node != tree->nil)
1024 node = node->parent;
1026 /* Add left branch, plus children, iff we came from the right */
1027 if (node->right == last)
1028 retval += node->parity - node->right->parity;
1030 if (node == tree->nil)
1032 node = tree->parent_node;
1033 tree = tree->parent_tree;
1035 /* Add the parent node, plus the left branch. */
1037 retval += node->left->parity + 1; /* 1 == GTK_RBNODE_GET_PARITY() */
1045 _gtk_rbtree_real_find_offset (GtkRBTree *tree,
1047 GtkRBTree **new_tree,
1048 GtkRBNode **new_node)
1050 GtkRBNode *tmp_node;
1063 tmp_node = tree->root;
1064 while (tmp_node != tree->nil &&
1065 (tmp_node->left->offset > height ||
1066 (tmp_node->offset - tmp_node->right->offset) < height))
1068 if (tmp_node->left->offset > height)
1069 tmp_node = tmp_node->left;
1072 height -= (tmp_node->offset - tmp_node->right->offset);
1073 tmp_node = tmp_node->right;
1076 if (tmp_node == tree->nil)
1082 if (tmp_node->children)
1084 if ((tmp_node->offset -
1085 tmp_node->right->offset -
1086 tmp_node->children->root->offset) > height)
1089 *new_node = tmp_node;
1090 return (height - tmp_node->left->offset);
1092 return _gtk_rbtree_real_find_offset (tmp_node->children,
1093 height - tmp_node->left->offset -
1095 tmp_node->left->offset -
1096 tmp_node->right->offset -
1097 tmp_node->children->root->offset),
1102 *new_node = tmp_node;
1103 return (height - tmp_node->left->offset);
1107 _gtk_rbtree_find_offset (GtkRBTree *tree,
1109 GtkRBTree **new_tree,
1110 GtkRBNode **new_node)
1112 GtkRBNode *tmp_node;
1117 (height >= tree->root->offset))
1124 _gtk_rbtree_real_find_offset (tree, height, new_tree, new_node);
1128 _gtk_rbtree_remove_node (GtkRBTree *tree,
1132 GtkRBTree *tmp_tree;
1133 GtkRBNode *tmp_node;
1137 g_return_if_fail (tree != NULL);
1138 g_return_if_fail (node != NULL);
1140 /* make sure we're deleting a node that's actually in the tree */
1141 for (x = node; x->parent != tree->nil; x = x->parent)
1143 g_return_if_fail (x == tree->root);
1145 if (gtk_debug_flags & GTK_DEBUG_TREE)
1146 _gtk_rbtree_test (G_STRLOC, tree);
1148 if (node->left == tree->nil || node->right == tree->nil)
1156 while (y->left != tree->nil)
1160 /* adjust count only beneath tree */
1161 for (x = y; x != tree->nil; x = x->parent)
1164 /* y->count = node->count; */
1166 /* offsets and parity adjust all the way up through parent trees */
1167 y_height = GTK_RBNODE_GET_HEIGHT (y);
1168 node_height = GTK_RBNODE_GET_HEIGHT (node) + (node->children?node->children->root->offset:0);
1170 /* Do this twice for code clarities sake. */
1173 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
1175 tmp_node->offset -= (y_height + (y->children?y->children->root->offset:0));
1176 tmp_node->parity -= (1 + (y->children?y->children->root->parity:0));
1178 tmp_node = tmp_node->parent;
1179 if (tmp_node == tmp_tree->nil)
1181 tmp_node = tmp_tree->parent_node;
1182 tmp_tree = tmp_tree->parent_tree;
1186 /* x is y's only child */
1187 if (y->left != tree->nil)
1192 /* remove y from the parent chain */
1193 x->parent = y->parent;
1194 if (y->parent != tree->nil)
1195 if (y == y->parent->left)
1196 y->parent->left = x;
1198 y->parent->right = x;
1206 /* Copy the node over */
1207 if (GTK_RBNODE_GET_COLOR (node) == GTK_RBNODE_BLACK)
1208 node->flags = ((y->flags & (GTK_RBNODE_NON_COLORS)) | GTK_RBNODE_BLACK);
1210 node->flags = ((y->flags & (GTK_RBNODE_NON_COLORS)) | GTK_RBNODE_RED);
1211 node->children = y->children;
1213 /* We want to see how different our height is from the previous node.
1214 * To do this, we compare our current height with our supposed height.
1216 diff = y_height - GTK_RBNODE_GET_HEIGHT (node);
1222 while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
1224 tmp_node->offset += diff;
1225 tmp_node = tmp_node->parent;
1226 if (tmp_node == tmp_tree->nil)
1228 tmp_node = tmp_tree->parent_node;
1229 tmp_tree = tmp_tree->parent_tree;
1235 if (GTK_RBNODE_GET_COLOR (y) == GTK_RBNODE_BLACK)
1236 _gtk_rbtree_remove_node_fixup (tree, x);
1238 _gtk_rbnode_free (y);
1240 if (gtk_debug_flags & GTK_DEBUG_TREE)
1241 _gtk_rbtree_test (G_STRLOC, tree);
1245 _gtk_rbtree_next (GtkRBTree *tree,
1248 g_return_val_if_fail (tree != NULL, NULL);
1249 g_return_val_if_fail (node != NULL, NULL);
1251 /* Case 1: the node's below us. */
1252 if (node->right != tree->nil)
1255 while (node->left != tree->nil)
1260 /* Case 2: it's an ancestor */
1261 while (node->parent != tree->nil)
1263 if (node->parent->right == node)
1264 node = node->parent;
1266 return (node->parent);
1269 /* Case 3: There is no next node */
1274 _gtk_rbtree_prev (GtkRBTree *tree,
1277 g_return_val_if_fail (tree != NULL, NULL);
1278 g_return_val_if_fail (node != NULL, NULL);
1280 /* Case 1: the node's below us. */
1281 if (node->left != tree->nil)
1284 while (node->right != tree->nil)
1289 /* Case 2: it's an ancestor */
1290 while (node->parent != tree->nil)
1292 if (node->parent->left == node)
1293 node = node->parent;
1295 return (node->parent);
1298 /* Case 3: There is no next node */
1303 _gtk_rbtree_next_full (GtkRBTree *tree,
1305 GtkRBTree **new_tree,
1306 GtkRBNode **new_node)
1308 g_return_if_fail (tree != NULL);
1309 g_return_if_fail (node != NULL);
1310 g_return_if_fail (new_tree != NULL);
1311 g_return_if_fail (new_node != NULL);
1315 *new_tree = node->children;
1316 *new_node = (*new_tree)->root;
1317 while ((*new_node)->left != (*new_tree)->nil)
1318 *new_node = (*new_node)->left;
1323 *new_node = _gtk_rbtree_next (tree, node);
1325 while ((*new_node == NULL) &&
1326 (*new_tree != NULL))
1328 *new_node = (*new_tree)->parent_node;
1329 *new_tree = (*new_tree)->parent_tree;
1331 *new_node = _gtk_rbtree_next (*new_tree, *new_node);
1336 _gtk_rbtree_prev_full (GtkRBTree *tree,
1338 GtkRBTree **new_tree,
1339 GtkRBNode **new_node)
1341 g_return_if_fail (tree != NULL);
1342 g_return_if_fail (node != NULL);
1343 g_return_if_fail (new_tree != NULL);
1344 g_return_if_fail (new_node != NULL);
1347 *new_node = _gtk_rbtree_prev (tree, node);
1349 if (*new_node == NULL)
1351 *new_node = (*new_tree)->parent_node;
1352 *new_tree = (*new_tree)->parent_tree;
1356 while ((*new_node)->children)
1358 *new_tree = (*new_node)->children;
1359 *new_node = (*new_tree)->root;
1360 while ((*new_node)->right != (*new_tree)->nil)
1361 *new_node = (*new_node)->right;
1367 _gtk_rbtree_get_depth (GtkRBTree *tree)
1369 GtkRBTree *tmp_tree;
1372 tmp_tree = tree->parent_tree;
1376 tmp_tree = tmp_tree->parent_tree;
1383 _gtk_rbtree_traverse_pre_order (GtkRBTree *tree,
1385 GtkRBTreeTraverseFunc func,
1388 if (node == tree->nil)
1391 (* func) (tree, node, data);
1392 _gtk_rbtree_traverse_pre_order (tree, node->left, func, data);
1393 _gtk_rbtree_traverse_pre_order (tree, node->right, func, data);
1397 _gtk_rbtree_traverse_post_order (GtkRBTree *tree,
1399 GtkRBTreeTraverseFunc func,
1402 if (node == tree->nil)
1405 _gtk_rbtree_traverse_post_order (tree, node->left, func, data);
1406 _gtk_rbtree_traverse_post_order (tree, node->right, func, data);
1407 (* func) (tree, node, data);
1411 _gtk_rbtree_traverse (GtkRBTree *tree,
1413 GTraverseType order,
1414 GtkRBTreeTraverseFunc func,
1417 g_return_if_fail (tree != NULL);
1418 g_return_if_fail (node != NULL);
1419 g_return_if_fail (func != NULL);
1420 g_return_if_fail (order <= G_LEVEL_ORDER);
1425 _gtk_rbtree_traverse_pre_order (tree, node, func, data);
1428 _gtk_rbtree_traverse_post_order (tree, node, func, data);
1433 g_warning ("unsupported traversal order.");
1439 _count_nodes (GtkRBTree *tree,
1443 if (node == tree->nil)
1446 g_assert (node->left);
1447 g_assert (node->right);
1449 res = (_count_nodes (tree, node->left) +
1450 _count_nodes (tree, node->right) + 1);
1452 if (res != node->count)
1453 g_print ("Tree failed\n");
1458 get_parity (GtkRBNode *node)
1460 guint child_total = 0;
1463 /* The parity of a node is node->parity minus
1464 * the parity of left, right, and children.
1466 * This is equivalent to saying that if left, right, children
1467 * sum to 0 parity, then node->parity is the parity of node,
1468 * and if left, right, children are odd parity, then
1469 * node->parity is the reverse of the node's parity.
1472 child_total += (guint) node->left->parity;
1473 child_total += (guint) node->right->parity;
1476 child_total += (guint) node->children->root->parity;
1478 rem = child_total % 2;
1481 return node->parity;
1483 return !node->parity;
1487 count_parity (GtkRBTree *tree,
1492 if (node == tree->nil)
1496 count_parity (tree, node->left) +
1497 count_parity (tree, node->right) +
1499 (node->children ? count_parity (node->children, node->children->root) : 0);
1501 res = res % (guint)2;
1503 if (res != node->parity)
1504 g_print ("parity incorrect for node\n");
1506 if (get_parity (node) != 1)
1507 g_error ("Node has incorrect parity %d", get_parity (node));
1513 _gtk_rbtree_test_height (GtkRBTree *tree,
1516 gint computed_offset = 0;
1518 /* This whole test is sort of a useless truism. */
1520 if (node->left != tree->nil)
1521 computed_offset += node->left->offset;
1523 if (node->right != tree->nil)
1524 computed_offset += node->right->offset;
1526 if (node->children && node->children->root != node->children->nil)
1527 computed_offset += node->children->root->offset;
1529 if (GTK_RBNODE_GET_HEIGHT (node) + computed_offset != node->offset)
1530 g_error ("node has broken offset\n");
1532 if (node->left != tree->nil)
1533 _gtk_rbtree_test_height (tree, node->left);
1535 if (node->right != tree->nil)
1536 _gtk_rbtree_test_height (tree, node->right);
1538 if (node->children && node->children->root != node->children->nil)
1539 _gtk_rbtree_test_height (node->children, node->children->root);
1543 _gtk_rbtree_test (const gchar *where,
1546 GtkRBTree *tmp_tree;
1548 /* Test the entire tree */
1550 while (tmp_tree->parent_tree)
1551 tmp_tree = tmp_tree->parent_tree;
1553 g_print ("%s: whole tree offset is %d\n", where, tmp_tree->root->offset);
1555 if (tmp_tree->root != tmp_tree->nil)
1557 g_assert ((_count_nodes (tmp_tree, tmp_tree->root->left) +
1558 _count_nodes (tmp_tree, tmp_tree->root->right) + 1) == tmp_tree->root->count);
1561 _gtk_rbtree_test_height (tmp_tree, tmp_tree->root);
1563 g_assert (count_parity (tmp_tree, tmp_tree->root) == tmp_tree->root->parity);