Check the return values of g_tree_remove().
authorMatthias Clasen <mclasen@redhat.com>
Tue, 17 May 2005 15:33:36 +0000 (15:33 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Tue, 17 May 2005 15:33:36 +0000 (15:33 +0000)
2005-05-17  Matthias Clasen  <mclasen@redhat.com>

* tests/tree-test.c (main): Check the return values of
g_tree_remove().

* glib/gtree.c (g_tree_remove, g_tree_steal): Return
a boolean indicating wether the key was found.  (#302545,
Matthew F. Barnes)

ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-8
glib/gtree.c
glib/gtree.h
tests/tree-test.c

index 2f9fd9c..632fcc4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2005-05-17  Matthias Clasen  <mclasen@redhat.com>
+
+       * tests/tree-test.c (main): Check the return values of
+       g_tree_remove().
+
+       * glib/gtree.c (g_tree_remove, g_tree_steal): Return
+       a boolean indicating wether the key was found.  (#302545,
+       Matthew F. Barnes)
+
 2005-05-06  Brian Cameron  <brian.cameron@sun.com>
 
        * configure.in, gmodule-no-export-2.0-uninstalled.pc.in,
index 2f9fd9c..632fcc4 100644 (file)
@@ -1,3 +1,12 @@
+2005-05-17  Matthias Clasen  <mclasen@redhat.com>
+
+       * tests/tree-test.c (main): Check the return values of
+       g_tree_remove().
+
+       * glib/gtree.c (g_tree_remove, g_tree_steal): Return
+       a boolean indicating wether the key was found.  (#302545,
+       Matthew F. Barnes)
+
 2005-05-06  Brian Cameron  <brian.cameron@sun.com>
 
        * configure.in, gmodule-no-export-2.0-uninstalled.pc.in,
index 2f9fd9c..632fcc4 100644 (file)
@@ -1,3 +1,12 @@
+2005-05-17  Matthias Clasen  <mclasen@redhat.com>
+
+       * tests/tree-test.c (main): Check the return values of
+       g_tree_remove().
+
+       * glib/gtree.c (g_tree_remove, g_tree_steal): Return
+       a boolean indicating wether the key was found.  (#302545,
+       Matthew F. Barnes)
+
 2005-05-06  Brian Cameron  <brian.cameron@sun.com>
 
        * configure.in, gmodule-no-export-2.0-uninstalled.pc.in,
index 2f9fd9c..632fcc4 100644 (file)
@@ -1,3 +1,12 @@
+2005-05-17  Matthias Clasen  <mclasen@redhat.com>
+
+       * tests/tree-test.c (main): Check the return values of
+       g_tree_remove().
+
+       * glib/gtree.c (g_tree_remove, g_tree_steal): Return
+       a boolean indicating wether the key was found.  (#302545,
+       Matthew F. Barnes)
+
 2005-05-06  Brian Cameron  <brian.cameron@sun.com>
 
        * configure.in, gmodule-no-export-2.0-uninstalled.pc.in,
index 2c5d1e0..1c81f3e 100644 (file)
@@ -69,7 +69,8 @@ static GTreeNode* g_tree_node_insert                (GTree            *tree,
 static GTreeNode* g_tree_node_remove                (GTree            *tree,
                                                      GTreeNode        *node,
                                                     gconstpointer     key,
-                                                     gboolean          notify);
+                                                     gboolean          notify,
+                                                    gboolean         *removed);
 static GTreeNode* g_tree_node_balance               (GTreeNode        *node);
 static GTreeNode* g_tree_node_remove_leftmost       (GTreeNode        *node,
                                                     GTreeNode       **leftmost);
@@ -341,14 +342,20 @@ g_tree_replace (GTree    *tree,
  * are freed using the supplied destroy functions, otherwise you have to 
  * make sure that any dynamically allocated values are freed yourself.
  * If the key does not exist in the #GTree, the function does nothing.
+ *
+ * Returns: %TRUE if the key was found (prior to 2.8, this function returned nothing)
  **/
-void
+gboolean
 g_tree_remove (GTree         *tree,
               gconstpointer  key)
 {
-  g_return_if_fail (tree != NULL);
+  gboolean removed;
 
-  tree->root = g_tree_node_remove (tree, tree->root, key, TRUE);
+  g_return_val_if_fail (tree != NULL, FALSE);
+
+  tree->root = g_tree_node_remove (tree, tree->root, key, TRUE, &removed);
+
+  return removed;
 }
 
 /**
@@ -360,14 +367,20 @@ g_tree_remove (GTree         *tree,
  * the key and value destroy functions.
  *
  * If the key does not exist in the #GTree, the function does nothing.
+ *
+ * Returns: %TRUE if the key was found (prior to 2.8, this function returned nothing)
  **/
-void
+gboolean
 g_tree_steal (GTree         *tree,
               gconstpointer  key)
 {
-  g_return_if_fail (tree != NULL);
+  gboolean removed;
+
+  g_return_val_if_fail (tree != NULL, FALSE);
+
+  tree->root = g_tree_node_remove (tree, tree->root, key, FALSE, &removed);
 
-  tree->root = g_tree_node_remove (tree, tree->root, key, FALSE);
+  return removed;
 }
 
 /**
@@ -682,12 +695,15 @@ static GTreeNode*
 g_tree_node_remove (GTree         *tree,
                     GTreeNode     *node,
                    gconstpointer  key,
-                    gboolean       notify)
+                    gboolean       notify,
+                   gboolean      *removed)
 {
   GTreeNode *new_root;
   gint old_balance;
   gint cmp;
 
+  *removed = FALSE;
+
   if (!node)
     return NULL;
 
@@ -730,13 +746,15 @@ g_tree_node_remove (GTree         *tree,
       garbage->right = node_free_list;
       node_free_list = garbage;
       G_UNLOCK (g_tree_global);
+
+      *removed = TRUE;
    }
   else if (cmp < 0)
     {
       if (node->left)
        {
          old_balance = node->left->balance;
-         node->left = g_tree_node_remove (tree, node->left, key, notify);
+         node->left = g_tree_node_remove (tree, node->left, key, notify, removed);
          node = g_tree_node_restore_left_balance (node, old_balance);
        }
     }
@@ -745,7 +763,7 @@ g_tree_node_remove (GTree         *tree,
       if (node->right)
        {
          old_balance = node->right->balance;
-         node->right = g_tree_node_remove (tree, node->right, key, notify);
+         node->right = g_tree_node_remove (tree, node->right, key, notify, removed);
          node = g_tree_node_restore_right_balance (node, old_balance);
        }
     }
index b6894d5..bab7127 100644 (file)
@@ -53,9 +53,9 @@ void     g_tree_insert          (GTree            *tree,
 void     g_tree_replace         (GTree            *tree,
                                  gpointer          key,
                                  gpointer          value);
-void     g_tree_remove          (GTree            *tree,
+gboolean g_tree_remove          (GTree            *tree,
                                  gconstpointer     key);
-void     g_tree_steal           (GTree            *tree,
+gboolean g_tree_steal           (GTree            *tree,
                                  gconstpointer     key);
 gpointer g_tree_lookup          (GTree            *tree,
                                  gconstpointer     key);
index 226ab88..18443d2 100644 (file)
@@ -83,7 +83,9 @@ main (int   argc,
 {
   gint i, j;
   GTree *tree;
+  gboolean removed;
   char chars[62];
+  char c;
 
   tree = g_tree_new (my_compare);
   i = 0;
@@ -108,7 +110,14 @@ main (int   argc,
   g_assert (g_tree_nnodes (tree) == (10 + 26 + 26));
 
   for (i = 0; i < 10; i++)
-    g_tree_remove (tree, &chars[i]);
+  {
+    removed = g_tree_remove (tree, &chars[i]);
+    g_assert (removed);
+  }
+
+  c = '\0';
+  removed = g_tree_remove (tree, &c);
+  g_assert (removed == FALSE);
 
   g_tree_foreach (tree, my_traverse, NULL);