static void g_tree_remove_all (GTree *tree) { GTreeNode *node; GTreeNode *next; g_return_if_fail (tree != NULL); node = g_tree_first_node (tree); while (node) { next = g_tree_node_next (node); if (tree->key_destroy_func) tree->key_destroy_func (node->key); if (tree->value_destroy_func) tree->value_destroy_func (node->value); g_slice_free (GTreeNode, node); node = next; } tree->root = NULL; tree->nnodes = 0; }
/** * g_tree_destroy: * @tree: a #GTree. * * Destroys the #GTree. If keys and/or values are dynamically allocated, you * should either free them first or create the #GTree using g_tree_new_full(). * In the latter case the destroy functions you supplied will be called on * all keys and values before destroying the #GTree. **/ void g_tree_destroy (GTree *tree) { GTreeNode *node; GTreeNode *next; g_return_if_fail (tree != NULL); node = g_tree_first_node (tree); while (node) { next = g_tree_node_next (node); if (tree->key_destroy_func) tree->key_destroy_func (node->key); if (tree->value_destroy_func) tree->value_destroy_func (node->value); g_slice_free (GTreeNode, node); node = next; } g_free (tree); }
/** * g_tree_foreach: * @tree: a #GTree * @func: the function to call for each node visited. * If this function returns %TRUE, the traversal is stopped. * @user_data: user data to pass to the function * * Calls the given function for each of the key/value pairs in the #GTree. * The function is passed the key and value of each pair, and the given * @data parameter. The tree is traversed in sorted order. * * The tree may not be modified while iterating over it (you can't * add/remove items). To remove all items matching a predicate, you need * to add each item to a list in your #GTraverseFunc as you walk over * the tree, then walk the list and remove each item. */ void g_tree_foreach (GTree *tree, GTraverseFunc func, gpointer user_data) { GTreeNode *node; g_return_if_fail (tree != NULL); if (!tree->root) return; node = g_tree_first_node (tree); while (node) { if ((*func) (node->key, node->value, user_data)) break; node = g_tree_node_next (node); } }
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); } }
/* internal remove routine */ static gboolean g_tree_remove_internal (GTree *tree, gconstpointer key, gboolean steal) { GTreeNode *node, *parent, *balance; GTreeNode *path[MAX_GTREE_HEIGHT]; int idx; gboolean left_node; g_return_val_if_fail (tree != NULL, FALSE); if (!tree->root) return FALSE; idx = 0; path[idx++] = NULL; node = tree->root; while (1) { int cmp = tree->key_compare (key, node->key, tree->key_compare_data); if (cmp == 0) break; else if (cmp < 0) { if (!node->left_child) return FALSE; path[idx++] = node; node = node->left; } else { if (!node->right_child) return FALSE; path[idx++] = node; node = node->right; } } /* The following code is almost equal to g_tree_remove_node, * except that we do not have to call g_tree_node_parent. */ balance = parent = path[--idx]; g_assert (!parent || parent->left == node || parent->right == node); left_node = (parent && node == parent->left); if (!node->left_child) { if (!node->right_child) { if (!parent) tree->root = NULL; else if (left_node) { parent->left_child = FALSE; parent->left = node->left; parent->balance += 1; } else { parent->right_child = FALSE; parent->right = node->right; parent->balance -= 1; } } else /* node has a right child */ { GTreeNode *tmp = g_tree_node_next (node); tmp->left = node->left; if (!parent) tree->root = node->right; else if (left_node) { parent->left = node->right; parent->balance += 1; } else { parent->right = node->right; parent->balance -= 1; } } } else /* node has a left child */ { if (!node->right_child) { GTreeNode *tmp = g_tree_node_previous (node); tmp->right = node->right; if (parent == NULL) tree->root = node->left; else if (left_node) { parent->left = node->left; parent->balance += 1; } else { parent->right = node->left; parent->balance -= 1; } } else /* node has a both children (pant, pant!) */ { GTreeNode *prev = node->left; GTreeNode *next = node->right; GTreeNode *nextp = node; int old_idx = idx + 1; idx++; /* path[idx] == parent */ /* find the immediately next node (and its parent) */ while (next->left_child) { path[++idx] = nextp = next; next = next->left; } path[old_idx] = next; balance = path[idx]; /* remove 'next' from the tree */ if (nextp != node) { if (next->right_child) nextp->left = next->right; else nextp->left_child = FALSE; nextp->balance += 1; next->right_child = TRUE; next->right = node->right; } else node->balance -= 1; /* set the prev to point to the right place */ while (prev->right_child) prev = prev->right; prev->right = next; /* prepare 'next' to replace 'node' */ next->left_child = TRUE; next->left = node->left; next->balance = node->balance; if (!parent) tree->root = next; else if (left_node) parent->left = next; else parent->right = next; } } /* restore balance */ if (balance) while (1) { GTreeNode *bparent = path[--idx]; g_assert (!bparent || bparent->left == balance || bparent->right == balance); left_node = (bparent && balance == bparent->left); if(balance->balance < -1 || balance->balance > 1) { balance = g_tree_node_balance (balance); if (!bparent) tree->root = balance; else if (left_node) bparent->left = balance; else bparent->right = balance; } if (balance->balance != 0 || !bparent) break; if (left_node) bparent->balance += 1; else bparent->balance -= 1; balance = bparent; } if (!steal) { if (tree->key_destroy_func) tree->key_destroy_func (node->key); if (tree->value_destroy_func) tree->value_destroy_func (node->value); } g_slice_free (GTreeNode, node); tree->nnodes--; return TRUE; }