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 #define MAX_GTREE_HEIGHT 40
40 typedef struct _GTreeNode GTreeNode;
45 GCompareDataFunc key_compare;
46 GDestroyNotify key_destroy_func;
47 GDestroyNotify value_destroy_func;
48 gpointer key_compare_data;
55 gpointer key; /* key for this node */
56 gpointer value; /* value stored at this node */
57 GTreeNode *left; /* left subtree */
58 GTreeNode *right; /* right subtree */
59 gint8 balance; /* height (left) - height (right) */
65 static GTreeNode* g_tree_node_new (gpointer key,
67 static void g_tree_insert_internal (GTree *tree,
71 static gboolean g_tree_remove_internal (GTree *tree,
74 static GTreeNode* g_tree_node_balance (GTreeNode *node);
75 static GTreeNode *g_tree_find_node (GTree *tree,
77 static gint g_tree_node_pre_order (GTreeNode *node,
78 GTraverseFunc traverse_func,
80 static gint g_tree_node_in_order (GTreeNode *node,
81 GTraverseFunc traverse_func,
83 static gint g_tree_node_post_order (GTreeNode *node,
84 GTraverseFunc traverse_func,
86 static gpointer g_tree_node_search (GTreeNode *node,
87 GCompareFunc search_func,
89 static GTreeNode* g_tree_node_rotate_left (GTreeNode *node);
90 static GTreeNode* g_tree_node_rotate_right (GTreeNode *node);
92 static void g_tree_node_check (GTreeNode *node);
97 g_tree_node_new (gpointer key,
100 GTreeNode *node = g_slice_new (GTreeNode);
105 node->left_child = FALSE;
106 node->right_child = FALSE;
115 * @key_compare_func: the function used to order the nodes in the #GTree.
116 * It should return values similar to the standard strcmp() function -
117 * 0 if the two arguments are equal, a negative value if the first argument
118 * comes before the second, or a positive value if the first argument comes
121 * Creates a new #GTree.
123 * Return value: a new #GTree.
126 g_tree_new (GCompareFunc key_compare_func)
128 g_return_val_if_fail (key_compare_func != NULL, NULL);
130 return g_tree_new_full ((GCompareDataFunc) key_compare_func, NULL,
135 * g_tree_new_with_data:
136 * @key_compare_func: qsort()-style comparison function.
137 * @key_compare_data: data to pass to comparison function.
139 * Creates a new #GTree with a comparison function that accepts user data.
140 * See g_tree_new() for more details.
142 * Return value: a new #GTree.
145 g_tree_new_with_data (GCompareDataFunc key_compare_func,
146 gpointer key_compare_data)
148 g_return_val_if_fail (key_compare_func != NULL, NULL);
150 return g_tree_new_full (key_compare_func, key_compare_data,
156 * @key_compare_func: qsort()-style comparison function.
157 * @key_compare_data: data to pass to comparison function.
158 * @key_destroy_func: a function to free the memory allocated for the key
159 * used when removing the entry from the #GTree or %NULL if you don't
160 * want to supply such a function.
161 * @value_destroy_func: a function to free the memory allocated for the
162 * value used when removing the entry from the #GTree or %NULL if you
163 * don't want to supply such a function.
165 * Creates a new #GTree like g_tree_new() and allows to specify functions
166 * to free the memory allocated for the key and value that get called when
167 * removing the entry from the #GTree.
169 * Return value: a new #GTree.
172 g_tree_new_full (GCompareDataFunc key_compare_func,
173 gpointer key_compare_data,
174 GDestroyNotify key_destroy_func,
175 GDestroyNotify value_destroy_func)
179 g_return_val_if_fail (key_compare_func != NULL, NULL);
181 tree = g_slice_new (GTree);
183 tree->key_compare = key_compare_func;
184 tree->key_destroy_func = key_destroy_func;
185 tree->value_destroy_func = value_destroy_func;
186 tree->key_compare_data = key_compare_data;
193 static inline GTreeNode *
194 g_tree_first_node (GTree *tree)
203 while (tmp->left_child)
209 static inline GTreeNode *
210 g_tree_node_previous (GTreeNode *node)
216 if (node->left_child)
217 while (tmp->right_child)
223 static inline GTreeNode *
224 g_tree_node_next (GTreeNode *node)
230 if (node->right_child)
231 while (tmp->left_child)
238 g_tree_remove_all (GTree *tree)
243 g_return_if_fail (tree != NULL);
245 node = g_tree_first_node (tree);
249 next = g_tree_node_next (node);
251 if (tree->key_destroy_func)
252 tree->key_destroy_func (node->key);
253 if (tree->value_destroy_func)
254 tree->value_destroy_func (node->value);
255 g_slice_free (GTreeNode, node);
266 * Increments the reference count of @tree by one. It is safe to call
267 * this function from any thread.
269 * Return value: the passed in #GTree.
274 g_tree_ref (GTree *tree)
276 g_return_val_if_fail (tree != NULL, NULL);
278 g_atomic_int_inc (&tree->ref_count);
287 * Decrements the reference count of @tree by one. If the reference count
288 * drops to 0, all keys and values will be destroyed (if destroy
289 * functions were specified) and all memory allocated by @tree will be
292 * It is safe to call this function from any thread.
297 g_tree_unref (GTree *tree)
299 g_return_if_fail (tree != NULL);
301 if (g_atomic_int_dec_and_test (&tree->ref_count))
303 g_tree_remove_all (tree);
304 g_slice_free (GTree, tree);
312 * Removes all keys and values from the #GTree and decreases its
313 * reference count by one. If keys and/or values are dynamically
314 * allocated, you should either free them first or create the #GTree
315 * using g_tree_new_full(). In the latter case the destroy functions
316 * you supplied will be called on all keys and values before destroying
320 g_tree_destroy (GTree *tree)
322 g_return_if_fail (tree != NULL);
324 g_tree_remove_all (tree);
331 * @key: the key to insert.
332 * @value: the value corresponding to the key.
334 * Inserts a key/value pair into a #GTree. If the given key already exists
335 * in the #GTree its corresponding value is set to the new value. If you
336 * supplied a value_destroy_func when creating the #GTree, the old value is
337 * freed using that function. If you supplied a @key_destroy_func when
338 * creating the #GTree, the passed key is freed using that function.
340 * The tree is automatically 'balanced' as new key/value pairs are added,
341 * so that the distance from the root to every leaf is as small as possible.
344 g_tree_insert (GTree *tree,
348 g_return_if_fail (tree != NULL);
350 g_tree_insert_internal (tree, key, value, FALSE);
353 g_tree_node_check (tree->root);
360 * @key: the key to insert.
361 * @value: the value corresponding to the key.
363 * Inserts a new key and value into a #GTree similar to g_tree_insert().
364 * The difference is that if the key already exists in the #GTree, it gets
365 * replaced by the new key. If you supplied a @value_destroy_func when
366 * creating the #GTree, the old value is freed using that function. If you
367 * supplied a @key_destroy_func when creating the #GTree, the old key is
368 * freed using that function.
370 * The tree is automatically 'balanced' as new key/value pairs are added,
371 * so that the distance from the root to every leaf is as small as possible.
374 g_tree_replace (GTree *tree,
378 g_return_if_fail (tree != NULL);
380 g_tree_insert_internal (tree, key, value, TRUE);
383 g_tree_node_check (tree->root);
387 /* internal insert routine */
389 g_tree_insert_internal (GTree *tree,
395 GTreeNode *path[MAX_GTREE_HEIGHT];
398 g_return_if_fail (tree != NULL);
402 tree->root = g_tree_node_new (key, value);
413 int cmp = tree->key_compare (key, node->key, tree->key_compare_data);
417 if (tree->value_destroy_func)
418 tree->value_destroy_func (node->value);
424 if (tree->key_destroy_func)
425 tree->key_destroy_func (node->key);
431 /* free the passed key */
432 if (tree->key_destroy_func)
433 tree->key_destroy_func (key);
440 if (node->left_child)
447 GTreeNode *child = g_tree_node_new (key, value);
449 child->left = node->left;
452 node->left_child = TRUE;
462 if (node->right_child)
469 GTreeNode *child = g_tree_node_new (key, value);
471 child->right = node->right;
474 node->right_child = TRUE;
484 /* restore balance. This is the goodness of a non-recursive
485 implementation, when we are done with balancing we 'break'
486 the loop and we are done. */
489 GTreeNode *bparent = path[--idx];
490 gboolean left_node = (bparent && node == bparent->left);
491 g_assert (!bparent || bparent->left == node || bparent->right == node);
493 if (node->balance < -1 || node->balance > 1)
495 node = g_tree_node_balance (node);
499 bparent->left = node;
501 bparent->right = node;
504 if (node->balance == 0 || bparent == NULL)
508 bparent->balance -= 1;
510 bparent->balance += 1;
519 * @key: the key to remove.
521 * Removes a key/value pair from a #GTree.
523 * If the #GTree was created using g_tree_new_full(), the key and value
524 * are freed using the supplied destroy functions, otherwise you have to
525 * make sure that any dynamically allocated values are freed yourself.
526 * If the key does not exist in the #GTree, the function does nothing.
528 * Returns: %TRUE if the key was found (prior to 2.8, this function returned
532 g_tree_remove (GTree *tree,
537 g_return_val_if_fail (tree != NULL, FALSE);
539 removed = g_tree_remove_internal (tree, key, FALSE);
542 g_tree_node_check (tree->root);
551 * @key: the key to remove.
553 * Removes a key and its associated value from a #GTree without calling
554 * the key and value destroy functions.
556 * If the key does not exist in the #GTree, the function does nothing.
558 * Returns: %TRUE if the key was found (prior to 2.8, this function returned
562 g_tree_steal (GTree *tree,
567 g_return_val_if_fail (tree != NULL, FALSE);
569 removed = g_tree_remove_internal (tree, key, TRUE);
572 g_tree_node_check (tree->root);
578 /* internal remove routine */
580 g_tree_remove_internal (GTree *tree,
584 GTreeNode *node, *parent, *balance;
585 GTreeNode *path[MAX_GTREE_HEIGHT];
589 g_return_val_if_fail (tree != NULL, FALSE);
600 int cmp = tree->key_compare (key, node->key, tree->key_compare_data);
606 if (!node->left_child)
614 if (!node->right_child)
622 /* the following code is almost equal to g_tree_remove_node,
623 except that we do not have to call g_tree_node_parent. */
624 balance = parent = path[--idx];
625 g_assert (!parent || parent->left == node || parent->right == node);
626 left_node = (parent && node == parent->left);
628 if (!node->left_child)
630 if (!node->right_child)
636 parent->left_child = FALSE;
637 parent->left = node->left;
638 parent->balance += 1;
642 parent->right_child = FALSE;
643 parent->right = node->right;
644 parent->balance -= 1;
647 else /* node has a right child */
649 GTreeNode *tmp = g_tree_node_next (node);
650 tmp->left = node->left;
653 tree->root = node->right;
656 parent->left = node->right;
657 parent->balance += 1;
661 parent->right = node->right;
662 parent->balance -= 1;
666 else /* node has a left child */
668 if (!node->right_child)
670 GTreeNode *tmp = g_tree_node_previous (node);
671 tmp->right = node->right;
674 tree->root = node->left;
677 parent->left = node->left;
678 parent->balance += 1;
682 parent->right = node->left;
683 parent->balance -= 1;
686 else /* node has a both children (pant, pant!) */
688 GTreeNode *prev = node->left;
689 GTreeNode *next = node->right;
690 GTreeNode *nextp = node;
691 int old_idx = idx + 1;
694 /* path[idx] == parent */
695 /* find the immediately next node (and its parent) */
696 while (next->left_child)
698 path[++idx] = nextp = next;
702 path[old_idx] = next;
705 /* remove 'next' from the tree */
708 if (next->right_child)
709 nextp->left = next->right;
711 nextp->left_child = FALSE;
714 next->right_child = TRUE;
715 next->right = node->right;
720 /* set the prev to point to the right place */
721 while (prev->right_child)
725 /* prepare 'next' to replace 'node' */
726 next->left_child = TRUE;
727 next->left = node->left;
728 next->balance = node->balance;
735 parent->right = next;
739 /* restore balance */
743 GTreeNode *bparent = path[--idx];
744 g_assert (!bparent || bparent->left == balance || bparent->right == balance);
745 left_node = (bparent && balance == bparent->left);
747 if(balance->balance < -1 || balance->balance > 1)
749 balance = g_tree_node_balance (balance);
751 tree->root = balance;
753 bparent->left = balance;
755 bparent->right = balance;
758 if (balance->balance != 0 || !bparent)
762 bparent->balance += 1;
764 bparent->balance -= 1;
771 if (tree->key_destroy_func)
772 tree->key_destroy_func (node->key);
773 if (tree->value_destroy_func)
774 tree->value_destroy_func (node->value);
777 g_slice_free (GTreeNode, node);
787 * @key: the key to look up.
789 * Gets the value corresponding to the given key. Since a #GTree is
790 * automatically balanced as key/value pairs are added, key lookup is very
793 * Return value: the value corresponding to the key, or %NULL if the key was
797 g_tree_lookup (GTree *tree,
802 g_return_val_if_fail (tree != NULL, NULL);
804 node = g_tree_find_node (tree, key);
806 return node ? node->value : NULL;
810 * g_tree_lookup_extended:
812 * @lookup_key: the key to look up.
813 * @orig_key: returns the original key.
814 * @value: returns the value associated with the key.
816 * Looks up a key in the #GTree, returning the original key and the
817 * associated value and a #gboolean which is %TRUE if the key was found. This
818 * is useful if you need to free the memory allocated for the original key,
819 * for example before calling g_tree_remove().
821 * Return value: %TRUE if the key was found in the #GTree.
824 g_tree_lookup_extended (GTree *tree,
825 gconstpointer lookup_key,
831 g_return_val_if_fail (tree != NULL, FALSE);
833 node = g_tree_find_node (tree, lookup_key);
838 *orig_key = node->key;
840 *value = node->value;
850 * @func: the function to call for each node visited. If this function
851 * returns %TRUE, the traversal is stopped.
852 * @user_data: user data to pass to the function.
854 * Calls the given function for each of the key/value pairs in the #GTree.
855 * The function is passed the key and value of each pair, and the given
856 * @data parameter. The tree is traversed in sorted order.
858 * The tree may not be modified while iterating over it (you can't
859 * add/remove items). To remove all items matching a predicate, you need
860 * to add each item to a list in your #GTraverseFunc as you walk over
861 * the tree, then walk the list and remove each item.
864 g_tree_foreach (GTree *tree,
870 g_return_if_fail (tree != NULL);
875 node = g_tree_first_node (tree);
879 if ((*func) (node->key, node->value, user_data))
882 node = g_tree_node_next (node);
889 * @traverse_func: the function to call for each node visited. If this
890 * function returns %TRUE, the traversal is stopped.
891 * @traverse_type: the order in which nodes are visited, one of %G_IN_ORDER,
892 * %G_PRE_ORDER and %G_POST_ORDER.
893 * @user_data: user data to pass to the function.
895 * Calls the given function for each node in the #GTree.
897 * Deprecated:2.2: The order of a balanced tree is somewhat arbitrary. If you
898 * just want to visit all nodes in sorted order, use g_tree_foreach()
899 * instead. If you really need to visit nodes in a different order, consider
900 * using an <link linkend="glib-N-ary-Trees">N-ary Tree</link>.
903 g_tree_traverse (GTree *tree,
904 GTraverseFunc traverse_func,
905 GTraverseType traverse_type,
908 g_return_if_fail (tree != NULL);
913 switch (traverse_type)
916 g_tree_node_pre_order (tree->root, traverse_func, user_data);
920 g_tree_node_in_order (tree->root, traverse_func, user_data);
924 g_tree_node_post_order (tree->root, traverse_func, user_data);
928 g_warning ("g_tree_traverse(): traverse type G_LEVEL_ORDER isn't implemented.");
936 * @search_func: a function used to search the #GTree.
937 * @user_data: the data passed as the second argument to the @search_func
940 * Searches a #GTree using @search_func.
942 * The @search_func is called with a pointer to the key of a key/value pair in
943 * the tree, and the passed in @user_data. If @search_func returns 0 for a
944 * key/value pair, then g_tree_search_func() will return the value of that
945 * pair. If @search_func returns -1, searching will proceed among the
946 * key/value pairs that have a smaller key; if @search_func returns 1,
947 * searching will proceed among the key/value pairs that have a larger key.
949 * Return value: the value corresponding to the found key, or %NULL if the key
953 g_tree_search (GTree *tree,
954 GCompareFunc search_func,
955 gconstpointer user_data)
957 g_return_val_if_fail (tree != NULL, NULL);
960 return g_tree_node_search (tree->root, search_func, user_data);
969 * Gets the height of a #GTree.
971 * If the #GTree contains no nodes, the height is 0.
972 * If the #GTree contains only one root node the height is 1.
973 * If the root node has children the height is 2, etc.
975 * Return value: the height of the #GTree.
978 g_tree_height (GTree *tree)
983 g_return_val_if_fail (tree != NULL, 0);
993 height += 1 + MAX(node->balance, 0);
995 if (!node->left_child)
1006 * Gets the number of nodes in a #GTree.
1008 * Return value: the number of nodes in the #GTree.
1011 g_tree_nnodes (GTree *tree)
1013 g_return_val_if_fail (tree != NULL, 0);
1015 return tree->nnodes;
1019 g_tree_node_balance (GTreeNode *node)
1021 if (node->balance < -1)
1023 if (node->left->balance > 0)
1024 node->left = g_tree_node_rotate_left (node->left);
1025 node = g_tree_node_rotate_right (node);
1027 else if (node->balance > 1)
1029 if (node->right->balance < 0)
1030 node->right = g_tree_node_rotate_right (node->right);
1031 node = g_tree_node_rotate_left (node);
1038 g_tree_find_node (GTree *tree,
1050 cmp = tree->key_compare (key, node->key, tree->key_compare_data);
1055 if (!node->left_child)
1062 if (!node->right_child)
1071 g_tree_node_pre_order (GTreeNode *node,
1072 GTraverseFunc traverse_func,
1075 if ((*traverse_func) (node->key, node->value, data))
1078 if (node->left_child)
1080 if (g_tree_node_pre_order (node->left, traverse_func, data))
1084 if (node->right_child)
1086 if (g_tree_node_pre_order (node->right, traverse_func, data))
1094 g_tree_node_in_order (GTreeNode *node,
1095 GTraverseFunc traverse_func,
1098 if (node->left_child)
1100 if (g_tree_node_in_order (node->left, traverse_func, data))
1104 if ((*traverse_func) (node->key, node->value, data))
1107 if (node->right_child)
1109 if (g_tree_node_in_order (node->right, traverse_func, data))
1117 g_tree_node_post_order (GTreeNode *node,
1118 GTraverseFunc traverse_func,
1121 if (node->left_child)
1123 if (g_tree_node_post_order (node->left, traverse_func, data))
1127 if (node->right_child)
1129 if (g_tree_node_post_order (node->right, traverse_func, data))
1133 if ((*traverse_func) (node->key, node->value, data))
1140 g_tree_node_search (GTreeNode *node,
1141 GCompareFunc search_func,
1151 dir = (* search_func) (node->key, data);
1156 if (!node->left_child)
1163 if (!node->right_child)
1172 g_tree_node_rotate_left (GTreeNode *node)
1178 right = node->right;
1180 if (right->left_child)
1181 node->right = right->left;
1184 node->right_child = FALSE;
1185 node->right = right;
1186 right->left_child = TRUE;
1190 a_bal = node->balance;
1191 b_bal = right->balance;
1196 right->balance = b_bal - 1;
1198 right->balance = a_bal + b_bal - 2;
1199 node->balance = a_bal - 1;
1204 right->balance = a_bal - 2;
1206 right->balance = b_bal - 1;
1207 node->balance = a_bal - b_bal - 1;
1214 g_tree_node_rotate_right (GTreeNode *node)
1222 if (left->right_child)
1223 node->left = left->right;
1226 node->left_child = FALSE;
1228 left->right_child = TRUE;
1232 a_bal = node->balance;
1233 b_bal = left->balance;
1238 left->balance = b_bal + 1;
1240 left->balance = a_bal + 2;
1241 node->balance = a_bal - b_bal + 1;
1246 left->balance = b_bal + 1;
1248 left->balance = a_bal + b_bal + 2;
1249 node->balance = a_bal + 1;
1257 g_tree_node_height (GTreeNode *node)
1267 if (node->left_child)
1268 left_height = g_tree_node_height (node->left);
1270 if (node->right_child)
1271 right_height = g_tree_node_height (node->right);
1273 return MAX (left_height, right_height) + 1;
1280 g_tree_node_check (GTreeNode *node)
1289 if (node->left_child)
1291 tmp = g_tree_node_previous (node);
1292 g_assert (tmp->right == node);
1295 if (node->right_child)
1297 tmp = g_tree_node_next (node);
1298 g_assert (tmp->left == node);
1304 if (node->left_child)
1305 left_height = g_tree_node_height (node->left);
1306 if (node->right_child)
1307 right_height = g_tree_node_height (node->right);
1309 balance = right_height - left_height;
1310 g_assert (balance == node->balance);
1312 if (node->left_child)
1313 g_tree_node_check (node->left);
1314 if (node->right_child)
1315 g_tree_node_check (node->right);
1320 g_tree_node_dump (GTreeNode *node,
1323 g_print ("%*s%c\n", indent, "", *(char *)node->key);
1325 if (node->left_child)
1326 g_tree_node_dump (node->left, indent + 2);
1327 else if (node->left)
1328 g_print ("%*s<%c\n", indent + 2, "", *(char *)node->left->key);
1330 if (node->right_child)
1331 g_tree_node_dump (node->right, indent + 2);
1332 else if (node->right)
1333 g_print ("%*s>%c\n", indent + 2, "", *(char *)node->right->key);
1338 g_tree_dump (GTree *tree)
1341 g_tree_node_dump (tree->root, 0);
1346 #define __G_TREE_C__
1347 #include "galiasdef.c"