[gtk+] rbtree: Introduce _gtk_rbtree_first()



commit c3056951dbd5ee7b3d9052ce5e9e8f5e1864f639
Author: Benjamin Otte <otte redhat com>
Date:   Tue Nov 22 02:58:05 2011 +0100

    rbtree: Introduce _gtk_rbtree_first()
    
    ... and use it.

 gtk/gtkrbtree.c        |   40 ++++++++++++++++++++------------------
 gtk/gtkrbtree.h        |    1 +
 gtk/gtktreeselection.c |   24 ++++------------------
 gtk/gtktreeview.c      |   49 +++++++++++------------------------------------
 4 files changed, 39 insertions(+), 75 deletions(-)
---
diff --git a/gtk/gtkrbtree.c b/gtk/gtkrbtree.c
index a9ec703..35d8134 100644
--- a/gtk/gtkrbtree.c
+++ b/gtk/gtkrbtree.c
@@ -722,11 +722,8 @@ _gtk_rbtree_column_invalid (GtkRBTree *tree)
 
   if (tree == NULL)
     return;
-  node = tree->root;
-  g_assert (node);
 
-  while (node->left != tree->nil)
-    node = node->left;
+  node = _gtk_rbtree_first (tree);
 
   do
     {
@@ -747,11 +744,8 @@ _gtk_rbtree_mark_invalid (GtkRBTree *tree)
 
   if (tree == NULL)
     return;
-  node = tree->root;
-  g_assert (node);
 
-  while (node->left != tree->nil)
-    node = node->left;
+  node = _gtk_rbtree_first (tree);
 
   do
     {
@@ -774,11 +768,7 @@ _gtk_rbtree_set_fixed_height (GtkRBTree *tree,
   if (tree == NULL)
     return;
 
-  node = tree->root;
-  g_assert (node);
-
-  while (node->left != tree->nil)
-    node = node->left;
+  node = _gtk_rbtree_first (tree);
 
   do
     {
@@ -888,9 +878,7 @@ _gtk_rbtree_reorder (GtkRBTree *tree,
   g_array_sort(array, gtk_rbtree_reorder_sort_func);
 
   /* rewind node*/
-  node = tree->root;
-  while (node && node->left != tree->nil)
-    node = node->left;
+  node = _gtk_rbtree_first (tree);
 
   for (i = 0; i < length; i++)
     {
@@ -905,9 +893,7 @@ _gtk_rbtree_reorder (GtkRBTree *tree,
   g_array_sort (array, gtk_rbtree_reorder_invert_func);
  
   /* rewind node*/
-  node = tree->root;
-  while (node && node->left != tree->nil)
-    node = node->left;
+  node = _gtk_rbtree_first (tree);
 
   /* Go through the tree and change the values to the new ones. */
   for (i = 0; i < length; i++)
@@ -1260,6 +1246,22 @@ _gtk_rbtree_remove_node (GtkRBTree *tree,
 }
 
 GtkRBNode *
+_gtk_rbtree_first (GtkRBTree *tree)
+{
+  GtkRBNode *node;
+
+  node = tree->root;
+
+  if (node == tree->nil)
+    return NULL;
+
+  while (node->left != tree->nil)
+    node = node->left;
+
+  return node;
+}
+
+GtkRBNode *
 _gtk_rbtree_next (GtkRBTree *tree,
 		  GtkRBNode *node)
 {
diff --git a/gtk/gtkrbtree.h b/gtk/gtkrbtree.h
index 337406e..caba434 100644
--- a/gtk/gtkrbtree.h
+++ b/gtk/gtkrbtree.h
@@ -148,6 +148,7 @@ void       _gtk_rbtree_traverse         (GtkRBTree              *tree,
 					 GTraverseType           order,
 					 GtkRBTreeTraverseFunc   func,
 					 gpointer                data);
+GtkRBNode *_gtk_rbtree_first            (GtkRBTree              *tree);
 GtkRBNode *_gtk_rbtree_next             (GtkRBTree              *tree,
 					 GtkRBNode              *node);
 GtkRBNode *_gtk_rbtree_prev             (GtkRBTree              *tree,
diff --git a/gtk/gtktreeselection.c b/gtk/gtktreeselection.c
index 727119d..d655ad3 100644
--- a/gtk/gtktreeselection.c
+++ b/gtk/gtktreeselection.c
@@ -605,10 +605,7 @@ gtk_tree_selection_get_selected_rows (GtkTreeSelection   *selection,
       return NULL;
     }
 
-  node = tree->root;
-
-  while (node->left != tree->nil)
-    node = node->left;
+  node = _gtk_rbtree_first (tree);
   path = gtk_tree_path_new_first ();
 
   do
@@ -619,10 +616,7 @@ gtk_tree_selection_get_selected_rows (GtkTreeSelection   *selection,
       if (node->children)
         {
 	  tree = node->children;
-	  node = tree->root;
-
-	  while (node->left != tree->nil)
-	    node = node->left;
+          node = _gtk_rbtree_first (tree);
 
 	  gtk_tree_path_append_index (path, 0);
 	}
@@ -786,10 +780,7 @@ gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
       return;
     }
 
-  node = tree->root;
-  
-  while (node->left != tree->nil)
-    node = node->left;
+  node = _gtk_rbtree_first (tree);
 
   g_object_ref (model);
 
@@ -824,10 +815,7 @@ gtk_tree_selection_selected_foreach (GtkTreeSelection            *selection,
       if (node->children)
 	{
 	  tree = node->children;
-	  node = tree->root;
-
-	  while (node->left != tree->nil)
-	    node = node->left;
+          node = _gtk_rbtree_first (tree);
 
 	  gtk_tree_path_append_index (path, 0);
 	}
@@ -1377,9 +1365,7 @@ gtk_tree_selection_real_modify_range (GtkTreeSelection *selection,
       if (start_node->children)
 	{
 	  start_tree = start_node->children;
-	  start_node = start_tree->root;
-	  while (start_node->left != start_tree->nil)
-	    start_node = start_node->left;
+          start_node = _gtk_rbtree_first (start_tree);
 	}
       else
 	{
diff --git a/gtk/gtktreeview.c b/gtk/gtktreeview.c
index 7c4192c..f7de6cc 100644
--- a/gtk/gtktreeview.c
+++ b/gtk/gtktreeview.c
@@ -4233,9 +4233,7 @@ skip_first:
       if (start_node->children)
         {
 	  start_tree = start_node->children;
-	  start_node = start_tree->root;
-	  while (start_node->left != start_tree->nil)
-	    start_node = start_node->left;
+          start_node = _gtk_rbtree_first (start_tree);
 	}
       else
         {
@@ -5270,12 +5268,8 @@ gtk_tree_view_bin_draw (GtkWidget      *widget,
 	  gboolean has_child;
 
 	  tree = node->children;
-	  node = tree->root;
+          node = _gtk_rbtree_first (tree);
 
-          g_assert (node != tree->nil);
-
-	  while (node->left != tree->nil)
-	    node = node->left;
 	  has_child = gtk_tree_model_iter_children (tree_view->priv->model,
 						    &iter,
 						    &parent);
@@ -6371,12 +6365,8 @@ validate_visible_area (GtkTreeView *tree_view)
 	  gboolean has_child;
 
 	  tree = node->children;
-	  node = tree->root;
-
-          g_assert (node != tree->nil);
+          node = _gtk_rbtree_first (tree);
 
-	  while (node->left != tree->nil)
-	    node = node->left;
 	  has_child = gtk_tree_model_iter_children (tree_view->priv->model,
 						    &iter,
 						    &parent);
@@ -9579,10 +9569,7 @@ gtk_tree_view_unref_tree_helper (GtkTreeModel *model,
 	  GtkRBNode *new_node;
 
 	  new_tree = node->children;
-	  new_node = new_tree->root;
-
-	  while (new_node && new_node->left != new_tree->nil)
-	    new_node = new_node->left;
+          new_node = _gtk_rbtree_first (new_tree);
 
 	  if (!gtk_tree_model_iter_children (model, &child, iter))
 	    return FALSE;
@@ -9612,9 +9599,7 @@ gtk_tree_view_unref_and_check_selection_tree (GtkTreeView *tree_view,
   if (!tree)
     return FALSE;
 
-  node = tree->root;
-  while (node && node->left != tree->nil)
-    node = node->left;
+  node = _gtk_rbtree_first (tree);
 
   g_return_val_if_fail (node != NULL, FALSE);
   path = _gtk_tree_view_find_path (tree_view, tree, node);
@@ -10553,12 +10538,10 @@ gtk_tree_view_move_cursor_start_end (GtkTreeView *tree_view,
   gtk_tree_view_get_cursor (tree_view, &old_path, NULL);
 
   cursor_tree = tree_view->priv->tree;
-  cursor_node = cursor_tree->root;
 
   if (count == -1)
     {
-      while (cursor_node && cursor_node->left != cursor_tree->nil)
-	cursor_node = cursor_node->left;
+      cursor_node = _gtk_rbtree_first (cursor_tree);
 
       /* Now go forward to find the first focusable row. */
       path = _gtk_tree_view_find_path (tree_view, cursor_tree, cursor_node);
@@ -10567,6 +10550,8 @@ gtk_tree_view_move_cursor_start_end (GtkTreeView *tree_view,
     }
   else
     {
+      cursor_node = cursor_tree->root;
+
       do
 	{
 	  while (cursor_node && cursor_node->right != cursor_tree->nil)
@@ -12623,9 +12608,7 @@ gtk_tree_view_collapse_all (GtkTreeView *tree_view)
   indices = gtk_tree_path_get_indices (path);
 
   tree = tree_view->priv->tree;
-  node = tree->root;
-  while (node && node->left != tree->nil)
-    node = node->left;
+  node = _gtk_rbtree_first (tree);
 
   while (node)
     {
@@ -12717,9 +12700,7 @@ gtk_tree_view_real_expand_row (GtkTreeView *tree_view,
 
       gtk_tree_path_append_index (tmp_path, 0);
       tree = node->children;
-      node = tree->root;
-      while (node->left != tree->nil)
-	node = node->left;
+      node = _gtk_rbtree_first (tree);
       /* try to expand the children */
       do
         {
@@ -13037,10 +13018,7 @@ gtk_tree_view_map_expanded_rows_helper (GtkTreeView            *tree_view,
   if (tree == NULL || tree->root == NULL)
     return;
 
-  node = tree->root;
-
-  while (node && node->left != tree->nil)
-    node = node->left;
+  node = _gtk_rbtree_first (tree);
 
   while (node)
     {
@@ -15449,10 +15427,7 @@ gtk_tree_view_search_iter (GtkTreeModel     *model,
 	  GtkTreeIter tmp;
 
 	  tree = node->children;
-	  node = tree->root;
-
-	  while (node->left != tree->nil)
-	    node = node->left;
+          node = _gtk_rbtree_first (tree);
 
 	  tmp = *iter;
 	  has_child = gtk_tree_model_iter_children (model, iter, &tmp);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]