glib/gtree.c

1118 lines
27 KiB
C
Raw Normal View History

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
* 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
* version 2 of the License, or (at your option) any later version.
*
* 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
* Lesser General Public License for more details.
1998-06-11 01:21:14 +02:00
*
* You should have received a copy of the GNU Lesser General Public
1998-06-11 01:21:14 +02:00
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* 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/.
*/
/*
* MT safe
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
1998-06-11 01:21:14 +02:00
#include "glib.h"
typedef struct _GRealTree GRealTree;
typedef struct _GTreeNode GTreeNode;
struct _GRealTree
{
GTreeNode *root;
changed prototype of g_boxed_type_register_static() to contain an optional Wed Mar 7 09:36:33 2001 Tim Janik <timj@gtk.org> * gboxed.[hc]: changed prototype of g_boxed_type_register_static() to contain an optional init function and a hint at whether the boxed structure uses ref counting internally. added g_value_set_boxed_take_ownership(). made G_TYPE_BOXED an abstract value type. * genums.[hc]: made G_TYPE_ENUM and G_TYPE_FLAGS abstract value types. * glib-genmarshal.c: argument type changes, preparation for third-party arg specification. * gobject.[hc]: cleaned up get/set property code. added g_strdup_value_contents() to improve warnings. * gparam.[hc]: added g_param_value_convert(), taking over responsibility of the old g_value_convert(). added G_PARAM_LAX_VALIDATION flag so validation alterations may be valid a part of the property setting process. * gparamspecs.[hc]: made value comparisons stable (for sort applications). added GParamSpecValueArray, a param spec for value arrays and GParamSpecClosure. nuked the value exchange functions and GParamSpecCCallback. * gtype.[hc]: catch unintialized usages of the type system with g_return_val_if_uninitialized(). introduced G_TYPE_FLAG_VALUE_ABSTRACT to flag types that introduce a value table, but can't be used for g_value_init(). cleaned up reserved type ids. * gvalue.[hc]: code cleanups and saner checking. nuked the value exchange API. implemented value transformations, we can't really "convert" values, rather transforms are an anylogy to C casts, real conversions need a param spec for validation, which is why g_param_value_convert() does real conversions now. * gvaluearray.[hc]: new files that implement a GValueArray, a struct that can hold inhomogeneous arrays of value (to that extend that it also allowes undefined values, i.e. G_VALUE_TYPE(value)==0). this is exposed to the type system as a boxed type. * gvaluetransform.c: new file implementing most of the former value exchange functions as single-sided transformations. * gvaluetypes.[hc]: nuked G_TYPE_CCALLBACK, added g_value_set_string_take_ownership(). * *.h: s/G_IS_VALUE_/G_VALUE_HOLDS_/. * *.[hc]: many fixes and cleanups. * many warning improvements. Tue Feb 27 18:35:15 2001 Tim Janik <timj@gtk.org> * gobject.c (g_object_get_valist): urg, pass G_VALUE_NOCOPY_CONTENTS into G_VALUE_LCOPY(), this needs proper documenting. * gparam.c: fixed G_PARAM_USER_MASK. * gtype.c (type_data_make_W): (type_data_last_unref_Wm): fixed invalid memory freeing. * gobject.c (g_object_last_unref): destroy signal handlers associated with object, right before finalization. * gsignal.c (g_signal_parse_name): catch destroyed nodes or signals that don't actually support details. * gobject.[hc]: got rid of property trailers. nuked GObject properties "data" and the "signal" variants. (g_object_connect): new convenience function to do multiple signal connections at once. (g_object_disconnect): likewise, for disconnections. * gparam.[hc] (g_param_spec_pool_lookup): took out trailer support. * gvalue.[hc]: marked g_value_fits_pointer() and g_value_peek_pointer() as private (the latter got renamed from g_value_get_as_pointer()). Wed Mar 7 09:32:06 2001 Tim Janik <timj@gtk.org> * glib-object.h: add gvaluearray.h. * gstring.[hc]: fixup naming of g_string_sprint*. * gtypes.h: fixed GCompareDataFunc naming. Wed Mar 7 09:33:27 2001 Tim Janik <timj@gtk.org> * gobject/Makefile.am: shuffled rules to avoid excessive rebuilds. * gobject/gobject-sections.txt: updates. * gobject/tmpl/*: bunch of updates, added another patch from Eric Lemings <eric.b.lemings@lmco.com>.
2001-03-07 15:46:45 +01:00
GCompareDataFunc key_compare;
GDestroyNotify key_destroy_func;
GDestroyNotify value_destroy_func;
gpointer key_compare_data;
1998-06-11 01:21:14 +02:00
};
struct _GTreeNode
{
gint balance; /* height (left) - height (right) */
GTreeNode *left; /* left subtree */
GTreeNode *right; /* right subtree */
gpointer key; /* key for this node */
gpointer value; /* value stored at this node */
};
static GTreeNode* g_tree_node_new (gpointer key,
gpointer value);
static void g_tree_node_destroy (GTreeNode *node,
GDestroyNotify key_destroy_func,
GDestroyNotify value_destroy_func);
static GTreeNode* g_tree_node_insert (GTree *tree,
GTreeNode *node,
gpointer key,
gpointer value,
gboolean replace,
gboolean *inserted);
static GTreeNode* g_tree_node_remove (GTree *tree,
GTreeNode *node,
gconstpointer key,
gboolean notify);
static GTreeNode* g_tree_node_balance (GTreeNode *node);
static GTreeNode* g_tree_node_remove_leftmost (GTreeNode *node,
GTreeNode **leftmost);
static GTreeNode* g_tree_node_restore_left_balance (GTreeNode *node,
gint old_balance);
static GTreeNode* g_tree_node_restore_right_balance (GTreeNode *node,
gint old_balance);
static GTreeNode* g_tree_node_lookup (GTreeNode *node,
GCompareDataFunc compare,
gpointer comp_data,
gconstpointer key);
static gint g_tree_node_count (GTreeNode *node);
static gint g_tree_node_pre_order (GTreeNode *node,
GTraverseFunc traverse_func,
gpointer data);
static gint g_tree_node_in_order (GTreeNode *node,
GTraverseFunc traverse_func,
gpointer data);
static gint g_tree_node_post_order (GTreeNode *node,
GTraverseFunc traverse_func,
gpointer data);
static gpointer g_tree_node_search (GTreeNode *node,
GCompareFunc search_func,
gconstpointer data);
static gint g_tree_node_height (GTreeNode *node);
static GTreeNode* g_tree_node_rotate_left (GTreeNode *node);
static GTreeNode* g_tree_node_rotate_right (GTreeNode *node);
static void g_tree_node_check (GTreeNode *node);
1998-06-11 01:21:14 +02:00
G_LOCK_DEFINE_STATIC (g_tree_global);
1998-06-11 01:21:14 +02:00
static GMemChunk *node_mem_chunk = NULL;
static GTreeNode *node_free_list = NULL;
static GTreeNode*
g_tree_node_new (gpointer key,
gpointer value)
{
GTreeNode *node;
G_LOCK (g_tree_global);
if (node_free_list)
{
node = node_free_list;
node_free_list = node->right;
}
else
{
if (!node_mem_chunk)
node_mem_chunk = g_mem_chunk_new ("GLib GTreeNode mem chunk",
sizeof (GTreeNode),
1024,
G_ALLOC_ONLY);
node = g_chunk_new (GTreeNode, node_mem_chunk);
}
G_UNLOCK (g_tree_global);
node->balance = 0;
node->left = NULL;
node->right = NULL;
node->key = key;
node->value = value;
return node;
}
static void
g_tree_node_destroy (GTreeNode *node,
GDestroyNotify key_destroy_func,
GDestroyNotify value_destroy_func)
{
if (node)
{
g_tree_node_destroy (node->right,
key_destroy_func, value_destroy_func);
g_tree_node_destroy (node->left,
key_destroy_func, value_destroy_func);
if (key_destroy_func)
key_destroy_func (node->key);
if (value_destroy_func)
value_destroy_func (node->value);
#ifdef ENABLE_GC_FRIENDLY
node->left = NULL;
node->key = NULL;
node->value = NULL;
#endif /* ENABLE_GC_FRIENDLY */
G_LOCK (g_tree_global);
node->right = node_free_list;
node_free_list = node;
G_UNLOCK (g_tree_global);
}
}
1998-06-11 01:21:14 +02:00
/**
* g_tree_new:
* @key_compare_func: the function used to order the nodes in the #GTree.
* It should return values similar to the standard
* <function>strcmp()</function> function -
* 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.
*
* Return value: a new #GTree.
**/
GTree*
g_tree_new (GCompareFunc key_compare_func)
1998-06-11 01:21:14 +02:00
{
g_return_val_if_fail (key_compare_func != NULL, NULL);
return g_tree_new_full ((GCompareDataFunc) key_compare_func, NULL,
NULL, NULL);
1998-06-11 01:21:14 +02:00
}
/**
* g_tree_new_with_data:
* @key_compare_func: qsort()-style comparison function.
* @key_compare_data: data to pass to comparison function.
*
* Creates a new #GTree with a comparison function that accepts user data.
* See g_tree_new() for more details.
*
* Return value: a new #GTree.
**/
GTree*
g_tree_new_with_data (GCompareDataFunc key_compare_func,
gpointer key_compare_data)
{
g_return_val_if_fail (key_compare_func != NULL, NULL);
return g_tree_new_full (key_compare_func, key_compare_data,
NULL, NULL);
}
/**
* g_tree_new_full:
* @key_compare_func: qsort()-style comparison function.
* @key_compare_data: data to pass to comparison function.
* @key_destroy_func: a function to free the memory allocated for the key
* used when removing the entry from the #GTree or #NULL if you don't
* want to supply such a function.
* @value_destroy_func: a function to free the memory allocated for the
* value used when removing the entry from the #GTree or #NULL if you
* don't want to supply such a function.
*
* 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.
*
* Return value: a new #GTree.
**/
GTree*
g_tree_new_full (GCompareDataFunc key_compare_func,
gpointer key_compare_data,
GDestroyNotify key_destroy_func,
GDestroyNotify value_destroy_func)
{
GRealTree *rtree;
g_return_val_if_fail (key_compare_func != NULL, NULL);
rtree = g_new (GRealTree, 1);
rtree->root = NULL;
rtree->key_compare = key_compare_func;
rtree->key_destroy_func = key_destroy_func;
rtree->value_destroy_func = value_destroy_func;
rtree->key_compare_data = key_compare_data;
return (GTree*) rtree;
}
/**
* 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.
**/
1998-06-11 01:21:14 +02:00
void
g_tree_destroy (GTree *tree)
{
GRealTree *rtree;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
g_tree_node_destroy (rtree->root,
rtree->key_destroy_func,
rtree->value_destroy_func);
1998-06-11 01:21:14 +02:00
g_free (rtree);
}
/**
* g_tree_insert:
* @tree: a #Gtree.
* @key: the key to insert.
* @value: the value corresponding to the key.
*
* Inserts a key/value pair into a #GTree. If the given key already exists
* in the #GTree it 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.
*
* 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.
**/
1998-06-11 01:21:14 +02:00
void
g_tree_insert (GTree *tree,
gpointer key,
gpointer value)
{
GRealTree *rtree;
gboolean inserted;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
inserted = FALSE;
rtree->root = g_tree_node_insert (tree,
rtree->root,
key, value,
FALSE, &inserted);
}
/**
* 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)
{
GRealTree *rtree;
gboolean inserted;
1998-06-11 01:21:14 +02:00
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
inserted = FALSE;
rtree->root = g_tree_node_insert (tree,
rtree->root,
key, value,
TRUE, &inserted);
1998-06-11 01:21:14 +02:00
}
/**
* g_tree_remove:
* @tree: a #Gtree.
* @key: the key to remove.
*
* Removes a key/value pair from a #GTree.
*
* If the #GTree was created using g_tree_new_full(), the key and value
* are freed using the supplied destroy_functions, otherwise you have to
* make sure that any dynamically allocated values are freed yourself.
**/
1998-06-11 01:21:14 +02:00
void
g_tree_remove (GTree *tree,
gconstpointer key)
1998-06-11 01:21:14 +02:00
{
GRealTree *rtree;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
rtree->root = g_tree_node_remove (tree, rtree->root, key, TRUE);
}
/**
* 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.
**/
void
g_tree_steal (GTree *tree,
gconstpointer key)
{
GRealTree *rtree;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
rtree->root = g_tree_node_remove (tree, rtree->root, key, FALSE);
1998-06-11 01:21:14 +02:00
}
/**
* g_tree_lookup:
* @tree: a #GTree.
* @key: the key to look up.
*
* Gets the value corresponding to the given key. Since a #GTree is
* automatically balanced as key/value pairs are added, key lookup is very
* fast.
*
* Return value: the value corresponding to the key.
**/
1998-06-11 01:21:14 +02:00
gpointer
g_tree_lookup (GTree *tree,
gconstpointer key)
1998-06-11 01:21:14 +02:00
{
GRealTree *rtree;
GTreeNode *node;
1998-06-11 01:21:14 +02:00
g_return_val_if_fail (tree != NULL, NULL);
rtree = (GRealTree*) tree;
node = g_tree_node_lookup (rtree->root,
rtree->key_compare, rtree->key_compare_data, key);
return node ? node->value : NULL;
1998-06-11 01:21:14 +02:00
}
/**
* g_tree_lookup_extended:
* @tree: a #GTree.
* @lookup_key: the key to look up.
* @orig_key: returns the original key.
* @value: returns the value associated with the key.
*
* Looks up a key in the #GTree, returning the original key and the
* associated value and a gboolean which is TRUE if the key was found. This
* is useful if you need to free the memory allocated for the original key,
* for example before calling g_tree_remove().
*
* Return value: #TRUE if the key was found in the #GTree.
**/
gboolean
g_tree_lookup_extended (GTree *tree,
gconstpointer lookup_key,
gpointer *orig_key,
gpointer *value)
{
GRealTree *rtree;
GTreeNode *node;
g_return_val_if_fail (tree != NULL, FALSE);
rtree = (GRealTree*) tree;
node = g_tree_node_lookup (rtree->root,
rtree->key_compare, rtree->key_compare_data, lookup_key);
if (node)
{
if (orig_key)
*orig_key = node->key;
if (value)
*value = node->value;
return TRUE;
}
else
return FALSE;
}
/**
* 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.
**/
void
g_tree_foreach (GTree *tree,
GTraverseFunc func,
gpointer user_data)
{
GRealTree *rtree;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
if (!rtree->root)
return;
g_tree_node_in_order (rtree->root, func, user_data);
}
/**
* g_tree_traverse:
* @tree: a #GTree.
* @traverse_func: the function to call for each node visited. If this
* function returns TRUE, the traversal is stopped.
* @traverse_type: the order in which nodes are visited, one of %G_IN_ORDER,
* %G_PRE_ORDER and %G_POST_ORDER.
* @user_data: user data to pass to the function.
*
* Calls the given function for each node in the GTree. This function is
* deprecated, use g_tree_foreach() instead.
**/
1998-06-11 01:21:14 +02:00
void
g_tree_traverse (GTree *tree,
GTraverseFunc traverse_func,
GTraverseType traverse_type,
gpointer user_data)
1998-06-11 01:21:14 +02:00
{
GRealTree *rtree;
g_return_if_fail (tree != NULL);
rtree = (GRealTree*) tree;
if (!rtree->root)
return;
1998-06-11 01:21:14 +02:00
switch (traverse_type)
{
case G_PRE_ORDER:
g_tree_node_pre_order (rtree->root, traverse_func, user_data);
1998-06-11 01:21:14 +02:00
break;
case G_IN_ORDER:
g_tree_node_in_order (rtree->root, traverse_func, user_data);
1998-06-11 01:21:14 +02:00
break;
case G_POST_ORDER:
g_tree_node_post_order (rtree->root, traverse_func, user_data);
1998-06-11 01:21:14 +02:00
break;
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
}
}
/**
* g_tree_search:
* @tree: a #GTree.
* @search_func: the comparison function used to search the #GTree.
* @user_data: the data passed as the second argument to the @search_func
* function.
*
* Searches a #GTree using an alternative form of the comparison function.
*
* This function is not as useful as it sounds.
* It allows you to use a different function for performing the lookup of
* a key. However, since the tree is ordered according to the @key_compare_func
* function passed to g_tree_new(), the function you pass to g_tree_search()
* must return exactly the same value as would be returned by the comparison
* function, for each pair of tree nodes, or the search will not work.
*
* To search for a specific value, you can use g_tree_foreach() or
* g_tree_traverse().
*
* Return value: the value corresponding to the found key, or NULL if the key
* is not found.
**/
1998-06-11 01:21:14 +02:00
gpointer
g_tree_search (GTree *tree,
GCompareFunc search_func,
gconstpointer user_data)
1998-06-11 01:21:14 +02:00
{
GRealTree *rtree;
g_return_val_if_fail (tree != NULL, NULL);
rtree = (GRealTree*) tree;
if (rtree->root)
return g_tree_node_search (rtree->root, search_func, user_data);
else
return NULL;
1998-06-11 01:21:14 +02:00
}
/**
* g_tree_height:
* @tree: a #GTree.
*
* 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.
*
* Return value: the height of the #GTree.
**/
1998-06-11 01:21:14 +02:00
gint
g_tree_height (GTree *tree)
{
GRealTree *rtree;
g_return_val_if_fail (tree != NULL, 0);
rtree = (GRealTree*) tree;
if (rtree->root)
return g_tree_node_height (rtree->root);
else
return 0;
1998-06-11 01:21:14 +02:00
}
/**
* g_tree_nnodes:
* @tree: a #GTree.
*
* Gets the number of nodes in a #GTree.
*
* Return value: the number of nodes in the #GTree.
**/
1998-06-11 01:21:14 +02:00
gint
g_tree_nnodes (GTree *tree)
{
GRealTree *rtree;
g_return_val_if_fail (tree != NULL, 0);
rtree = (GRealTree*) tree;
if (rtree->root)
return g_tree_node_count (rtree->root);
else
return 0;
1998-06-11 01:21:14 +02:00
}
static GTreeNode*
g_tree_node_insert (GTree *tree,
GTreeNode *node,
gpointer key,
gpointer value,
gboolean replace,
gboolean *inserted)
1998-06-11 01:21:14 +02:00
{
GRealTree *rtree;
gint old_balance;
gint cmp;
rtree = (GRealTree*) tree;
1998-06-11 01:21:14 +02:00
if (!node)
{
*inserted = TRUE;
return g_tree_node_new (key, value);
}
cmp = rtree->key_compare (key, node->key, rtree->key_compare_data);
1998-06-11 01:21:14 +02:00
if (cmp == 0)
{
*inserted = FALSE;
if (rtree->value_destroy_func)
rtree->value_destroy_func (node->value);
1998-06-11 01:21:14 +02:00
node->value = value;
if (replace)
{
if (rtree->key_destroy_func)
rtree->key_destroy_func (node->key);
node->key = key;
}
else
{
/* free the passed key */
if (rtree->key_destroy_func)
rtree->key_destroy_func (key);
}
1998-06-11 01:21:14 +02:00
return node;
}
if (cmp < 0)
{
if (node->left)
{
old_balance = node->left->balance;
node->left = g_tree_node_insert (tree,
node->left,
key, value,
replace, inserted);
1998-06-11 01:21:14 +02:00
if ((old_balance != node->left->balance) && node->left->balance)
node->balance -= 1;
}
else
{
*inserted = TRUE;
node->left = g_tree_node_new (key, value);
node->balance -= 1;
}
}
else if (cmp > 0)
{
if (node->right)
{
old_balance = node->right->balance;
node->right = g_tree_node_insert (tree,
node->right,
key, value,
replace, inserted);
1998-06-11 01:21:14 +02:00
if ((old_balance != node->right->balance) && node->right->balance)
node->balance += 1;
}
else
{
*inserted = TRUE;
node->right = g_tree_node_new (key, value);
node->balance += 1;
}
}
if (*inserted)
{
if ((node->balance < -1) || (node->balance > 1))
node = g_tree_node_balance (node);
}
return node;
}
static GTreeNode*
g_tree_node_remove (GTree *tree,
GTreeNode *node,
gconstpointer key,
gboolean notify)
1998-06-11 01:21:14 +02:00
{
GRealTree *rtree;
1998-06-11 01:21:14 +02:00
GTreeNode *new_root;
gint old_balance;
gint cmp;
if (!node)
return NULL;
rtree = (GRealTree *) tree;
cmp = rtree->key_compare (key, node->key, rtree->key_compare_data);
1998-06-11 01:21:14 +02:00
if (cmp == 0)
{
GTreeNode *garbage;
1998-06-11 01:21:14 +02:00
garbage = node;
if (!node->right)
{
node = node->left;
}
else
{
old_balance = node->right->balance;
node->right = g_tree_node_remove_leftmost (node->right, &new_root);
new_root->left = node->left;
new_root->right = node->right;
new_root->balance = node->balance;
node = g_tree_node_restore_right_balance (new_root, old_balance);
}
if (notify)
{
if (rtree->key_destroy_func)
rtree->key_destroy_func (garbage->key);
if (rtree->value_destroy_func)
rtree->value_destroy_func (garbage->value);
}
#ifdef ENABLE_GC_FRIENDLY
garbage->left = NULL;
garbage->key = NULL;
garbage->value = NULL;
#endif /* ENABLE_GC_FRIENDLY */
G_LOCK (g_tree_global);
garbage->right = node_free_list;
node_free_list = garbage;
G_UNLOCK (g_tree_global);
}
1998-06-11 01:21:14 +02:00
else if (cmp < 0)
{
if (node->left)
{
old_balance = node->left->balance;
node->left = g_tree_node_remove (tree, node->left, key, notify);
1998-06-11 01:21:14 +02:00
node = g_tree_node_restore_left_balance (node, old_balance);
}
}
else if (cmp > 0)
{
if (node->right)
{
old_balance = node->right->balance;
node->right = g_tree_node_remove (tree, node->right, key, notify);
1998-06-11 01:21:14 +02:00
node = g_tree_node_restore_right_balance (node, old_balance);
}
}
return node;
}
static GTreeNode*
g_tree_node_balance (GTreeNode *node)
{
if (node->balance < -1)
{
if (node->left->balance > 0)
node->left = g_tree_node_rotate_left (node->left);
node = g_tree_node_rotate_right (node);
}
else if (node->balance > 1)
{
if (node->right->balance < 0)
node->right = g_tree_node_rotate_right (node->right);
node = g_tree_node_rotate_left (node);
}
return node;
}
static GTreeNode*
g_tree_node_remove_leftmost (GTreeNode *node,
GTreeNode **leftmost)
{
gint old_balance;
if (!node->left)
{
*leftmost = node;
return node->right;
}
old_balance = node->left->balance;
node->left = g_tree_node_remove_leftmost (node->left, leftmost);
return g_tree_node_restore_left_balance (node, old_balance);
}
static GTreeNode*
g_tree_node_restore_left_balance (GTreeNode *node,
gint old_balance)
{
if (!node->left)
node->balance += 1;
else if ((node->left->balance != old_balance) &&
(node->left->balance == 0))
node->balance += 1;
if (node->balance > 1)
return g_tree_node_balance (node);
return node;
}
static GTreeNode*
g_tree_node_restore_right_balance (GTreeNode *node,
gint old_balance)
{
if (!node->right)
node->balance -= 1;
else if ((node->right->balance != old_balance) &&
(node->right->balance == 0))
node->balance -= 1;
if (node->balance < -1)
return g_tree_node_balance (node);
return node;
}
static GTreeNode *
g_tree_node_lookup (GTreeNode *node,
GCompareDataFunc compare,
gpointer compare_data,
gconstpointer key)
1998-06-11 01:21:14 +02:00
{
gint cmp;
if (!node)
return NULL;
cmp = (* compare) (key, node->key, compare_data);
1998-06-11 01:21:14 +02:00
if (cmp == 0)
return node;
1998-06-11 01:21:14 +02:00
if (cmp < 0)
{
if (node->left)
return g_tree_node_lookup (node->left, compare, compare_data, key);
1998-06-11 01:21:14 +02:00
}
else if (cmp > 0)
{
if (node->right)
return g_tree_node_lookup (node->right, compare, compare_data, key);
1998-06-11 01:21:14 +02:00
}
return NULL;
}
static gint
g_tree_node_count (GTreeNode *node)
{
gint count;
count = 1;
if (node->left)
count += g_tree_node_count (node->left);
if (node->right)
count += g_tree_node_count (node->right);
return count;
}
static gint
g_tree_node_pre_order (GTreeNode *node,
GTraverseFunc traverse_func,
gpointer data)
{
if ((*traverse_func) (node->key, node->value, data))
return TRUE;
if (node->left)
{
if (g_tree_node_pre_order (node->left, traverse_func, data))
return TRUE;
}
if (node->right)
{
if (g_tree_node_pre_order (node->right, traverse_func, data))
return TRUE;
}
return FALSE;
}
static gint
g_tree_node_in_order (GTreeNode *node,
GTraverseFunc traverse_func,
gpointer data)
{
if (node->left)
{
if (g_tree_node_in_order (node->left, traverse_func, data))
return TRUE;
}
if ((*traverse_func) (node->key, node->value, data))
return TRUE;
if (node->right)
{
if (g_tree_node_in_order (node->right, traverse_func, data))
return TRUE;
}
return FALSE;
}
static gint
g_tree_node_post_order (GTreeNode *node,
GTraverseFunc traverse_func,
gpointer data)
{
if (node->left)
{
if (g_tree_node_post_order (node->left, traverse_func, data))
return TRUE;
}
if (node->right)
{
if (g_tree_node_post_order (node->right, traverse_func, data))
return TRUE;
}
if ((*traverse_func) (node->key, node->value, data))
return TRUE;
return FALSE;
}
static gpointer
g_tree_node_search (GTreeNode *node,
GCompareFunc search_func,
gconstpointer data)
1998-06-11 01:21:14 +02:00
{
gint dir;
if (!node)
return NULL;
do {
dir = (* search_func) (node->key, data);
if (dir == 0)
return node->value;
if (dir < 0)
node = node->left;
else if (dir > 0)
node = node->right;
} while (node);
1998-06-11 01:21:14 +02:00
return NULL;
}
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)
left_height = g_tree_node_height (node->left);
if (node->right)
right_height = g_tree_node_height (node->right);
return MAX (left_height, right_height) + 1;
}
return 0;
}
static GTreeNode*
g_tree_node_rotate_left (GTreeNode *node)
{
GTreeNode *right;
gint a_bal;
gint b_bal;
right = node->right;
node->right = right->left;
right->left = node;
a_bal = node->balance;
b_bal = right->balance;
if (b_bal <= 0)
{
if (a_bal >= 1)
right->balance = b_bal - 1;
else
right->balance = a_bal + b_bal - 2;
node->balance = a_bal - 1;
}
else
{
if (a_bal <= b_bal)
right->balance = a_bal - 2;
else
right->balance = b_bal - 1;
node->balance = a_bal - b_bal - 1;
}
return right;
}
static GTreeNode*
g_tree_node_rotate_right (GTreeNode *node)
{
GTreeNode *left;
gint a_bal;
gint b_bal;
left = node->left;
node->left = left->right;
left->right = node;
a_bal = node->balance;
b_bal = left->balance;
if (b_bal <= 0)
{
if (b_bal > a_bal)
left->balance = b_bal + 1;
else
left->balance = a_bal + 2;
node->balance = a_bal - b_bal + 1;
}
else
{
if (a_bal <= -1)
left->balance = b_bal + 1;
else
left->balance = a_bal + b_bal + 2;
node->balance = a_bal + 1;
}
return left;
}
static void
g_tree_node_check (GTreeNode *node)
{
gint left_height;
gint right_height;
gint balance;
1998-06-11 01:21:14 +02:00
if (node)
{
left_height = 0;
right_height = 0;
1998-06-11 01:21:14 +02:00
if (node->left)
left_height = g_tree_node_height (node->left);
if (node->right)
right_height = g_tree_node_height (node->right);
1998-06-11 01:21:14 +02:00
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);
1998-06-11 01:21:14 +02:00
if (node->left)
g_tree_node_check (node->left);
if (node->right)
g_tree_node_check (node->right);
}
}