* gas/arm/addthumb2err.s: New test file.
[binutils.git] / libiberty / splay-tree.c
blob12bfa8bbdcc97990d837b6e1280007481ea5c3b5
1 /* A splay-tree datatype.
2 Copyright (C) 1998, 1999, 2000, 2001, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4 Contributed by Mark Mitchell (mark@markmitchell.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* For an easily readable description of splay-trees, see:
25 Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
26 Algorithms. Harper-Collins, Inc. 1991. */
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
36 #include <stdio.h>
38 #include "libiberty.h"
39 #include "splay-tree.h"
41 static void splay_tree_delete_helper (splay_tree, splay_tree_node);
42 static inline void rotate_left (splay_tree_node *,
43 splay_tree_node, splay_tree_node);
44 static inline void rotate_right (splay_tree_node *,
45 splay_tree_node, splay_tree_node);
46 static void splay_tree_splay (splay_tree, splay_tree_key);
47 static int splay_tree_foreach_helper (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 /* Rotate the edge joining the left child N with its parent P. PP is the
111 grandparents' pointer to P. */
113 static inline void
114 rotate_left (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
116 splay_tree_node tmp;
117 tmp = n->right;
118 n->right = p;
119 p->left = tmp;
120 *pp = n;
123 /* Rotate the edge joining the right child N with its parent P. PP is the
124 grandparents' pointer to P. */
126 static inline void
127 rotate_right (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
129 splay_tree_node tmp;
130 tmp = n->left;
131 n->left = p;
132 p->right = tmp;
133 *pp = n;
136 /* Bottom up splay of key. */
138 static void
139 splay_tree_splay (splay_tree sp, splay_tree_key key)
141 if (sp->root == 0)
142 return;
144 do {
145 int cmp1, cmp2;
146 splay_tree_node n, c;
148 n = sp->root;
149 cmp1 = (*sp->comp) (key, n->key);
151 /* Found. */
152 if (cmp1 == 0)
153 return;
155 /* Left or right? If no child, then we're done. */
156 if (cmp1 < 0)
157 c = n->left;
158 else
159 c = n->right;
160 if (!c)
161 return;
163 /* Next one left or right? If found or no child, we're done
164 after one rotation. */
165 cmp2 = (*sp->comp) (key, c->key);
166 if (cmp2 == 0
167 || (cmp2 < 0 && !c->left)
168 || (cmp2 > 0 && !c->right))
170 if (cmp1 < 0)
171 rotate_left (&sp->root, n, c);
172 else
173 rotate_right (&sp->root, n, c);
174 return;
177 /* Now we have the four cases of double-rotation. */
178 if (cmp1 < 0 && cmp2 < 0)
180 rotate_left (&n->left, c, c->left);
181 rotate_left (&sp->root, n, n->left);
183 else if (cmp1 > 0 && cmp2 > 0)
185 rotate_right (&n->right, c, c->right);
186 rotate_right (&sp->root, n, n->right);
188 else if (cmp1 < 0 && cmp2 > 0)
190 rotate_right (&n->left, c, c->right);
191 rotate_left (&sp->root, n, n->left);
193 else if (cmp1 > 0 && cmp2 < 0)
195 rotate_left (&n->right, c, c->left);
196 rotate_right (&sp->root, n, n->right);
198 } while (1);
201 /* Call FN, passing it the DATA, for every node below NODE, all of
202 which are from SP, following an in-order traversal. If FN every
203 returns a non-zero value, the iteration ceases immediately, and the
204 value is returned. Otherwise, this function returns 0. */
206 static int
207 splay_tree_foreach_helper (splay_tree_node node,
208 splay_tree_foreach_fn fn, void *data)
210 int val;
211 splay_tree_node *stack;
212 int stack_ptr, stack_size;
214 /* A non-recursive implementation is used to avoid filling the stack
215 for large trees. Splay trees are worst case O(n) in the depth of
216 the tree. */
218 #define INITIAL_STACK_SIZE 100
219 stack_size = INITIAL_STACK_SIZE;
220 stack_ptr = 0;
221 stack = XNEWVEC (splay_tree_node, stack_size);
222 val = 0;
224 for (;;)
226 while (node != NULL)
228 if (stack_ptr == stack_size)
230 stack_size *= 2;
231 stack = XRESIZEVEC (splay_tree_node, stack, stack_size);
233 stack[stack_ptr++] = node;
234 node = node->left;
237 if (stack_ptr == 0)
238 break;
240 node = stack[--stack_ptr];
242 val = (*fn) (node, data);
243 if (val)
244 break;
246 node = node->right;
249 XDELETEVEC (stack);
250 return val;
253 /* An allocator and deallocator based on xmalloc. */
254 static void *
255 splay_tree_xmalloc_allocate (int size, void *data ATTRIBUTE_UNUSED)
257 return (void *) xmalloc (size);
260 static void
261 splay_tree_xmalloc_deallocate (void *object, void *data ATTRIBUTE_UNUSED)
263 free (object);
267 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
268 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
269 values. Use xmalloc to allocate the splay tree structure, and any
270 nodes added. */
272 splay_tree
273 splay_tree_new (splay_tree_compare_fn compare_fn,
274 splay_tree_delete_key_fn delete_key_fn,
275 splay_tree_delete_value_fn delete_value_fn)
277 return (splay_tree_new_with_allocator
278 (compare_fn, delete_key_fn, delete_value_fn,
279 splay_tree_xmalloc_allocate, splay_tree_xmalloc_deallocate, 0));
283 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
284 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
285 values. */
287 splay_tree
288 splay_tree_new_with_allocator (splay_tree_compare_fn compare_fn,
289 splay_tree_delete_key_fn delete_key_fn,
290 splay_tree_delete_value_fn delete_value_fn,
291 splay_tree_allocate_fn allocate_fn,
292 splay_tree_deallocate_fn deallocate_fn,
293 void *allocate_data)
295 return
296 splay_tree_new_typed_alloc (compare_fn, delete_key_fn, delete_value_fn,
297 allocate_fn, allocate_fn, deallocate_fn,
298 allocate_data);
303 @deftypefn Supplemental splay_tree splay_tree_new_with_typed_alloc @
304 (splay_tree_compare_fn @var{compare_fn}, @
305 splay_tree_delete_key_fn @var{delete_key_fn}, @
306 splay_tree_delete_value_fn @var{delete_value_fn}, @
307 splay_tree_allocate_fn @var{tree_allocate_fn}, @
308 splay_tree_allocate_fn @var{node_allocate_fn}, @
309 splay_tree_deallocate_fn @var{deallocate_fn}, @
310 void * @var{allocate_data})
312 This function creates a splay tree that uses two different allocators
313 @var{tree_allocate_fn} and @var{node_allocate_fn} to use for allocating the
314 tree itself and its nodes respectively. This is useful when variables of
315 different types need to be allocated with different allocators.
317 The splay tree will use @var{compare_fn} to compare nodes,
318 @var{delete_key_fn} to deallocate keys, and @var{delete_value_fn} to
319 deallocate values.
321 @end deftypefn
325 splay_tree
326 splay_tree_new_typed_alloc (splay_tree_compare_fn compare_fn,
327 splay_tree_delete_key_fn delete_key_fn,
328 splay_tree_delete_value_fn delete_value_fn,
329 splay_tree_allocate_fn tree_allocate_fn,
330 splay_tree_allocate_fn node_allocate_fn,
331 splay_tree_deallocate_fn deallocate_fn,
332 void * allocate_data)
334 splay_tree sp = (splay_tree) (*tree_allocate_fn)
335 (sizeof (struct splay_tree_s), allocate_data);
337 sp->root = 0;
338 sp->comp = compare_fn;
339 sp->delete_key = delete_key_fn;
340 sp->delete_value = delete_value_fn;
341 sp->allocate = node_allocate_fn;
342 sp->deallocate = deallocate_fn;
343 sp->allocate_data = allocate_data;
345 return sp;
348 /* Deallocate SP. */
350 void
351 splay_tree_delete (splay_tree sp)
353 splay_tree_delete_helper (sp, sp->root);
354 (*sp->deallocate) ((char*) sp, sp->allocate_data);
357 /* Insert a new node (associating KEY with DATA) into SP. If a
358 previous node with the indicated KEY exists, its data is replaced
359 with the new value. Returns the new node. */
361 splay_tree_node
362 splay_tree_insert (splay_tree sp, splay_tree_key key, splay_tree_value value)
364 int comparison = 0;
366 splay_tree_splay (sp, key);
368 if (sp->root)
369 comparison = (*sp->comp)(sp->root->key, key);
371 if (sp->root && comparison == 0)
373 /* If the root of the tree already has the indicated KEY, just
374 replace the value with VALUE. */
375 if (sp->delete_value)
376 (*sp->delete_value)(sp->root->value);
377 sp->root->value = value;
379 else
381 /* Create a new node, and insert it at the root. */
382 splay_tree_node node;
384 node = ((splay_tree_node)
385 (*sp->allocate) (sizeof (struct splay_tree_node_s),
386 sp->allocate_data));
387 node->key = key;
388 node->value = value;
390 if (!sp->root)
391 node->left = node->right = 0;
392 else if (comparison < 0)
394 node->left = sp->root;
395 node->right = node->left->right;
396 node->left->right = 0;
398 else
400 node->right = sp->root;
401 node->left = node->right->left;
402 node->right->left = 0;
405 sp->root = node;
408 return sp->root;
411 /* Remove KEY from SP. It is not an error if it did not exist. */
413 void
414 splay_tree_remove (splay_tree sp, splay_tree_key key)
416 splay_tree_splay (sp, key);
418 if (sp->root && (*sp->comp) (sp->root->key, key) == 0)
420 splay_tree_node left, right;
422 left = sp->root->left;
423 right = sp->root->right;
425 /* Delete the root node itself. */
426 if (sp->delete_value)
427 (*sp->delete_value) (sp->root->value);
428 (*sp->deallocate) (sp->root, sp->allocate_data);
430 /* One of the children is now the root. Doesn't matter much
431 which, so long as we preserve the properties of the tree. */
432 if (left)
434 sp->root = left;
436 /* If there was a right child as well, hang it off the
437 right-most leaf of the left child. */
438 if (right)
440 while (left->right)
441 left = left->right;
442 left->right = right;
445 else
446 sp->root = right;
450 /* Lookup KEY in SP, returning VALUE if present, and NULL
451 otherwise. */
453 splay_tree_node
454 splay_tree_lookup (splay_tree sp, splay_tree_key key)
456 splay_tree_splay (sp, key);
458 if (sp->root && (*sp->comp)(sp->root->key, key) == 0)
459 return sp->root;
460 else
461 return 0;
464 /* Return the node in SP with the greatest key. */
466 splay_tree_node
467 splay_tree_max (splay_tree sp)
469 splay_tree_node n = sp->root;
471 if (!n)
472 return NULL;
474 while (n->right)
475 n = n->right;
477 return n;
480 /* Return the node in SP with the smallest key. */
482 splay_tree_node
483 splay_tree_min (splay_tree sp)
485 splay_tree_node n = sp->root;
487 if (!n)
488 return NULL;
490 while (n->left)
491 n = n->left;
493 return n;
496 /* Return the immediate predecessor KEY, or NULL if there is no
497 predecessor. KEY need not be present in the tree. */
499 splay_tree_node
500 splay_tree_predecessor (splay_tree sp, splay_tree_key key)
502 int comparison;
503 splay_tree_node node;
505 /* If the tree is empty, there is certainly no predecessor. */
506 if (!sp->root)
507 return NULL;
509 /* Splay the tree around KEY. That will leave either the KEY
510 itself, its predecessor, or its successor at the root. */
511 splay_tree_splay (sp, key);
512 comparison = (*sp->comp)(sp->root->key, key);
514 /* If the predecessor is at the root, just return it. */
515 if (comparison < 0)
516 return sp->root;
518 /* Otherwise, find the rightmost element of the left subtree. */
519 node = sp->root->left;
520 if (node)
521 while (node->right)
522 node = node->right;
524 return node;
527 /* Return the immediate successor KEY, or NULL if there is no
528 successor. KEY need not be present in the tree. */
530 splay_tree_node
531 splay_tree_successor (splay_tree sp, splay_tree_key key)
533 int comparison;
534 splay_tree_node node;
536 /* If the tree is empty, there is certainly no successor. */
537 if (!sp->root)
538 return NULL;
540 /* Splay the tree around KEY. That will leave either the KEY
541 itself, its predecessor, or its successor at the root. */
542 splay_tree_splay (sp, key);
543 comparison = (*sp->comp)(sp->root->key, key);
545 /* If the successor is at the root, just return it. */
546 if (comparison > 0)
547 return sp->root;
549 /* Otherwise, find the leftmost element of the right subtree. */
550 node = sp->root->right;
551 if (node)
552 while (node->left)
553 node = node->left;
555 return node;
558 /* Call FN, passing it the DATA, for every node in SP, following an
559 in-order traversal. If FN every returns a non-zero value, the
560 iteration ceases immediately, and the value is returned.
561 Otherwise, this function returns 0. */
564 splay_tree_foreach (splay_tree sp, splay_tree_foreach_fn fn, void *data)
566 return splay_tree_foreach_helper (sp->root, fn, data);
569 /* Splay-tree comparison function, treating the keys as ints. */
572 splay_tree_compare_ints (splay_tree_key k1, splay_tree_key k2)
574 if ((int) k1 < (int) k2)
575 return -1;
576 else if ((int) k1 > (int) k2)
577 return 1;
578 else
579 return 0;
582 /* Splay-tree comparison function, treating the keys as pointers. */
585 splay_tree_compare_pointers (splay_tree_key k1, splay_tree_key k2)
587 if ((char*) k1 < (char*) k2)
588 return -1;
589 else if ((char*) k1 > (char*) k2)
590 return 1;
591 else
592 return 0;