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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
34 typedef struct _GRealTree GRealTree;
35 typedef struct _GTreeNode GTreeNode;
40 GCompareFunc key_compare;
45 gint balance; /* height (left) - height (right) */
46 GTreeNode *left; /* left subtree */
47 GTreeNode *right; /* right subtree */
48 gpointer key; /* key for this node */
49 gpointer value; /* value stored at this node */
53 static GTreeNode* g_tree_node_new (gpointer key,
55 static void g_tree_node_destroy (GTreeNode *node);
56 static GTreeNode* g_tree_node_insert (GTreeNode *node,
61 static GTreeNode* g_tree_node_remove (GTreeNode *node,
64 static GTreeNode* g_tree_node_balance (GTreeNode *node);
65 static GTreeNode* g_tree_node_remove_leftmost (GTreeNode *node,
66 GTreeNode **leftmost);
67 static GTreeNode* g_tree_node_restore_left_balance (GTreeNode *node,
69 static GTreeNode* g_tree_node_restore_right_balance (GTreeNode *node,
71 static gpointer g_tree_node_lookup (GTreeNode *node,
74 static gint g_tree_node_count (GTreeNode *node);
75 static gint g_tree_node_pre_order (GTreeNode *node,
76 GTraverseFunc traverse_func,
78 static gint g_tree_node_in_order (GTreeNode *node,
79 GTraverseFunc traverse_func,
81 static gint g_tree_node_post_order (GTreeNode *node,
82 GTraverseFunc traverse_func,
84 static gpointer g_tree_node_search (GTreeNode *node,
85 GSearchFunc search_func,
87 static gint g_tree_node_height (GTreeNode *node);
88 static GTreeNode* g_tree_node_rotate_left (GTreeNode *node);
89 static GTreeNode* g_tree_node_rotate_right (GTreeNode *node);
90 static void g_tree_node_check (GTreeNode *node);
93 G_LOCK_DEFINE_STATIC (g_tree_global);
94 static GMemChunk *node_mem_chunk = NULL;
95 static GTreeNode *node_free_list = NULL;
99 g_tree_node_new (gpointer key,
104 G_LOCK (g_tree_global);
107 node = node_free_list;
108 node_free_list = node->right;
113 node_mem_chunk = g_mem_chunk_new ("GLib GTreeNode mem chunk",
118 node = g_chunk_new (GTreeNode, node_mem_chunk);
120 G_UNLOCK (g_tree_global);
132 g_tree_node_destroy (GTreeNode *node)
136 g_tree_node_destroy (node->right);
137 g_tree_node_destroy (node->left);
138 G_LOCK (g_tree_global);
139 node->right = node_free_list;
140 node_free_list = node;
141 G_UNLOCK (g_tree_global);
147 g_tree_new (GCompareFunc key_compare_func)
151 g_return_val_if_fail (key_compare_func != NULL, NULL);
153 rtree = g_new (GRealTree, 1);
155 rtree->key_compare = key_compare_func;
157 return (GTree*) rtree;
161 g_tree_destroy (GTree *tree)
165 g_return_if_fail (tree != NULL);
167 rtree = (GRealTree*) tree;
169 g_tree_node_destroy (rtree->root);
174 g_tree_insert (GTree *tree,
181 g_return_if_fail (tree != NULL);
183 rtree = (GRealTree*) tree;
186 rtree->root = g_tree_node_insert (rtree->root, rtree->key_compare,
187 key, value, &inserted);
191 g_tree_remove (GTree *tree,
196 g_return_if_fail (tree != NULL);
198 rtree = (GRealTree*) tree;
200 rtree->root = g_tree_node_remove (rtree->root, rtree->key_compare, key);
204 g_tree_lookup (GTree *tree,
209 g_return_val_if_fail (tree != NULL, NULL);
211 rtree = (GRealTree*) tree;
213 return g_tree_node_lookup (rtree->root, rtree->key_compare, key);
217 g_tree_traverse (GTree *tree,
218 GTraverseFunc traverse_func,
219 GTraverseType traverse_type,
224 g_return_if_fail (tree != NULL);
226 rtree = (GRealTree*) tree;
231 switch (traverse_type)
234 g_tree_node_pre_order (rtree->root, traverse_func, data);
238 g_tree_node_in_order (rtree->root, traverse_func, data);
242 g_tree_node_post_order (rtree->root, traverse_func, data);
246 g_warning ("g_tree_traverse(): traverse type G_LEVEL_ORDER isn't implemented.");
252 g_tree_search (GTree *tree,
253 GSearchFunc search_func,
258 g_return_val_if_fail (tree != NULL, NULL);
260 rtree = (GRealTree*) tree;
263 return g_tree_node_search (rtree->root, search_func, data);
269 g_tree_height (GTree *tree)
273 g_return_val_if_fail (tree != NULL, 0);
275 rtree = (GRealTree*) tree;
278 return g_tree_node_height (rtree->root);
284 g_tree_nnodes (GTree *tree)
288 g_return_val_if_fail (tree != NULL, 0);
290 rtree = (GRealTree*) tree;
293 return g_tree_node_count (rtree->root);
299 g_tree_node_insert (GTreeNode *node,
300 GCompareFunc compare,
311 return g_tree_node_new (key, value);
314 cmp = (* compare) (key, node->key);
326 old_balance = node->left->balance;
327 node->left = g_tree_node_insert (node->left, compare, key, value, inserted);
329 if ((old_balance != node->left->balance) && node->left->balance)
335 node->left = g_tree_node_new (key, value);
343 old_balance = node->right->balance;
344 node->right = g_tree_node_insert (node->right, compare, key, value, inserted);
346 if ((old_balance != node->right->balance) && node->right->balance)
352 node->right = g_tree_node_new (key, value);
359 if ((node->balance < -1) || (node->balance > 1))
360 node = g_tree_node_balance (node);
367 g_tree_node_remove (GTreeNode *node,
368 GCompareFunc compare,
378 cmp = (* compare) (key, node->key);
391 old_balance = node->right->balance;
392 node->right = g_tree_node_remove_leftmost (node->right, &new_root);
393 new_root->left = node->left;
394 new_root->right = node->right;
395 new_root->balance = node->balance;
396 node = g_tree_node_restore_right_balance (new_root, old_balance);
399 G_LOCK (g_tree_global);
400 garbage->right = node_free_list;
401 node_free_list = garbage;
402 G_UNLOCK (g_tree_global);
408 old_balance = node->left->balance;
409 node->left = g_tree_node_remove (node->left, compare, key);
410 node = g_tree_node_restore_left_balance (node, old_balance);
417 old_balance = node->right->balance;
418 node->right = g_tree_node_remove (node->right, compare, key);
419 node = g_tree_node_restore_right_balance (node, old_balance);
427 g_tree_node_balance (GTreeNode *node)
429 if (node->balance < -1)
431 if (node->left->balance > 0)
432 node->left = g_tree_node_rotate_left (node->left);
433 node = g_tree_node_rotate_right (node);
435 else if (node->balance > 1)
437 if (node->right->balance < 0)
438 node->right = g_tree_node_rotate_right (node->right);
439 node = g_tree_node_rotate_left (node);
446 g_tree_node_remove_leftmost (GTreeNode *node,
447 GTreeNode **leftmost)
457 old_balance = node->left->balance;
458 node->left = g_tree_node_remove_leftmost (node->left, leftmost);
459 return g_tree_node_restore_left_balance (node, old_balance);
463 g_tree_node_restore_left_balance (GTreeNode *node,
468 else if ((node->left->balance != old_balance) &&
469 (node->left->balance == 0))
472 if (node->balance > 1)
473 return g_tree_node_balance (node);
478 g_tree_node_restore_right_balance (GTreeNode *node,
483 else if ((node->right->balance != old_balance) &&
484 (node->right->balance == 0))
487 if (node->balance < -1)
488 return g_tree_node_balance (node);
493 g_tree_node_lookup (GTreeNode *node,
494 GCompareFunc compare,
502 cmp = (* compare) (key, node->key);
509 return g_tree_node_lookup (node->left, compare, key);
514 return g_tree_node_lookup (node->right, compare, key);
521 g_tree_node_count (GTreeNode *node)
527 count += g_tree_node_count (node->left);
529 count += g_tree_node_count (node->right);
535 g_tree_node_pre_order (GTreeNode *node,
536 GTraverseFunc traverse_func,
539 if ((*traverse_func) (node->key, node->value, data))
543 if (g_tree_node_pre_order (node->left, traverse_func, data))
548 if (g_tree_node_pre_order (node->right, traverse_func, data))
556 g_tree_node_in_order (GTreeNode *node,
557 GTraverseFunc traverse_func,
562 if (g_tree_node_in_order (node->left, traverse_func, data))
565 if ((*traverse_func) (node->key, node->value, data))
569 if (g_tree_node_in_order (node->right, traverse_func, data))
577 g_tree_node_post_order (GTreeNode *node,
578 GTraverseFunc traverse_func,
583 if (g_tree_node_post_order (node->left, traverse_func, data))
588 if (g_tree_node_post_order (node->right, traverse_func, data))
591 if ((*traverse_func) (node->key, node->value, data))
598 g_tree_node_search (GTreeNode *node,
599 GSearchFunc search_func,
608 dir = (* search_func) (node->key, data);
616 } while (node && (dir != 0));
622 g_tree_node_height (GTreeNode *node)
633 left_height = g_tree_node_height (node->left);
636 right_height = g_tree_node_height (node->right);
638 return MAX (left_height, right_height) + 1;
645 g_tree_node_rotate_left (GTreeNode *node)
655 node->right = right->left;
658 a_bal = node->balance;
659 b_bal = right->balance;
664 right->balance = b_bal - 1;
666 right->balance = a_bal + b_bal - 2;
667 node->balance = a_bal - 1;
672 right->balance = a_bal - 2;
674 right->balance = b_bal - 1;
675 node->balance = a_bal - b_bal - 1;
682 g_tree_node_rotate_right (GTreeNode *node)
692 node->left = left->right;
695 a_bal = node->balance;
696 b_bal = left->balance;
701 left->balance = b_bal + 1;
703 left->balance = a_bal + 2;
704 node->balance = a_bal - b_bal + 1;
709 left->balance = b_bal + 1;
711 left->balance = a_bal + b_bal + 2;
712 node->balance = a_bal + 1;
719 g_tree_node_check (GTreeNode *node)
731 left_height = g_tree_node_height (node->left);
733 right_height = g_tree_node_height (node->right);
735 balance = right_height - left_height;
736 if (balance != node->balance)
737 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
738 "g_tree_node_check: failed: %d ( %d )\n",
739 balance, node->balance);
742 g_tree_node_check (node->left);
744 g_tree_node_check (node->right);