]> Pileus Git - ~andy/gtk/commitdiff
rbtree: Split out a common function
authorBenjamin Otte <otte@redhat.com>
Sat, 19 Nov 2011 11:59:39 +0000 (12:59 +0100)
committerBenjamin Otte <otte@redhat.com>
Mon, 21 Nov 2011 21:33:45 +0000 (22:33 +0100)
gtk_rbtree_adjust() will adjust the summed values of a node and all its
parents in the tree. Currently only implemented by splitting out the
function from gtk_rbtree_free().

gtk/gtkrbtree.c

index 228abfece06532bb67dc1cc18741052ac93439ec..99261ad881ed137a95bcb0efd4905c1a36df0c41 100644 (file)
@@ -383,43 +383,54 @@ _gtk_rbtree_free (GtkRBTree *tree)
   g_free (tree);
 }
 
+static void
+gtk_rbnode_adjust (GtkRBTree *tree,
+                   GtkRBNode *node,
+                   int        count_diff,
+                   int        total_count_diff,
+                   int        offset_diff)
+{
+  while (tree && node && node != tree->nil)
+    {
+      _fixup_validation (tree, node);
+      node->offset += offset_diff;
+      node->count += count_diff;
+      node->total_count += total_count_diff;
+      
+      node = node->parent;
+      if (node == tree->nil)
+       {
+         node = tree->parent_node;
+         tree = tree->parent_tree;
+          count_diff = 0;
+       }
+    }
+}
+
 void
 _gtk_rbtree_remove (GtkRBTree *tree)
 {
+#ifdef G_ENABLE_DEBUG  
   GtkRBTree *tmp_tree;
-  GtkRBNode *tmp_node;
-
-  gint height = tree->root->offset;
-  guint total_count = tree->root->total_count;
 
-#ifdef G_ENABLE_DEBUG  
   if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
     _gtk_rbtree_test (G_STRLOC, tree);
 #endif
   
-  tmp_tree = tree->parent_tree;
-  tmp_node = tree->parent_node;
-
   /* ugly hack to make _fixup_validation work in the first iteration of the
    * loop below */
   GTK_RBNODE_UNSET_FLAG (tree->root, GTK_RBNODE_DESCENDANTS_INVALID);
   
-  while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
-    {
-      _fixup_validation (tmp_tree, tmp_node);
-      tmp_node->offset -= height;
-      tmp_node->total_count -= total_count;
-      
-      tmp_node = tmp_node->parent;
-      if (tmp_node == tmp_tree->nil)
-       {
-         tmp_node = tmp_tree->parent_node;
-         tmp_tree = tmp_tree->parent_tree;
-       }
-    }
+  gtk_rbnode_adjust (tree->parent_tree, 
+                     tree->parent_node,
+                     0,
+                     - (int) tree->root->total_count,
+                     - tree->root->offset);
 
+#ifdef G_ENABLE_DEBUG  
   tmp_tree = tree->parent_tree;
-  tmp_node = tree->parent_node;
+#endif
+
   _gtk_rbtree_free (tree);
 
 #ifdef G_ENABLE_DEBUG