Documentation fixes: #71778, Owen Taylor; #85095, Bill Janssen, Owen
[platform/upstream/glib.git] / glib / gtree.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 /* 
28  * MT safe
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include "glib.h"
36
37
38 typedef struct _GTreeNode  GTreeNode;
39
40 struct _GTree
41 {
42   GTreeNode *root;
43   GCompareDataFunc key_compare;
44   GDestroyNotify   key_destroy_func;
45   GDestroyNotify   value_destroy_func;
46   gpointer         key_compare_data;
47 };
48
49 struct _GTreeNode
50 {
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 */
56 };
57
58
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);
102
103
104 G_LOCK_DEFINE_STATIC (g_tree_global);
105 static GMemChunk *node_mem_chunk = NULL;
106 static GTreeNode *node_free_list = NULL;
107
108
109 static GTreeNode*
110 g_tree_node_new (gpointer key,
111                  gpointer value)
112 {
113   GTreeNode *node;
114
115   G_LOCK (g_tree_global);
116   if (node_free_list)
117     {
118       node = node_free_list;
119       node_free_list = node->right;
120     }
121   else
122     {
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);
128
129       node = g_chunk_new (GTreeNode, node_mem_chunk);
130    }
131   G_UNLOCK (g_tree_global);
132
133   node->balance = 0;
134   node->left = NULL;
135   node->right = NULL;
136   node->key = key;
137   node->value = value;
138
139   return node;
140 }
141
142 static void
143 g_tree_node_destroy (GTreeNode      *node,
144                      GDestroyNotify  key_destroy_func,
145                      GDestroyNotify  value_destroy_func)
146 {
147   if (node)
148     {
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);
153
154       if (key_destroy_func)
155         key_destroy_func (node->key);
156       if (value_destroy_func)
157         value_destroy_func (node->value);
158       
159 #ifdef ENABLE_GC_FRIENDLY
160       node->left = NULL;
161       node->key = NULL;
162       node->value = NULL;
163 #endif /* ENABLE_GC_FRIENDLY */
164
165       G_LOCK (g_tree_global);
166       node->right = node_free_list;
167       node_free_list = node;
168       G_UNLOCK (g_tree_global);
169    }
170 }
171
172 /**
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.
180  * 
181  * Creates a new #GTree.
182  * 
183  * Return value: a new #GTree.
184  **/
185 GTree*
186 g_tree_new (GCompareFunc key_compare_func)
187 {
188   g_return_val_if_fail (key_compare_func != NULL, NULL);
189
190   return g_tree_new_full ((GCompareDataFunc) key_compare_func, NULL,
191                           NULL, NULL);
192 }
193
194 /**
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.
198  * 
199  * Creates a new #GTree with a comparison function that accepts user data.
200  * See g_tree_new() for more details.
201  * 
202  * Return value: a new #GTree.
203  **/
204 GTree*
205 g_tree_new_with_data (GCompareDataFunc key_compare_func,
206                       gpointer         key_compare_data)
207 {
208   g_return_val_if_fail (key_compare_func != NULL, NULL);
209   
210   return g_tree_new_full (key_compare_func, key_compare_data, 
211                           NULL, NULL);
212 }
213
214 /**
215  * g_tree_new_full:
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.
224  * 
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.
228  * 
229  * Return value: a new #GTree.
230  **/
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)
236 {
237   GTree *tree;
238   
239   g_return_val_if_fail (key_compare_func != NULL, NULL);
240   
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;
247   
248   return tree;
249 }
250
251 /**
252  * g_tree_destroy:
253  * @tree: a #GTree.
254  * 
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.
259  **/
260 void
261 g_tree_destroy (GTree *tree)
262 {
263   g_return_if_fail (tree != NULL);
264
265   g_tree_node_destroy (tree->root,
266                        tree->key_destroy_func,
267                        tree->value_destroy_func);
268
269   g_free (tree);
270 }
271
272 /**
273  * g_tree_insert:
274  * @tree: a #GTree.
275  * @key: the key to insert.
276  * @value: the value corresponding to the key.
277  * 
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.
283  *
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.
286  **/
287 void
288 g_tree_insert (GTree    *tree,
289                gpointer  key,
290                gpointer  value)
291 {
292   gboolean   inserted;
293
294   g_return_if_fail (tree != NULL);
295
296   inserted = FALSE;
297   tree->root = g_tree_node_insert (tree,
298                                    tree->root,
299                                    key, value, 
300                                    FALSE, &inserted);
301 }
302
303 /**
304  * g_tree_replace:
305  * @tree: a #GTree.
306  * @key: the key to insert.
307  * @value: the value corresponding to the key.
308  * 
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. 
315  *
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.
318  **/
319 void
320 g_tree_replace (GTree    *tree,
321                 gpointer  key,
322                 gpointer  value)
323 {
324   gboolean   inserted;
325
326   g_return_if_fail (tree != NULL);
327
328   inserted = FALSE;
329   tree->root = g_tree_node_insert (tree,
330                                    tree->root,
331                                    key, value, 
332                                    TRUE, &inserted);
333 }
334
335 /**
336  * g_tree_remove:
337  * @tree: a #GTree.
338  * @key: the key to remove.
339  * 
340  * Removes a key/value pair from a #GTree.
341  *
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.
345  **/
346 void
347 g_tree_remove (GTree         *tree,
348                gconstpointer  key)
349 {
350   g_return_if_fail (tree != NULL);
351
352   tree->root = g_tree_node_remove (tree, tree->root, key, TRUE);
353 }
354
355 /**
356  * g_tree_steal:
357  * @tree: a #GTree.
358  * @key: the key to remove.
359  * 
360  * Removes a key and its associated value from a #GTree without calling 
361  * the key and value destroy functions.
362  **/
363 void
364 g_tree_steal (GTree         *tree,
365               gconstpointer  key)
366 {
367   g_return_if_fail (tree != NULL);
368
369   tree->root = g_tree_node_remove (tree, tree->root, key, FALSE);
370 }
371
372 /**
373  * g_tree_lookup:
374  * @tree: a #GTree.
375  * @key: the key to look up.
376  * 
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.
380  *
381  * Return value: the value corresponding to the key.
382  **/
383 gpointer
384 g_tree_lookup (GTree         *tree,
385                gconstpointer  key)
386 {
387   GTreeNode *node;
388
389   g_return_val_if_fail (tree != NULL, NULL);
390
391   node = g_tree_node_lookup (tree->root, 
392                              tree->key_compare, tree->key_compare_data, key);
393
394   return node ? node->value : NULL;
395 }
396
397 /**
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.
403  * 
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().
408  * 
409  * Return value: %TRUE if the key was found in the #GTree.
410  **/
411 gboolean
412 g_tree_lookup_extended (GTree         *tree,
413                         gconstpointer  lookup_key,
414                         gpointer      *orig_key,
415                         gpointer      *value)
416 {
417   GTreeNode *node;
418   
419   g_return_val_if_fail (tree != NULL, FALSE);
420   
421   node = g_tree_node_lookup (tree->root, 
422                              tree->key_compare, tree->key_compare_data, lookup_key);
423
424   if (node)
425     {
426       if (orig_key)
427         *orig_key = node->key;
428       if (value)
429         *value = node->value;
430       return TRUE;
431     }
432   else
433     return FALSE;
434 }
435
436 /**
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.
442  * 
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.
446  *
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.
451  **/
452 void
453 g_tree_foreach (GTree         *tree,
454                 GTraverseFunc  func,
455                 gpointer       user_data)
456 {
457   g_return_if_fail (tree != NULL);
458   
459   if (!tree->root)
460     return;
461
462   g_tree_node_in_order (tree->root, func, user_data);
463 }
464
465 /**
466  * g_tree_traverse:
467  * @tree: a #GTree.
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.
473  * 
474  * Calls the given function for each node in the #GTree. This function is
475  * deprecated, since the order of a balanced tree is somewhat arbitrary.
476  * If you just want to visit all nodes in sorted order, use g_tree_foreach() 
477  * instead. If you really need to visit nodes in a different order, consider
478  * using an <link linkend="glib-N-ary-Trees">N-ary Tree</link>.
479  **/
480 void
481 g_tree_traverse (GTree         *tree,
482                  GTraverseFunc  traverse_func,
483                  GTraverseType  traverse_type,
484                  gpointer       user_data)
485 {
486   g_return_if_fail (tree != NULL);
487
488   if (!tree->root)
489     return;
490
491   switch (traverse_type)
492     {
493     case G_PRE_ORDER:
494       g_tree_node_pre_order (tree->root, traverse_func, user_data);
495       break;
496
497     case G_IN_ORDER:
498       g_tree_node_in_order (tree->root, traverse_func, user_data);
499       break;
500
501     case G_POST_ORDER:
502       g_tree_node_post_order (tree->root, traverse_func, user_data);
503       break;
504     
505     case G_LEVEL_ORDER:
506       g_warning ("g_tree_traverse(): traverse type G_LEVEL_ORDER isn't implemented.");
507       break;
508     }
509 }
510
511 /**
512  * g_tree_search:
513  * @tree: a #GTree.
514  * @search_func: a function used to search the #GTree. 
515  * @user_data: the data passed as the second argument to the @search_func 
516  * function.
517  * 
518  * Searches a #GTree using @search_func.
519  *
520  * The @search_func is called with a pointer to the key of a key/value pair in the tree,
521  * and the passed in @user_data. If @search_func returns 0 for a key/value pair, then
522  * g_tree_search_func() will return the value of that pair. If @search_func returns -1,
523  * searching will proceed among the key/value pairs that have a smaller key; if @search_func
524  * returns 1, searching will proceed among the key/value pairs that have a larger key.
525  *
526  * Return value: the value corresponding to the found key, or %NULL if the key 
527  * was not found.
528  **/
529 gpointer
530 g_tree_search (GTree         *tree,
531                GCompareFunc   search_func,
532                gconstpointer  user_data)
533 {
534   g_return_val_if_fail (tree != NULL, NULL);
535
536   if (tree->root)
537     return g_tree_node_search (tree->root, search_func, user_data);
538   else
539     return NULL;
540 }
541
542 /**
543  * g_tree_height:
544  * @tree: a #GTree.
545  * 
546  * Gets the height of a #GTree.
547  *
548  * If the #GTree contains no nodes, the height is 0.
549  * If the #GTree contains only one root node the height is 1.
550  * If the root node has children the height is 2, etc.
551  * 
552  * Return value: the height of the #GTree.
553  **/
554 gint
555 g_tree_height (GTree *tree)
556 {
557   g_return_val_if_fail (tree != NULL, 0);
558
559   if (tree->root)
560     return g_tree_node_height (tree->root);
561   else
562     return 0;
563 }
564
565 /**
566  * g_tree_nnodes:
567  * @tree: a #GTree.
568  * 
569  * Gets the number of nodes in a #GTree.
570  * 
571  * Return value: the number of nodes in the #GTree.
572  **/
573 gint
574 g_tree_nnodes (GTree *tree)
575 {
576   g_return_val_if_fail (tree != NULL, 0);
577
578   if (tree->root)
579     return g_tree_node_count (tree->root);
580   else
581     return 0;
582 }
583
584 static GTreeNode*
585 g_tree_node_insert (GTree     *tree,
586                     GTreeNode *node,
587                     gpointer   key,
588                     gpointer   value,
589                     gboolean   replace,
590                     gboolean  *inserted)
591 {
592   gint  old_balance;
593   gint  cmp;
594
595   if (!node)
596     {
597       *inserted = TRUE;
598       return g_tree_node_new (key, value);
599     }
600
601   cmp = tree->key_compare (key, node->key, tree->key_compare_data);
602   if (cmp == 0)
603     {
604       *inserted = FALSE;
605
606       if (tree->value_destroy_func)
607         tree->value_destroy_func (node->value);
608
609       node->value = value;
610       
611       if (replace)
612         {
613           if (tree->key_destroy_func)
614             tree->key_destroy_func (node->key);
615
616           node->key = key;
617         }
618       else
619         {
620           /* free the passed key */
621           if (tree->key_destroy_func)
622             tree->key_destroy_func (key);
623         }
624
625       return node;
626     }
627
628   if (cmp < 0)
629     {
630       if (node->left)
631         {
632           old_balance = node->left->balance;
633           node->left = g_tree_node_insert (tree,
634                                            node->left,
635                                            key, value,
636                                            replace, inserted);
637
638           if ((old_balance != node->left->balance) && node->left->balance)
639             node->balance -= 1;
640         }
641       else
642         {
643           *inserted = TRUE;
644           node->left = g_tree_node_new (key, value);
645           node->balance -= 1;
646         }
647     }
648   else if (cmp > 0)
649     {
650       if (node->right)
651         {
652           old_balance = node->right->balance;
653           node->right = g_tree_node_insert (tree,
654                                             node->right,
655                                             key, value, 
656                                             replace, inserted);
657
658           if ((old_balance != node->right->balance) && node->right->balance)
659             node->balance += 1;
660         }
661       else
662         {
663           *inserted = TRUE;
664           node->right = g_tree_node_new (key, value);
665           node->balance += 1;
666         }
667     }
668
669   if (*inserted)
670     {
671       if ((node->balance < -1) || (node->balance > 1))
672         node = g_tree_node_balance (node);
673     }
674
675   return node;
676 }
677
678 static GTreeNode*
679 g_tree_node_remove (GTree         *tree,
680                     GTreeNode     *node,
681                     gconstpointer  key,
682                     gboolean       notify)
683 {
684   GTreeNode *new_root;
685   gint old_balance;
686   gint cmp;
687
688   if (!node)
689     return NULL;
690
691   cmp = tree->key_compare (key, node->key, tree->key_compare_data);
692   if (cmp == 0)
693     {
694       GTreeNode *garbage;
695
696       garbage = node;
697
698       if (!node->right)
699         {
700           node = node->left;
701         }
702       else
703         {
704           old_balance = node->right->balance;
705           node->right = g_tree_node_remove_leftmost (node->right, &new_root);
706           new_root->left = node->left;
707           new_root->right = node->right;
708           new_root->balance = node->balance;
709           node = g_tree_node_restore_right_balance (new_root, old_balance);
710         }
711
712       if (notify)
713         {
714           if (tree->key_destroy_func)
715             tree->key_destroy_func (garbage->key);
716           if (tree->value_destroy_func)
717             tree->value_destroy_func (garbage->value);
718         }
719
720 #ifdef ENABLE_GC_FRIENDLY
721       garbage->left = NULL;
722       garbage->key = NULL;
723       garbage->value = NULL;
724 #endif /* ENABLE_GC_FRIENDLY */
725
726       G_LOCK (g_tree_global);
727       garbage->right = node_free_list;
728       node_free_list = garbage;
729       G_UNLOCK (g_tree_global);
730    }
731   else if (cmp < 0)
732     {
733       if (node->left)
734         {
735           old_balance = node->left->balance;
736           node->left = g_tree_node_remove (tree, node->left, key, notify);
737           node = g_tree_node_restore_left_balance (node, old_balance);
738         }
739     }
740   else if (cmp > 0)
741     {
742       if (node->right)
743         {
744           old_balance = node->right->balance;
745           node->right = g_tree_node_remove (tree, node->right, key, notify);
746           node = g_tree_node_restore_right_balance (node, old_balance);
747         }
748     }
749
750   return node;
751 }
752
753 static GTreeNode*
754 g_tree_node_balance (GTreeNode *node)
755 {
756   if (node->balance < -1)
757     {
758       if (node->left->balance > 0)
759         node->left = g_tree_node_rotate_left (node->left);
760       node = g_tree_node_rotate_right (node);
761     }
762   else if (node->balance > 1)
763     {
764       if (node->right->balance < 0)
765         node->right = g_tree_node_rotate_right (node->right);
766       node = g_tree_node_rotate_left (node);
767     }
768
769   return node;
770 }
771
772 static GTreeNode*
773 g_tree_node_remove_leftmost (GTreeNode  *node,
774                              GTreeNode **leftmost)
775 {
776   gint old_balance;
777
778   if (!node->left)
779     {
780       *leftmost = node;
781       return node->right;
782     }
783
784   old_balance = node->left->balance;
785   node->left = g_tree_node_remove_leftmost (node->left, leftmost);
786   return g_tree_node_restore_left_balance (node, old_balance);
787 }
788
789 static GTreeNode*
790 g_tree_node_restore_left_balance (GTreeNode *node,
791                                   gint       old_balance)
792 {
793   if (!node->left)
794     node->balance += 1;
795   else if ((node->left->balance != old_balance) &&
796            (node->left->balance == 0))
797     node->balance += 1;
798
799   if (node->balance > 1)
800     return g_tree_node_balance (node);
801   return node;
802 }
803
804 static GTreeNode*
805 g_tree_node_restore_right_balance (GTreeNode *node,
806                                    gint       old_balance)
807 {
808   if (!node->right)
809     node->balance -= 1;
810   else if ((node->right->balance != old_balance) &&
811            (node->right->balance == 0))
812     node->balance -= 1;
813
814   if (node->balance < -1)
815     return g_tree_node_balance (node);
816   return node;
817 }
818
819 static GTreeNode *
820 g_tree_node_lookup (GTreeNode        *node,
821                     GCompareDataFunc  compare,
822                     gpointer          compare_data,
823                     gconstpointer     key)
824 {
825   gint cmp;
826
827   if (!node)
828     return NULL;
829
830   cmp = (* compare) (key, node->key, compare_data);
831   if (cmp == 0)
832     return node;
833
834   if (cmp < 0)
835     {
836       if (node->left)
837         return g_tree_node_lookup (node->left, compare, compare_data, key);
838     }
839   else if (cmp > 0)
840     {
841       if (node->right)
842         return g_tree_node_lookup (node->right, compare, compare_data, key);
843     }
844
845   return NULL;
846 }
847
848 static gint
849 g_tree_node_count (GTreeNode *node)
850 {
851   gint count;
852
853   count = 1;
854   if (node->left)
855     count += g_tree_node_count (node->left);
856   if (node->right)
857     count += g_tree_node_count (node->right);
858
859   return count;
860 }
861
862 static gint
863 g_tree_node_pre_order (GTreeNode     *node,
864                        GTraverseFunc  traverse_func,
865                        gpointer       data)
866 {
867   if ((*traverse_func) (node->key, node->value, data))
868     return TRUE;
869   if (node->left)
870     {
871       if (g_tree_node_pre_order (node->left, traverse_func, data))
872         return TRUE;
873     }
874   if (node->right)
875     {
876       if (g_tree_node_pre_order (node->right, traverse_func, data))
877         return TRUE;
878     }
879
880   return FALSE;
881 }
882
883 static gint
884 g_tree_node_in_order (GTreeNode     *node,
885                       GTraverseFunc  traverse_func,
886                       gpointer       data)
887 {
888   if (node->left)
889     {
890       if (g_tree_node_in_order (node->left, traverse_func, data))
891         return TRUE;
892     }
893   if ((*traverse_func) (node->key, node->value, data))
894     return TRUE;
895   if (node->right)
896     {
897       if (g_tree_node_in_order (node->right, traverse_func, data))
898         return TRUE;
899     }
900
901   return FALSE;
902 }
903
904 static gint
905 g_tree_node_post_order (GTreeNode     *node,
906                         GTraverseFunc  traverse_func,
907                         gpointer       data)
908 {
909   if (node->left)
910     {
911       if (g_tree_node_post_order (node->left, traverse_func, data))
912         return TRUE;
913     }
914   if (node->right)
915     {
916       if (g_tree_node_post_order (node->right, traverse_func, data))
917         return TRUE;
918     }
919   if ((*traverse_func) (node->key, node->value, data))
920     return TRUE;
921
922   return FALSE;
923 }
924
925 static gpointer
926 g_tree_node_search (GTreeNode     *node,
927                     GCompareFunc   search_func,
928                     gconstpointer  data)
929 {
930   gint dir;
931
932   if (!node)
933     return NULL;
934
935   do {
936     dir = (* search_func) (node->key, data);
937     if (dir == 0)
938       return node->value;
939
940     if (dir < 0)
941       node = node->left;
942     else if (dir > 0)
943       node = node->right;
944   } while (node);
945
946   return NULL;
947 }
948
949 static gint
950 g_tree_node_height (GTreeNode *node)
951 {
952   gint left_height;
953   gint right_height;
954
955   if (node)
956     {
957       left_height = 0;
958       right_height = 0;
959
960       if (node->left)
961         left_height = g_tree_node_height (node->left);
962
963       if (node->right)
964         right_height = g_tree_node_height (node->right);
965
966       return MAX (left_height, right_height) + 1;
967     }
968
969   return 0;
970 }
971
972 static GTreeNode*
973 g_tree_node_rotate_left (GTreeNode *node)
974 {
975   GTreeNode *right;
976   gint a_bal;
977   gint b_bal;
978
979   right = node->right;
980
981   node->right = right->left;
982   right->left = node;
983
984   a_bal = node->balance;
985   b_bal = right->balance;
986
987   if (b_bal <= 0)
988     {
989       if (a_bal >= 1)
990         right->balance = b_bal - 1;
991       else
992         right->balance = a_bal + b_bal - 2;
993       node->balance = a_bal - 1;
994     }
995   else
996     {
997       if (a_bal <= b_bal)
998         right->balance = a_bal - 2;
999       else
1000         right->balance = b_bal - 1;
1001       node->balance = a_bal - b_bal - 1;
1002     }
1003
1004   return right;
1005 }
1006
1007 static GTreeNode*
1008 g_tree_node_rotate_right (GTreeNode *node)
1009 {
1010   GTreeNode *left;
1011   gint a_bal;
1012   gint b_bal;
1013
1014   left = node->left;
1015
1016   node->left = left->right;
1017   left->right = node;
1018
1019   a_bal = node->balance;
1020   b_bal = left->balance;
1021
1022   if (b_bal <= 0)
1023     {
1024       if (b_bal > a_bal)
1025         left->balance = b_bal + 1;
1026       else
1027         left->balance = a_bal + 2;
1028       node->balance = a_bal - b_bal + 1;
1029     }
1030   else
1031     {
1032       if (a_bal <= -1)
1033         left->balance = b_bal + 1;
1034       else
1035         left->balance = a_bal + b_bal + 2;
1036       node->balance = a_bal + 1;
1037     }
1038
1039   return left;
1040 }
1041
1042 static void
1043 g_tree_node_check (GTreeNode *node)
1044 {
1045   gint left_height;
1046   gint right_height;
1047   gint balance;
1048   
1049   if (node)
1050     {
1051       left_height = 0;
1052       right_height = 0;
1053       
1054       if (node->left)
1055         left_height = g_tree_node_height (node->left);
1056       if (node->right)
1057         right_height = g_tree_node_height (node->right);
1058       
1059       balance = right_height - left_height;
1060       if (balance != node->balance)
1061         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO,
1062                "g_tree_node_check: failed: %d ( %d )\n",
1063                balance, node->balance);
1064       
1065       if (node->left)
1066         g_tree_node_check (node->left);
1067       if (node->right)
1068         g_tree_node_check (node->right);
1069     }
1070 }