1 /* A splay-tree datatype.
2 Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Mark Mitchell (mark@markmitchell.com).
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* For an easily readable description of splay-trees, see:
24 Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
25 Algorithms. Harper-Collins, Inc. 1991. */
35 #include "libiberty.h"
36 #include "splay-tree.h"
38 static void splay_tree_delete_helper PARAMS((splay_tree,
40 static void splay_tree_splay PARAMS((splay_tree,
42 static splay_tree_node splay_tree_splay_helper
48 static int splay_tree_foreach_helper PARAMS((splay_tree,
50 splay_tree_foreach_fn,
53 /* Deallocate NODE (a member of SP), and all its sub-trees. */
56 splay_tree_delete_helper (sp, node)
63 splay_tree_delete_helper (sp, node->left);
64 splay_tree_delete_helper (sp, node->right);
67 (*sp->delete_key)(node->key);
69 (*sp->delete_value)(node->value);
74 /* Help splay SP around KEY. PARENT and GRANDPARENT are the parent
75 and grandparent, respectively, of NODE. */
77 static splay_tree_node
78 splay_tree_splay_helper (sp, key, node, parent, grandparent)
81 splay_tree_node *node;
82 splay_tree_node *parent;
83 splay_tree_node *grandparent;
85 splay_tree_node *next;
94 comparison = (*sp->comp) (key, n->key);
97 /* We've found the target. */
99 else if (comparison < 0)
100 /* The target is to the left. */
103 /* The target is to the right. */
108 /* Continue down the tree. */
109 n = splay_tree_splay_helper (sp, key, next, node, parent);
111 /* The recursive call will change the place to which NODE
118 /* NODE is the root. We are done. */
121 /* First, handle the case where there is no grandparent (i.e.,
122 *PARENT is the root of the tree.) */
125 if (n == (*parent)->left)
139 /* Next handle the cases where both N and *PARENT are left children,
140 or where both are right children. */
141 if (n == (*parent)->left && *parent == (*grandparent)->left)
143 splay_tree_node p = *parent;
145 (*grandparent)->left = p->right;
146 p->right = *grandparent;
152 else if (n == (*parent)->right && *parent == (*grandparent)->right)
154 splay_tree_node p = *parent;
156 (*grandparent)->right = p->left;
157 p->left = *grandparent;
164 /* Finally, deal with the case where N is a left child, but *PARENT
165 is a right child, or vice versa. */
166 if (n == (*parent)->left)
168 (*parent)->left = n->right;
170 (*grandparent)->right = n->left;
171 n->left = *grandparent;
177 (*parent)->right = n->left;
179 (*grandparent)->left = n->right;
180 n->right = *grandparent;
186 /* Splay SP around KEY. */
189 splay_tree_splay (sp, key)
196 splay_tree_splay_helper (sp, key, &sp->root,
197 /*grandparent=*/0, /*parent=*/0);
200 /* Call FN, passing it the DATA, for every node below NODE, all of
201 which are from SP, following an in-order traversal. If FN every
202 returns a non-zero value, the iteration ceases immediately, and the
203 value is returned. Otherwise, this function returns 0. */
206 splay_tree_foreach_helper (sp, node, fn, data)
208 splay_tree_node node;
209 splay_tree_foreach_fn fn;
217 val = splay_tree_foreach_helper (sp, node->left, fn, data);
221 val = (*fn)(node, data);
225 return splay_tree_foreach_helper (sp, node->right, fn, data);
228 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
229 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
233 splay_tree_new (compare_fn, delete_key_fn, delete_value_fn)
234 splay_tree_compare_fn compare_fn;
235 splay_tree_delete_key_fn delete_key_fn;
236 splay_tree_delete_value_fn delete_value_fn;
238 splay_tree sp = (splay_tree) xmalloc (sizeof (struct splay_tree_s));
240 sp->comp = compare_fn;
241 sp->delete_key = delete_key_fn;
242 sp->delete_value = delete_value_fn;
250 splay_tree_delete (sp)
253 splay_tree_delete_helper (sp, sp->root);
257 /* Insert a new node (associating KEY with DATA) into SP. If a
258 previous node with the indicated KEY exists, its data is replaced
259 with the new value. Returns the new node. */
262 splay_tree_insert (sp, key, value)
265 splay_tree_value value;
269 splay_tree_splay (sp, key);
272 comparison = (*sp->comp)(sp->root->key, key);
274 if (sp->root && comparison == 0)
276 /* If the root of the tree already has the indicated KEY, just
277 replace the value with VALUE. */
278 if (sp->delete_value)
279 (*sp->delete_value)(sp->root->value);
280 sp->root->value = value;
284 /* Create a new node, and insert it at the root. */
285 splay_tree_node node;
287 node = (splay_tree_node) xmalloc (sizeof (struct splay_tree_node_s));
292 node->left = node->right = 0;
293 else if (comparison < 0)
295 node->left = sp->root;
296 node->right = node->left->right;
297 node->left->right = 0;
301 node->right = sp->root;
302 node->left = node->right->left;
303 node->right->left = 0;
312 /* Remove KEY from SP. It is not an error if it did not exist. */
315 splay_tree_remove (sp, key)
319 splay_tree_splay (sp, key);
321 if (sp->root && (*sp->comp) (sp->root->key, key) == 0)
323 splay_tree_node left, right;
325 left = sp->root->left;
326 right = sp->root->right;
328 /* Delete the root node itself. */
329 if (sp->delete_value)
330 (*sp->delete_value) (sp->root->value);
333 /* One of the children is now the root. Doesn't matter much
334 which, so long as we preserve the properties of the tree. */
339 /* If there was a right child as well, hang it off the
340 right-most leaf of the left child. */
353 /* Lookup KEY in SP, returning VALUE if present, and NULL
357 splay_tree_lookup (sp, key)
361 splay_tree_splay (sp, key);
363 if (sp->root && (*sp->comp)(sp->root->key, key) == 0)
369 /* Call FN, passing it the DATA, for every node in SP, following an
370 in-order traversal. If FN every returns a non-zero value, the
371 iteration ceases immediately, and the value is returned.
372 Otherwise, this function returns 0. */
375 splay_tree_foreach (sp, fn, data)
377 splay_tree_foreach_fn fn;
380 return splay_tree_foreach_helper (sp, sp->root, fn, data);
383 /* Splay-tree comparison function, treating the keys as ints. */
386 splay_tree_compare_ints (k1, k2)
390 if ((int) k1 < (int) k2)
392 else if ((int) k1 > (int) k2)
398 /* Splay-tree comparison function, treating the keys as pointers. */
401 splay_tree_compare_pointers (k1, k2)
405 if ((char*) k1 < (char*) k2)
407 else if ((char*) k1 > (char*) k2)