Add a couple of languages that have LANG_* codes in newest headers, just
[glib.git] / glib / gtree.c
blob5b0406bc2a347d85a75b6992f9343502a4f2c04e
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/.
27 /*
28 * MT safe
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include "glib.h"
38 typedef struct _GTreeNode GTreeNode;
40 struct _GTree
42 GTreeNode *root;
43 GCompareDataFunc key_compare;
44 GDestroyNotify key_destroy_func;
45 GDestroyNotify value_destroy_func;
46 gpointer key_compare_data;
49 struct _GTreeNode
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,
60 gpointer value);
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,
65 GTreeNode *node,
66 gpointer key,
67 gpointer value,
68 gboolean replace,
69 gboolean *inserted);
70 static GTreeNode* g_tree_node_remove (GTree *tree,
71 GTreeNode *node,
72 gconstpointer key,
73 gboolean notify);
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,
78 gint old_balance);
79 static GTreeNode* g_tree_node_restore_right_balance (GTreeNode *node,
80 gint old_balance);
81 static GTreeNode* g_tree_node_lookup (GTreeNode *node,
82 GCompareDataFunc compare,
83 gpointer comp_data,
84 gconstpointer key);
85 static gint g_tree_node_count (GTreeNode *node);
86 static gint g_tree_node_pre_order (GTreeNode *node,
87 GTraverseFunc traverse_func,
88 gpointer data);
89 static gint g_tree_node_in_order (GTreeNode *node,
90 GTraverseFunc traverse_func,
91 gpointer data);
92 static gint g_tree_node_post_order (GTreeNode *node,
93 GTraverseFunc traverse_func,
94 gpointer data);
95 static gpointer g_tree_node_search (GTreeNode *node,
96 GCompareFunc search_func,
97 gconstpointer data);
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;
109 static GTreeNode*
110 g_tree_node_new (gpointer key,
111 gpointer value)
113 GTreeNode *node;
115 G_LOCK (g_tree_global);
116 if (node_free_list)
118 node = node_free_list;
119 node_free_list = node->right;
121 else
123 if (!node_mem_chunk)
124 node_mem_chunk = g_mem_chunk_new ("GLib GTreeNode mem chunk",
125 sizeof (GTreeNode),
126 1024,
127 G_ALLOC_ONLY);
129 node = g_chunk_new (GTreeNode, node_mem_chunk);
131 G_UNLOCK (g_tree_global);
133 node->balance = 0;
134 node->left = NULL;
135 node->right = NULL;
136 node->key = key;
137 node->value = value;
139 return node;
142 static void
143 g_tree_node_destroy (GTreeNode *node,
144 GDestroyNotify key_destroy_func,
145 GDestroyNotify value_destroy_func)
147 if (node)
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
160 node->left = NULL;
161 node->key = NULL;
162 node->value = NULL;
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);
173 * g_tree_new:
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
179 * after the second.
181 * Creates a new #GTree.
183 * Return value: a new #GTree.
185 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,
191 NULL, NULL);
195 * g_tree_new_with_data:
196 * @key_compare_func: qsort()-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.
204 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,
211 NULL, NULL);
215 * g_tree_new_full:
216 * @key_compare_func: qsort()-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.
231 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)
237 GTree *tree;
239 g_return_val_if_fail (key_compare_func != NULL, NULL);
241 tree = g_new (GTree, 1);
242 tree->root = NULL;
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;
248 return tree;
252 * g_tree_destroy:
253 * @tree: a #GTree.
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.
260 void
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);
269 g_free (tree);
273 * g_tree_insert:
274 * @tree: a #Gtree.
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.
287 void
288 g_tree_insert (GTree *tree,
289 gpointer key,
290 gpointer value)
292 gboolean inserted;
294 g_return_if_fail (tree != NULL);
296 inserted = FALSE;
297 tree->root = g_tree_node_insert (tree,
298 tree->root,
299 key, value,
300 FALSE, &inserted);
304 * g_tree_replace:
305 * @tree: a #Gtree.
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.
319 void
320 g_tree_replace (GTree *tree,
321 gpointer key,
322 gpointer value)
324 gboolean inserted;
326 g_return_if_fail (tree != NULL);
328 inserted = FALSE;
329 tree->root = g_tree_node_insert (tree,
330 tree->root,
331 key, value,
332 TRUE, &inserted);
336 * g_tree_remove:
337 * @tree: a #Gtree.
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.
346 void
347 g_tree_remove (GTree *tree,
348 gconstpointer key)
350 g_return_if_fail (tree != NULL);
352 tree->root = g_tree_node_remove (tree, tree->root, key, TRUE);
356 * g_tree_steal:
357 * @tree: a #Gtree.
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.
363 void
364 g_tree_steal (GTree *tree,
365 gconstpointer key)
367 g_return_if_fail (tree != NULL);
369 tree->root = g_tree_node_remove (tree, tree->root, key, FALSE);
373 * g_tree_lookup:
374 * @tree: a #GTree.
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
379 * fast.
381 * Return value: the value corresponding to the key.
383 gpointer
384 g_tree_lookup (GTree *tree,
385 gconstpointer key)
387 GTreeNode *node;
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:
399 * @tree: a #GTree.
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.
411 gboolean
412 g_tree_lookup_extended (GTree *tree,
413 gconstpointer lookup_key,
414 gpointer *orig_key,
415 gpointer *value)
417 GTreeNode *node;
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);
424 if (node)
426 if (orig_key)
427 *orig_key = node->key;
428 if (value)
429 *value = node->value;
430 return TRUE;
432 else
433 return FALSE;
437 * g_tree_foreach:
438 * @tree: a #GTree.
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.
447 void
448 g_tree_foreach (GTree *tree,
449 GTraverseFunc func,
450 gpointer user_data)
452 g_return_if_fail (tree != NULL);
454 if (!tree->root)
455 return;
457 g_tree_node_in_order (tree->root, func, user_data);
461 * g_tree_traverse:
462 * @tree: a #GTree.
463 * @traverse_func: the function to call for each node visited. If this
464 * function returns TRUE, the traversal is stopped.
465 * @traverse_type: the order in which nodes are visited, one of %G_IN_ORDER,
466 * %G_PRE_ORDER and %G_POST_ORDER.
467 * @user_data: user data to pass to the function.
469 * Calls the given function for each node in the GTree. This function is
470 * deprecated, use g_tree_foreach() instead.
472 void
473 g_tree_traverse (GTree *tree,
474 GTraverseFunc traverse_func,
475 GTraverseType traverse_type,
476 gpointer user_data)
478 g_return_if_fail (tree != NULL);
480 if (!tree->root)
481 return;
483 switch (traverse_type)
485 case G_PRE_ORDER:
486 g_tree_node_pre_order (tree->root, traverse_func, user_data);
487 break;
489 case G_IN_ORDER:
490 g_tree_node_in_order (tree->root, traverse_func, user_data);
491 break;
493 case G_POST_ORDER:
494 g_tree_node_post_order (tree->root, traverse_func, user_data);
495 break;
497 case G_LEVEL_ORDER:
498 g_warning ("g_tree_traverse(): traverse type G_LEVEL_ORDER isn't implemented.");
499 break;
504 * g_tree_search:
505 * @tree: a #GTree.
506 * @search_func: the comparison function used to search the #GTree.
507 * @user_data: the data passed as the second argument to the @search_func
508 * function.
510 * Searches a #GTree using an alternative form of the comparison function.
512 * This function is not as useful as it sounds.
513 * It allows you to use a different function for performing the lookup of
514 * a key. However, since the tree is ordered according to the @key_compare_func
515 * function passed to g_tree_new(), the function you pass to g_tree_search()
516 * must return exactly the same value as would be returned by the comparison
517 * function, for each pair of tree nodes, or the search will not work.
519 * To search for a specific value, you can use g_tree_foreach() or
520 * g_tree_traverse().
522 * Return value: the value corresponding to the found key, or NULL if the key
523 * is not found.
525 gpointer
526 g_tree_search (GTree *tree,
527 GCompareFunc search_func,
528 gconstpointer user_data)
530 g_return_val_if_fail (tree != NULL, NULL);
532 if (tree->root)
533 return g_tree_node_search (tree->root, search_func, user_data);
534 else
535 return NULL;
539 * g_tree_height:
540 * @tree: a #GTree.
542 * Gets the height of a #GTree.
544 * If the #GTree contains no nodes, the height is 0.
545 * If the #GTree contains only one root node the height is 1.
546 * If the root node has children the height is 2, etc.
548 * Return value: the height of the #GTree.
550 gint
551 g_tree_height (GTree *tree)
553 g_return_val_if_fail (tree != NULL, 0);
555 if (tree->root)
556 return g_tree_node_height (tree->root);
557 else
558 return 0;
562 * g_tree_nnodes:
563 * @tree: a #GTree.
565 * Gets the number of nodes in a #GTree.
567 * Return value: the number of nodes in the #GTree.
569 gint
570 g_tree_nnodes (GTree *tree)
572 g_return_val_if_fail (tree != NULL, 0);
574 if (tree->root)
575 return g_tree_node_count (tree->root);
576 else
577 return 0;
580 static GTreeNode*
581 g_tree_node_insert (GTree *tree,
582 GTreeNode *node,
583 gpointer key,
584 gpointer value,
585 gboolean replace,
586 gboolean *inserted)
588 gint old_balance;
589 gint cmp;
591 if (!node)
593 *inserted = TRUE;
594 return g_tree_node_new (key, value);
597 cmp = tree->key_compare (key, node->key, tree->key_compare_data);
598 if (cmp == 0)
600 *inserted = FALSE;
602 if (tree->value_destroy_func)
603 tree->value_destroy_func (node->value);
605 node->value = value;
607 if (replace)
609 if (tree->key_destroy_func)
610 tree->key_destroy_func (node->key);
612 node->key = key;
614 else
616 /* free the passed key */
617 if (tree->key_destroy_func)
618 tree->key_destroy_func (key);
621 return node;
624 if (cmp < 0)
626 if (node->left)
628 old_balance = node->left->balance;
629 node->left = g_tree_node_insert (tree,
630 node->left,
631 key, value,
632 replace, inserted);
634 if ((old_balance != node->left->balance) && node->left->balance)
635 node->balance -= 1;
637 else
639 *inserted = TRUE;
640 node->left = g_tree_node_new (key, value);
641 node->balance -= 1;
644 else if (cmp > 0)
646 if (node->right)
648 old_balance = node->right->balance;
649 node->right = g_tree_node_insert (tree,
650 node->right,
651 key, value,
652 replace, inserted);
654 if ((old_balance != node->right->balance) && node->right->balance)
655 node->balance += 1;
657 else
659 *inserted = TRUE;
660 node->right = g_tree_node_new (key, value);
661 node->balance += 1;
665 if (*inserted)
667 if ((node->balance < -1) || (node->balance > 1))
668 node = g_tree_node_balance (node);
671 return node;
674 static GTreeNode*
675 g_tree_node_remove (GTree *tree,
676 GTreeNode *node,
677 gconstpointer key,
678 gboolean notify)
680 GTreeNode *new_root;
681 gint old_balance;
682 gint cmp;
684 if (!node)
685 return NULL;
687 cmp = tree->key_compare (key, node->key, tree->key_compare_data);
688 if (cmp == 0)
690 GTreeNode *garbage;
692 garbage = node;
694 if (!node->right)
696 node = node->left;
698 else
700 old_balance = node->right->balance;
701 node->right = g_tree_node_remove_leftmost (node->right, &new_root);
702 new_root->left = node->left;
703 new_root->right = node->right;
704 new_root->balance = node->balance;
705 node = g_tree_node_restore_right_balance (new_root, old_balance);
708 if (notify)
710 if (tree->key_destroy_func)
711 tree->key_destroy_func (garbage->key);
712 if (tree->value_destroy_func)
713 tree->value_destroy_func (garbage->value);
716 #ifdef ENABLE_GC_FRIENDLY
717 garbage->left = NULL;
718 garbage->key = NULL;
719 garbage->value = NULL;
720 #endif /* ENABLE_GC_FRIENDLY */
722 G_LOCK (g_tree_global);
723 garbage->right = node_free_list;
724 node_free_list = garbage;
725 G_UNLOCK (g_tree_global);
727 else if (cmp < 0)
729 if (node->left)
731 old_balance = node->left->balance;
732 node->left = g_tree_node_remove (tree, node->left, key, notify);
733 node = g_tree_node_restore_left_balance (node, old_balance);
736 else if (cmp > 0)
738 if (node->right)
740 old_balance = node->right->balance;
741 node->right = g_tree_node_remove (tree, node->right, key, notify);
742 node = g_tree_node_restore_right_balance (node, old_balance);
746 return node;
749 static GTreeNode*
750 g_tree_node_balance (GTreeNode *node)
752 if (node->balance < -1)
754 if (node->left->balance > 0)
755 node->left = g_tree_node_rotate_left (node->left);
756 node = g_tree_node_rotate_right (node);
758 else if (node->balance > 1)
760 if (node->right->balance < 0)
761 node->right = g_tree_node_rotate_right (node->right);
762 node = g_tree_node_rotate_left (node);
765 return node;
768 static GTreeNode*
769 g_tree_node_remove_leftmost (GTreeNode *node,
770 GTreeNode **leftmost)
772 gint old_balance;
774 if (!node->left)
776 *leftmost = node;
777 return node->right;
780 old_balance = node->left->balance;
781 node->left = g_tree_node_remove_leftmost (node->left, leftmost);
782 return g_tree_node_restore_left_balance (node, old_balance);
785 static GTreeNode*
786 g_tree_node_restore_left_balance (GTreeNode *node,
787 gint old_balance)
789 if (!node->left)
790 node->balance += 1;
791 else if ((node->left->balance != old_balance) &&
792 (node->left->balance == 0))
793 node->balance += 1;
795 if (node->balance > 1)
796 return g_tree_node_balance (node);
797 return node;
800 static GTreeNode*
801 g_tree_node_restore_right_balance (GTreeNode *node,
802 gint old_balance)
804 if (!node->right)
805 node->balance -= 1;
806 else if ((node->right->balance != old_balance) &&
807 (node->right->balance == 0))
808 node->balance -= 1;
810 if (node->balance < -1)
811 return g_tree_node_balance (node);
812 return node;
815 static GTreeNode *
816 g_tree_node_lookup (GTreeNode *node,
817 GCompareDataFunc compare,
818 gpointer compare_data,
819 gconstpointer key)
821 gint cmp;
823 if (!node)
824 return NULL;
826 cmp = (* compare) (key, node->key, compare_data);
827 if (cmp == 0)
828 return node;
830 if (cmp < 0)
832 if (node->left)
833 return g_tree_node_lookup (node->left, compare, compare_data, key);
835 else if (cmp > 0)
837 if (node->right)
838 return g_tree_node_lookup (node->right, compare, compare_data, key);
841 return NULL;
844 static gint
845 g_tree_node_count (GTreeNode *node)
847 gint count;
849 count = 1;
850 if (node->left)
851 count += g_tree_node_count (node->left);
852 if (node->right)
853 count += g_tree_node_count (node->right);
855 return count;
858 static gint
859 g_tree_node_pre_order (GTreeNode *node,
860 GTraverseFunc traverse_func,
861 gpointer data)
863 if ((*traverse_func) (node->key, node->value, data))
864 return TRUE;
865 if (node->left)
867 if (g_tree_node_pre_order (node->left, traverse_func, data))
868 return TRUE;
870 if (node->right)
872 if (g_tree_node_pre_order (node->right, traverse_func, data))
873 return TRUE;
876 return FALSE;
879 static gint
880 g_tree_node_in_order (GTreeNode *node,
881 GTraverseFunc traverse_func,
882 gpointer data)
884 if (node->left)
886 if (g_tree_node_in_order (node->left, traverse_func, data))
887 return TRUE;
889 if ((*traverse_func) (node->key, node->value, data))
890 return TRUE;
891 if (node->right)
893 if (g_tree_node_in_order (node->right, traverse_func, data))
894 return TRUE;
897 return FALSE;
900 static gint
901 g_tree_node_post_order (GTreeNode *node,
902 GTraverseFunc traverse_func,
903 gpointer data)
905 if (node->left)
907 if (g_tree_node_post_order (node->left, traverse_func, data))
908 return TRUE;
910 if (node->right)
912 if (g_tree_node_post_order (node->right, traverse_func, data))
913 return TRUE;
915 if ((*traverse_func) (node->key, node->value, data))
916 return TRUE;
918 return FALSE;
921 static gpointer
922 g_tree_node_search (GTreeNode *node,
923 GCompareFunc search_func,
924 gconstpointer data)
926 gint dir;
928 if (!node)
929 return NULL;
931 do {
932 dir = (* search_func) (node->key, data);
933 if (dir == 0)
934 return node->value;
936 if (dir < 0)
937 node = node->left;
938 else if (dir > 0)
939 node = node->right;
940 } while (node);
942 return NULL;
945 static gint
946 g_tree_node_height (GTreeNode *node)
948 gint left_height;
949 gint right_height;
951 if (node)
953 left_height = 0;
954 right_height = 0;
956 if (node->left)
957 left_height = g_tree_node_height (node->left);
959 if (node->right)
960 right_height = g_tree_node_height (node->right);
962 return MAX (left_height, right_height) + 1;
965 return 0;
968 static GTreeNode*
969 g_tree_node_rotate_left (GTreeNode *node)
971 GTreeNode *right;
972 gint a_bal;
973 gint b_bal;
975 right = node->right;
977 node->right = right->left;
978 right->left = node;
980 a_bal = node->balance;
981 b_bal = right->balance;
983 if (b_bal <= 0)
985 if (a_bal >= 1)
986 right->balance = b_bal - 1;
987 else
988 right->balance = a_bal + b_bal - 2;
989 node->balance = a_bal - 1;
991 else
993 if (a_bal <= b_bal)
994 right->balance = a_bal - 2;
995 else
996 right->balance = b_bal - 1;
997 node->balance = a_bal - b_bal - 1;
1000 return right;
1003 static GTreeNode*
1004 g_tree_node_rotate_right (GTreeNode *node)
1006 GTreeNode *left;
1007 gint a_bal;
1008 gint b_bal;
1010 left = node->left;
1012 node->left = left->right;
1013 left->right = node;
1015 a_bal = node->balance;
1016 b_bal = left->balance;
1018 if (b_bal <= 0)
1020 if (b_bal > a_bal)
1021 left->balance = b_bal + 1;
1022 else
1023 left->balance = a_bal + 2;
1024 node->balance = a_bal - b_bal + 1;
1026 else
1028 if (a_bal <= -1)
1029 left->balance = b_bal + 1;
1030 else
1031 left->balance = a_bal + b_bal + 2;
1032 node->balance = a_bal + 1;
1035 return left;
1038 static void
1039 g_tree_node_check (GTreeNode *node)
1041 gint left_height;
1042 gint right_height;
1043 gint balance;
1045 if (node)
1047 left_height = 0;
1048 right_height = 0;
1050 if (node->left)
1051 left_height = g_tree_node_height (node->left);
1052 if (node->right)
1053 right_height = g_tree_node_height (node->right);
1055 balance = right_height - left_height;
1056 if (balance != node->balance)
1057 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
1058 "g_tree_node_check: failed: %d ( %d )\n",
1059 balance, node->balance);
1061 if (node->left)
1062 g_tree_node_check (node->left);
1063 if (node->right)
1064 g_tree_node_check (node->right);