* config.sub: Add cases for the Renesas m32c. (This patch has been
[official-gcc.git] / libiberty / splay-tree.c
blob311b49edffbcf011f88993fd7d553fdd80a19e35
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)
10 any later version.
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, 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, 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. */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
35 #include <stdio.h>
37 #include "libiberty.h"
38 #include "splay-tree.h"
40 static void splay_tree_delete_helper (splay_tree, splay_tree_node);
41 static void splay_tree_splay (splay_tree, splay_tree_key);
42 static splay_tree_node splay_tree_splay_helper (splay_tree,
43 splay_tree_key,
44 splay_tree_node*,
45 splay_tree_node*,
46 splay_tree_node*);
47 static int splay_tree_foreach_helper (splay_tree, splay_tree_node,
48 splay_tree_foreach_fn, void*);
50 /* Deallocate NODE (a member of SP), and all its sub-trees. */
52 static void
53 splay_tree_delete_helper (splay_tree sp, splay_tree_node node)
55 splay_tree_node pending = 0;
56 splay_tree_node active = 0;
58 if (!node)
59 return;
61 #define KDEL(x) if (sp->delete_key) (*sp->delete_key)(x);
62 #define VDEL(x) if (sp->delete_value) (*sp->delete_value)(x);
64 KDEL (node->key);
65 VDEL (node->value);
67 /* We use the "key" field to hold the "next" pointer. */
68 node->key = (splay_tree_key)pending;
69 pending = (splay_tree_node)node;
71 /* Now, keep processing the pending list until there aren't any
72 more. This is a little more complicated than just recursing, but
73 it doesn't toast the stack for large trees. */
75 while (pending)
77 active = pending;
78 pending = 0;
79 while (active)
81 splay_tree_node temp;
83 /* active points to a node which has its key and value
84 deallocated, we just need to process left and right. */
86 if (active->left)
88 KDEL (active->left->key);
89 VDEL (active->left->value);
90 active->left->key = (splay_tree_key)pending;
91 pending = (splay_tree_node)(active->left);
93 if (active->right)
95 KDEL (active->right->key);
96 VDEL (active->right->value);
97 active->right->key = (splay_tree_key)pending;
98 pending = (splay_tree_node)(active->right);
101 temp = active;
102 active = (splay_tree_node)(temp->key);
103 (*sp->deallocate) ((char*) temp, sp->allocate_data);
106 #undef KDEL
107 #undef VDEL
110 /* Help splay SP around KEY. PARENT and GRANDPARENT are the parent
111 and grandparent, respectively, of NODE. */
113 static splay_tree_node
114 splay_tree_splay_helper (splay_tree sp, splay_tree_key key,
115 splay_tree_node *node, splay_tree_node *parent,
116 splay_tree_node *grandparent)
118 splay_tree_node *next;
119 splay_tree_node n;
120 int comparison;
122 n = *node;
124 if (!n)
125 return *parent;
127 comparison = (*sp->comp) (key, n->key);
129 if (comparison == 0)
130 /* We've found the target. */
131 next = 0;
132 else if (comparison < 0)
133 /* The target is to the left. */
134 next = &n->left;
135 else
136 /* The target is to the right. */
137 next = &n->right;
139 if (next)
141 /* Continue down the tree. */
142 n = splay_tree_splay_helper (sp, key, next, node, parent);
144 /* The recursive call will change the place to which NODE
145 points. */
146 if (*node != n)
147 return n;
150 if (!parent)
151 /* NODE is the root. We are done. */
152 return n;
154 /* First, handle the case where there is no grandparent (i.e.,
155 *PARENT is the root of the tree.) */
156 if (!grandparent)
158 if (n == (*parent)->left)
160 *node = n->right;
161 n->right = *parent;
163 else
165 *node = n->left;
166 n->left = *parent;
168 *parent = n;
169 return n;
172 /* Next handle the cases where both N and *PARENT are left children,
173 or where both are right children. */
174 if (n == (*parent)->left && *parent == (*grandparent)->left)
176 splay_tree_node p = *parent;
178 (*grandparent)->left = p->right;
179 p->right = *grandparent;
180 p->left = n->right;
181 n->right = p;
182 *grandparent = n;
183 return n;
185 else if (n == (*parent)->right && *parent == (*grandparent)->right)
187 splay_tree_node p = *parent;
189 (*grandparent)->right = p->left;
190 p->left = *grandparent;
191 p->right = n->left;
192 n->left = p;
193 *grandparent = n;
194 return n;
197 /* Finally, deal with the case where N is a left child, but *PARENT
198 is a right child, or vice versa. */
199 if (n == (*parent)->left)
201 (*parent)->left = n->right;
202 n->right = *parent;
203 (*grandparent)->right = n->left;
204 n->left = *grandparent;
205 *grandparent = n;
206 return n;
208 else
210 (*parent)->right = n->left;
211 n->left = *parent;
212 (*grandparent)->left = n->right;
213 n->right = *grandparent;
214 *grandparent = n;
215 return n;
219 /* Splay SP around KEY. */
221 static void
222 splay_tree_splay (splay_tree sp, splay_tree_key key)
224 if (sp->root == 0)
225 return;
227 splay_tree_splay_helper (sp, key, &sp->root,
228 /*grandparent=*/0, /*parent=*/0);
231 /* Call FN, passing it the DATA, for every node below NODE, all of
232 which are from SP, following an in-order traversal. If FN every
233 returns a non-zero value, the iteration ceases immediately, and the
234 value is returned. Otherwise, this function returns 0. */
236 static int
237 splay_tree_foreach_helper (splay_tree sp, splay_tree_node node,
238 splay_tree_foreach_fn fn, void *data)
240 int val;
242 if (!node)
243 return 0;
245 val = splay_tree_foreach_helper (sp, node->left, fn, data);
246 if (val)
247 return val;
249 val = (*fn)(node, data);
250 if (val)
251 return val;
253 return splay_tree_foreach_helper (sp, node->right, fn, data);
257 /* An allocator and deallocator based on xmalloc. */
258 static void *
259 splay_tree_xmalloc_allocate (int size, void *data ATTRIBUTE_UNUSED)
261 return (void *) xmalloc (size);
264 static void
265 splay_tree_xmalloc_deallocate (void *object, void *data ATTRIBUTE_UNUSED)
267 free (object);
271 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
272 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
273 values. Use xmalloc to allocate the splay tree structure, and any
274 nodes added. */
276 splay_tree
277 splay_tree_new (splay_tree_compare_fn compare_fn,
278 splay_tree_delete_key_fn delete_key_fn,
279 splay_tree_delete_value_fn delete_value_fn)
281 return (splay_tree_new_with_allocator
282 (compare_fn, delete_key_fn, delete_value_fn,
283 splay_tree_xmalloc_allocate, splay_tree_xmalloc_deallocate, 0));
287 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
288 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
289 values. */
291 splay_tree
292 splay_tree_new_with_allocator (splay_tree_compare_fn compare_fn,
293 splay_tree_delete_key_fn delete_key_fn,
294 splay_tree_delete_value_fn delete_value_fn,
295 splay_tree_allocate_fn allocate_fn,
296 splay_tree_deallocate_fn deallocate_fn,
297 void *allocate_data)
299 splay_tree sp = (splay_tree) (*allocate_fn) (sizeof (struct splay_tree_s),
300 allocate_data);
301 sp->root = 0;
302 sp->comp = compare_fn;
303 sp->delete_key = delete_key_fn;
304 sp->delete_value = delete_value_fn;
305 sp->allocate = allocate_fn;
306 sp->deallocate = deallocate_fn;
307 sp->allocate_data = allocate_data;
309 return sp;
312 /* Deallocate SP. */
314 void
315 splay_tree_delete (splay_tree sp)
317 splay_tree_delete_helper (sp, sp->root);
318 (*sp->deallocate) ((char*) sp, sp->allocate_data);
321 /* Insert a new node (associating KEY with DATA) into SP. If a
322 previous node with the indicated KEY exists, its data is replaced
323 with the new value. Returns the new node. */
325 splay_tree_node
326 splay_tree_insert (splay_tree sp, splay_tree_key key, splay_tree_value value)
328 int comparison = 0;
330 splay_tree_splay (sp, key);
332 if (sp->root)
333 comparison = (*sp->comp)(sp->root->key, key);
335 if (sp->root && comparison == 0)
337 /* If the root of the tree already has the indicated KEY, just
338 replace the value with VALUE. */
339 if (sp->delete_value)
340 (*sp->delete_value)(sp->root->value);
341 sp->root->value = value;
343 else
345 /* Create a new node, and insert it at the root. */
346 splay_tree_node node;
348 node = ((splay_tree_node)
349 (*sp->allocate) (sizeof (struct splay_tree_node_s),
350 sp->allocate_data));
351 node->key = key;
352 node->value = value;
354 if (!sp->root)
355 node->left = node->right = 0;
356 else if (comparison < 0)
358 node->left = sp->root;
359 node->right = node->left->right;
360 node->left->right = 0;
362 else
364 node->right = sp->root;
365 node->left = node->right->left;
366 node->right->left = 0;
369 sp->root = node;
372 return sp->root;
375 /* Remove KEY from SP. It is not an error if it did not exist. */
377 void
378 splay_tree_remove (splay_tree sp, splay_tree_key key)
380 splay_tree_splay (sp, key);
382 if (sp->root && (*sp->comp) (sp->root->key, key) == 0)
384 splay_tree_node left, right;
386 left = sp->root->left;
387 right = sp->root->right;
389 /* Delete the root node itself. */
390 if (sp->delete_value)
391 (*sp->delete_value) (sp->root->value);
392 (*sp->deallocate) (sp->root, sp->allocate_data);
394 /* One of the children is now the root. Doesn't matter much
395 which, so long as we preserve the properties of the tree. */
396 if (left)
398 sp->root = left;
400 /* If there was a right child as well, hang it off the
401 right-most leaf of the left child. */
402 if (right)
404 while (left->right)
405 left = left->right;
406 left->right = right;
409 else
410 sp->root = right;
414 /* Lookup KEY in SP, returning VALUE if present, and NULL
415 otherwise. */
417 splay_tree_node
418 splay_tree_lookup (splay_tree sp, splay_tree_key key)
420 splay_tree_splay (sp, key);
422 if (sp->root && (*sp->comp)(sp->root->key, key) == 0)
423 return sp->root;
424 else
425 return 0;
428 /* Return the node in SP with the greatest key. */
430 splay_tree_node
431 splay_tree_max (splay_tree sp)
433 splay_tree_node n = sp->root;
435 if (!n)
436 return NULL;
438 while (n->right)
439 n = n->right;
441 return n;
444 /* Return the node in SP with the smallest key. */
446 splay_tree_node
447 splay_tree_min (splay_tree sp)
449 splay_tree_node n = sp->root;
451 if (!n)
452 return NULL;
454 while (n->left)
455 n = n->left;
457 return n;
460 /* Return the immediate predecessor KEY, or NULL if there is no
461 predecessor. KEY need not be present in the tree. */
463 splay_tree_node
464 splay_tree_predecessor (splay_tree sp, splay_tree_key key)
466 int comparison;
467 splay_tree_node node;
469 /* If the tree is empty, there is certainly no predecessor. */
470 if (!sp->root)
471 return NULL;
473 /* Splay the tree around KEY. That will leave either the KEY
474 itself, its predecessor, or its successor at the root. */
475 splay_tree_splay (sp, key);
476 comparison = (*sp->comp)(sp->root->key, key);
478 /* If the predecessor is at the root, just return it. */
479 if (comparison < 0)
480 return sp->root;
482 /* Otherwise, find the rightmost element of the left subtree. */
483 node = sp->root->left;
484 if (node)
485 while (node->right)
486 node = node->right;
488 return node;
491 /* Return the immediate successor KEY, or NULL if there is no
492 successor. KEY need not be present in the tree. */
494 splay_tree_node
495 splay_tree_successor (splay_tree sp, splay_tree_key key)
497 int comparison;
498 splay_tree_node node;
500 /* If the tree is empty, there is certainly no successor. */
501 if (!sp->root)
502 return NULL;
504 /* Splay the tree around KEY. That will leave either the KEY
505 itself, its predecessor, or its successor at the root. */
506 splay_tree_splay (sp, key);
507 comparison = (*sp->comp)(sp->root->key, key);
509 /* If the successor is at the root, just return it. */
510 if (comparison > 0)
511 return sp->root;
513 /* Otherwise, find the leftmost element of the right subtree. */
514 node = sp->root->right;
515 if (node)
516 while (node->left)
517 node = node->left;
519 return node;
522 /* Call FN, passing it the DATA, for every node in SP, following an
523 in-order traversal. If FN every returns a non-zero value, the
524 iteration ceases immediately, and the value is returned.
525 Otherwise, this function returns 0. */
528 splay_tree_foreach (splay_tree sp, splay_tree_foreach_fn fn, void *data)
530 return splay_tree_foreach_helper (sp, sp->root, fn, data);
533 /* Splay-tree comparison function, treating the keys as ints. */
536 splay_tree_compare_ints (splay_tree_key k1, splay_tree_key k2)
538 if ((int) k1 < (int) k2)
539 return -1;
540 else if ((int) k1 > (int) k2)
541 return 1;
542 else
543 return 0;
546 /* Splay-tree comparison function, treating the keys as pointers. */
549 splay_tree_compare_pointers (splay_tree_key k1, splay_tree_key k2)
551 if ((char*) k1 < (char*) k2)
552 return -1;
553 else if ((char*) k1 > (char*) k2)
554 return 1;
555 else
556 return 0;