1 /* A splay-tree datatype.
2 Copyright (C) 1998-2018 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. */
37 #include "libiberty.h"
38 #include "splay-tree.h"
40 static void splay_tree_delete_helper (splay_tree
, splay_tree_node
);
41 static inline void rotate_left (splay_tree_node
*,
42 splay_tree_node
, splay_tree_node
);
43 static inline void rotate_right (splay_tree_node
*,
44 splay_tree_node
, splay_tree_node
);
45 static void splay_tree_splay (splay_tree
, splay_tree_key
);
46 static int splay_tree_foreach_helper (splay_tree_node
,
47 splay_tree_foreach_fn
, void*);
49 /* Deallocate NODE (a member of SP), and all its sub-trees. */
52 splay_tree_delete_helper (splay_tree sp
, splay_tree_node node
)
54 splay_tree_node pending
= 0;
55 splay_tree_node active
= 0;
60 #define KDEL(x) if (sp->delete_key) (*sp->delete_key)(x);
61 #define VDEL(x) if (sp->delete_value) (*sp->delete_value)(x);
66 /* We use the "key" field to hold the "next" pointer. */
67 node
->key
= (splay_tree_key
)pending
;
68 pending
= (splay_tree_node
)node
;
70 /* Now, keep processing the pending list until there aren't any
71 more. This is a little more complicated than just recursing, but
72 it doesn't toast the stack for large trees. */
82 /* active points to a node which has its key and value
83 deallocated, we just need to process left and right. */
87 KDEL (active
->left
->key
);
88 VDEL (active
->left
->value
);
89 active
->left
->key
= (splay_tree_key
)pending
;
90 pending
= (splay_tree_node
)(active
->left
);
94 KDEL (active
->right
->key
);
95 VDEL (active
->right
->value
);
96 active
->right
->key
= (splay_tree_key
)pending
;
97 pending
= (splay_tree_node
)(active
->right
);
101 active
= (splay_tree_node
)(temp
->key
);
102 (*sp
->deallocate
) ((char*) temp
, sp
->allocate_data
);
109 /* Rotate the edge joining the left child N with its parent P. PP is the
110 grandparents' pointer to P. */
113 rotate_left (splay_tree_node
*pp
, splay_tree_node p
, splay_tree_node n
)
122 /* Rotate the edge joining the right child N with its parent P. PP is the
123 grandparents' pointer to P. */
126 rotate_right (splay_tree_node
*pp
, splay_tree_node p
, splay_tree_node n
)
135 /* Bottom up splay of key. */
138 splay_tree_splay (splay_tree sp
, splay_tree_key key
)
145 splay_tree_node n
, c
;
148 cmp1
= (*sp
->comp
) (key
, n
->key
);
154 /* Left or right? If no child, then we're done. */
162 /* Next one left or right? If found or no child, we're done
163 after one rotation. */
164 cmp2
= (*sp
->comp
) (key
, c
->key
);
166 || (cmp2
< 0 && !c
->left
)
167 || (cmp2
> 0 && !c
->right
))
170 rotate_left (&sp
->root
, n
, c
);
172 rotate_right (&sp
->root
, n
, c
);
176 /* Now we have the four cases of double-rotation. */
177 if (cmp1
< 0 && cmp2
< 0)
179 rotate_left (&n
->left
, c
, c
->left
);
180 rotate_left (&sp
->root
, n
, n
->left
);
182 else if (cmp1
> 0 && cmp2
> 0)
184 rotate_right (&n
->right
, c
, c
->right
);
185 rotate_right (&sp
->root
, n
, n
->right
);
187 else if (cmp1
< 0 && cmp2
> 0)
189 rotate_right (&n
->left
, c
, c
->right
);
190 rotate_left (&sp
->root
, n
, n
->left
);
192 else if (cmp1
> 0 && cmp2
< 0)
194 rotate_left (&n
->right
, c
, c
->left
);
195 rotate_right (&sp
->root
, n
, n
->right
);
200 /* Call FN, passing it the DATA, for every node below NODE, all of
201 which are from SP, following an in-order traversal. If FN every
202 returns a non-zero value, the iteration ceases immediately, and the
203 value is returned. Otherwise, this function returns 0. */
206 splay_tree_foreach_helper (splay_tree_node node
,
207 splay_tree_foreach_fn fn
, void *data
)
210 splay_tree_node
*stack
;
211 int stack_ptr
, stack_size
;
213 /* A non-recursive implementation is used to avoid filling the stack
214 for large trees. Splay trees are worst case O(n) in the depth of
217 #define INITIAL_STACK_SIZE 100
218 stack_size
= INITIAL_STACK_SIZE
;
220 stack
= XNEWVEC (splay_tree_node
, stack_size
);
227 if (stack_ptr
== stack_size
)
230 stack
= XRESIZEVEC (splay_tree_node
, stack
, stack_size
);
232 stack
[stack_ptr
++] = node
;
239 node
= stack
[--stack_ptr
];
241 val
= (*fn
) (node
, data
);
252 /* An allocator and deallocator based on xmalloc. */
254 splay_tree_xmalloc_allocate (int size
, void *data ATTRIBUTE_UNUSED
)
256 return (void *) xmalloc (size
);
260 splay_tree_xmalloc_deallocate (void *object
, void *data ATTRIBUTE_UNUSED
)
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
268 values. Use xmalloc to allocate the splay tree structure, and any
272 splay_tree_new (splay_tree_compare_fn compare_fn
,
273 splay_tree_delete_key_fn delete_key_fn
,
274 splay_tree_delete_value_fn delete_value_fn
)
276 return (splay_tree_new_with_allocator
277 (compare_fn
, delete_key_fn
, delete_value_fn
,
278 splay_tree_xmalloc_allocate
, splay_tree_xmalloc_deallocate
, 0));
282 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
283 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
287 splay_tree_new_with_allocator (splay_tree_compare_fn compare_fn
,
288 splay_tree_delete_key_fn delete_key_fn
,
289 splay_tree_delete_value_fn delete_value_fn
,
290 splay_tree_allocate_fn allocate_fn
,
291 splay_tree_deallocate_fn deallocate_fn
,
295 splay_tree_new_typed_alloc (compare_fn
, delete_key_fn
, delete_value_fn
,
296 allocate_fn
, allocate_fn
, deallocate_fn
,
302 @deftypefn Supplemental splay_tree splay_tree_new_with_typed_alloc @
303 (splay_tree_compare_fn @var{compare_fn}, @
304 splay_tree_delete_key_fn @var{delete_key_fn}, @
305 splay_tree_delete_value_fn @var{delete_value_fn}, @
306 splay_tree_allocate_fn @var{tree_allocate_fn}, @
307 splay_tree_allocate_fn @var{node_allocate_fn}, @
308 splay_tree_deallocate_fn @var{deallocate_fn}, @
309 void * @var{allocate_data})
311 This function creates a splay tree that uses two different allocators
312 @var{tree_allocate_fn} and @var{node_allocate_fn} to use for allocating the
313 tree itself and its nodes respectively. This is useful when variables of
314 different types need to be allocated with different allocators.
316 The splay tree will use @var{compare_fn} to compare nodes,
317 @var{delete_key_fn} to deallocate keys, and @var{delete_value_fn} to
325 splay_tree_new_typed_alloc (splay_tree_compare_fn compare_fn
,
326 splay_tree_delete_key_fn delete_key_fn
,
327 splay_tree_delete_value_fn delete_value_fn
,
328 splay_tree_allocate_fn tree_allocate_fn
,
329 splay_tree_allocate_fn node_allocate_fn
,
330 splay_tree_deallocate_fn deallocate_fn
,
331 void * allocate_data
)
333 splay_tree sp
= (splay_tree
) (*tree_allocate_fn
)
334 (sizeof (struct splay_tree_s
), allocate_data
);
337 sp
->comp
= compare_fn
;
338 sp
->delete_key
= delete_key_fn
;
339 sp
->delete_value
= delete_value_fn
;
340 sp
->allocate
= node_allocate_fn
;
341 sp
->deallocate
= deallocate_fn
;
342 sp
->allocate_data
= allocate_data
;
350 splay_tree_delete (splay_tree sp
)
352 splay_tree_delete_helper (sp
, sp
->root
);
353 (*sp
->deallocate
) ((char*) sp
, sp
->allocate_data
);
356 /* Insert a new node (associating KEY with DATA) into SP. If a
357 previous node with the indicated KEY exists, its data is replaced
358 with the new value. Returns the new node. */
361 splay_tree_insert (splay_tree sp
, splay_tree_key key
, splay_tree_value value
)
365 splay_tree_splay (sp
, key
);
368 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
370 if (sp
->root
&& comparison
== 0)
372 /* If the root of the tree already has the indicated KEY, just
373 replace the value with VALUE. */
374 if (sp
->delete_value
)
375 (*sp
->delete_value
)(sp
->root
->value
);
376 sp
->root
->value
= value
;
380 /* Create a new node, and insert it at the root. */
381 splay_tree_node node
;
383 node
= ((splay_tree_node
)
384 (*sp
->allocate
) (sizeof (struct splay_tree_node_s
),
390 node
->left
= node
->right
= 0;
391 else if (comparison
< 0)
393 node
->left
= sp
->root
;
394 node
->right
= node
->left
->right
;
395 node
->left
->right
= 0;
399 node
->right
= sp
->root
;
400 node
->left
= node
->right
->left
;
401 node
->right
->left
= 0;
410 /* Remove KEY from SP. It is not an error if it did not exist. */
413 splay_tree_remove (splay_tree sp
, splay_tree_key key
)
415 splay_tree_splay (sp
, key
);
417 if (sp
->root
&& (*sp
->comp
) (sp
->root
->key
, key
) == 0)
419 splay_tree_node left
, right
;
421 left
= sp
->root
->left
;
422 right
= sp
->root
->right
;
424 /* Delete the root node itself. */
425 if (sp
->delete_value
)
426 (*sp
->delete_value
) (sp
->root
->value
);
427 (*sp
->deallocate
) (sp
->root
, sp
->allocate_data
);
429 /* One of the children is now the root. Doesn't matter much
430 which, so long as we preserve the properties of the tree. */
435 /* If there was a right child as well, hang it off the
436 right-most leaf of the left child. */
449 /* Lookup KEY in SP, returning VALUE if present, and NULL
453 splay_tree_lookup (splay_tree sp
, splay_tree_key key
)
455 splay_tree_splay (sp
, key
);
457 if (sp
->root
&& (*sp
->comp
)(sp
->root
->key
, key
) == 0)
463 /* Return the node in SP with the greatest key. */
466 splay_tree_max (splay_tree sp
)
468 splay_tree_node n
= sp
->root
;
479 /* Return the node in SP with the smallest key. */
482 splay_tree_min (splay_tree sp
)
484 splay_tree_node n
= sp
->root
;
495 /* Return the immediate predecessor KEY, or NULL if there is no
496 predecessor. KEY need not be present in the tree. */
499 splay_tree_predecessor (splay_tree sp
, splay_tree_key key
)
502 splay_tree_node node
;
504 /* If the tree is empty, there is certainly no predecessor. */
508 /* Splay the tree around KEY. That will leave either the KEY
509 itself, its predecessor, or its successor at the root. */
510 splay_tree_splay (sp
, key
);
511 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
513 /* If the predecessor is at the root, just return it. */
517 /* Otherwise, find the rightmost element of the left subtree. */
518 node
= sp
->root
->left
;
526 /* Return the immediate successor KEY, or NULL if there is no
527 successor. KEY need not be present in the tree. */
530 splay_tree_successor (splay_tree sp
, splay_tree_key key
)
533 splay_tree_node node
;
535 /* If the tree is empty, there is certainly no successor. */
539 /* Splay the tree around KEY. That will leave either the KEY
540 itself, its predecessor, or its successor at the root. */
541 splay_tree_splay (sp
, key
);
542 comparison
= (*sp
->comp
)(sp
->root
->key
, key
);
544 /* If the successor is at the root, just return it. */
548 /* Otherwise, find the leftmost element of the right subtree. */
549 node
= sp
->root
->right
;
557 /* Call FN, passing it the DATA, for every node in SP, following an
558 in-order traversal. If FN every returns a non-zero value, the
559 iteration ceases immediately, and the value is returned.
560 Otherwise, this function returns 0. */
563 splay_tree_foreach (splay_tree sp
, splay_tree_foreach_fn fn
, void *data
)
565 return splay_tree_foreach_helper (sp
->root
, fn
, data
);
568 /* Splay-tree comparison function, treating the keys as ints. */
571 splay_tree_compare_ints (splay_tree_key k1
, splay_tree_key k2
)
573 if ((int) k1
< (int) k2
)
575 else if ((int) k1
> (int) k2
)
581 /* Splay-tree comparison function, treating the keys as pointers. */
584 splay_tree_compare_pointers (splay_tree_key k1
, splay_tree_key k2
)
586 if ((char*) k1
< (char*) k2
)
588 else if ((char*) k1
> (char*) k2
)