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.
This commit is contained in:
liuyangming 2021-03-12 11:00:57 +08:00 committed by Philip Withnall
parent 9bad38887d
commit 8fb3101715
4 changed files with 46 additions and 1 deletions

View File

@ -3055,6 +3055,7 @@ g_tree_lower_bound
g_tree_upper_bound g_tree_upper_bound
g_tree_remove g_tree_remove
g_tree_steal g_tree_steal
g_tree_remove_all
g_tree_destroy g_tree_destroy
</SECTION> </SECTION>

View File

@ -338,7 +338,16 @@ g_tree_node_next (GTreeNode *node)
return tmp; 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 #GTrees root to %NULL.
*
* Since: 2.70
*/
void
g_tree_remove_all (GTree *tree) g_tree_remove_all (GTree *tree)
{ {
GTreeNode *node; GTreeNode *node;

View File

@ -111,6 +111,10 @@ void g_tree_replace (GTree *tree,
GLIB_AVAILABLE_IN_ALL GLIB_AVAILABLE_IN_ALL
gboolean g_tree_remove (GTree *tree, gboolean g_tree_remove (GTree *tree,
gconstpointer key); gconstpointer key);
GLIB_AVAILABLE_IN_2_70
void g_tree_remove_all (GTree *tree);
GLIB_AVAILABLE_IN_ALL GLIB_AVAILABLE_IN_ALL
gboolean g_tree_steal (GTree *tree, gboolean g_tree_steal (GTree *tree,
gconstpointer key); gconstpointer key);

View File

@ -68,17 +68,21 @@ my_search (gconstpointer a,
static gpointer destroyed_key = NULL; static gpointer destroyed_key = NULL;
static gpointer destroyed_value = NULL; static gpointer destroyed_value = NULL;
static guint destroyed_key_count = 0;
static guint destroyed_value_count = 0;
static void static void
my_key_destroy (gpointer key) my_key_destroy (gpointer key)
{ {
destroyed_key = key; destroyed_key = key;
destroyed_key_count++;
} }
static void static void
my_value_destroy (gpointer value) my_value_destroy (gpointer value)
{ {
destroyed_value = value; destroyed_value = value;
destroyed_value_count++;
} }
static gint static gint
@ -281,6 +285,32 @@ test_tree_remove (void)
g_tree_destroy (tree); 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 static void
test_tree_destroy (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/destroy", test_tree_destroy);
g_test_add_func ("/tree/traverse", test_tree_traverse); g_test_add_func ("/tree/traverse", test_tree_traverse);
g_test_add_func ("/tree/insert", test_tree_insert); g_test_add_func ("/tree/insert", test_tree_insert);
g_test_add_func ("/tree/remove-all", test_tree_remove_all);
return g_test_run (); return g_test_run ();
} }