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)
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. */
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. */
53
splay_tree_delete_helper (splay_tree sp, splay_tree_node node)
55
splay_tree_node pending = 0;
56
splay_tree_node active = 0;
61
#define KDEL(x) if (sp->delete_key) (*sp->delete_key)(x);
62
#define VDEL(x) if (sp->delete_value) (*sp->delete_value)(x);
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. */
83
/* active points to a node which has its key and value
84
deallocated, we just need to process left and right. */
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);
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);
102
active = (splay_tree_node)(temp->key);
103
(*sp->deallocate) ((char*) temp, sp->allocate_data);
110
/* Rotate the edge joining the left child N with its parent P. PP is the
111
grandparents' pointer to P. */
114
rotate_left (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
123
/* Rotate the edge joining the right child N with its parent P. PP is the
124
grandparents' pointer to P. */
127
rotate_right (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
136
/* Bottom up splay of key. */
139
splay_tree_splay (splay_tree sp, splay_tree_key key)
146
splay_tree_node n, c;
149
cmp1 = (*sp->comp) (key, n->key);
155
/* Left or right? If no child, then we're done. */
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);
167
|| (cmp2 < 0 && !c->left)
168
|| (cmp2 > 0 && !c->right))
171
rotate_left (&sp->root, n, c);
173
rotate_right (&sp->root, n, c);
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);
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. */
207
splay_tree_foreach_helper (splay_tree_node node,
208
splay_tree_foreach_fn fn, void *data)
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
218
#define INITIAL_STACK_SIZE 100
219
stack_size = INITIAL_STACK_SIZE;
221
stack = XNEWVEC (splay_tree_node, stack_size);
228
if (stack_ptr == stack_size)
231
stack = XRESIZEVEC (splay_tree_node, stack, stack_size);
233
stack[stack_ptr++] = node;
240
node = stack[--stack_ptr];
242
val = (*fn) (node, data);
253
/* An allocator and deallocator based on xmalloc. */
255
splay_tree_xmalloc_allocate (int size, void *data ATTRIBUTE_UNUSED)
257
return (void *) xmalloc (size);
261
splay_tree_xmalloc_deallocate (void *object, void *data ATTRIBUTE_UNUSED)
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
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
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,
296
splay_tree_new_typed_alloc (compare_fn, delete_key_fn, delete_value_fn,
297
allocate_fn, allocate_fn, deallocate_fn,
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
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);
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;
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. */
362
splay_tree_insert (splay_tree sp, splay_tree_key key, splay_tree_value value)
366
splay_tree_splay (sp, key);
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;
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),
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;
400
node->right = sp->root;
401
node->left = node->right->left;
402
node->right->left = 0;
411
/* Remove KEY from SP. It is not an error if it did not exist. */
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. */
436
/* If there was a right child as well, hang it off the
437
right-most leaf of the left child. */
450
/* Lookup KEY in SP, returning VALUE if present, and NULL
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)
464
/* Return the node in SP with the greatest key. */
467
splay_tree_max (splay_tree sp)
469
splay_tree_node n = sp->root;
480
/* Return the node in SP with the smallest key. */
483
splay_tree_min (splay_tree sp)
485
splay_tree_node n = sp->root;
496
/* Return the immediate predecessor KEY, or NULL if there is no
497
predecessor. KEY need not be present in the tree. */
500
splay_tree_predecessor (splay_tree sp, splay_tree_key key)
503
splay_tree_node node;
505
/* If the tree is empty, there is certainly no predecessor. */
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. */
518
/* Otherwise, find the rightmost element of the left subtree. */
519
node = sp->root->left;
527
/* Return the immediate successor KEY, or NULL if there is no
528
successor. KEY need not be present in the tree. */
531
splay_tree_successor (splay_tree sp, splay_tree_key key)
534
splay_tree_node node;
536
/* If the tree is empty, there is certainly no successor. */
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. */
549
/* Otherwise, find the leftmost element of the right subtree. */
550
node = sp->root->right;
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)
576
else if ((int) k1 > (int) k2)
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)
589
else if ((char*) k1 > (char*) k2)