static void g_tree_node_check (GTreeNode *node) { gint left_height; gint right_height; gint balance; if (node) { left_height = 0; right_height = 0; if (node->left) left_height = g_tree_node_height (node->left); if (node->right) right_height = g_tree_node_height (node->right); balance = right_height - left_height; if (balance != node->balance) g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "g_tree_node_check: failed: %d ( %d )\n", balance, node->balance); if (node->left) g_tree_node_check (node->left); if (node->right) g_tree_node_check (node->right); } }
/** * g_tree_replace: * @tree: a #GTree * @key: the key to insert * @value: the value corresponding to the key * * Inserts a new key and value into a #GTree similar to g_tree_insert(). * The difference is that if the key already exists in the #GTree, it gets * replaced by the new key. If you supplied a @value_destroy_func when * creating the #GTree, the old value is freed using that function. If you * supplied a @key_destroy_func when creating the #GTree, the old key is * freed using that function. * * The tree is automatically 'balanced' as new key/value pairs are added, * so that the distance from the root to every leaf is as small as possible. */ void g_tree_replace (GTree *tree, gpointer key, gpointer value) { g_return_if_fail (tree != NULL); g_tree_insert_internal (tree, key, value, TRUE); #ifdef G_TREE_DEBUG g_tree_node_check (tree->root); #endif }
static void g_tree_node_check (GTreeNode *node) { gint left_height; gint right_height; gint balance; GTreeNode *tmp; if (node) { if (node->left_child) { tmp = g_tree_node_previous (node); g_assert (tmp->right == node); } if (node->right_child) { tmp = g_tree_node_next (node); g_assert (tmp->left == node); } left_height = 0; right_height = 0; if (node->left_child) left_height = g_tree_node_height (node->left); if (node->right_child) right_height = g_tree_node_height (node->right); balance = right_height - left_height; g_assert (balance == node->balance); if (node->left_child) g_tree_node_check (node->left); if (node->right_child) g_tree_node_check (node->right); } }
/** * g_tree_steal: * @tree: a #GTree * @key: the key to remove * * Removes a key and its associated value from a #GTree without calling * 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) */ gboolean g_tree_steal (GTree *tree, gconstpointer key) { gboolean removed; g_return_val_if_fail (tree != NULL, FALSE); removed = g_tree_remove_internal (tree, key, TRUE); #ifdef G_TREE_DEBUG g_tree_node_check (tree->root); #endif return removed; }