Commit 0c04a92b authored by Matthias Clasen's avatar Matthias Clasen Committed by Matthias Clasen

Check the return values of g_tree_remove().

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)
parent 16d8ccb0
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,
......
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,
......
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,
......
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,
......
......@@ -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);
}
}
......
......@@ -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);
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment