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)
This commit is contained in:
Matthias Clasen
2005-05-17 15:33:36 +00:00
committed by Matthias Clasen
parent 16d8ccb0b5
commit 0c04a92b2b
7 changed files with 76 additions and 13 deletions

View 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> 2005-05-06 Brian Cameron <brian.cameron@sun.com>
* configure.in, gmodule-no-export-2.0-uninstalled.pc.in, * configure.in, gmodule-no-export-2.0-uninstalled.pc.in,

View 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> 2005-05-06 Brian Cameron <brian.cameron@sun.com>
* configure.in, gmodule-no-export-2.0-uninstalled.pc.in, * configure.in, gmodule-no-export-2.0-uninstalled.pc.in,

View 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> 2005-05-06 Brian Cameron <brian.cameron@sun.com>
* configure.in, gmodule-no-export-2.0-uninstalled.pc.in, * configure.in, gmodule-no-export-2.0-uninstalled.pc.in,

View 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> 2005-05-06 Brian Cameron <brian.cameron@sun.com>
* configure.in, gmodule-no-export-2.0-uninstalled.pc.in, * configure.in, gmodule-no-export-2.0-uninstalled.pc.in,

View File

@@ -69,7 +69,8 @@ static GTreeNode* g_tree_node_insert (GTree *tree,
static GTreeNode* g_tree_node_remove (GTree *tree, static GTreeNode* g_tree_node_remove (GTree *tree,
GTreeNode *node, GTreeNode *node,
gconstpointer key, gconstpointer key,
gboolean notify); gboolean notify,
gboolean *removed);
static GTreeNode* g_tree_node_balance (GTreeNode *node); static GTreeNode* g_tree_node_balance (GTreeNode *node);
static GTreeNode* g_tree_node_remove_leftmost (GTreeNode *node, static GTreeNode* g_tree_node_remove_leftmost (GTreeNode *node,
GTreeNode **leftmost); GTreeNode **leftmost);
@@ -341,14 +342,20 @@ g_tree_replace (GTree *tree,
* are freed using the supplied destroy functions, otherwise you have to * are freed using the supplied destroy functions, otherwise you have to
* make sure that any dynamically allocated values are freed yourself. * make sure that any dynamically allocated values are freed yourself.
* If the key does not exist in the #GTree, the function does nothing. * 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, g_tree_remove (GTree *tree,
gconstpointer key) 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. * the key and value destroy functions.
* *
* If the key does not exist in the #GTree, the function does nothing. * 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, g_tree_steal (GTree *tree,
gconstpointer key) gconstpointer key)
{ {
g_return_if_fail (tree != NULL); gboolean removed;
tree->root = g_tree_node_remove (tree, tree->root, key, FALSE); g_return_val_if_fail (tree != NULL, FALSE);
tree->root = g_tree_node_remove (tree, tree->root, key, FALSE, &removed);
return removed;
} }
/** /**
@@ -682,12 +695,15 @@ static GTreeNode*
g_tree_node_remove (GTree *tree, g_tree_node_remove (GTree *tree,
GTreeNode *node, GTreeNode *node,
gconstpointer key, gconstpointer key,
gboolean notify) gboolean notify,
gboolean *removed)
{ {
GTreeNode *new_root; GTreeNode *new_root;
gint old_balance; gint old_balance;
gint cmp; gint cmp;
*removed = FALSE;
if (!node) if (!node)
return NULL; return NULL;
@@ -730,13 +746,15 @@ g_tree_node_remove (GTree *tree,
garbage->right = node_free_list; garbage->right = node_free_list;
node_free_list = garbage; node_free_list = garbage;
G_UNLOCK (g_tree_global); G_UNLOCK (g_tree_global);
*removed = TRUE;
} }
else if (cmp < 0) else if (cmp < 0)
{ {
if (node->left) if (node->left)
{ {
old_balance = node->left->balance; 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); node = g_tree_node_restore_left_balance (node, old_balance);
} }
} }
@@ -745,7 +763,7 @@ g_tree_node_remove (GTree *tree,
if (node->right) if (node->right)
{ {
old_balance = node->right->balance; 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); node = g_tree_node_restore_right_balance (node, old_balance);
} }
} }

View File

@@ -53,9 +53,9 @@ void g_tree_insert (GTree *tree,
void g_tree_replace (GTree *tree, void g_tree_replace (GTree *tree,
gpointer key, gpointer key,
gpointer value); gpointer value);
void g_tree_remove (GTree *tree, gboolean g_tree_remove (GTree *tree,
gconstpointer key); gconstpointer key);
void g_tree_steal (GTree *tree, gboolean g_tree_steal (GTree *tree,
gconstpointer key); gconstpointer key);
gpointer g_tree_lookup (GTree *tree, gpointer g_tree_lookup (GTree *tree,
gconstpointer key); gconstpointer key);

View File

@@ -83,7 +83,9 @@ main (int argc,
{ {
gint i, j; gint i, j;
GTree *tree; GTree *tree;
gboolean removed;
char chars[62]; char chars[62];
char c;
tree = g_tree_new (my_compare); tree = g_tree_new (my_compare);
i = 0; i = 0;
@@ -108,7 +110,14 @@ main (int argc,
g_assert (g_tree_nnodes (tree) == (10 + 26 + 26)); g_assert (g_tree_nnodes (tree) == (10 + 26 + 26));
for (i = 0; i < 10; i++) 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); g_tree_foreach (tree, my_traverse, NULL);