1998-06-11 01:21:14 +02:00
|
|
|
/* GLIB - Library of useful routines for C programming
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2000-07-26 13:02:02 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1998-06-11 01:21:14 +02:00
|
|
|
* License as published by the Free Software Foundation; either
|
2017-01-05 12:47:07 +01:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
1998-06-11 01:21:14 +02:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2000-07-26 13:02:02 +02:00
|
|
|
* Lesser General Public License for more details.
|
1998-06-11 01:21:14 +02:00
|
|
|
*
|
2000-07-26 13:02:02 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2014-01-23 12:58:29 +01:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
1998-06-11 01:21:14 +02:00
|
|
|
*/
|
1998-12-15 06:28:02 +01:00
|
|
|
|
1999-02-24 07:14:27 +01:00
|
|
|
/*
|
2000-07-26 13:02:02 +02:00
|
|
|
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
1999-02-24 07:14:27 +01:00
|
|
|
* file for a list of people on the GLib Team. See the ChangeLog
|
|
|
|
* files for a list of changes. These files are distributed with
|
|
|
|
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
|
|
|
*/
|
|
|
|
|
1998-12-15 06:28:02 +01:00
|
|
|
/*
|
|
|
|
* MT safe
|
|
|
|
*/
|
|
|
|
|
2002-12-04 02:27:44 +01:00
|
|
|
#include "config.h"
|
Ok, I'm a moron. When I originally implemented ENABLE_GC_FRIENDLY, I
2000-12-19 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* gslist.c, glist.c: Ok, I'm a moron. When I originally
implemented ENABLE_GC_FRIENDLY, I forgot to include config.h into
the affected files. Now that Alex did that for those two,
inevitable typos surfaced, which are now fixed.
* garray.c, ghash.c, gqueue.c, gtree.c: Include config.h as well,
as ENABLE_GC_FRIENDLY should be known.
2000-12-19 16:40:30 +01:00
|
|
|
|
2010-09-04 02:05:27 +02:00
|
|
|
#include "gtree.h"
|
|
|
|
|
|
|
|
#include "gatomic.h"
|
|
|
|
#include "gtestutils.h"
|
2011-09-19 00:59:20 +02:00
|
|
|
#include "gslice.h"
|
1998-06-11 01:21:14 +02:00
|
|
|
|
2010-01-31 06:27:28 +01:00
|
|
|
/**
|
2011-02-01 19:17:23 +01:00
|
|
|
* SECTION:trees-binary
|
2010-01-31 06:27:28 +01:00
|
|
|
* @title: Balanced Binary Trees
|
|
|
|
* @short_description: a sorted collection of key/value pairs optimized
|
|
|
|
* for searching and traversing in order
|
|
|
|
*
|
|
|
|
* The #GTree structure and its associated functions provide a sorted
|
|
|
|
* collection of key/value pairs optimized for searching and traversing
|
2019-08-06 21:12:39 +02:00
|
|
|
* in order. This means that most of the operations (access, search,
|
|
|
|
* insertion, deletion, ...) on #GTree are O(log(n)) in average and O(n)
|
|
|
|
* in worst case for time complexity. But, note that maintaining a
|
|
|
|
* balanced sorted #GTree of n elements is done in time O(n log(n)).
|
2010-01-31 06:27:28 +01:00
|
|
|
*
|
|
|
|
* To create a new #GTree use g_tree_new().
|
|
|
|
*
|
2019-08-06 21:12:39 +02:00
|
|
|
* To insert a key/value pair into a #GTree use g_tree_insert()
|
|
|
|
* (O(n log(n))).
|
|
|
|
*
|
|
|
|
* To remove a key/value pair use g_tree_remove() (O(n log(n))).
|
2010-01-31 06:27:28 +01:00
|
|
|
*
|
2019-04-26 13:12:31 +02:00
|
|
|
* To look up the value corresponding to a given key, use
|
2010-01-31 06:27:28 +01:00
|
|
|
* g_tree_lookup() and g_tree_lookup_extended().
|
|
|
|
*
|
|
|
|
* To find out the number of nodes in a #GTree, use g_tree_nnodes(). To
|
|
|
|
* get the height of a #GTree, use g_tree_height().
|
|
|
|
*
|
|
|
|
* To traverse a #GTree, calling a function for each node visited in
|
|
|
|
* the traversal, use g_tree_foreach().
|
|
|
|
*
|
|
|
|
* To destroy a #GTree, use g_tree_destroy().
|
|
|
|
**/
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
#define MAX_GTREE_HEIGHT 40
|
1998-06-11 01:21:14 +02:00
|
|
|
|
2010-01-31 06:27:28 +01:00
|
|
|
/**
|
|
|
|
* GTree:
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
* The GTree struct is an opaque data structure representing a
|
|
|
|
* [balanced binary tree][glib-Balanced-Binary-Trees]. It should be
|
|
|
|
* accessed only by using the following functions.
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
2001-09-19 20:08:19 +02:00
|
|
|
struct _GTree
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
2006-01-14 06:24:10 +01:00
|
|
|
GTreeNode *root;
|
|
|
|
GCompareDataFunc key_compare;
|
|
|
|
GDestroyNotify key_destroy_func;
|
|
|
|
GDestroyNotify value_destroy_func;
|
|
|
|
gpointer key_compare_data;
|
|
|
|
guint nnodes;
|
2011-05-29 04:33:37 +02:00
|
|
|
gint ref_count;
|
1998-06-11 01:21:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GTreeNode
|
|
|
|
{
|
2006-01-14 06:24:10 +01:00
|
|
|
gpointer key; /* key for this node */
|
|
|
|
gpointer value; /* value stored at this node */
|
|
|
|
GTreeNode *left; /* left subtree */
|
|
|
|
GTreeNode *right; /* right subtree */
|
2011-07-25 13:06:02 +02:00
|
|
|
gint8 balance; /* height (right) - height (left) */
|
2011-05-29 04:03:05 +02:00
|
|
|
guint8 left_child;
|
2006-01-14 06:24:10 +01:00
|
|
|
guint8 right_child;
|
1998-06-11 01:21:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
static GTreeNode* g_tree_node_new (gpointer key,
|
2014-01-20 05:49:12 +01:00
|
|
|
gpointer value);
|
2020-08-04 21:31:36 +02:00
|
|
|
static GTreeNode *g_tree_insert_internal (GTree *tree,
|
|
|
|
gpointer key,
|
|
|
|
gpointer value,
|
|
|
|
gboolean replace);
|
2006-01-14 06:24:10 +01:00
|
|
|
static gboolean g_tree_remove_internal (GTree *tree,
|
2014-01-20 05:49:12 +01:00
|
|
|
gconstpointer key,
|
|
|
|
gboolean steal);
|
2006-01-14 06:24:10 +01:00
|
|
|
static GTreeNode* g_tree_node_balance (GTreeNode *node);
|
|
|
|
static GTreeNode *g_tree_find_node (GTree *tree,
|
2014-01-20 05:49:12 +01:00
|
|
|
gconstpointer key);
|
2006-01-14 06:24:10 +01:00
|
|
|
static gint g_tree_node_pre_order (GTreeNode *node,
|
2014-01-20 05:49:12 +01:00
|
|
|
GTraverseFunc traverse_func,
|
|
|
|
gpointer data);
|
2006-01-14 06:24:10 +01:00
|
|
|
static gint g_tree_node_in_order (GTreeNode *node,
|
2014-01-20 05:49:12 +01:00
|
|
|
GTraverseFunc traverse_func,
|
|
|
|
gpointer data);
|
2006-01-14 06:24:10 +01:00
|
|
|
static gint g_tree_node_post_order (GTreeNode *node,
|
2014-01-20 05:49:12 +01:00
|
|
|
GTraverseFunc traverse_func,
|
|
|
|
gpointer data);
|
2020-08-04 21:31:36 +02:00
|
|
|
static GTreeNode *g_tree_node_search (GTreeNode *node,
|
|
|
|
GCompareFunc search_func,
|
|
|
|
gconstpointer data);
|
2006-01-14 06:24:10 +01:00
|
|
|
static GTreeNode* g_tree_node_rotate_left (GTreeNode *node);
|
|
|
|
static GTreeNode* g_tree_node_rotate_right (GTreeNode *node);
|
|
|
|
#ifdef G_TREE_DEBUG
|
|
|
|
static void g_tree_node_check (GTreeNode *node);
|
|
|
|
#endif
|
1998-06-11 01:21:14 +02:00
|
|
|
|
|
|
|
|
1998-11-24 13:18:22 +01:00
|
|
|
static GTreeNode*
|
|
|
|
g_tree_node_new (gpointer key,
|
2014-01-20 05:49:12 +01:00
|
|
|
gpointer value)
|
1998-11-24 13:18:22 +01:00
|
|
|
{
|
2005-11-01 19:10:31 +01:00
|
|
|
GTreeNode *node = g_slice_new (GTreeNode);
|
1998-11-24 13:18:22 +01:00
|
|
|
|
|
|
|
node->balance = 0;
|
|
|
|
node->left = NULL;
|
|
|
|
node->right = NULL;
|
2006-01-14 06:24:10 +01:00
|
|
|
node->left_child = FALSE;
|
|
|
|
node->right_child = FALSE;
|
1998-11-24 13:18:22 +01:00
|
|
|
node->key = key;
|
|
|
|
node->value = value;
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
/**
|
|
|
|
* g_tree_new:
|
|
|
|
* @key_compare_func: the function used to order the nodes in the #GTree.
|
2003-07-25 23:32:47 +02:00
|
|
|
* It should return values similar to the standard strcmp() function -
|
2001-05-04 19:01:56 +02:00
|
|
|
* 0 if the two arguments are equal, a negative value if the first argument
|
|
|
|
* comes before the second, or a positive value if the first argument comes
|
|
|
|
* after the second.
|
|
|
|
*
|
|
|
|
* Creates a new #GTree.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: a newly allocated #GTree
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
|
|
|
GTree *
|
2001-05-04 19:01:56 +02:00
|
|
|
g_tree_new (GCompareFunc key_compare_func)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
1998-09-18 04:12:32 +02:00
|
|
|
g_return_val_if_fail (key_compare_func != NULL, NULL);
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
return g_tree_new_full ((GCompareDataFunc) key_compare_func, NULL,
|
|
|
|
NULL, NULL);
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
/**
|
|
|
|
* g_tree_new_with_data:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @key_compare_func: qsort()-style comparison function
|
|
|
|
* @key_compare_data: data to pass to comparison function
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
|
|
|
* Creates a new #GTree with a comparison function that accepts user data.
|
|
|
|
* See g_tree_new() for more details.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: a newly allocated #GTree
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
|
|
|
GTree *
|
2001-05-04 19:01:56 +02:00
|
|
|
g_tree_new_with_data (GCompareDataFunc key_compare_func,
|
2014-01-20 05:49:12 +01:00
|
|
|
gpointer key_compare_data)
|
2000-11-21 00:59:32 +01:00
|
|
|
{
|
2001-05-04 19:01:56 +02:00
|
|
|
g_return_val_if_fail (key_compare_func != NULL, NULL);
|
|
|
|
|
|
|
|
return g_tree_new_full (key_compare_func, key_compare_data,
|
2014-01-20 05:49:12 +01:00
|
|
|
NULL, NULL);
|
2000-11-21 00:59:32 +01:00
|
|
|
}
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
/**
|
|
|
|
* g_tree_new_full:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @key_compare_func: qsort()-style comparison function
|
|
|
|
* @key_compare_data: data to pass to comparison function
|
2001-05-04 19:01:56 +02:00
|
|
|
* @key_destroy_func: a function to free the memory allocated for the key
|
2001-12-16 20:31:36 +01:00
|
|
|
* used when removing the entry from the #GTree or %NULL if you don't
|
2014-01-20 05:49:12 +01:00
|
|
|
* want to supply such a function
|
2001-05-04 19:01:56 +02:00
|
|
|
* @value_destroy_func: a function to free the memory allocated for the
|
2001-12-16 20:31:36 +01:00
|
|
|
* value used when removing the entry from the #GTree or %NULL if you
|
2014-01-20 05:49:12 +01:00
|
|
|
* don't want to supply such a function
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
|
|
|
* Creates a new #GTree like g_tree_new() and allows to specify functions
|
|
|
|
* to free the memory allocated for the key and value that get called when
|
|
|
|
* removing the entry from the #GTree.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: a newly allocated #GTree
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
|
|
|
GTree *
|
2001-05-04 19:01:56 +02:00
|
|
|
g_tree_new_full (GCompareDataFunc key_compare_func,
|
2014-01-20 05:49:12 +01:00
|
|
|
gpointer key_compare_data,
|
2001-05-04 19:01:56 +02:00
|
|
|
GDestroyNotify key_destroy_func,
|
2014-01-20 05:49:12 +01:00
|
|
|
GDestroyNotify value_destroy_func)
|
2001-05-04 19:01:56 +02:00
|
|
|
{
|
2001-09-19 20:08:19 +02:00
|
|
|
GTree *tree;
|
2001-05-04 19:01:56 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail (key_compare_func != NULL, NULL);
|
|
|
|
|
2009-07-05 13:30:54 +02:00
|
|
|
tree = g_slice_new (GTree);
|
2001-09-19 20:08:19 +02:00
|
|
|
tree->root = NULL;
|
|
|
|
tree->key_compare = key_compare_func;
|
|
|
|
tree->key_destroy_func = key_destroy_func;
|
|
|
|
tree->value_destroy_func = value_destroy_func;
|
|
|
|
tree->key_compare_data = key_compare_data;
|
2006-01-14 06:24:10 +01:00
|
|
|
tree->nnodes = 0;
|
2009-07-05 13:30:54 +02:00
|
|
|
tree->ref_count = 1;
|
2001-05-04 19:01:56 +02:00
|
|
|
|
2001-09-19 20:08:19 +02:00
|
|
|
return tree;
|
2001-05-04 19:01:56 +02:00
|
|
|
}
|
2000-11-21 00:59:32 +01:00
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
/**
|
|
|
|
* g_tree_node_first:
|
|
|
|
* @tree: a #GTree
|
|
|
|
*
|
|
|
|
* Returns the first in-order node of the tree, or %NULL
|
|
|
|
* for an empty tree.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): the first node in the tree
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
|
|
|
*/
|
|
|
|
GTreeNode *
|
|
|
|
g_tree_node_first (GTree *tree)
|
2006-01-14 06:24:10 +01:00
|
|
|
{
|
|
|
|
GTreeNode *tmp;
|
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
g_return_val_if_fail (tree != NULL, NULL);
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
if (!tree->root)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tmp = tree->root;
|
|
|
|
|
|
|
|
while (tmp->left_child)
|
|
|
|
tmp = tmp->left;
|
|
|
|
|
|
|
|
return tmp;
|
2020-08-04 21:31:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_tree_node_last:
|
|
|
|
* @tree: a #GTree
|
|
|
|
*
|
|
|
|
* Returns the last in-order node of the tree, or %NULL
|
|
|
|
* for an empty tree.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): the last node in the tree
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
|
|
|
*/
|
|
|
|
GTreeNode *
|
|
|
|
g_tree_node_last (GTree *tree)
|
|
|
|
{
|
|
|
|
GTreeNode *tmp;
|
|
|
|
|
|
|
|
g_return_val_if_fail (tree != NULL, NULL);
|
|
|
|
|
|
|
|
if (!tree->root)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tmp = tree->root;
|
|
|
|
|
|
|
|
while (tmp->right_child)
|
|
|
|
tmp = tmp->right;
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
/**
|
|
|
|
* g_tree_node_previous
|
|
|
|
* @node: a #GTree node
|
|
|
|
*
|
|
|
|
* Returns the previous in-order node of the tree, or %NULL
|
|
|
|
* if the passed node was already the first one.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): the previous node in the tree
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
|
|
|
*/
|
|
|
|
GTreeNode *
|
2006-01-14 06:24:10 +01:00
|
|
|
g_tree_node_previous (GTreeNode *node)
|
|
|
|
{
|
|
|
|
GTreeNode *tmp;
|
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
g_return_val_if_fail (node != NULL, NULL);
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
tmp = node->left;
|
|
|
|
|
|
|
|
if (node->left_child)
|
|
|
|
while (tmp->right_child)
|
|
|
|
tmp = tmp->right;
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
2014-01-20 05:49:12 +01:00
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
/**
|
|
|
|
* g_tree_node_next
|
|
|
|
* @node: a #GTree node
|
|
|
|
*
|
|
|
|
* Returns the next in-order node of the tree, or %NULL
|
|
|
|
* if the passed node was already the last one.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): the next node in the tree
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
|
|
|
*/
|
|
|
|
GTreeNode *
|
2006-01-14 06:24:10 +01:00
|
|
|
g_tree_node_next (GTreeNode *node)
|
|
|
|
{
|
|
|
|
GTreeNode *tmp;
|
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
g_return_val_if_fail (node != NULL, NULL);
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
tmp = node->right;
|
|
|
|
|
|
|
|
if (node->right_child)
|
|
|
|
while (tmp->left_child)
|
|
|
|
tmp = tmp->left;
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
2009-07-05 13:30:54 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
g_tree_remove_all (GTree *tree)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
2006-01-14 06:24:10 +01:00
|
|
|
GTreeNode *node;
|
|
|
|
GTreeNode *next;
|
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
g_return_if_fail (tree != NULL);
|
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
node = g_tree_node_first (tree);
|
2009-07-05 13:30:54 +02:00
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
next = g_tree_node_next (node);
|
|
|
|
|
|
|
|
if (tree->key_destroy_func)
|
2014-01-20 05:49:12 +01:00
|
|
|
tree->key_destroy_func (node->key);
|
2006-01-14 06:24:10 +01:00
|
|
|
if (tree->value_destroy_func)
|
2014-01-20 05:49:12 +01:00
|
|
|
tree->value_destroy_func (node->value);
|
2006-01-14 06:24:10 +01:00
|
|
|
g_slice_free (GTreeNode, node);
|
|
|
|
|
2020-08-04 21:23:54 +02:00
|
|
|
#ifdef G_TREE_DEBUG
|
|
|
|
g_assert (tree->nnodes > 0);
|
|
|
|
tree->nnodes--;
|
|
|
|
#endif
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
node = next;
|
|
|
|
}
|
2001-05-04 19:01:56 +02:00
|
|
|
|
2020-08-04 21:23:54 +02:00
|
|
|
#ifdef G_TREE_DEBUG
|
|
|
|
g_assert (tree->nnodes == 0);
|
|
|
|
#endif
|
|
|
|
|
2009-07-07 22:29:46 +02:00
|
|
|
tree->root = NULL;
|
2020-08-04 21:23:54 +02:00
|
|
|
#ifndef G_TREE_DEBUG
|
2009-07-07 22:29:46 +02:00
|
|
|
tree->nnodes = 0;
|
2020-08-04 21:23:54 +02:00
|
|
|
#endif
|
2009-07-05 13:30:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_tree_ref:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
2009-07-05 13:30:54 +02:00
|
|
|
*
|
2014-01-20 05:49:12 +01:00
|
|
|
* Increments the reference count of @tree by one.
|
|
|
|
*
|
|
|
|
* It is safe to call this function from any thread.
|
2009-07-05 13:30:54 +02:00
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: the passed in #GTree
|
2009-07-05 13:30:54 +02:00
|
|
|
*
|
|
|
|
* Since: 2.22
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
2009-07-05 13:30:54 +02:00
|
|
|
GTree *
|
|
|
|
g_tree_ref (GTree *tree)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (tree != NULL, NULL);
|
|
|
|
|
|
|
|
g_atomic_int_inc (&tree->ref_count);
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_tree_unref:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
2009-07-05 13:30:54 +02:00
|
|
|
*
|
2014-01-20 05:49:12 +01:00
|
|
|
* Decrements the reference count of @tree by one.
|
|
|
|
* If the reference count drops to 0, all keys and values will
|
|
|
|
* be destroyed (if destroy functions were specified) and all
|
|
|
|
* memory allocated by @tree will be released.
|
2009-07-05 13:30:54 +02:00
|
|
|
*
|
|
|
|
* It is safe to call this function from any thread.
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
2009-07-05 13:30:54 +02:00
|
|
|
void
|
|
|
|
g_tree_unref (GTree *tree)
|
|
|
|
{
|
|
|
|
g_return_if_fail (tree != NULL);
|
|
|
|
|
|
|
|
if (g_atomic_int_dec_and_test (&tree->ref_count))
|
|
|
|
{
|
|
|
|
g_tree_remove_all (tree);
|
|
|
|
g_slice_free (GTree, tree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_tree_destroy:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
2009-07-05 13:30:54 +02:00
|
|
|
*
|
|
|
|
* Removes all keys and values from the #GTree and decreases its
|
|
|
|
* reference count by one. If keys and/or values are dynamically
|
|
|
|
* allocated, you should either free them first or create the #GTree
|
2014-01-20 05:49:12 +01:00
|
|
|
* using g_tree_new_full(). In the latter case the destroy functions
|
2009-07-05 13:30:54 +02:00
|
|
|
* you supplied will be called on all keys and values before destroying
|
|
|
|
* the #GTree.
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
2009-07-05 13:30:54 +02:00
|
|
|
void
|
|
|
|
g_tree_destroy (GTree *tree)
|
|
|
|
{
|
|
|
|
g_return_if_fail (tree != NULL);
|
|
|
|
|
|
|
|
g_tree_remove_all (tree);
|
|
|
|
g_tree_unref (tree);
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
/**
|
2020-08-04 21:31:36 +02:00
|
|
|
* g_tree_insert_node:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
|
|
|
* @key: the key to insert
|
|
|
|
* @value: the value corresponding to the key
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
2014-01-20 05:49:12 +01:00
|
|
|
* Inserts a key/value pair into a #GTree.
|
|
|
|
*
|
|
|
|
* If the given key already exists in the #GTree its corresponding value
|
|
|
|
* is set to the new value. 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 passed
|
|
|
|
* key is freed using that function.
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2019-08-06 21:12:39 +02:00
|
|
|
* The cost of maintaining a balanced tree while inserting new key/value
|
|
|
|
* result in a O(n log(n)) operation where most of the other operations
|
|
|
|
* are O(log(n)).
|
2020-08-04 21:31:36 +02:00
|
|
|
*
|
|
|
|
* Returns: (transfer none): the inserted (or set) node.
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
2020-08-04 21:31:36 +02:00
|
|
|
GTreeNode *
|
|
|
|
g_tree_insert_node (GTree *tree,
|
|
|
|
gpointer key,
|
|
|
|
gpointer value)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
2020-08-04 21:31:36 +02:00
|
|
|
GTreeNode *node;
|
2001-05-04 19:01:56 +02:00
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
g_return_val_if_fail (tree != NULL, NULL);
|
|
|
|
|
|
|
|
node = g_tree_insert_internal (tree, key, value, FALSE);
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
#ifdef G_TREE_DEBUG
|
|
|
|
g_tree_node_check (tree->root);
|
|
|
|
#endif
|
2020-08-04 21:31:36 +02:00
|
|
|
|
|
|
|
return node;
|
2001-05-04 19:01:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-04 21:31:36 +02:00
|
|
|
* g_tree_insert:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
|
|
|
* @key: the key to insert
|
|
|
|
* @value: the value corresponding to the key
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
2020-08-04 21:31:36 +02:00
|
|
|
* Inserts a key/value pair into a #GTree.
|
|
|
|
*
|
|
|
|
* Inserts a new key and value into a #GTree as g_tree_insert_node() does,
|
|
|
|
* only this function does not return the inserted or set node.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_tree_insert (GTree *tree,
|
|
|
|
gpointer key,
|
|
|
|
gpointer value)
|
|
|
|
{
|
|
|
|
g_tree_insert_node (tree, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_tree_replace_node:
|
|
|
|
* @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_node().
|
2001-05-04 19:01:56 +02:00
|
|
|
* The difference is that if the key already exists in the #GTree, it gets
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
* replaced by the new key. If you supplied a @value_destroy_func when
|
2001-05-04 19:01:56 +02:00
|
|
|
* creating the #GTree, the old value is freed using that function. If you
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
* supplied a @key_destroy_func when creating the #GTree, the old key is
|
2001-05-04 19:01:56 +02:00
|
|
|
* 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.
|
2020-08-04 21:31:36 +02:00
|
|
|
*
|
|
|
|
* Returns: (transfer none): the inserted (or set) node.
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
2020-08-04 21:31:36 +02:00
|
|
|
GTreeNode *
|
|
|
|
g_tree_replace_node (GTree *tree,
|
|
|
|
gpointer key,
|
|
|
|
gpointer value)
|
2001-05-04 19:01:56 +02:00
|
|
|
{
|
2020-08-04 21:31:36 +02:00
|
|
|
GTreeNode *node;
|
2006-01-14 06:24:10 +01:00
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
g_return_val_if_fail (tree != NULL, NULL);
|
|
|
|
|
|
|
|
node = g_tree_insert_internal (tree, key, value, TRUE);
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
#ifdef G_TREE_DEBUG
|
|
|
|
g_tree_node_check (tree->root);
|
|
|
|
#endif
|
2020-08-04 21:31:36 +02:00
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 as g_tree_replace_node() does,
|
|
|
|
* only this function does not return the inserted or set node.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_tree_replace (GTree *tree,
|
|
|
|
gpointer key,
|
|
|
|
gpointer value)
|
|
|
|
{
|
|
|
|
g_tree_replace_node (tree, key, value);
|
2006-01-14 06:24:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* internal insert routine */
|
2020-08-04 21:31:36 +02:00
|
|
|
static GTreeNode *
|
2006-01-14 06:24:10 +01:00
|
|
|
g_tree_insert_internal (GTree *tree,
|
|
|
|
gpointer key,
|
|
|
|
gpointer value,
|
|
|
|
gboolean replace)
|
|
|
|
{
|
2020-08-04 21:31:36 +02:00
|
|
|
GTreeNode *node, *retnode;
|
2006-01-14 06:24:10 +01:00
|
|
|
GTreeNode *path[MAX_GTREE_HEIGHT];
|
|
|
|
int idx;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
g_return_val_if_fail (tree != NULL, NULL);
|
1998-06-11 01:21:14 +02:00
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
if (!tree->root)
|
|
|
|
{
|
|
|
|
tree->root = g_tree_node_new (key, value);
|
|
|
|
tree->nnodes++;
|
2020-08-04 21:31:36 +02:00
|
|
|
return tree->root;
|
2006-01-14 06:24:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (tree->value_destroy_func)
|
|
|
|
tree->value_destroy_func (node->value);
|
|
|
|
|
|
|
|
node->value = value;
|
|
|
|
|
|
|
|
if (replace)
|
|
|
|
{
|
|
|
|
if (tree->key_destroy_func)
|
|
|
|
tree->key_destroy_func (node->key);
|
|
|
|
|
|
|
|
node->key = key;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* free the passed key */
|
|
|
|
if (tree->key_destroy_func)
|
|
|
|
tree->key_destroy_func (key);
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
return node;
|
2006-01-14 06:24:10 +01:00
|
|
|
}
|
|
|
|
else if (cmp < 0)
|
|
|
|
{
|
|
|
|
if (node->left_child)
|
|
|
|
{
|
|
|
|
path[idx++] = node;
|
|
|
|
node = node->left;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GTreeNode *child = g_tree_node_new (key, value);
|
|
|
|
|
|
|
|
child->left = node->left;
|
|
|
|
child->right = node;
|
|
|
|
node->left = child;
|
|
|
|
node->left_child = TRUE;
|
|
|
|
node->balance -= 1;
|
|
|
|
|
2014-01-20 05:49:12 +01:00
|
|
|
tree->nnodes++;
|
2006-01-14 06:24:10 +01:00
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
retnode = child;
|
2006-01-14 06:24:10 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (node->right_child)
|
|
|
|
{
|
|
|
|
path[idx++] = node;
|
|
|
|
node = node->right;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GTreeNode *child = g_tree_node_new (key, value);
|
|
|
|
|
|
|
|
child->right = node->right;
|
|
|
|
child->left = node;
|
|
|
|
node->right = child;
|
|
|
|
node->right_child = TRUE;
|
|
|
|
node->balance += 1;
|
|
|
|
|
2014-01-20 05:49:12 +01:00
|
|
|
tree->nnodes++;
|
2006-01-14 06:24:10 +01:00
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
retnode = child;
|
2006-01-14 06:24:10 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 05:49:12 +01:00
|
|
|
/* Restore balance. This is the goodness of a non-recursive
|
|
|
|
* implementation, when we are done with balancing we 'break'
|
|
|
|
* the loop and we are done.
|
|
|
|
*/
|
2006-01-14 06:24:10 +01:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
GTreeNode *bparent = path[--idx];
|
|
|
|
gboolean left_node = (bparent && node == bparent->left);
|
|
|
|
g_assert (!bparent || bparent->left == node || bparent->right == node);
|
|
|
|
|
|
|
|
if (node->balance < -1 || node->balance > 1)
|
|
|
|
{
|
|
|
|
node = g_tree_node_balance (node);
|
|
|
|
if (bparent == NULL)
|
|
|
|
tree->root = node;
|
|
|
|
else if (left_node)
|
|
|
|
bparent->left = node;
|
|
|
|
else
|
|
|
|
bparent->right = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->balance == 0 || bparent == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (left_node)
|
|
|
|
bparent->balance -= 1;
|
|
|
|
else
|
|
|
|
bparent->balance += 1;
|
|
|
|
|
|
|
|
node = bparent;
|
|
|
|
}
|
2020-08-04 21:31:36 +02:00
|
|
|
|
|
|
|
return retnode;
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
/**
|
|
|
|
* g_tree_remove:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
|
|
|
* @key: the key to remove
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
|
|
|
* Removes a key/value pair from a #GTree.
|
|
|
|
*
|
|
|
|
* If the #GTree was created using g_tree_new_full(), the key and value
|
2001-12-16 20:31:36 +01:00
|
|
|
* are freed using the supplied destroy functions, otherwise you have to
|
2001-05-04 19:01:56 +02:00
|
|
|
* make sure that any dynamically allocated values are freed yourself.
|
2005-04-29 20:28:56 +02:00
|
|
|
* If the key does not exist in the #GTree, the function does nothing.
|
2005-05-17 17:33:36 +02:00
|
|
|
*
|
2019-08-06 21:12:39 +02:00
|
|
|
* The cost of maintaining a balanced tree while removing a key/value
|
|
|
|
* result in a O(n log(n)) operation where most of the other operations
|
|
|
|
* are O(log(n)).
|
|
|
|
*
|
2014-01-20 05:49:12 +01:00
|
|
|
* Returns: %TRUE if the key was found (prior to 2.8, this function
|
|
|
|
* returned nothing)
|
|
|
|
*/
|
2005-05-17 17:33:36 +02:00
|
|
|
gboolean
|
2000-04-26 10:42:19 +02:00
|
|
|
g_tree_remove (GTree *tree,
|
2014-01-20 05:49:12 +01:00
|
|
|
gconstpointer key)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
2005-05-17 17:33:36 +02:00
|
|
|
gboolean removed;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
2005-05-17 17:33:36 +02:00
|
|
|
g_return_val_if_fail (tree != NULL, FALSE);
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
removed = g_tree_remove_internal (tree, key, FALSE);
|
|
|
|
|
|
|
|
#ifdef G_TREE_DEBUG
|
|
|
|
g_tree_node_check (tree->root);
|
|
|
|
#endif
|
2005-05-17 17:33:36 +02:00
|
|
|
|
|
|
|
return removed;
|
2001-05-04 19:01:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_tree_steal:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
|
|
|
* @key: the key to remove
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
|
|
|
* Removes a key and its associated value from a #GTree without calling
|
|
|
|
* the key and value destroy functions.
|
2005-04-29 20:28:56 +02:00
|
|
|
*
|
|
|
|
* If the key does not exist in the #GTree, the function does nothing.
|
2005-05-17 17:33:36 +02:00
|
|
|
*
|
2014-01-20 05:49:12 +01:00
|
|
|
* Returns: %TRUE if the key was found (prior to 2.8, this function
|
|
|
|
* returned nothing)
|
|
|
|
*/
|
2005-05-17 17:33:36 +02:00
|
|
|
gboolean
|
2001-05-04 19:01:56 +02:00
|
|
|
g_tree_steal (GTree *tree,
|
|
|
|
gconstpointer key)
|
|
|
|
{
|
2005-05-17 17:33:36 +02:00
|
|
|
gboolean removed;
|
|
|
|
|
|
|
|
g_return_val_if_fail (tree != NULL, FALSE);
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
removed = g_tree_remove_internal (tree, key, TRUE);
|
|
|
|
|
|
|
|
#ifdef G_TREE_DEBUG
|
|
|
|
g_tree_node_check (tree->root);
|
|
|
|
#endif
|
2001-05-04 19:01:56 +02:00
|
|
|
|
2005-05-17 17:33:36 +02:00
|
|
|
return removed;
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
/* 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);
|
2014-01-20 05:49:12 +01:00
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
if (cmp == 0)
|
|
|
|
break;
|
|
|
|
else if (cmp < 0)
|
|
|
|
{
|
|
|
|
if (!node->left_child)
|
|
|
|
return FALSE;
|
2014-01-20 05:49:12 +01:00
|
|
|
|
|
|
|
path[idx++] = node;
|
|
|
|
node = node->left;
|
2006-01-14 06:24:10 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!node->right_child)
|
|
|
|
return FALSE;
|
2014-01-20 05:49:12 +01:00
|
|
|
|
|
|
|
path[idx++] = node;
|
|
|
|
node = node->right;
|
2006-01-14 06:24:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 05:49:12 +01:00
|
|
|
/* The following code is almost equal to g_tree_remove_node,
|
|
|
|
* except that we do not have to call g_tree_node_parent.
|
|
|
|
*/
|
2006-01-14 06:24:10 +01:00
|
|
|
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);
|
2014-01-20 05:49:12 +01:00
|
|
|
tmp->left = node->left;
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
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;
|
2014-01-20 05:49:12 +01:00
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
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!) */
|
|
|
|
{
|
2014-01-20 05:49:12 +01:00
|
|
|
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)
|
2006-01-14 06:24:10 +01:00
|
|
|
{
|
2014-01-20 05:49:12 +01:00
|
|
|
path[++idx] = nextp = next;
|
|
|
|
next = next->left;
|
2006-01-14 06:24:10 +01:00
|
|
|
}
|
2014-01-20 05:49:12 +01:00
|
|
|
|
|
|
|
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;
|
2006-01-14 06:24:10 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-20 05:49:12 +01:00
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
/* restore balance */
|
|
|
|
if (balance)
|
|
|
|
while (1)
|
|
|
|
{
|
2014-01-20 05:49:12 +01:00
|
|
|
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;
|
2006-01-14 06:24:10 +01:00
|
|
|
}
|
2014-01-20 05:49:12 +01:00
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
/**
|
|
|
|
* g_tree_node_key:
|
|
|
|
* @node: a #GTree node
|
|
|
|
*
|
|
|
|
* Gets the key stored at a particular tree node.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): the key at the node.
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
|
|
|
*/
|
|
|
|
gpointer
|
|
|
|
g_tree_node_key (GTreeNode *node)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (node != NULL, NULL);
|
|
|
|
|
|
|
|
return node->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_tree_node_value:
|
|
|
|
* @node: a #GTree node
|
|
|
|
*
|
|
|
|
* Gets the value stored at a particular tree node.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): the value at the node.
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
|
|
|
*/
|
|
|
|
gpointer
|
|
|
|
g_tree_node_value (GTreeNode *node)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (node != NULL, NULL);
|
|
|
|
|
|
|
|
return node->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_tree_lookup_node:
|
|
|
|
* @tree: a #GTree
|
|
|
|
* @key: the key to look up
|
|
|
|
*
|
|
|
|
* Gets the tree node corresponding to the given key. Since a #GTree is
|
|
|
|
* automatically balanced as key/value pairs are added, key lookup
|
|
|
|
* is O(log n) (where n is the number of key/value pairs in the tree).
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): the tree node corresponding to
|
|
|
|
* the key, or %NULL if the key was not found
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
|
|
|
*/
|
|
|
|
GTreeNode *
|
|
|
|
g_tree_lookup_node (GTree *tree,
|
|
|
|
gconstpointer key)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (tree != NULL, NULL);
|
|
|
|
|
|
|
|
return g_tree_find_node (tree, key);
|
|
|
|
}
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
/**
|
|
|
|
* g_tree_lookup:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
|
|
|
* @key: the key to look up
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
|
|
|
* Gets the value corresponding to the given key. Since a #GTree is
|
2014-01-20 05:49:12 +01:00
|
|
|
* automatically balanced as key/value pairs are added, key lookup
|
|
|
|
* is O(log n) (where n is the number of key/value pairs in the tree).
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: the value corresponding to the key, or %NULL
|
2014-02-02 02:53:17 +01:00
|
|
|
* if the key was not found
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
1998-06-11 01:21:14 +02:00
|
|
|
gpointer
|
2000-04-26 10:42:19 +02:00
|
|
|
g_tree_lookup (GTree *tree,
|
2014-01-20 05:49:12 +01:00
|
|
|
gconstpointer key)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
2001-03-08 19:18:16 +01:00
|
|
|
GTreeNode *node;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
node = g_tree_lookup_node (tree, key);
|
1998-06-11 01:21:14 +02:00
|
|
|
|
2001-03-08 19:18:16 +01:00
|
|
|
return node ? node->value : NULL;
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
/**
|
|
|
|
* g_tree_lookup_extended:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
|
|
|
* @lookup_key: the key to look up
|
2019-07-19 13:45:49 +02:00
|
|
|
* @orig_key: (out) (optional) (nullable): returns the original key
|
|
|
|
* @value: (out) (optional) (nullable): returns the value associated with the key
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
|
|
|
* Looks up a key in the #GTree, returning the original key and the
|
2014-01-20 05:49:12 +01:00
|
|
|
* associated value. This is useful if you need to free the memory
|
|
|
|
* allocated for the original key, for example before calling
|
|
|
|
* g_tree_remove().
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: %TRUE if the key was found in the #GTree
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
2001-03-08 18:51:39 +01:00
|
|
|
gboolean
|
|
|
|
g_tree_lookup_extended (GTree *tree,
|
|
|
|
gconstpointer lookup_key,
|
|
|
|
gpointer *orig_key,
|
|
|
|
gpointer *value)
|
|
|
|
{
|
|
|
|
GTreeNode *node;
|
|
|
|
|
|
|
|
g_return_val_if_fail (tree != NULL, FALSE);
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
node = g_tree_find_node (tree, lookup_key);
|
|
|
|
|
2001-03-08 18:51:39 +01:00
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
if (orig_key)
|
|
|
|
*orig_key = node->key;
|
|
|
|
if (value)
|
|
|
|
*value = node->value;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
/**
|
|
|
|
* g_tree_foreach:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
2012-09-23 07:58:44 +02:00
|
|
|
* @func: the function to call for each node visited.
|
|
|
|
* If this function returns %TRUE, the traversal is stopped.
|
2014-01-20 05:49:12 +01:00
|
|
|
* @user_data: user data to pass to the function
|
2012-09-23 07:58:44 +02:00
|
|
|
*
|
2001-05-04 19:01:56 +02:00
|
|
|
* 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
|
2001-12-05 02:38:12 +01:00
|
|
|
* @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.
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
2001-05-04 19:01:56 +02:00
|
|
|
void
|
|
|
|
g_tree_foreach (GTree *tree,
|
|
|
|
GTraverseFunc func,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2006-01-14 06:24:10 +01:00
|
|
|
GTreeNode *node;
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
g_return_if_fail (tree != NULL);
|
|
|
|
|
2001-09-19 20:08:19 +02:00
|
|
|
if (!tree->root)
|
2001-05-04 19:01:56 +02:00
|
|
|
return;
|
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
node = g_tree_node_first (tree);
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
if ((*func) (node->key, node->value, user_data))
|
2014-01-20 05:49:12 +01:00
|
|
|
break;
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
node = g_tree_node_next (node);
|
|
|
|
}
|
2001-05-04 19:01:56 +02:00
|
|
|
}
|
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
/**
|
|
|
|
* g_tree_foreach_node:
|
|
|
|
* @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 nodes in the #GTree.
|
|
|
|
* The function is passed the pointer to the particular node, and the given
|
|
|
|
* @data parameter. The tree traversal happens in-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.
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_tree_foreach_node (GTree *tree,
|
|
|
|
GTraverseNodeFunc func,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GTreeNode *node;
|
|
|
|
|
|
|
|
g_return_if_fail (tree != NULL);
|
|
|
|
|
|
|
|
if (!tree->root)
|
|
|
|
return;
|
|
|
|
|
|
|
|
node = g_tree_node_first (tree);
|
|
|
|
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
if ((*func) (node, user_data))
|
|
|
|
break;
|
|
|
|
|
|
|
|
node = g_tree_node_next (node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
/**
|
|
|
|
* g_tree_traverse:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
2001-05-04 19:01:56 +02:00
|
|
|
* @traverse_func: the function to call for each node visited. If this
|
2001-12-16 20:31:36 +01:00
|
|
|
* function returns %TRUE, the traversal is stopped.
|
2001-05-04 19:01:56 +02:00
|
|
|
* @traverse_type: the order in which nodes are visited, one of %G_IN_ORDER,
|
2014-01-20 05:49:12 +01:00
|
|
|
* %G_PRE_ORDER and %G_POST_ORDER
|
|
|
|
* @user_data: user data to pass to the function
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
2002-11-28 21:46:29 +01:00
|
|
|
* Calls the given function for each node in the #GTree.
|
|
|
|
*
|
2014-01-20 05:49:12 +01:00
|
|
|
* Deprecated:2.2: The order of a balanced tree is somewhat arbitrary.
|
|
|
|
* If you just want to visit all nodes in sorted order, use
|
|
|
|
* g_tree_foreach() instead. If you really need to visit nodes in
|
2014-02-08 18:26:56 +01:00
|
|
|
* a different order, consider using an [n-ary tree][glib-N-ary-Trees].
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
2010-01-31 06:27:28 +01:00
|
|
|
/**
|
|
|
|
* GTraverseFunc:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @key: a key of a #GTree node
|
|
|
|
* @value: the value corresponding to the key
|
|
|
|
* @data: user data passed to g_tree_traverse()
|
2010-01-31 06:27:28 +01:00
|
|
|
*
|
|
|
|
* Specifies the type of function passed to g_tree_traverse(). It is
|
|
|
|
* passed the key and value of each node, together with the @user_data
|
|
|
|
* parameter passed to g_tree_traverse(). If the function returns
|
|
|
|
* %TRUE, the traversal is stopped.
|
2012-10-31 02:56:00 +01:00
|
|
|
*
|
2014-01-20 05:49:12 +01:00
|
|
|
* Returns: %TRUE to stop the traversal
|
|
|
|
*/
|
1998-06-11 01:21:14 +02:00
|
|
|
void
|
|
|
|
g_tree_traverse (GTree *tree,
|
2014-01-20 05:49:12 +01:00
|
|
|
GTraverseFunc traverse_func,
|
|
|
|
GTraverseType traverse_type,
|
|
|
|
gpointer user_data)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
|
|
|
g_return_if_fail (tree != NULL);
|
|
|
|
|
2001-09-19 20:08:19 +02:00
|
|
|
if (!tree->root)
|
1999-01-17 17:56:28 +01:00
|
|
|
return;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
|
|
|
switch (traverse_type)
|
|
|
|
{
|
|
|
|
case G_PRE_ORDER:
|
2001-09-19 20:08:19 +02:00
|
|
|
g_tree_node_pre_order (tree->root, traverse_func, user_data);
|
1998-06-11 01:21:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case G_IN_ORDER:
|
2001-09-19 20:08:19 +02:00
|
|
|
g_tree_node_in_order (tree->root, traverse_func, user_data);
|
1998-06-11 01:21:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case G_POST_ORDER:
|
2001-09-19 20:08:19 +02:00
|
|
|
g_tree_node_post_order (tree->root, traverse_func, user_data);
|
1998-06-11 01:21:14 +02:00
|
|
|
break;
|
1998-07-31 22:21:10 +02:00
|
|
|
|
|
|
|
case G_LEVEL_ORDER:
|
|
|
|
g_warning ("g_tree_traverse(): traverse type G_LEVEL_ORDER isn't implemented.");
|
|
|
|
break;
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
/**
|
|
|
|
* g_tree_search_node:
|
|
|
|
* @tree: a #GTree
|
|
|
|
* @search_func: a function used to search the #GTree
|
|
|
|
* @user_data: the data passed as the second argument to @search_func
|
|
|
|
*
|
|
|
|
* Searches a #GTree using @search_func.
|
|
|
|
*
|
|
|
|
* The @search_func is called with a pointer to the key of a key/value
|
|
|
|
* pair in the tree, and the passed in @user_data. If @search_func returns
|
|
|
|
* 0 for a key/value pair, then the corresponding node is returned as
|
|
|
|
* the result of g_tree_search(). If @search_func returns -1, searching
|
|
|
|
* will proceed among the key/value pairs that have a smaller key; if
|
|
|
|
* @search_func returns 1, searching will proceed among the key/value
|
|
|
|
* pairs that have a larger key.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): the node corresponding to the
|
|
|
|
* found key, or %NULL if the key was not found
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
|
|
|
*/
|
|
|
|
GTreeNode *
|
|
|
|
g_tree_search_node (GTree *tree,
|
|
|
|
GCompareFunc search_func,
|
|
|
|
gconstpointer user_data)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (tree != NULL, NULL);
|
|
|
|
|
|
|
|
if (!tree->root)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return g_tree_node_search (tree->root, search_func, user_data);
|
|
|
|
}
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
/**
|
|
|
|
* g_tree_search:
|
2011-06-04 17:57:10 +02:00
|
|
|
* @tree: a #GTree
|
|
|
|
* @search_func: a function used to search the #GTree
|
|
|
|
* @user_data: the data passed as the second argument to @search_func
|
|
|
|
*
|
2002-10-12 12:36:45 +02:00
|
|
|
* Searches a #GTree using @search_func.
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
2011-06-04 17:57:10 +02:00
|
|
|
* The @search_func is called with a pointer to the key of a key/value
|
|
|
|
* pair in the tree, and the passed in @user_data. If @search_func returns
|
|
|
|
* 0 for a key/value pair, then the corresponding value is returned as
|
|
|
|
* the result of g_tree_search(). If @search_func returns -1, searching
|
|
|
|
* will proceed among the key/value pairs that have a smaller key; if
|
|
|
|
* @search_func returns 1, searching will proceed among the key/value
|
|
|
|
* pairs that have a larger key.
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: the value corresponding to the found key, or %NULL
|
2014-02-02 02:53:17 +01:00
|
|
|
* if the key was not found
|
2011-06-04 17:57:10 +02:00
|
|
|
*/
|
1998-06-11 01:21:14 +02:00
|
|
|
gpointer
|
2001-05-04 19:01:56 +02:00
|
|
|
g_tree_search (GTree *tree,
|
2014-01-20 05:49:12 +01:00
|
|
|
GCompareFunc search_func,
|
|
|
|
gconstpointer user_data)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
2020-08-04 21:31:36 +02:00
|
|
|
GTreeNode *node;
|
|
|
|
|
|
|
|
node = g_tree_search_node (tree, search_func, user_data);
|
|
|
|
|
|
|
|
return node ? node->value : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_tree_lower_bound:
|
|
|
|
* @tree: a #GTree
|
|
|
|
* @key: the key to calculate the lower bound for
|
|
|
|
*
|
|
|
|
* Gets the lower bound node corresponding to the given key,
|
|
|
|
* or %NULL if the tree is empty or all the nodes in the tree
|
|
|
|
* have keys that are strictly lower than the searched key.
|
|
|
|
*
|
|
|
|
* The lower bound is the first node that has its key greater
|
|
|
|
* than or equal to the searched key.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): the tree node corresponding to
|
|
|
|
* the lower bound, or %NULL if the tree is empty or has only
|
|
|
|
* keys strictly lower than the searched key.
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
|
|
|
*/
|
|
|
|
GTreeNode *
|
|
|
|
g_tree_lower_bound (GTree *tree,
|
|
|
|
gconstpointer key)
|
|
|
|
{
|
|
|
|
GTreeNode *node, *result;
|
|
|
|
gint cmp;
|
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
g_return_val_if_fail (tree != NULL, NULL);
|
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
node = tree->root;
|
|
|
|
if (!node)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
result = NULL;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
cmp = tree->key_compare (key, node->key, tree->key_compare_data);
|
|
|
|
if (cmp <= 0)
|
|
|
|
{
|
|
|
|
result = node;
|
|
|
|
|
|
|
|
if (!node->left_child)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
node = node->left;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!node->right_child)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
node = node->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_tree_upper_bound:
|
|
|
|
* @tree: a #GTree
|
|
|
|
* @key: the key to calculate the upper bound for
|
|
|
|
*
|
|
|
|
* Gets the upper bound node corresponding to the given key,
|
|
|
|
* or %NULL if the tree is empty or all the nodes in the tree
|
|
|
|
* have keys that are lower than or equal to the searched key.
|
|
|
|
*
|
|
|
|
* The upper bound is the first node that has its key strictly greater
|
|
|
|
* than the searched key.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): the tree node corresponding to the
|
|
|
|
* upper bound, or %NULL if the tree is empty or has only keys
|
|
|
|
* lower than or equal to the searched key.
|
|
|
|
*
|
|
|
|
* Since: 2.68
|
|
|
|
*/
|
|
|
|
GTreeNode *
|
|
|
|
g_tree_upper_bound (GTree *tree,
|
|
|
|
gconstpointer key)
|
|
|
|
{
|
|
|
|
GTreeNode *node, *result;
|
|
|
|
gint cmp;
|
|
|
|
|
|
|
|
g_return_val_if_fail (tree != NULL, NULL);
|
|
|
|
|
|
|
|
node = tree->root;
|
|
|
|
if (!node)
|
1999-01-17 17:56:28 +01:00
|
|
|
return NULL;
|
2020-08-04 21:31:36 +02:00
|
|
|
|
|
|
|
result = NULL;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
cmp = tree->key_compare (key, node->key, tree->key_compare_data);
|
|
|
|
if (cmp < 0)
|
|
|
|
{
|
|
|
|
result = node;
|
|
|
|
|
|
|
|
if (!node->left_child)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
node = node->left;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!node->right_child)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
node = node->right;
|
|
|
|
}
|
|
|
|
}
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
/**
|
|
|
|
* g_tree_height:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
|
|
|
* Gets the height of a #GTree.
|
|
|
|
*
|
|
|
|
* If the #GTree contains no nodes, the height is 0.
|
|
|
|
* If the #GTree contains only one root node the height is 1.
|
|
|
|
* If the root node has children the height is 2, etc.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: the height of @tree
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
1998-06-11 01:21:14 +02:00
|
|
|
gint
|
|
|
|
g_tree_height (GTree *tree)
|
|
|
|
{
|
2006-01-14 06:24:10 +01:00
|
|
|
GTreeNode *node;
|
|
|
|
gint height;
|
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
g_return_val_if_fail (tree != NULL, 0);
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
if (!tree->root)
|
1999-01-17 17:56:28 +01:00
|
|
|
return 0;
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
height = 0;
|
|
|
|
node = tree->root;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
height += 1 + MAX(node->balance, 0);
|
|
|
|
|
|
|
|
if (!node->left_child)
|
2014-01-20 05:49:12 +01:00
|
|
|
return height;
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
node = node->left;
|
|
|
|
}
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
|
2001-05-04 19:01:56 +02:00
|
|
|
/**
|
|
|
|
* g_tree_nnodes:
|
2014-01-20 05:49:12 +01:00
|
|
|
* @tree: a #GTree
|
2001-05-04 19:01:56 +02:00
|
|
|
*
|
|
|
|
* Gets the number of nodes in a #GTree.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: the number of nodes in @tree
|
2014-01-20 05:49:12 +01:00
|
|
|
*/
|
1998-06-11 01:21:14 +02:00
|
|
|
gint
|
|
|
|
g_tree_nnodes (GTree *tree)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (tree != NULL, 0);
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
return tree->nnodes;
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
|
2014-01-20 05:49:12 +01:00
|
|
|
static GTreeNode *
|
1998-06-11 01:21:14 +02:00
|
|
|
g_tree_node_balance (GTreeNode *node)
|
|
|
|
{
|
|
|
|
if (node->balance < -1)
|
|
|
|
{
|
|
|
|
if (node->left->balance > 0)
|
2014-01-20 05:49:12 +01:00
|
|
|
node->left = g_tree_node_rotate_left (node->left);
|
1998-06-11 01:21:14 +02:00
|
|
|
node = g_tree_node_rotate_right (node);
|
|
|
|
}
|
|
|
|
else if (node->balance > 1)
|
|
|
|
{
|
|
|
|
if (node->right->balance < 0)
|
2014-01-20 05:49:12 +01:00
|
|
|
node->right = g_tree_node_rotate_right (node->right);
|
1998-06-11 01:21:14 +02:00
|
|
|
node = g_tree_node_rotate_left (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2001-03-08 19:18:16 +01:00
|
|
|
static GTreeNode *
|
2006-01-14 06:24:10 +01:00
|
|
|
g_tree_find_node (GTree *tree,
|
2014-01-20 05:49:12 +01:00
|
|
|
gconstpointer key)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
2006-01-14 06:24:10 +01:00
|
|
|
GTreeNode *node;
|
1998-06-11 01:21:14 +02:00
|
|
|
gint cmp;
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
node = tree->root;
|
1998-06-11 01:21:14 +02:00
|
|
|
if (!node)
|
|
|
|
return NULL;
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
while (1)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
2006-01-14 06:24:10 +01:00
|
|
|
cmp = tree->key_compare (key, node->key, tree->key_compare_data);
|
|
|
|
if (cmp == 0)
|
2014-01-20 05:49:12 +01:00
|
|
|
return node;
|
2006-01-14 06:24:10 +01:00
|
|
|
else if (cmp < 0)
|
2014-01-20 05:49:12 +01:00
|
|
|
{
|
|
|
|
if (!node->left_child)
|
|
|
|
return NULL;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
2014-01-20 05:49:12 +01:00
|
|
|
node = node->left;
|
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
else
|
2014-01-20 05:49:12 +01:00
|
|
|
{
|
|
|
|
if (!node->right_child)
|
|
|
|
return NULL;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
2014-01-20 05:49:12 +01:00
|
|
|
node = node->right;
|
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
}
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
g_tree_node_pre_order (GTreeNode *node,
|
2014-01-20 05:49:12 +01:00
|
|
|
GTraverseFunc traverse_func,
|
|
|
|
gpointer data)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
|
|
|
if ((*traverse_func) (node->key, node->value, data))
|
|
|
|
return TRUE;
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
if (node->left_child)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
|
|
|
if (g_tree_node_pre_order (node->left, traverse_func, data))
|
2014-01-20 05:49:12 +01:00
|
|
|
return TRUE;
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
if (node->right_child)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
|
|
|
if (g_tree_node_pre_order (node->right, traverse_func, data))
|
2014-01-20 05:49:12 +01:00
|
|
|
return TRUE;
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
g_tree_node_in_order (GTreeNode *node,
|
2014-01-20 05:49:12 +01:00
|
|
|
GTraverseFunc traverse_func,
|
|
|
|
gpointer data)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
2006-01-14 06:24:10 +01:00
|
|
|
if (node->left_child)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
|
|
|
if (g_tree_node_in_order (node->left, traverse_func, data))
|
2014-01-20 05:49:12 +01:00
|
|
|
return TRUE;
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
if ((*traverse_func) (node->key, node->value, data))
|
|
|
|
return TRUE;
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
if (node->right_child)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
|
|
|
if (g_tree_node_in_order (node->right, traverse_func, data))
|
2014-01-20 05:49:12 +01:00
|
|
|
return TRUE;
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
g_tree_node_post_order (GTreeNode *node,
|
2014-01-20 05:49:12 +01:00
|
|
|
GTraverseFunc traverse_func,
|
|
|
|
gpointer data)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
2006-01-14 06:24:10 +01:00
|
|
|
if (node->left_child)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
|
|
|
if (g_tree_node_post_order (node->left, traverse_func, data))
|
2014-01-20 05:49:12 +01:00
|
|
|
return TRUE;
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
if (node->right_child)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
|
|
|
if (g_tree_node_post_order (node->right, traverse_func, data))
|
2014-01-20 05:49:12 +01:00
|
|
|
return TRUE;
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
if ((*traverse_func) (node->key, node->value, data))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:31:36 +02:00
|
|
|
static GTreeNode *
|
2000-04-26 10:42:19 +02:00
|
|
|
g_tree_node_search (GTreeNode *node,
|
2014-01-20 05:49:12 +01:00
|
|
|
GCompareFunc search_func,
|
|
|
|
gconstpointer data)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
|
|
|
gint dir;
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return NULL;
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
while (1)
|
1998-06-11 01:21:14 +02:00
|
|
|
{
|
2006-01-14 06:24:10 +01:00
|
|
|
dir = (* search_func) (node->key, data);
|
|
|
|
if (dir == 0)
|
2020-08-04 21:31:36 +02:00
|
|
|
return node;
|
2006-01-14 06:24:10 +01:00
|
|
|
else if (dir < 0)
|
2014-01-20 05:49:12 +01:00
|
|
|
{
|
|
|
|
if (!node->left_child)
|
|
|
|
return NULL;
|
1998-06-11 01:21:14 +02:00
|
|
|
|
2014-01-20 05:49:12 +01:00
|
|
|
node = node->left;
|
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
else
|
2014-01-20 05:49:12 +01:00
|
|
|
{
|
|
|
|
if (!node->right_child)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
node = node->right;
|
|
|
|
}
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 05:49:12 +01:00
|
|
|
static GTreeNode *
|
1998-06-11 01:21:14 +02:00
|
|
|
g_tree_node_rotate_left (GTreeNode *node)
|
|
|
|
{
|
|
|
|
GTreeNode *right;
|
|
|
|
gint a_bal;
|
|
|
|
gint b_bal;
|
|
|
|
|
|
|
|
right = node->right;
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
if (right->left_child)
|
|
|
|
node->right = right->left;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->right_child = FALSE;
|
|
|
|
right->left_child = TRUE;
|
|
|
|
}
|
1998-06-11 01:21:14 +02:00
|
|
|
right->left = node;
|
|
|
|
|
|
|
|
a_bal = node->balance;
|
|
|
|
b_bal = right->balance;
|
|
|
|
|
|
|
|
if (b_bal <= 0)
|
|
|
|
{
|
|
|
|
if (a_bal >= 1)
|
2014-01-20 05:49:12 +01:00
|
|
|
right->balance = b_bal - 1;
|
1998-06-11 01:21:14 +02:00
|
|
|
else
|
2014-01-20 05:49:12 +01:00
|
|
|
right->balance = a_bal + b_bal - 2;
|
1998-06-11 01:21:14 +02:00
|
|
|
node->balance = a_bal - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (a_bal <= b_bal)
|
2014-01-20 05:49:12 +01:00
|
|
|
right->balance = a_bal - 2;
|
1998-06-11 01:21:14 +02:00
|
|
|
else
|
2014-01-20 05:49:12 +01:00
|
|
|
right->balance = b_bal - 1;
|
1998-06-11 01:21:14 +02:00
|
|
|
node->balance = a_bal - b_bal - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return right;
|
|
|
|
}
|
|
|
|
|
2014-01-20 05:49:12 +01:00
|
|
|
static GTreeNode *
|
1998-06-11 01:21:14 +02:00
|
|
|
g_tree_node_rotate_right (GTreeNode *node)
|
|
|
|
{
|
|
|
|
GTreeNode *left;
|
|
|
|
gint a_bal;
|
|
|
|
gint b_bal;
|
|
|
|
|
|
|
|
left = node->left;
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
if (left->right_child)
|
|
|
|
node->left = left->right;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->left_child = FALSE;
|
|
|
|
left->right_child = TRUE;
|
|
|
|
}
|
1998-06-11 01:21:14 +02:00
|
|
|
left->right = node;
|
|
|
|
|
|
|
|
a_bal = node->balance;
|
|
|
|
b_bal = left->balance;
|
|
|
|
|
|
|
|
if (b_bal <= 0)
|
|
|
|
{
|
|
|
|
if (b_bal > a_bal)
|
2014-01-20 05:49:12 +01:00
|
|
|
left->balance = b_bal + 1;
|
1998-06-11 01:21:14 +02:00
|
|
|
else
|
2014-01-20 05:49:12 +01:00
|
|
|
left->balance = a_bal + 2;
|
1998-06-11 01:21:14 +02:00
|
|
|
node->balance = a_bal - b_bal + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (a_bal <= -1)
|
2014-01-20 05:49:12 +01:00
|
|
|
left->balance = b_bal + 1;
|
1998-06-11 01:21:14 +02:00
|
|
|
else
|
2014-01-20 05:49:12 +01:00
|
|
|
left->balance = a_bal + b_bal + 2;
|
1998-06-11 01:21:14 +02:00
|
|
|
node->balance = a_bal + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
#ifdef G_TREE_DEBUG
|
|
|
|
static gint
|
|
|
|
g_tree_node_height (GTreeNode *node)
|
|
|
|
{
|
|
|
|
gint left_height;
|
|
|
|
gint right_height;
|
|
|
|
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
left_height = 0;
|
|
|
|
right_height = 0;
|
|
|
|
|
|
|
|
if (node->left_child)
|
2014-01-20 05:49:12 +01:00
|
|
|
left_height = g_tree_node_height (node->left);
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
if (node->right_child)
|
2014-01-20 05:49:12 +01:00
|
|
|
right_height = g_tree_node_height (node->right);
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
return MAX (left_height, right_height) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
static void
|
|
|
|
g_tree_node_check (GTreeNode *node)
|
|
|
|
{
|
|
|
|
gint left_height;
|
|
|
|
gint right_height;
|
|
|
|
gint balance;
|
2006-01-14 06:24:10 +01:00
|
|
|
GTreeNode *tmp;
|
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
if (node)
|
|
|
|
{
|
2006-01-14 06:24:10 +01:00
|
|
|
if (node->left_child)
|
2014-01-20 05:49:12 +01:00
|
|
|
{
|
|
|
|
tmp = g_tree_node_previous (node);
|
|
|
|
g_assert (tmp->right == node);
|
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
|
|
|
|
if (node->right_child)
|
2014-01-20 05:49:12 +01:00
|
|
|
{
|
|
|
|
tmp = g_tree_node_next (node);
|
|
|
|
g_assert (tmp->left == node);
|
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
left_height = 0;
|
|
|
|
right_height = 0;
|
1998-08-18 05:50:35 +02:00
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
if (node->left_child)
|
2014-01-20 05:49:12 +01:00
|
|
|
left_height = g_tree_node_height (node->left);
|
2006-01-14 06:24:10 +01:00
|
|
|
if (node->right_child)
|
2014-01-20 05:49:12 +01:00
|
|
|
right_height = g_tree_node_height (node->right);
|
1998-08-18 05:50:35 +02:00
|
|
|
|
1998-06-11 01:21:14 +02:00
|
|
|
balance = right_height - left_height;
|
2006-01-14 06:24:10 +01:00
|
|
|
g_assert (balance == node->balance);
|
1998-08-18 05:50:35 +02:00
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
if (node->left_child)
|
2014-01-20 05:49:12 +01:00
|
|
|
g_tree_node_check (node->left);
|
2006-01-14 06:24:10 +01:00
|
|
|
if (node->right_child)
|
2014-01-20 05:49:12 +01:00
|
|
|
g_tree_node_check (node->right);
|
1998-06-11 01:21:14 +02:00
|
|
|
}
|
|
|
|
}
|
2005-03-14 05:26:57 +01:00
|
|
|
|
2006-01-14 06:24:10 +01:00
|
|
|
static void
|
|
|
|
g_tree_node_dump (GTreeNode *node,
|
2014-01-20 05:49:12 +01:00
|
|
|
gint indent)
|
2006-01-14 06:24:10 +01:00
|
|
|
{
|
|
|
|
g_print ("%*s%c\n", indent, "", *(char *)node->key);
|
|
|
|
|
|
|
|
if (node->left_child)
|
2020-08-04 21:23:54 +02:00
|
|
|
{
|
|
|
|
g_print ("%*sLEFT\n", indent, "");
|
|
|
|
g_tree_node_dump (node->left, indent + 2);
|
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
else if (node->left)
|
|
|
|
g_print ("%*s<%c\n", indent + 2, "", *(char *)node->left->key);
|
|
|
|
|
|
|
|
if (node->right_child)
|
2020-08-04 21:23:54 +02:00
|
|
|
{
|
|
|
|
g_print ("%*sRIGHT\n", indent, "");
|
|
|
|
g_tree_node_dump (node->right, indent + 2);
|
|
|
|
}
|
2006-01-14 06:24:10 +01:00
|
|
|
else if (node->right)
|
|
|
|
g_print ("%*s>%c\n", indent + 2, "", *(char *)node->right->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_tree_dump (GTree *tree)
|
|
|
|
{
|
|
|
|
if (tree->root)
|
|
|
|
g_tree_node_dump (tree->root, 0);
|
|
|
|
}
|
|
|
|
#endif
|