1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
38 typedef struct _GTreeNode GTreeNode;
43 GCompareDataFunc key_compare;
44 GDestroyNotify key_destroy_func;
45 GDestroyNotify value_destroy_func;
46 gpointer key_compare_data;
51 gint balance; /* height (left) - height (right) */
52 GTreeNode *left; /* left subtree */
53 GTreeNode *right; /* right subtree */
54 gpointer key; /* key for this node */
55 gpointer value; /* value stored at this node */
59 static GTreeNode* g_tree_node_new (gpointer key,
61 static void g_tree_node_destroy (GTreeNode *node,
62 GDestroyNotify key_destroy_func,
63 GDestroyNotify value_destroy_func);
64 static GTreeNode* g_tree_node_insert (GTree *tree,
70 static GTreeNode* g_tree_node_remove (GTree *tree,
74 static GTreeNode* g_tree_node_balance (GTreeNode *node);
75 static GTreeNode* g_tree_node_remove_leftmost (GTreeNode *node,
76 GTreeNode **leftmost);
77 static GTreeNode* g_tree_node_restore_left_balance (GTreeNode *node,
79 static GTreeNode* g_tree_node_restore_right_balance (GTreeNode *node,
81 static GTreeNode* g_tree_node_lookup (GTreeNode *node,
82 GCompareDataFunc compare,
85 static gint g_tree_node_count (GTreeNode *node);
86 static gint g_tree_node_pre_order (GTreeNode *node,
87 GTraverseFunc traverse_func,
89 static gint g_tree_node_in_order (GTreeNode *node,
90 GTraverseFunc traverse_func,
92 static gint g_tree_node_post_order (GTreeNode *node,
93 GTraverseFunc traverse_func,
95 static gpointer g_tree_node_search (GTreeNode *node,
96 GCompareFunc search_func,
98 static gint g_tree_node_height (GTreeNode *node);
99 static GTreeNode* g_tree_node_rotate_left (GTreeNode *node);
100 static GTreeNode* g_tree_node_rotate_right (GTreeNode *node);
101 static void g_tree_node_check (GTreeNode *node);
104 G_LOCK_DEFINE_STATIC (g_tree_global);
105 static GMemChunk *node_mem_chunk = NULL;
106 static GTreeNode *node_free_list = NULL;
110 g_tree_node_new (gpointer key,
115 G_LOCK (g_tree_global);
118 node = node_free_list;
119 node_free_list = node->right;
124 node_mem_chunk = g_mem_chunk_new ("GLib GTreeNode mem chunk",
129 node = g_chunk_new (GTreeNode, node_mem_chunk);
131 G_UNLOCK (g_tree_global);
143 g_tree_node_destroy (GTreeNode *node,
144 GDestroyNotify key_destroy_func,
145 GDestroyNotify value_destroy_func)
149 g_tree_node_destroy (node->right,
150 key_destroy_func, value_destroy_func);
151 g_tree_node_destroy (node->left,
152 key_destroy_func, value_destroy_func);
154 if (key_destroy_func)
155 key_destroy_func (node->key);
156 if (value_destroy_func)
157 value_destroy_func (node->value);
159 #ifdef ENABLE_GC_FRIENDLY
163 #endif /* ENABLE_GC_FRIENDLY */
165 G_LOCK (g_tree_global);
166 node->right = node_free_list;
167 node_free_list = node;
168 G_UNLOCK (g_tree_global);
174 * @key_compare_func: the function used to order the nodes in the #GTree.
175 * It should return values similar to the standard
176 * <function>strcmp()</function> function -
177 * 0 if the two arguments are equal, a negative value if the first argument
178 * comes before the second, or a positive value if the first argument comes
181 * Creates a new #GTree.
183 * Return value: a new #GTree.
186 g_tree_new (GCompareFunc key_compare_func)
188 g_return_val_if_fail (key_compare_func != NULL, NULL);
190 return g_tree_new_full ((GCompareDataFunc) key_compare_func, NULL,
195 * g_tree_new_with_data:
196 * @key_compare_func: <function>qsort()</function>-style comparison function.
197 * @key_compare_data: data to pass to comparison function.
199 * Creates a new #GTree with a comparison function that accepts user data.
200 * See g_tree_new() for more details.
202 * Return value: a new #GTree.
205 g_tree_new_with_data (GCompareDataFunc key_compare_func,
206 gpointer key_compare_data)
208 g_return_val_if_fail (key_compare_func != NULL, NULL);
210 return g_tree_new_full (key_compare_func, key_compare_data,
216 * @key_compare_func: <function>qsort()</function>-style comparison function.
217 * @key_compare_data: data to pass to comparison function.
218 * @key_destroy_func: a function to free the memory allocated for the key
219 * used when removing the entry from the #GTree or %NULL if you don't
220 * want to supply such a function.
221 * @value_destroy_func: a function to free the memory allocated for the
222 * value used when removing the entry from the #GTree or %NULL if you
223 * don't want to supply such a function.
225 * Creates a new #GTree like g_tree_new() and allows to specify functions
226 * to free the memory allocated for the key and value that get called when
227 * removing the entry from the #GTree.
229 * Return value: a new #GTree.
232 g_tree_new_full (GCompareDataFunc key_compare_func,
233 gpointer key_compare_data,
234 GDestroyNotify key_destroy_func,
235 GDestroyNotify value_destroy_func)
239 g_return_val_if_fail (key_compare_func != NULL, NULL);
241 tree = g_new (GTree, 1);
243 tree->key_compare = key_compare_func;
244 tree->key_destroy_func = key_destroy_func;
245 tree->value_destroy_func = value_destroy_func;
246 tree->key_compare_data = key_compare_data;
255 * Destroys the #GTree. If keys and/or values are dynamically allocated, you
256 * should either free them first or create the #GTree using g_tree_new_full().
257 * In the latter case the destroy functions you supplied will be called on
258 * all keys and values before destroying the #GTree.
261 g_tree_destroy (GTree *tree)
263 g_return_if_fail (tree != NULL);
265 g_tree_node_destroy (tree->root,
266 tree->key_destroy_func,
267 tree->value_destroy_func);
275 * @key: the key to insert.
276 * @value: the value corresponding to the key.
278 * Inserts a key/value pair into a #GTree. If the given key already exists
279 * in the #GTree its corresponding value is set to the new value. If you
280 * supplied a value_destroy_func when creating the #GTree, the old value is
281 * freed using that function. If you supplied a @key_destroy_func when
282 * creating the #GTree, the passed key is freed using that function.
284 * The tree is automatically 'balanced' as new key/value pairs are added,
285 * so that the distance from the root to every leaf is as small as possible.
288 g_tree_insert (GTree *tree,
294 g_return_if_fail (tree != NULL);
297 tree->root = g_tree_node_insert (tree,
306 * @key: the key to insert.
307 * @value: the value corresponding to the key.
309 * Inserts a new key and value into a #GTree similar to g_tree_insert().
310 * The difference is that if the key already exists in the #GTree, it gets
311 * replaced by the new key. If you supplied a @value_destroy_func when
312 * creating the #GTree, the old value is freed using that function. If you
313 * supplied a @key_destroy_func when creating the #GTree, the old key is
314 * freed using that function.
316 * The tree is automatically 'balanced' as new key/value pairs are added,
317 * so that the distance from the root to every leaf is as small as possible.
320 g_tree_replace (GTree *tree,
326 g_return_if_fail (tree != NULL);
329 tree->root = g_tree_node_insert (tree,
338 * @key: the key to remove.
340 * Removes a key/value pair from a #GTree.
342 * If the #GTree was created using g_tree_new_full(), the key and value
343 * are freed using the supplied destroy functions, otherwise you have to
344 * make sure that any dynamically allocated values are freed yourself.
347 g_tree_remove (GTree *tree,
350 g_return_if_fail (tree != NULL);
352 tree->root = g_tree_node_remove (tree, tree->root, key, TRUE);
358 * @key: the key to remove.
360 * Removes a key and its associated value from a #GTree without calling
361 * the key and value destroy functions.
364 g_tree_steal (GTree *tree,
367 g_return_if_fail (tree != NULL);
369 tree->root = g_tree_node_remove (tree, tree->root, key, FALSE);
375 * @key: the key to look up.
377 * Gets the value corresponding to the given key. Since a #GTree is
378 * automatically balanced as key/value pairs are added, key lookup is very
381 * Return value: the value corresponding to the key.
384 g_tree_lookup (GTree *tree,
389 g_return_val_if_fail (tree != NULL, NULL);
391 node = g_tree_node_lookup (tree->root,
392 tree->key_compare, tree->key_compare_data, key);
394 return node ? node->value : NULL;
398 * g_tree_lookup_extended:
400 * @lookup_key: the key to look up.
401 * @orig_key: returns the original key.
402 * @value: returns the value associated with the key.
404 * Looks up a key in the #GTree, returning the original key and the
405 * associated value and a #gboolean which is %TRUE if the key was found. This
406 * is useful if you need to free the memory allocated for the original key,
407 * for example before calling g_tree_remove().
409 * Return value: %TRUE if the key was found in the #GTree.
412 g_tree_lookup_extended (GTree *tree,
413 gconstpointer lookup_key,
419 g_return_val_if_fail (tree != NULL, FALSE);
421 node = g_tree_node_lookup (tree->root,
422 tree->key_compare, tree->key_compare_data, lookup_key);
427 *orig_key = node->key;
429 *value = node->value;
439 * @func: the function to call for each node visited. If this function
440 * returns %TRUE, the traversal is stopped.
441 * @user_data: user data to pass to the function.
443 * Calls the given function for each of the key/value pairs in the #GTree.
444 * The function is passed the key and value of each pair, and the given
445 * @data parameter. The tree is traversed in sorted order.
447 * The tree may not be modified while iterating over it (you can't
448 * add/remove items). To remove all items matching a predicate, you need
449 * to add each item to a list in your #GTraverseFunc as you walk over
450 * the tree, then walk the list and remove each item.
453 g_tree_foreach (GTree *tree,
457 g_return_if_fail (tree != NULL);
462 g_tree_node_in_order (tree->root, func, user_data);
468 * @traverse_func: the function to call for each node visited. If this
469 * function returns %TRUE, the traversal is stopped.
470 * @traverse_type: the order in which nodes are visited, one of %G_IN_ORDER,
471 * %G_PRE_ORDER and %G_POST_ORDER.
472 * @user_data: user data to pass to the function.
474 * Calls the given function for each node in the #GTree.
476 * Deprecated: The order of a balanced tree is somewhat arbitrary. If you
477 * just want to visit all nodes in sorted order, use g_tree_foreach()
478 * instead. If you really need to visit nodes in a different order, consider
479 * using an <link linkend="glib-N-ary-Trees">N-ary Tree</link>.
482 g_tree_traverse (GTree *tree,
483 GTraverseFunc traverse_func,
484 GTraverseType traverse_type,
487 g_return_if_fail (tree != NULL);
492 switch (traverse_type)
495 g_tree_node_pre_order (tree->root, traverse_func, user_data);
499 g_tree_node_in_order (tree->root, traverse_func, user_data);
503 g_tree_node_post_order (tree->root, traverse_func, user_data);
507 g_warning ("g_tree_traverse(): traverse type G_LEVEL_ORDER isn't implemented.");
515 * @search_func: a function used to search the #GTree.
516 * @user_data: the data passed as the second argument to the @search_func
519 * Searches a #GTree using @search_func.
521 * The @search_func is called with a pointer to the key of a key/value pair in the tree,
522 * and the passed in @user_data. If @search_func returns 0 for a key/value pair, then
523 * g_tree_search_func() will return the value of that pair. If @search_func returns -1,
524 * searching will proceed among the key/value pairs that have a smaller key; if @search_func
525 * returns 1, searching will proceed among the key/value pairs that have a larger key.
527 * Return value: the value corresponding to the found key, or %NULL if the key
531 g_tree_search (GTree *tree,
532 GCompareFunc search_func,
533 gconstpointer user_data)
535 g_return_val_if_fail (tree != NULL, NULL);
538 return g_tree_node_search (tree->root, search_func, user_data);
547 * Gets the height of a #GTree.
549 * If the #GTree contains no nodes, the height is 0.
550 * If the #GTree contains only one root node the height is 1.
551 * If the root node has children the height is 2, etc.
553 * Return value: the height of the #GTree.
556 g_tree_height (GTree *tree)
558 g_return_val_if_fail (tree != NULL, 0);
561 return g_tree_node_height (tree->root);
570 * Gets the number of nodes in a #GTree.
572 * Return value: the number of nodes in the #GTree.
575 g_tree_nnodes (GTree *tree)
577 g_return_val_if_fail (tree != NULL, 0);
580 return g_tree_node_count (tree->root);
586 g_tree_node_insert (GTree *tree,
599 return g_tree_node_new (key, value);
602 cmp = tree->key_compare (key, node->key, tree->key_compare_data);
607 if (tree->value_destroy_func)
608 tree->value_destroy_func (node->value);
614 if (tree->key_destroy_func)
615 tree->key_destroy_func (node->key);
621 /* free the passed key */
622 if (tree->key_destroy_func)
623 tree->key_destroy_func (key);
633 old_balance = node->left->balance;
634 node->left = g_tree_node_insert (tree,
639 if ((old_balance != node->left->balance) && node->left->balance)
645 node->left = g_tree_node_new (key, value);
653 old_balance = node->right->balance;
654 node->right = g_tree_node_insert (tree,
659 if ((old_balance != node->right->balance) && node->right->balance)
665 node->right = g_tree_node_new (key, value);
672 if ((node->balance < -1) || (node->balance > 1))
673 node = g_tree_node_balance (node);
680 g_tree_node_remove (GTree *tree,
692 cmp = tree->key_compare (key, node->key, tree->key_compare_data);
705 old_balance = node->right->balance;
706 node->right = g_tree_node_remove_leftmost (node->right, &new_root);
707 new_root->left = node->left;
708 new_root->right = node->right;
709 new_root->balance = node->balance;
710 node = g_tree_node_restore_right_balance (new_root, old_balance);
715 if (tree->key_destroy_func)
716 tree->key_destroy_func (garbage->key);
717 if (tree->value_destroy_func)
718 tree->value_destroy_func (garbage->value);
721 #ifdef ENABLE_GC_FRIENDLY
722 garbage->left = NULL;
724 garbage->value = NULL;
725 #endif /* ENABLE_GC_FRIENDLY */
727 G_LOCK (g_tree_global);
728 garbage->right = node_free_list;
729 node_free_list = garbage;
730 G_UNLOCK (g_tree_global);
736 old_balance = node->left->balance;
737 node->left = g_tree_node_remove (tree, node->left, key, notify);
738 node = g_tree_node_restore_left_balance (node, old_balance);
745 old_balance = node->right->balance;
746 node->right = g_tree_node_remove (tree, node->right, key, notify);
747 node = g_tree_node_restore_right_balance (node, old_balance);
755 g_tree_node_balance (GTreeNode *node)
757 if (node->balance < -1)
759 if (node->left->balance > 0)
760 node->left = g_tree_node_rotate_left (node->left);
761 node = g_tree_node_rotate_right (node);
763 else if (node->balance > 1)
765 if (node->right->balance < 0)
766 node->right = g_tree_node_rotate_right (node->right);
767 node = g_tree_node_rotate_left (node);
774 g_tree_node_remove_leftmost (GTreeNode *node,
775 GTreeNode **leftmost)
785 old_balance = node->left->balance;
786 node->left = g_tree_node_remove_leftmost (node->left, leftmost);
787 return g_tree_node_restore_left_balance (node, old_balance);
791 g_tree_node_restore_left_balance (GTreeNode *node,
796 else if ((node->left->balance != old_balance) &&
797 (node->left->balance == 0))
800 if (node->balance > 1)
801 return g_tree_node_balance (node);
806 g_tree_node_restore_right_balance (GTreeNode *node,
811 else if ((node->right->balance != old_balance) &&
812 (node->right->balance == 0))
815 if (node->balance < -1)
816 return g_tree_node_balance (node);
821 g_tree_node_lookup (GTreeNode *node,
822 GCompareDataFunc compare,
823 gpointer compare_data,
831 cmp = (* compare) (key, node->key, compare_data);
838 return g_tree_node_lookup (node->left, compare, compare_data, key);
843 return g_tree_node_lookup (node->right, compare, compare_data, key);
850 g_tree_node_count (GTreeNode *node)
856 count += g_tree_node_count (node->left);
858 count += g_tree_node_count (node->right);
864 g_tree_node_pre_order (GTreeNode *node,
865 GTraverseFunc traverse_func,
868 if ((*traverse_func) (node->key, node->value, data))
872 if (g_tree_node_pre_order (node->left, traverse_func, data))
877 if (g_tree_node_pre_order (node->right, traverse_func, data))
885 g_tree_node_in_order (GTreeNode *node,
886 GTraverseFunc traverse_func,
891 if (g_tree_node_in_order (node->left, traverse_func, data))
894 if ((*traverse_func) (node->key, node->value, data))
898 if (g_tree_node_in_order (node->right, traverse_func, data))
906 g_tree_node_post_order (GTreeNode *node,
907 GTraverseFunc traverse_func,
912 if (g_tree_node_post_order (node->left, traverse_func, data))
917 if (g_tree_node_post_order (node->right, traverse_func, data))
920 if ((*traverse_func) (node->key, node->value, data))
927 g_tree_node_search (GTreeNode *node,
928 GCompareFunc search_func,
937 dir = (* search_func) (node->key, data);
951 g_tree_node_height (GTreeNode *node)
962 left_height = g_tree_node_height (node->left);
965 right_height = g_tree_node_height (node->right);
967 return MAX (left_height, right_height) + 1;
974 g_tree_node_rotate_left (GTreeNode *node)
982 node->right = right->left;
985 a_bal = node->balance;
986 b_bal = right->balance;
991 right->balance = b_bal - 1;
993 right->balance = a_bal + b_bal - 2;
994 node->balance = a_bal - 1;
999 right->balance = a_bal - 2;
1001 right->balance = b_bal - 1;
1002 node->balance = a_bal - b_bal - 1;
1009 g_tree_node_rotate_right (GTreeNode *node)
1017 node->left = left->right;
1020 a_bal = node->balance;
1021 b_bal = left->balance;
1026 left->balance = b_bal + 1;
1028 left->balance = a_bal + 2;
1029 node->balance = a_bal - b_bal + 1;
1034 left->balance = b_bal + 1;
1036 left->balance = a_bal + b_bal + 2;
1037 node->balance = a_bal + 1;
1044 g_tree_node_check (GTreeNode *node)
1056 left_height = g_tree_node_height (node->left);
1058 right_height = g_tree_node_height (node->right);
1060 balance = right_height - left_height;
1061 if (balance != node->balance)
1062 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO,
1063 "g_tree_node_check: failed: %d ( %d )\n",
1064 balance, node->balance);
1067 g_tree_node_check (node->left);
1069 g_tree_node_check (node->right);