1 /* A splay-tree datatype.
2 Copyright (C) 1998-2021 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, 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. */
40 #include "libiberty.h"
41 #include "splay-tree.h"
43 static void splay_tree_delete_helper (splay_tree
, splay_tree_node
);
44 static inline void rotate_left (splay_tree_node
*,
45 splay_tree_node
, splay_tree_node
);
46 static inline void rotate_right (splay_tree_node
*,
47 splay_tree_node
, splay_tree_node
);
48 static void splay_tree_splay (splay_tree
, splay_tree_key
);
49 static int splay_tree_foreach_helper (splay_tree_node
,
50 splay_tree_foreach_fn
, void*);
52 /* Deallocate NODE (a member of SP), and all its sub-trees. */
55 splay_tree_delete_helper (splay_tree sp
, splay_tree_node node
)
57 splay_tree_node pending
= 0;
58 splay_tree_node active
= 0;
63 #define KDEL(x) if (sp->delete_key) (*sp->delete_key)(x);
64 #define VDEL(x) if (sp->delete_value) (*sp->delete_value)(x);
69 /* We use the "key" field to hold the "next" pointer. */
70 node
->key
= (splay_tree_key
)pending
;
71 pending
= (splay_tree_node
)node
;
73 /* Now, keep processing the pending list until there aren't any
74 more. This is a little more complicated than just recursing, but
75 it doesn't toast the stack for large trees. */
85 /* active points to a node which has its key and value
86 deallocated, we just need to process left and right. */
90 KDEL (active
->left
->key
);
91 VDEL (active
->left
->value
);
92 active
->left
->key
= (splay_tree_key
)pending
;
93 pending
= (splay_tree_node
)(active
->left
);
97 KDEL (active
->right
->key
);
98 VDEL (active
->right
->value
);
99 active
->right
->key
= (splay_tree_key
)pending
;
100 pending
= (splay_tree_node
)(active
->right
);
104 active
= (splay_tree_node
)(temp
->key
);
105 (*sp
->deallocate
) ((char*) temp
, sp
->allocate_data
);
112 /* Rotate the edge joining the left child N with its parent P. PP is the
113 grandparents' pointer to P. */
116 rotate_left (splay_tree_node
*pp
, splay_tree_node p
, splay_tree_node n
)
125 /* Rotate the edge joining the right child N with its parent P. PP is the
126 grandparents' pointer to P. */
129 rotate_right (splay_tree_node
*pp
, splay_tree_node p
, splay_tree_node n
)
138 /* Bottom up splay of key. */
141 splay_tree_splay (splay_tree sp
, splay_tree_key key
)
148 splay_tree_node n
, c
;
151 cmp1
= (*sp
->comp
) (key
, n
->key
);
157 /* Left or right? If no child, then we're done. */
165 /* Next one left or right? If found or no child, we're done
166 after one rotation. */
167 cmp2
= (*sp
->comp
) (key
, c
->key
);
169 || (cmp2
< 0 && !c
->left
)
170 || (cmp2
> 0 && !c
->right
))
173 rotate_left (&sp
->root
, n
, c
);
175 rotate_right (&sp
->root
, n
, c
);
179 /* Now we have the four cases of double-rotation. */
180 if (cmp1
< 0 && cmp2
< 0)
182 rotate_left (&n
->left
, c
, c
->left
);
183 rotate_left (&sp
->root
, n
, n
->left
);
185 else if (cmp1
> 0 && cmp2
> 0)
187 rotate_right (&n
->right
, c
, c
->right
);
188 rotate_right (&sp
->root
, n
, n
->right
);
190 else if (cmp1
< 0 && cmp2
> 0)
192 rotate_right (&n
->left
, c
, c
->right
);
193 rotate_left (&sp
->root
, n
, n
->left
);
195 else if (cmp1
> 0 && cmp2
< 0)
197 rotate_left (&n
->right
, c
, c
->left
);
198 rotate_right (&sp
->root
, n
, n
->right
);
203 /* Call FN, passing it the DATA, for every node below NODE, all of
204 which are from SP, following an in-order traversal. If FN every
205 returns a non-zero value, the iteration ceases immediately, and the
206 value is returned. Otherwise, this function returns 0. */
209 splay_tree_foreach_helper (splay_tree_node node
,
210 splay_tree_foreach_fn fn
, void *data
)
213 splay_tree_node
*stack
;
214 int stack_ptr
, stack_size
;
216 /* A non-recursive implementation is used to avoid filling the stack
217 for large trees. Splay trees are worst case O(n) in the depth of
220 #define INITIAL_STACK_SIZE 100
221 stack_size
= INITIAL_STACK_SIZE
;
223 stack
= XNEWVEC (splay_tree_node
, stack_size
);
230 if (stack_ptr
== stack_size
)
233 stack
= XRESIZEVEC (splay_tree_node
, stack
, stack_size
);
235 stack
[stack_ptr
++] = node
;
242 node
= stack
[--stack_ptr
];
244 val
= (*fn
) (node
, data
);
255 /* An allocator and deallocator based on xmalloc. */
257 splay_tree_xmalloc_allocate (int size
, void *data ATTRIBUTE_UNUSED
)
259 return (void *) xmalloc (size
);
263 splay_tree_xmalloc_deallocate (void *object
, void *data ATTRIBUTE_UNUSED
)
269 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
270 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
271 values. Use xmalloc to allocate the splay tree structure, and any
275 splay_tree_new (splay_tree_compare_fn compare_fn
,
276 splay_tree_delete_key_fn delete_key_fn
,
277 splay_tree_delete_value_fn delete_value_fn
)
279 return (splay_tree_new_with_allocator
280 (compare_fn
, delete_key_fn
, delete_value_fn
,
281 splay_tree_xmalloc_allocate
, splay_tree_xmalloc_deallocate
, 0));
285 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
286 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
290 splay_tree_new_with_allocator (splay_tree_compare_fn compare_fn
,
291 splay_tree_delete_key_fn delete_key_fn
,
292 splay_tree_delete_value_fn delete_value_fn
,
293 splay_tree_allocate_fn allocate_fn
,
294 splay_tree_deallocate_fn deallocate_fn
,
298 splay_tree_new_typed_alloc (compare_fn
, delete_key_fn
, delete_value_fn
,
299 allocate_fn
, allocate_fn
, deallocate_fn
,
305 @deftypefn Supplemental splay_tree splay_tree_new_with_typed_alloc @
306 (splay_tree_compare_fn @var{compare_fn}, @
307 splay_tree_delete_key_fn @var{delete_key_fn}, @
308 splay_tree_delete_value_fn @var{delete_value_fn}, @
309 splay_tree_allocate_fn @var{tree_allocate_fn}, @
310 splay_tree_allocate_fn @var{node_allocate_fn}, @
311 splay_tree_deallocate_fn @var{deallocate_fn}, @
312 void * @var{allocate_data})
314 This function creates a splay tree that uses two different allocators
315 @var{tree_allocate_fn} and @var{node_allocate_fn} to use for allocating the
316 tree itself and its nodes respectively. This is useful when variables of
317 different types need to be allocated with different allocators.
319 The splay tree will use @var{compare_fn} to compare nodes,
320 @var{delete_key_fn} to deallocate keys, and @var{delete_value_fn} to
321 deallocate values. Keys and values will be deallocated when the
322 tree is deleted using splay_tree_delete or when a node is removed
323 using splay_tree_remove. splay_tree_insert will release the previously
324 inserted key and value using @var{delete_key_fn} and @var{delete_value_fn}
325 if the inserted key is already found in the tree.
332 splay_tree_new_typed_alloc (splay_tree_compare_fn compare_fn
,
333 splay_tree_delete_key_fn delete_key_fn
,
334 splay_tree_delete_value_fn delete_value_fn
,
335 splay_tree_allocate_fn tree_allocate_fn
,
336 splay_tree_allocate_fn node_allocate_fn
,
337 splay_tree_deallocate_fn deallocate_fn
,
338 void * allocate_data
)
340 splay_tree sp
= (splay_tree
) (*tree_allocate_fn
)
341 (sizeof (struct splay_tree_s
), allocate_data
);
344 sp
->comp
= compare_fn
;
345 sp
->delete_key
= delete_key_fn
;
346 sp
->delete_value
= delete_value_fn
;
347 sp
->allocate
= node_allocate_fn
;
348 sp
->deallocate
= deallocate_fn
;
349 sp
->allocate_data
= allocate_data
;
357 splay_tree_delete (splay_tree sp
)
359 splay_tree_delete_helper (sp
, sp
->root
);
360 (*sp
->deallocate
) ((char*) sp
, sp
->allocate_data
);
363 /* Insert a new node (associating KEY with DATA) into SP. If a
364 previous node with the indicated KEY exists, its data is replaced
365 with the new value. Returns the new node. */
368 splay_tree_insert (splay_tree sp
, splay_tree_key key
, splay_tree_value value
)
372 splay_tree_splay (sp
, key
);
375 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
377 if (sp
->root
&& comparison
== 0)
379 /* If the root of the tree already has the indicated KEY, delete
380 the old key and old value, and replace them with KEY and VALUE. */
382 (*sp
->delete_key
) (sp
->root
->key
);
383 if (sp
->delete_value
)
384 (*sp
->delete_value
)(sp
->root
->value
);
386 sp
->root
->value
= value
;
390 /* Create a new node, and insert it at the root. */
391 splay_tree_node node
;
393 node
= ((splay_tree_node
)
394 (*sp
->allocate
) (sizeof (struct splay_tree_node_s
),
400 node
->left
= node
->right
= 0;
401 else if (comparison
< 0)
403 node
->left
= sp
->root
;
404 node
->right
= node
->left
->right
;
405 node
->left
->right
= 0;
409 node
->right
= sp
->root
;
410 node
->left
= node
->right
->left
;
411 node
->right
->left
= 0;
420 /* Remove KEY from SP. It is not an error if it did not exist. */
423 splay_tree_remove (splay_tree sp
, splay_tree_key key
)
425 splay_tree_splay (sp
, key
);
427 if (sp
->root
&& (*sp
->comp
) (sp
->root
->key
, key
) == 0)
429 splay_tree_node left
, right
;
431 left
= sp
->root
->left
;
432 right
= sp
->root
->right
;
434 /* Delete the root node itself. */
436 (*sp
->delete_key
) (sp
->root
->key
);
437 if (sp
->delete_value
)
438 (*sp
->delete_value
) (sp
->root
->value
);
439 (*sp
->deallocate
) (sp
->root
, sp
->allocate_data
);
441 /* One of the children is now the root. Doesn't matter much
442 which, so long as we preserve the properties of the tree. */
447 /* If there was a right child as well, hang it off the
448 right-most leaf of the left child. */
461 /* Lookup KEY in SP, returning VALUE if present, and NULL
465 splay_tree_lookup (splay_tree sp
, splay_tree_key key
)
467 splay_tree_splay (sp
, key
);
469 if (sp
->root
&& (*sp
->comp
)(sp
->root
->key
, key
) == 0)
475 /* Return the node in SP with the greatest key. */
478 splay_tree_max (splay_tree sp
)
480 splay_tree_node n
= sp
->root
;
491 /* Return the node in SP with the smallest key. */
494 splay_tree_min (splay_tree sp
)
496 splay_tree_node n
= sp
->root
;
507 /* Return the immediate predecessor KEY, or NULL if there is no
508 predecessor. KEY need not be present in the tree. */
511 splay_tree_predecessor (splay_tree sp
, splay_tree_key key
)
514 splay_tree_node node
;
516 /* If the tree is empty, there is certainly no predecessor. */
520 /* Splay the tree around KEY. That will leave either the KEY
521 itself, its predecessor, or its successor at the root. */
522 splay_tree_splay (sp
, key
);
523 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
525 /* If the predecessor is at the root, just return it. */
529 /* Otherwise, find the rightmost element of the left subtree. */
530 node
= sp
->root
->left
;
538 /* Return the immediate successor KEY, or NULL if there is no
539 successor. KEY need not be present in the tree. */
542 splay_tree_successor (splay_tree sp
, splay_tree_key key
)
545 splay_tree_node node
;
547 /* If the tree is empty, there is certainly no successor. */
551 /* Splay the tree around KEY. That will leave either the KEY
552 itself, its predecessor, or its successor at the root. */
553 splay_tree_splay (sp
, key
);
554 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
556 /* If the successor is at the root, just return it. */
560 /* Otherwise, find the leftmost element of the right subtree. */
561 node
= sp
->root
->right
;
569 /* Call FN, passing it the DATA, for every node in SP, following an
570 in-order traversal. If FN every returns a non-zero value, the
571 iteration ceases immediately, and the value is returned.
572 Otherwise, this function returns 0. */
575 splay_tree_foreach (splay_tree sp
, splay_tree_foreach_fn fn
, void *data
)
577 return splay_tree_foreach_helper (sp
->root
, fn
, data
);
580 /* Splay-tree comparison function, treating the keys as ints. */
583 splay_tree_compare_ints (splay_tree_key k1
, splay_tree_key k2
)
585 if ((int) k1
< (int) k2
)
587 else if ((int) k1
> (int) k2
)
593 /* Splay-tree comparison function, treating the keys as pointers. */
596 splay_tree_compare_pointers (splay_tree_key k1
, splay_tree_key k2
)
598 if ((char*) k1
< (char*) k2
)
600 else if ((char*) k1
> (char*) k2
)
606 /* Splay-tree comparison function, treating the keys as strings. */
609 splay_tree_compare_strings (splay_tree_key k1
, splay_tree_key k2
)
611 return strcmp ((char *) k1
, (char *) k2
);
614 /* Splay-tree delete function, simply using free. */
617 splay_tree_delete_pointers (splay_tree_value value
)
619 free ((void *) value
);