]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/splay-tree.c
re PR debug/43092 (Wrong debuginfo with VTA and -fomit-frame-pointer/-mno-accumulate...
[thirdparty/gcc.git] / libiberty / splay-tree.c
CommitLineData
ed87f9c8 1/* A splay-tree datatype.
a9429e29
LB
2 Copyright (C) 1998, 1999, 2000, 2001, 2009,
3 2010 Free Software Foundation, Inc.
ed87f9c8
MM
4 Contributed by Mark Mitchell (mark@markmitchell.com).
5
28923099 6This file is part of GNU CC.
ed87f9c8 7
28923099
JL
8GNU CC is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
ed87f9c8 12
28923099
JL
13GNU CC is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16General Public License for more details.
ed87f9c8 17
28923099
JL
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
ee58dffd
NC
20the Free Software Foundation, 51 Franklin Street - Fifth Floor,
21Boston, MA 02110-1301, USA. */
ed87f9c8 22
28923099 23/* For an easily readable description of splay-trees, see:
ed87f9c8
MM
24
25 Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
26 Algorithms. Harper-Collins, Inc. 1991. */
27
11a0bb74 28#ifdef HAVE_CONFIG_H
ad3ef78e
MM
29#include "config.h"
30#endif
31
32#ifdef HAVE_STDLIB_H
33#include <stdlib.h>
34#endif
35
4eaa189a
MS
36#include <stdio.h>
37
ed87f9c8 38#include "libiberty.h"
ed87f9c8
MM
39#include "splay-tree.h"
40
885f2199 41static void splay_tree_delete_helper (splay_tree, splay_tree_node);
73a08f87
RG
42static inline void rotate_left (splay_tree_node *,
43 splay_tree_node, splay_tree_node);
44static inline void rotate_right (splay_tree_node *,
45 splay_tree_node, splay_tree_node);
885f2199 46static void splay_tree_splay (splay_tree, splay_tree_key);
23346f36 47static int splay_tree_foreach_helper (splay_tree_node,
885f2199 48 splay_tree_foreach_fn, void*);
ed87f9c8
MM
49
50/* Deallocate NODE (a member of SP), and all its sub-trees. */
51
52static void
885f2199 53splay_tree_delete_helper (splay_tree sp, splay_tree_node node)
ed87f9c8 54{
b180d5fb
DD
55 splay_tree_node pending = 0;
56 splay_tree_node active = 0;
57
ed87f9c8
MM
58 if (!node)
59 return;
60
b180d5fb
DD
61#define KDEL(x) if (sp->delete_key) (*sp->delete_key)(x);
62#define VDEL(x) if (sp->delete_value) (*sp->delete_value)(x);
63
64 KDEL (node->key);
65 VDEL (node->value);
ed87f9c8 66
b180d5fb
DD
67 /* We use the "key" field to hold the "next" pointer. */
68 node->key = (splay_tree_key)pending;
69 pending = (splay_tree_node)node;
ed87f9c8 70
b180d5fb
DD
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. */
74
75 while (pending)
76 {
77 active = pending;
78 pending = 0;
79 while (active)
80 {
81 splay_tree_node temp;
82
83 /* active points to a node which has its key and value
84 deallocated, we just need to process left and right. */
85
86 if (active->left)
87 {
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);
92 }
93 if (active->right)
94 {
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);
99 }
100
101 temp = active;
102 active = (splay_tree_node)(temp->key);
103 (*sp->deallocate) ((char*) temp, sp->allocate_data);
104 }
105 }
106#undef KDEL
107#undef VDEL
ed87f9c8
MM
108}
109
73a08f87 110/* Rotate the edge joining the left child N with its parent P. PP is the
daf6ff4c 111 grandparents' pointer to P. */
ed87f9c8 112
73a08f87
RG
113static inline void
114rotate_left (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
ed87f9c8 115{
73a08f87
RG
116 splay_tree_node tmp;
117 tmp = n->right;
118 n->right = p;
119 p->left = tmp;
120 *pp = n;
121}
ed87f9c8 122
73a08f87 123/* Rotate the edge joining the right child N with its parent P. PP is the
daf6ff4c 124 grandparents' pointer to P. */
ed87f9c8 125
73a08f87
RG
126static inline void
127rotate_right (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
128{
129 splay_tree_node tmp;
130 tmp = n->left;
131 n->left = p;
132 p->right = tmp;
133 *pp = n;
ed87f9c8
MM
134}
135
73a08f87 136/* Bottom up splay of key. */
ed87f9c8
MM
137
138static void
885f2199 139splay_tree_splay (splay_tree sp, splay_tree_key key)
ed87f9c8
MM
140{
141 if (sp->root == 0)
142 return;
143
73a08f87
RG
144 do {
145 int cmp1, cmp2;
146 splay_tree_node n, c;
147
148 n = sp->root;
149 cmp1 = (*sp->comp) (key, n->key);
150
151 /* Found. */
152 if (cmp1 == 0)
153 return;
154
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;
162
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))
169 {
170 if (cmp1 < 0)
171 rotate_left (&sp->root, n, c);
172 else
173 rotate_right (&sp->root, n, c);
174 return;
175 }
176
177 /* Now we have the four cases of double-rotation. */
178 if (cmp1 < 0 && cmp2 < 0)
179 {
180 rotate_left (&n->left, c, c->left);
181 rotate_left (&sp->root, n, n->left);
182 }
183 else if (cmp1 > 0 && cmp2 > 0)
184 {
185 rotate_right (&n->right, c, c->right);
186 rotate_right (&sp->root, n, n->right);
187 }
188 else if (cmp1 < 0 && cmp2 > 0)
189 {
190 rotate_right (&n->left, c, c->right);
191 rotate_left (&sp->root, n, n->left);
192 }
193 else if (cmp1 > 0 && cmp2 < 0)
194 {
195 rotate_left (&n->right, c, c->left);
196 rotate_right (&sp->root, n, n->right);
197 }
198 } while (1);
ed87f9c8
MM
199}
200
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. */
205
b056ad1c 206static int
23346f36 207splay_tree_foreach_helper (splay_tree_node node,
885f2199 208 splay_tree_foreach_fn fn, void *data)
ed87f9c8
MM
209{
210 int val;
23346f36
DE
211 splay_tree_node *stack;
212 int stack_ptr, stack_size;
ed87f9c8 213
23346f36
DE
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. */
217
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;
223
224 for (;;)
225 {
226 while (node != NULL)
227 {
228 if (stack_ptr == stack_size)
229 {
230 stack_size *= 2;
231 stack = XRESIZEVEC (splay_tree_node, stack, stack_size);
232 }
233 stack[stack_ptr++] = node;
234 node = node->left;
235 }
ed87f9c8 236
23346f36
DE
237 if (stack_ptr == 0)
238 break;
ed87f9c8 239
23346f36 240 node = stack[--stack_ptr];
ed87f9c8 241
23346f36
DE
242 val = (*fn) (node, data);
243 if (val)
244 break;
ed87f9c8 245
23346f36
DE
246 node = node->right;
247 }
248
249 XDELETEVEC (stack);
250 return val;
251}
00c2f96f
JB
252
253/* An allocator and deallocator based on xmalloc. */
254static void *
885f2199 255splay_tree_xmalloc_allocate (int size, void *data ATTRIBUTE_UNUSED)
00c2f96f 256{
f08b7eee 257 return (void *) xmalloc (size);
00c2f96f
JB
258}
259
260static void
885f2199 261splay_tree_xmalloc_deallocate (void *object, void *data ATTRIBUTE_UNUSED)
00c2f96f
JB
262{
263 free (object);
264}
265
266
ed87f9c8
MM
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
00c2f96f
JB
269 values. Use xmalloc to allocate the splay tree structure, and any
270 nodes added. */
ed87f9c8
MM
271
272splay_tree
885f2199
GDR
273splay_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)
ed87f9c8 276{
00c2f96f
JB
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));
280}
281
282
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. */
286
287splay_tree
885f2199
GDR
288splay_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)
00c2f96f 294{
a9429e29
LB
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);
299}
300
301/*
302
303@deftypefn Supplemental splay_tree splay_tree_new_with_typed_alloc
304(splay_tree_compare_fn @var{compare_fn},
305splay_tree_delete_key_fn @var{delete_key_fn},
306splay_tree_delete_value_fn @var{delete_value_fn},
307splay_tree_allocate_fn @var{tree_allocate_fn},
308splay_tree_allocate_fn @var{node_allocate_fn},
309splay_tree_deallocate_fn @var{deallocate_fn},
310void * @var{allocate_data})
311
312This 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
314tree itself and its nodes respectively. This is useful when variables of
315different types need to be allocated with different allocators.
316
317The splay tree will use @var{compare_fn} to compare nodes,
318@var{delete_key_fn} to deallocate keys, and @var{delete_value_fn} to
319deallocate values.
320
321@end deftypefn
322
323*/
324
325splay_tree
326splay_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)
333{
334 splay_tree sp = (splay_tree) (*tree_allocate_fn)
335 (sizeof (struct splay_tree_s), allocate_data);
336
ed87f9c8
MM
337 sp->root = 0;
338 sp->comp = compare_fn;
339 sp->delete_key = delete_key_fn;
340 sp->delete_value = delete_value_fn;
a9429e29 341 sp->allocate = node_allocate_fn;
00c2f96f
JB
342 sp->deallocate = deallocate_fn;
343 sp->allocate_data = allocate_data;
ed87f9c8
MM
344
345 return sp;
346}
347
348/* Deallocate SP. */
349
350void
885f2199 351splay_tree_delete (splay_tree sp)
ed87f9c8
MM
352{
353 splay_tree_delete_helper (sp, sp->root);
00c2f96f 354 (*sp->deallocate) ((char*) sp, sp->allocate_data);
ed87f9c8
MM
355}
356
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
d080bbfa 359 with the new value. Returns the new node. */
ed87f9c8 360
d080bbfa 361splay_tree_node
885f2199 362splay_tree_insert (splay_tree sp, splay_tree_key key, splay_tree_value value)
ed87f9c8 363{
652374d3 364 int comparison = 0;
ed87f9c8
MM
365
366 splay_tree_splay (sp, key);
367
368 if (sp->root)
369 comparison = (*sp->comp)(sp->root->key, key);
370
371 if (sp->root && comparison == 0)
372 {
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;
378 }
379 else
380 {
381 /* Create a new node, and insert it at the root. */
382 splay_tree_node node;
a9429e29 383
00c2f96f 384 node = ((splay_tree_node)
a9429e29
LB
385 (*sp->allocate) (sizeof (struct splay_tree_node_s),
386 sp->allocate_data));
ed87f9c8
MM
387 node->key = key;
388 node->value = value;
389
390 if (!sp->root)
391 node->left = node->right = 0;
392 else if (comparison < 0)
393 {
394 node->left = sp->root;
395 node->right = node->left->right;
396 node->left->right = 0;
397 }
398 else
399 {
400 node->right = sp->root;
401 node->left = node->right->left;
402 node->right->left = 0;
403 }
404
f15b9af9
MM
405 sp->root = node;
406 }
d080bbfa
MM
407
408 return sp->root;
ed87f9c8
MM
409}
410
dc17cc7b
RH
411/* Remove KEY from SP. It is not an error if it did not exist. */
412
413void
885f2199 414splay_tree_remove (splay_tree sp, splay_tree_key key)
dc17cc7b
RH
415{
416 splay_tree_splay (sp, key);
417
418 if (sp->root && (*sp->comp) (sp->root->key, key) == 0)
419 {
420 splay_tree_node left, right;
421
422 left = sp->root->left;
423 right = sp->root->right;
424
425 /* Delete the root node itself. */
426 if (sp->delete_value)
427 (*sp->delete_value) (sp->root->value);
00c2f96f 428 (*sp->deallocate) (sp->root, sp->allocate_data);
dc17cc7b
RH
429
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)
433 {
434 sp->root = left;
435
436 /* If there was a right child as well, hang it off the
437 right-most leaf of the left child. */
438 if (right)
439 {
440 while (left->right)
441 left = left->right;
442 left->right = right;
443 }
444 }
445 else
446 sp->root = right;
447 }
448}
449
ed87f9c8
MM
450/* Lookup KEY in SP, returning VALUE if present, and NULL
451 otherwise. */
452
453splay_tree_node
885f2199 454splay_tree_lookup (splay_tree sp, splay_tree_key key)
ed87f9c8
MM
455{
456 splay_tree_splay (sp, key);
457
458 if (sp->root && (*sp->comp)(sp->root->key, key) == 0)
459 return sp->root;
460 else
461 return 0;
462}
463
5cdba4ff
MM
464/* Return the node in SP with the greatest key. */
465
466splay_tree_node
885f2199 467splay_tree_max (splay_tree sp)
5cdba4ff
MM
468{
469 splay_tree_node n = sp->root;
470
471 if (!n)
472 return NULL;
473
474 while (n->right)
475 n = n->right;
476
477 return n;
478}
479
480/* Return the node in SP with the smallest key. */
481
482splay_tree_node
885f2199 483splay_tree_min (splay_tree sp)
5cdba4ff
MM
484{
485 splay_tree_node n = sp->root;
486
487 if (!n)
488 return NULL;
489
490 while (n->left)
491 n = n->left;
492
493 return n;
494}
495
2c9f4db7
MM
496/* Return the immediate predecessor KEY, or NULL if there is no
497 predecessor. KEY need not be present in the tree. */
498
499splay_tree_node
885f2199 500splay_tree_predecessor (splay_tree sp, splay_tree_key key)
2c9f4db7
MM
501{
502 int comparison;
503 splay_tree_node node;
504
505 /* If the tree is empty, there is certainly no predecessor. */
506 if (!sp->root)
507 return NULL;
508
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);
513
514 /* If the predecessor is at the root, just return it. */
515 if (comparison < 0)
516 return sp->root;
517
d5d4eae2 518 /* Otherwise, find the rightmost element of the left subtree. */
2c9f4db7
MM
519 node = sp->root->left;
520 if (node)
521 while (node->right)
522 node = node->right;
523
524 return node;
525}
526
527/* Return the immediate successor KEY, or NULL if there is no
6eedb9ca 528 successor. KEY need not be present in the tree. */
2c9f4db7
MM
529
530splay_tree_node
885f2199 531splay_tree_successor (splay_tree sp, splay_tree_key key)
2c9f4db7
MM
532{
533 int comparison;
534 splay_tree_node node;
535
6eedb9ca 536 /* If the tree is empty, there is certainly no successor. */
2c9f4db7
MM
537 if (!sp->root)
538 return NULL;
539
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);
544
545 /* If the successor is at the root, just return it. */
546 if (comparison > 0)
547 return sp->root;
548
d5d4eae2 549 /* Otherwise, find the leftmost element of the right subtree. */
2c9f4db7
MM
550 node = sp->root->right;
551 if (node)
552 while (node->left)
553 node = node->left;
554
555 return node;
556}
557
ed87f9c8
MM
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. */
562
563int
885f2199 564splay_tree_foreach (splay_tree sp, splay_tree_foreach_fn fn, void *data)
ed87f9c8 565{
23346f36 566 return splay_tree_foreach_helper (sp->root, fn, data);
ed87f9c8 567}
30f72379
MM
568
569/* Splay-tree comparison function, treating the keys as ints. */
570
571int
885f2199 572splay_tree_compare_ints (splay_tree_key k1, splay_tree_key k2)
30f72379
MM
573{
574 if ((int) k1 < (int) k2)
575 return -1;
576 else if ((int) k1 > (int) k2)
577 return 1;
578 else
579 return 0;
580}
ae7f7270
MM
581
582/* Splay-tree comparison function, treating the keys as pointers. */
583
584int
885f2199 585splay_tree_compare_pointers (splay_tree_key k1, splay_tree_key k2)
ae7f7270
MM
586{
587 if ((char*) k1 < (char*) k2)
588 return -1;
589 else if ((char*) k1 > (char*) k2)
590 return 1;
591 else
592 return 0;
593}