1 /* A splay-tree datatype.
2 Copyright (C) 1998, 1999, 2000, 2001 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. */
37 #include "libiberty.h"
38 #include "splay-tree.h"
40 static void splay_tree_delete_helper PARAMS((splay_tree,
42 static void splay_tree_splay PARAMS((splay_tree,
44 static splay_tree_node splay_tree_splay_helper
50 static int splay_tree_foreach_helper PARAMS((splay_tree,
52 splay_tree_foreach_fn,
55 /* Deallocate NODE (a member of SP), and all its sub-trees. */
58 splay_tree_delete_helper (sp, node)
65 splay_tree_delete_helper (sp, node->left);
66 splay_tree_delete_helper (sp, node->right);
69 (*sp->delete_key)(node->key);
71 (*sp->delete_value)(node->value);
73 (*sp->deallocate) ((char*) node, sp->allocate_data);
76 /* Help splay SP around KEY. PARENT and GRANDPARENT are the parent
77 and grandparent, respectively, of NODE. */
79 static splay_tree_node
80 splay_tree_splay_helper (sp, key, node, parent, grandparent)
83 splay_tree_node *node;
84 splay_tree_node *parent;
85 splay_tree_node *grandparent;
87 splay_tree_node *next;
96 comparison = (*sp->comp) (key, n->key);
99 /* We've found the target. */
101 else if (comparison < 0)
102 /* The target is to the left. */
105 /* The target is to the right. */
110 /* Continue down the tree. */
111 n = splay_tree_splay_helper (sp, key, next, node, parent);
113 /* The recursive call will change the place to which NODE
120 /* NODE is the root. We are done. */
123 /* First, handle the case where there is no grandparent (i.e.,
124 *PARENT is the root of the tree.) */
127 if (n == (*parent)->left)
141 /* Next handle the cases where both N and *PARENT are left children,
142 or where both are right children. */
143 if (n == (*parent)->left && *parent == (*grandparent)->left)
145 splay_tree_node p = *parent;
147 (*grandparent)->left = p->right;
148 p->right = *grandparent;
154 else if (n == (*parent)->right && *parent == (*grandparent)->right)
156 splay_tree_node p = *parent;
158 (*grandparent)->right = p->left;
159 p->left = *grandparent;
166 /* Finally, deal with the case where N is a left child, but *PARENT
167 is a right child, or vice versa. */
168 if (n == (*parent)->left)
170 (*parent)->left = n->right;
172 (*grandparent)->right = n->left;
173 n->left = *grandparent;
179 (*parent)->right = n->left;
181 (*grandparent)->left = n->right;
182 n->right = *grandparent;
188 /* Splay SP around KEY. */
191 splay_tree_splay (sp, key)
198 splay_tree_splay_helper (sp, key, &sp->root,
199 /*grandparent=*/0, /*parent=*/0);
202 /* Call FN, passing it the DATA, for every node below NODE, all of
203 which are from SP, following an in-order traversal. If FN every
204 returns a non-zero value, the iteration ceases immediately, and the
205 value is returned. Otherwise, this function returns 0. */
208 splay_tree_foreach_helper (sp, node, fn, data)
210 splay_tree_node node;
211 splay_tree_foreach_fn fn;
219 val = splay_tree_foreach_helper (sp, node->left, fn, data);
223 val = (*fn)(node, data);
227 return splay_tree_foreach_helper (sp, node->right, fn, data);
231 /* An allocator and deallocator based on xmalloc. */
233 splay_tree_xmalloc_allocate (size, data)
235 void *data ATTRIBUTE_UNUSED;
237 return (void *) xmalloc (size);
241 splay_tree_xmalloc_deallocate (object, data)
243 void *data ATTRIBUTE_UNUSED;
249 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
250 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
251 values. Use xmalloc to allocate the splay tree structure, and any
255 splay_tree_new (compare_fn, delete_key_fn, delete_value_fn)
256 splay_tree_compare_fn compare_fn;
257 splay_tree_delete_key_fn delete_key_fn;
258 splay_tree_delete_value_fn delete_value_fn;
260 return (splay_tree_new_with_allocator
261 (compare_fn, delete_key_fn, delete_value_fn,
262 splay_tree_xmalloc_allocate, splay_tree_xmalloc_deallocate, 0));
266 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
267 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
271 splay_tree_new_with_allocator (compare_fn, delete_key_fn, delete_value_fn,
272 allocate_fn, deallocate_fn, allocate_data)
273 splay_tree_compare_fn compare_fn;
274 splay_tree_delete_key_fn delete_key_fn;
275 splay_tree_delete_value_fn delete_value_fn;
276 splay_tree_allocate_fn allocate_fn;
277 splay_tree_deallocate_fn deallocate_fn;
280 splay_tree sp = (splay_tree) (*allocate_fn) (sizeof (struct splay_tree_s),
283 sp->comp = compare_fn;
284 sp->delete_key = delete_key_fn;
285 sp->delete_value = delete_value_fn;
286 sp->allocate = allocate_fn;
287 sp->deallocate = deallocate_fn;
288 sp->allocate_data = allocate_data;
296 splay_tree_delete (sp)
299 splay_tree_delete_helper (sp, sp->root);
300 (*sp->deallocate) ((char*) sp, sp->allocate_data);
303 /* Insert a new node (associating KEY with DATA) into SP. If a
304 previous node with the indicated KEY exists, its data is replaced
305 with the new value. Returns the new node. */
308 splay_tree_insert (sp, key, value)
311 splay_tree_value value;
315 splay_tree_splay (sp, key);
318 comparison = (*sp->comp)(sp->root->key, key);
320 if (sp->root && comparison == 0)
322 /* If the root of the tree already has the indicated KEY, just
323 replace the value with VALUE. */
324 if (sp->delete_value)
325 (*sp->delete_value)(sp->root->value);
326 sp->root->value = value;
330 /* Create a new node, and insert it at the root. */
331 splay_tree_node node;
333 node = ((splay_tree_node)
334 (*sp->allocate) (sizeof (struct splay_tree_node_s),
340 node->left = node->right = 0;
341 else if (comparison < 0)
343 node->left = sp->root;
344 node->right = node->left->right;
345 node->left->right = 0;
349 node->right = sp->root;
350 node->left = node->right->left;
351 node->right->left = 0;
360 /* Remove KEY from SP. It is not an error if it did not exist. */
363 splay_tree_remove (sp, key)
367 splay_tree_splay (sp, key);
369 if (sp->root && (*sp->comp) (sp->root->key, key) == 0)
371 splay_tree_node left, right;
373 left = sp->root->left;
374 right = sp->root->right;
376 /* Delete the root node itself. */
377 if (sp->delete_value)
378 (*sp->delete_value) (sp->root->value);
379 (*sp->deallocate) (sp->root, sp->allocate_data);
381 /* One of the children is now the root. Doesn't matter much
382 which, so long as we preserve the properties of the tree. */
387 /* If there was a right child as well, hang it off the
388 right-most leaf of the left child. */
401 /* Lookup KEY in SP, returning VALUE if present, and NULL
405 splay_tree_lookup (sp, key)
409 splay_tree_splay (sp, key);
411 if (sp->root && (*sp->comp)(sp->root->key, key) == 0)
417 /* Return the node in SP with the greatest key. */
423 splay_tree_node n = sp->root;
434 /* Return the node in SP with the smallest key. */
440 splay_tree_node n = sp->root;
451 /* Return the immediate predecessor KEY, or NULL if there is no
452 predecessor. KEY need not be present in the tree. */
455 splay_tree_predecessor (sp, key)
460 splay_tree_node node;
462 /* If the tree is empty, there is certainly no predecessor. */
466 /* Splay the tree around KEY. That will leave either the KEY
467 itself, its predecessor, or its successor at the root. */
468 splay_tree_splay (sp, key);
469 comparison = (*sp->comp)(sp->root->key, key);
471 /* If the predecessor is at the root, just return it. */
475 /* Otherwise, find the rightmost element of the left subtree. */
476 node = sp->root->left;
484 /* Return the immediate successor KEY, or NULL if there is no
485 successor. KEY need not be present in the tree. */
488 splay_tree_successor (sp, key)
493 splay_tree_node node;
495 /* If the tree is empty, there is certainly no successor. */
499 /* Splay the tree around KEY. That will leave either the KEY
500 itself, its predecessor, or its successor at the root. */
501 splay_tree_splay (sp, key);
502 comparison = (*sp->comp)(sp->root->key, key);
504 /* If the successor is at the root, just return it. */
508 /* Otherwise, find the leftmost element of the right subtree. */
509 node = sp->root->right;
517 /* Call FN, passing it the DATA, for every node in SP, following an
518 in-order traversal. If FN every returns a non-zero value, the
519 iteration ceases immediately, and the value is returned.
520 Otherwise, this function returns 0. */
523 splay_tree_foreach (sp, fn, data)
525 splay_tree_foreach_fn fn;
528 return splay_tree_foreach_helper (sp, sp->root, fn, data);
531 /* Splay-tree comparison function, treating the keys as ints. */
534 splay_tree_compare_ints (k1, k2)
538 if ((int) k1 < (int) k2)
540 else if ((int) k1 > (int) k2)
546 /* Splay-tree comparison function, treating the keys as pointers. */
549 splay_tree_compare_pointers (k1, k2)
553 if ((char*) k1 < (char*) k2)
555 else if ((char*) k1 > (char*) k2)