[glib: 1/2] gtree: Make g_tree_remove_all() public




commit 8fb31017150ab0ce194e639382614e85014b6b9c
Author: liuyangming <liuyangming uniontech com>
Date:   Fri Mar 12 11:00:57 2021 +0800

    gtree: Make g_tree_remove_all() public
    
    g_tree_remove_all is useful and the corresponding function in GHashTable
    is exposed, so make this function public is meaningful.

 docs/reference/glib/glib-sections.txt |  1 +
 glib/gtree.c                          | 11 ++++++++++-
 glib/gtree.h                          |  4 ++++
 glib/tests/tree.c                     | 31 +++++++++++++++++++++++++++++++
 4 files changed, 46 insertions(+), 1 deletion(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index c9697b45d..b03c693d4 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -3055,6 +3055,7 @@ g_tree_lower_bound
 g_tree_upper_bound
 g_tree_remove
 g_tree_steal
+g_tree_remove_all
 g_tree_destroy
 </SECTION>
 
diff --git a/glib/gtree.c b/glib/gtree.c
index 94842ac92..6ed917514 100644
--- a/glib/gtree.c
+++ b/glib/gtree.c
@@ -338,7 +338,16 @@ g_tree_node_next (GTreeNode *node)
   return tmp;
 }
 
-static void
+/**
+ * g_tree_remove_all:
+ * @tree: a #GTree
+ *
+ * Removes all nodes from a #GTree and destroys their keys and values,
+ * then resets the #GTreeā€™s root to %NULL.
+ *
+ * Since: 2.70
+ */
+void
 g_tree_remove_all (GTree *tree)
 {
   GTreeNode *node;
diff --git a/glib/gtree.h b/glib/gtree.h
index 19f9f7ea1..ff11ba435 100644
--- a/glib/gtree.h
+++ b/glib/gtree.h
@@ -111,6 +111,10 @@ void     g_tree_replace         (GTree            *tree,
 GLIB_AVAILABLE_IN_ALL
 gboolean g_tree_remove          (GTree            *tree,
                                  gconstpointer     key);
+
+GLIB_AVAILABLE_IN_2_70
+void     g_tree_remove_all      (GTree            *tree);
+
 GLIB_AVAILABLE_IN_ALL
 gboolean g_tree_steal           (GTree            *tree,
                                  gconstpointer     key);
diff --git a/glib/tests/tree.c b/glib/tests/tree.c
index 8811d962d..5174479b9 100644
--- a/glib/tests/tree.c
+++ b/glib/tests/tree.c
@@ -68,17 +68,21 @@ my_search (gconstpointer a,
 
 static gpointer destroyed_key = NULL;
 static gpointer destroyed_value = NULL;
+static guint destroyed_key_count = 0;
+static guint destroyed_value_count = 0;
 
 static void
 my_key_destroy (gpointer key)
 {
   destroyed_key = key;
+  destroyed_key_count++;
 }
 
 static void
 my_value_destroy (gpointer value)
 {
   destroyed_value = value;
+  destroyed_value_count++;
 }
 
 static gint
@@ -281,6 +285,32 @@ test_tree_remove (void)
   g_tree_destroy (tree);
 }
 
+static void
+test_tree_remove_all (void)
+{
+  GTree *tree;
+  gsize i;
+
+  tree = g_tree_new_full ((GCompareDataFunc)my_compare, NULL,
+                          my_key_destroy,
+                          my_value_destroy);
+
+  for (i = 0; chars[i]; i++)
+    g_tree_insert (tree, &chars[i], &chars[i]);
+
+  destroyed_key_count = 0;
+  destroyed_value_count = 0;
+
+  g_tree_remove_all (tree);
+
+  g_assert_cmpuint (destroyed_key_count, ==, strlen (chars));
+  g_assert_cmpuint (destroyed_value_count, ==, strlen (chars));
+  g_assert_cmpint (g_tree_height (tree), ==, 0);
+  g_assert_cmpint (g_tree_nnodes (tree), ==, 0);
+
+  g_tree_unref (tree);
+}
+
 static void
 test_tree_destroy (void)
 {
@@ -462,6 +492,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/tree/destroy", test_tree_destroy);
   g_test_add_func ("/tree/traverse", test_tree_traverse);
   g_test_add_func ("/tree/insert", test_tree_insert);
+  g_test_add_func ("/tree/remove-all", test_tree_remove_all);
 
   return g_test_run ();
 }


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