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);
268 * Increments the reference count of @tree by one. It is safe to call
269 * this function from any thread.
271 * Return value: the passed in #GTree.
276 g_tree_ref (GTree *tree)
278 g_return_val_if_fail (tree != NULL, NULL);
280 g_atomic_int_inc (&tree->ref_count);
289 * Decrements the reference count of @tree by one. If the reference count
290 * drops to 0, all keys and values will be destroyed (if destroy
291 * functions were specified) and all memory allocated by @tree will be
294 * It is safe to call this function from any thread.
299 g_tree_unref (GTree *tree)
301 g_return_if_fail (tree != NULL);
303 if (g_atomic_int_dec_and_test (&tree->ref_count))
305 g_tree_remove_all (tree);
306 g_slice_free (GTree, tree);
314 * Removes all keys and values from the #GTree and decreases its
315 * reference count by one. If keys and/or values are dynamically
316 * allocated, you should either free them first or create the #GTree
317 * using g_tree_new_full(). In the latter case the destroy functions
318 * you supplied will be called on all keys and values before destroying
322 g_tree_destroy (GTree *tree)
324 g_return_if_fail (tree != NULL);
326 g_tree_remove_all (tree);
333 * @key: the key to insert.
334 * @value: the value corresponding to the key.
336 * Inserts a key/value pair into a #GTree. If the given key already exists
337 * in the #GTree its corresponding value is set to the new value. If you
338 * supplied a value_destroy_func when creating the #GTree, the old value is
339 * freed using that function. If you supplied a @key_destroy_func when
340 * creating the #GTree, the passed key is freed using that function.
342 * The tree is automatically 'balanced' as new key/value pairs are added,
343 * so that the distance from the root to every leaf is as small as possible.
346 g_tree_insert (GTree *tree,
350 g_return_if_fail (tree != NULL);
352 g_tree_insert_internal (tree, key, value, FALSE);
355 g_tree_node_check (tree->root);
362 * @key: the key to insert.
363 * @value: the value corresponding to the key.
365 * Inserts a new key and value into a #GTree similar to g_tree_insert().
366 * The difference is that if the key already exists in the #GTree, it gets
367 * replaced by the new key. If you supplied a @value_destroy_func when
368 * creating the #GTree, the old value is freed using that function. If you
369 * supplied a @key_destroy_func when creating the #GTree, the old key is
370 * freed using that function.
372 * The tree is automatically 'balanced' as new key/value pairs are added,
373 * so that the distance from the root to every leaf is as small as possible.
376 g_tree_replace (GTree *tree,
380 g_return_if_fail (tree != NULL);
382 g_tree_insert_internal (tree, key, value, TRUE);
385 g_tree_node_check (tree->root);
389 /* internal insert routine */
391 g_tree_insert_internal (GTree *tree,
397 GTreeNode *path[MAX_GTREE_HEIGHT];
400 g_return_if_fail (tree != NULL);
404 tree->root = g_tree_node_new (key, value);
415 int cmp = tree->key_compare (key, node->key, tree->key_compare_data);
419 if (tree->value_destroy_func)
420 tree->value_destroy_func (node->value);
426 if (tree->key_destroy_func)
427 tree->key_destroy_func (node->key);
433 /* free the passed key */
434 if (tree->key_destroy_func)
435 tree->key_destroy_func (key);
442 if (node->left_child)
449 GTreeNode *child = g_tree_node_new (key, value);
451 child->left = node->left;
454 node->left_child = TRUE;
464 if (node->right_child)
471 GTreeNode *child = g_tree_node_new (key, value);
473 child->right = node->right;
476 node->right_child = TRUE;
486 /* restore balance. This is the goodness of a non-recursive
487 implementation, when we are done with balancing we 'break'
488 the loop and we are done. */
491 GTreeNode *bparent = path[--idx];
492 gboolean left_node = (bparent && node == bparent->left);
493 g_assert (!bparent || bparent->left == node || bparent->right == node);
495 if (node->balance < -1 || node->balance > 1)
497 node = g_tree_node_balance (node);
501 bparent->left = node;
503 bparent->right = node;
506 if (node->balance == 0 || bparent == NULL)
510 bparent->balance -= 1;
512 bparent->balance += 1;
521 * @key: the key to remove.
523 * Removes a key/value pair from a #GTree.
525 * If the #GTree was created using g_tree_new_full(), the key and value
526 * are freed using the supplied destroy functions, otherwise you have to
527 * make sure that any dynamically allocated values are freed yourself.
528 * If the key does not exist in the #GTree, the function does nothing.
530 * Returns: %TRUE if the key was found (prior to 2.8, this function returned
534 g_tree_remove (GTree *tree,
539 g_return_val_if_fail (tree != NULL, FALSE);
541 removed = g_tree_remove_internal (tree, key, FALSE);
544 g_tree_node_check (tree->root);
553 * @key: the key to remove.
555 * Removes a key and its associated value from a #GTree without calling
556 * the key and value destroy functions.
558 * If the key does not exist in the #GTree, the function does nothing.
560 * Returns: %TRUE if the key was found (prior to 2.8, this function returned
564 g_tree_steal (GTree *tree,
569 g_return_val_if_fail (tree != NULL, FALSE);
571 removed = g_tree_remove_internal (tree, key, TRUE);
574 g_tree_node_check (tree->root);
580 /* internal remove routine */
582 g_tree_remove_internal (GTree *tree,
586 GTreeNode *node, *parent, *balance;
587 GTreeNode *path[MAX_GTREE_HEIGHT];
591 g_return_val_if_fail (tree != NULL, FALSE);
602 int cmp = tree->key_compare (key, node->key, tree->key_compare_data);
608 if (!node->left_child)
616 if (!node->right_child)
624 /* the following code is almost equal to g_tree_remove_node,
625 except that we do not have to call g_tree_node_parent. */
626 balance = parent = path[--idx];
627 g_assert (!parent || parent->left == node || parent->right == node);
628 left_node = (parent && node == parent->left);
630 if (!node->left_child)
632 if (!node->right_child)
638 parent->left_child = FALSE;
639 parent->left = node->left;
640 parent->balance += 1;
644 parent->right_child = FALSE;
645 parent->right = node->right;
646 parent->balance -= 1;
649 else /* node has a right child */
651 GTreeNode *tmp = g_tree_node_next (node);
652 tmp->left = node->left;
655 tree->root = node->right;
658 parent->left = node->right;
659 parent->balance += 1;
663 parent->right = node->right;
664 parent->balance -= 1;
668 else /* node has a left child */
670 if (!node->right_child)
672 GTreeNode *tmp = g_tree_node_previous (node);
673 tmp->right = node->right;
676 tree->root = node->left;
679 parent->left = node->left;
680 parent->balance += 1;
684 parent->right = node->left;
685 parent->balance -= 1;
688 else /* node has a both children (pant, pant!) */
690 GTreeNode *prev = node->left;
691 GTreeNode *next = node->right;
692 GTreeNode *nextp = node;
693 int old_idx = idx + 1;
696 /* path[idx] == parent */
697 /* find the immediately next node (and its parent) */
698 while (next->left_child)
700 path[++idx] = nextp = next;
704 path[old_idx] = next;
707 /* remove 'next' from the tree */
710 if (next->right_child)
711 nextp->left = next->right;
713 nextp->left_child = FALSE;
716 next->right_child = TRUE;
717 next->right = node->right;
722 /* set the prev to point to the right place */
723 while (prev->right_child)
727 /* prepare 'next' to replace 'node' */
728 next->left_child = TRUE;
729 next->left = node->left;
730 next->balance = node->balance;
737 parent->right = next;
741 /* restore balance */
745 GTreeNode *bparent = path[--idx];
746 g_assert (!bparent || bparent->left == balance || bparent->right == balance);
747 left_node = (bparent && balance == bparent->left);
749 if(balance->balance < -1 || balance->balance > 1)
751 balance = g_tree_node_balance (balance);
753 tree->root = balance;
755 bparent->left = balance;
757 bparent->right = balance;
760 if (balance->balance != 0 || !bparent)
764 bparent->balance += 1;
766 bparent->balance -= 1;
773 if (tree->key_destroy_func)
774 tree->key_destroy_func (node->key);
775 if (tree->value_destroy_func)
776 tree->value_destroy_func (node->value);
779 g_slice_free (GTreeNode, node);
789 * @key: the key to look up.
791 * Gets the value corresponding to the given key. Since a #GTree is
792 * automatically balanced as key/value pairs are added, key lookup is very
795 * Return value: the value corresponding to the key, or %NULL if the key was
799 g_tree_lookup (GTree *tree,
804 g_return_val_if_fail (tree != NULL, NULL);
806 node = g_tree_find_node (tree, key);
808 return node ? node->value : NULL;
812 * g_tree_lookup_extended:
814 * @lookup_key: the key to look up.
815 * @orig_key: returns the original key.
816 * @value: returns the value associated with the key.
818 * Looks up a key in the #GTree, returning the original key and the
819 * associated value and a #gboolean which is %TRUE if the key was found. This
820 * is useful if you need to free the memory allocated for the original key,
821 * for example before calling g_tree_remove().
823 * Return value: %TRUE if the key was found in the #GTree.
826 g_tree_lookup_extended (GTree *tree,
827 gconstpointer lookup_key,
833 g_return_val_if_fail (tree != NULL, FALSE);
835 node = g_tree_find_node (tree, lookup_key);
840 *orig_key = node->key;
842 *value = node->value;
852 * @func: the function to call for each node visited. If this function
853 * returns %TRUE, the traversal is stopped.
854 * @user_data: user data to pass to the function.
856 * Calls the given function for each of the key/value pairs in the #GTree.
857 * The function is passed the key and value of each pair, and the given
858 * @data parameter. The tree is traversed in sorted order.
860 * The tree may not be modified while iterating over it (you can't
861 * add/remove items). To remove all items matching a predicate, you need
862 * to add each item to a list in your #GTraverseFunc as you walk over
863 * the tree, then walk the list and remove each item.
866 g_tree_foreach (GTree *tree,
872 g_return_if_fail (tree != NULL);
877 node = g_tree_first_node (tree);
881 if ((*func) (node->key, node->value, user_data))
884 node = g_tree_node_next (node);
891 * @traverse_func: the function to call for each node visited. If this
892 * function returns %TRUE, the traversal is stopped.
893 * @traverse_type: the order in which nodes are visited, one of %G_IN_ORDER,
894 * %G_PRE_ORDER and %G_POST_ORDER.
895 * @user_data: user data to pass to the function.
897 * Calls the given function for each node in the #GTree.
899 * Deprecated:2.2: The order of a balanced tree is somewhat arbitrary. If you
900 * just want to visit all nodes in sorted order, use g_tree_foreach()
901 * instead. If you really need to visit nodes in a different order, consider
902 * using an <link linkend="glib-N-ary-Trees">N-ary Tree</link>.
905 g_tree_traverse (GTree *tree,
906 GTraverseFunc traverse_func,
907 GTraverseType traverse_type,
910 g_return_if_fail (tree != NULL);
915 switch (traverse_type)
918 g_tree_node_pre_order (tree->root, traverse_func, user_data);
922 g_tree_node_in_order (tree->root, traverse_func, user_data);
926 g_tree_node_post_order (tree->root, traverse_func, user_data);
930 g_warning ("g_tree_traverse(): traverse type G_LEVEL_ORDER isn't implemented.");
938 * @search_func: a function used to search the #GTree.
939 * @user_data: the data passed as the second argument to the @search_func
942 * Searches a #GTree using @search_func.
944 * The @search_func is called with a pointer to the key of a key/value pair in
945 * the tree, and the passed in @user_data. If @search_func returns 0 for a
946 * key/value pair, then g_tree_search_func() will return the value of that
947 * pair. If @search_func returns -1, searching will proceed among the
948 * key/value pairs that have a smaller key; if @search_func returns 1,
949 * searching will proceed among the key/value pairs that have a larger key.
951 * Return value: the value corresponding to the found key, or %NULL if the key
955 g_tree_search (GTree *tree,
956 GCompareFunc search_func,
957 gconstpointer user_data)
959 g_return_val_if_fail (tree != NULL, NULL);
962 return g_tree_node_search (tree->root, search_func, user_data);
971 * Gets the height of a #GTree.
973 * If the #GTree contains no nodes, the height is 0.
974 * If the #GTree contains only one root node the height is 1.
975 * If the root node has children the height is 2, etc.
977 * Return value: the height of the #GTree.
980 g_tree_height (GTree *tree)
985 g_return_val_if_fail (tree != NULL, 0);
995 height += 1 + MAX(node->balance, 0);
997 if (!node->left_child)
1008 * Gets the number of nodes in a #GTree.
1010 * Return value: the number of nodes in the #GTree.
1013 g_tree_nnodes (GTree *tree)
1015 g_return_val_if_fail (tree != NULL, 0);
1017 return tree->nnodes;
1021 g_tree_node_balance (GTreeNode *node)
1023 if (node->balance < -1)
1025 if (node->left->balance > 0)
1026 node->left = g_tree_node_rotate_left (node->left);
1027 node = g_tree_node_rotate_right (node);
1029 else if (node->balance > 1)
1031 if (node->right->balance < 0)
1032 node->right = g_tree_node_rotate_right (node->right);
1033 node = g_tree_node_rotate_left (node);
1040 g_tree_find_node (GTree *tree,
1052 cmp = tree->key_compare (key, node->key, tree->key_compare_data);
1057 if (!node->left_child)
1064 if (!node->right_child)
1073 g_tree_node_pre_order (GTreeNode *node,
1074 GTraverseFunc traverse_func,
1077 if ((*traverse_func) (node->key, node->value, data))
1080 if (node->left_child)
1082 if (g_tree_node_pre_order (node->left, traverse_func, data))
1086 if (node->right_child)
1088 if (g_tree_node_pre_order (node->right, traverse_func, data))
1096 g_tree_node_in_order (GTreeNode *node,
1097 GTraverseFunc traverse_func,
1100 if (node->left_child)
1102 if (g_tree_node_in_order (node->left, traverse_func, data))
1106 if ((*traverse_func) (node->key, node->value, data))
1109 if (node->right_child)
1111 if (g_tree_node_in_order (node->right, traverse_func, data))
1119 g_tree_node_post_order (GTreeNode *node,
1120 GTraverseFunc traverse_func,
1123 if (node->left_child)
1125 if (g_tree_node_post_order (node->left, traverse_func, data))
1129 if (node->right_child)
1131 if (g_tree_node_post_order (node->right, traverse_func, data))
1135 if ((*traverse_func) (node->key, node->value, data))
1142 g_tree_node_search (GTreeNode *node,
1143 GCompareFunc search_func,
1153 dir = (* search_func) (node->key, data);
1158 if (!node->left_child)
1165 if (!node->right_child)
1174 g_tree_node_rotate_left (GTreeNode *node)
1180 right = node->right;
1182 if (right->left_child)
1183 node->right = right->left;
1186 node->right_child = FALSE;
1187 node->right = right;
1188 right->left_child = TRUE;
1192 a_bal = node->balance;
1193 b_bal = right->balance;
1198 right->balance = b_bal - 1;
1200 right->balance = a_bal + b_bal - 2;
1201 node->balance = a_bal - 1;
1206 right->balance = a_bal - 2;
1208 right->balance = b_bal - 1;
1209 node->balance = a_bal - b_bal - 1;
1216 g_tree_node_rotate_right (GTreeNode *node)
1224 if (left->right_child)
1225 node->left = left->right;
1228 node->left_child = FALSE;
1230 left->right_child = TRUE;
1234 a_bal = node->balance;
1235 b_bal = left->balance;
1240 left->balance = b_bal + 1;
1242 left->balance = a_bal + 2;
1243 node->balance = a_bal - b_bal + 1;
1248 left->balance = b_bal + 1;
1250 left->balance = a_bal + b_bal + 2;
1251 node->balance = a_bal + 1;
1259 g_tree_node_height (GTreeNode *node)
1269 if (node->left_child)
1270 left_height = g_tree_node_height (node->left);
1272 if (node->right_child)
1273 right_height = g_tree_node_height (node->right);
1275 return MAX (left_height, right_height) + 1;
1282 g_tree_node_check (GTreeNode *node)
1291 if (node->left_child)
1293 tmp = g_tree_node_previous (node);
1294 g_assert (tmp->right == node);
1297 if (node->right_child)
1299 tmp = g_tree_node_next (node);
1300 g_assert (tmp->left == node);
1306 if (node->left_child)
1307 left_height = g_tree_node_height (node->left);
1308 if (node->right_child)
1309 right_height = g_tree_node_height (node->right);
1311 balance = right_height - left_height;
1312 g_assert (balance == node->balance);
1314 if (node->left_child)
1315 g_tree_node_check (node->left);
1316 if (node->right_child)
1317 g_tree_node_check (node->right);
1322 g_tree_node_dump (GTreeNode *node,
1325 g_print ("%*s%c\n", indent, "", *(char *)node->key);
1327 if (node->left_child)
1328 g_tree_node_dump (node->left, indent + 2);
1329 else if (node->left)
1330 g_print ("%*s<%c\n", indent + 2, "", *(char *)node->left->key);
1332 if (node->right_child)
1333 g_tree_node_dump (node->right, indent + 2);
1334 else if (node->right)
1335 g_print ("%*s>%c\n", indent + 2, "", *(char *)node->right->key);
1340 g_tree_dump (GTree *tree)
1343 g_tree_node_dump (tree->root, 0);
1348 #define __G_TREE_C__
1349 #include "galiasdef.c"