xref: /dflybsd-src/contrib/gcc-8.0/libiberty/splay-tree.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* A splay-tree datatype.
2*38fd1498Szrj    Copyright (C) 1998-2018 Free Software Foundation, Inc.
3*38fd1498Szrj    Contributed by Mark Mitchell (mark@markmitchell.com).
4*38fd1498Szrj 
5*38fd1498Szrj This file is part of GNU CC.
6*38fd1498Szrj 
7*38fd1498Szrj GNU CC is free software; you can redistribute it and/or modify it
8*38fd1498Szrj under the terms of the GNU General Public License as published by
9*38fd1498Szrj the Free Software Foundation; either version 2, or (at your option)
10*38fd1498Szrj any later version.
11*38fd1498Szrj 
12*38fd1498Szrj GNU CC is distributed in the hope that it will be useful, but
13*38fd1498Szrj WITHOUT ANY WARRANTY; without even the implied warranty of
14*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*38fd1498Szrj General Public License for more details.
16*38fd1498Szrj 
17*38fd1498Szrj You should have received a copy of the GNU General Public License
18*38fd1498Szrj along with GNU CC; see the file COPYING.  If not, write to
19*38fd1498Szrj the Free Software Foundation, 51 Franklin Street - Fifth Floor,
20*38fd1498Szrj Boston, MA 02110-1301, USA.  */
21*38fd1498Szrj 
22*38fd1498Szrj /* For an easily readable description of splay-trees, see:
23*38fd1498Szrj 
24*38fd1498Szrj      Lewis, Harry R. and Denenberg, Larry.  Data Structures and Their
25*38fd1498Szrj      Algorithms.  Harper-Collins, Inc.  1991.  */
26*38fd1498Szrj 
27*38fd1498Szrj #ifdef HAVE_CONFIG_H
28*38fd1498Szrj #include "config.h"
29*38fd1498Szrj #endif
30*38fd1498Szrj 
31*38fd1498Szrj #ifdef HAVE_STDLIB_H
32*38fd1498Szrj #include <stdlib.h>
33*38fd1498Szrj #endif
34*38fd1498Szrj 
35*38fd1498Szrj #include <stdio.h>
36*38fd1498Szrj 
37*38fd1498Szrj #include "libiberty.h"
38*38fd1498Szrj #include "splay-tree.h"
39*38fd1498Szrj 
40*38fd1498Szrj static void splay_tree_delete_helper (splay_tree, splay_tree_node);
41*38fd1498Szrj static inline void rotate_left (splay_tree_node *,
42*38fd1498Szrj 				splay_tree_node, splay_tree_node);
43*38fd1498Szrj static inline void rotate_right (splay_tree_node *,
44*38fd1498Szrj 				splay_tree_node, splay_tree_node);
45*38fd1498Szrj static void splay_tree_splay (splay_tree, splay_tree_key);
46*38fd1498Szrj static int splay_tree_foreach_helper (splay_tree_node,
47*38fd1498Szrj                                       splay_tree_foreach_fn, void*);
48*38fd1498Szrj 
49*38fd1498Szrj /* Deallocate NODE (a member of SP), and all its sub-trees.  */
50*38fd1498Szrj 
51*38fd1498Szrj static void
splay_tree_delete_helper(splay_tree sp,splay_tree_node node)52*38fd1498Szrj splay_tree_delete_helper (splay_tree sp, splay_tree_node node)
53*38fd1498Szrj {
54*38fd1498Szrj   splay_tree_node pending = 0;
55*38fd1498Szrj   splay_tree_node active = 0;
56*38fd1498Szrj 
57*38fd1498Szrj   if (!node)
58*38fd1498Szrj     return;
59*38fd1498Szrj 
60*38fd1498Szrj #define KDEL(x)  if (sp->delete_key) (*sp->delete_key)(x);
61*38fd1498Szrj #define VDEL(x)  if (sp->delete_value) (*sp->delete_value)(x);
62*38fd1498Szrj 
63*38fd1498Szrj   KDEL (node->key);
64*38fd1498Szrj   VDEL (node->value);
65*38fd1498Szrj 
66*38fd1498Szrj   /* We use the "key" field to hold the "next" pointer.  */
67*38fd1498Szrj   node->key = (splay_tree_key)pending;
68*38fd1498Szrj   pending = (splay_tree_node)node;
69*38fd1498Szrj 
70*38fd1498Szrj   /* Now, keep processing the pending list until there aren't any
71*38fd1498Szrj      more.  This is a little more complicated than just recursing, but
72*38fd1498Szrj      it doesn't toast the stack for large trees.  */
73*38fd1498Szrj 
74*38fd1498Szrj   while (pending)
75*38fd1498Szrj     {
76*38fd1498Szrj       active = pending;
77*38fd1498Szrj       pending = 0;
78*38fd1498Szrj       while (active)
79*38fd1498Szrj 	{
80*38fd1498Szrj 	  splay_tree_node temp;
81*38fd1498Szrj 
82*38fd1498Szrj 	  /* active points to a node which has its key and value
83*38fd1498Szrj 	     deallocated, we just need to process left and right.  */
84*38fd1498Szrj 
85*38fd1498Szrj 	  if (active->left)
86*38fd1498Szrj 	    {
87*38fd1498Szrj 	      KDEL (active->left->key);
88*38fd1498Szrj 	      VDEL (active->left->value);
89*38fd1498Szrj 	      active->left->key = (splay_tree_key)pending;
90*38fd1498Szrj 	      pending = (splay_tree_node)(active->left);
91*38fd1498Szrj 	    }
92*38fd1498Szrj 	  if (active->right)
93*38fd1498Szrj 	    {
94*38fd1498Szrj 	      KDEL (active->right->key);
95*38fd1498Szrj 	      VDEL (active->right->value);
96*38fd1498Szrj 	      active->right->key = (splay_tree_key)pending;
97*38fd1498Szrj 	      pending = (splay_tree_node)(active->right);
98*38fd1498Szrj 	    }
99*38fd1498Szrj 
100*38fd1498Szrj 	  temp = active;
101*38fd1498Szrj 	  active = (splay_tree_node)(temp->key);
102*38fd1498Szrj 	  (*sp->deallocate) ((char*) temp, sp->allocate_data);
103*38fd1498Szrj 	}
104*38fd1498Szrj     }
105*38fd1498Szrj #undef KDEL
106*38fd1498Szrj #undef VDEL
107*38fd1498Szrj }
108*38fd1498Szrj 
109*38fd1498Szrj /* Rotate the edge joining the left child N with its parent P.  PP is the
110*38fd1498Szrj    grandparents' pointer to P.  */
111*38fd1498Szrj 
112*38fd1498Szrj static inline void
rotate_left(splay_tree_node * pp,splay_tree_node p,splay_tree_node n)113*38fd1498Szrj rotate_left (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
114*38fd1498Szrj {
115*38fd1498Szrj   splay_tree_node tmp;
116*38fd1498Szrj   tmp = n->right;
117*38fd1498Szrj   n->right = p;
118*38fd1498Szrj   p->left = tmp;
119*38fd1498Szrj   *pp = n;
120*38fd1498Szrj }
121*38fd1498Szrj 
122*38fd1498Szrj /* Rotate the edge joining the right child N with its parent P.  PP is the
123*38fd1498Szrj    grandparents' pointer to P.  */
124*38fd1498Szrj 
125*38fd1498Szrj static inline void
rotate_right(splay_tree_node * pp,splay_tree_node p,splay_tree_node n)126*38fd1498Szrj rotate_right (splay_tree_node *pp, splay_tree_node p, splay_tree_node n)
127*38fd1498Szrj {
128*38fd1498Szrj   splay_tree_node tmp;
129*38fd1498Szrj   tmp = n->left;
130*38fd1498Szrj   n->left = p;
131*38fd1498Szrj   p->right = tmp;
132*38fd1498Szrj   *pp = n;
133*38fd1498Szrj }
134*38fd1498Szrj 
135*38fd1498Szrj /* Bottom up splay of key.  */
136*38fd1498Szrj 
137*38fd1498Szrj static void
splay_tree_splay(splay_tree sp,splay_tree_key key)138*38fd1498Szrj splay_tree_splay (splay_tree sp, splay_tree_key key)
139*38fd1498Szrj {
140*38fd1498Szrj   if (sp->root == 0)
141*38fd1498Szrj     return;
142*38fd1498Szrj 
143*38fd1498Szrj   do {
144*38fd1498Szrj     int cmp1, cmp2;
145*38fd1498Szrj     splay_tree_node n, c;
146*38fd1498Szrj 
147*38fd1498Szrj     n = sp->root;
148*38fd1498Szrj     cmp1 = (*sp->comp) (key, n->key);
149*38fd1498Szrj 
150*38fd1498Szrj     /* Found.  */
151*38fd1498Szrj     if (cmp1 == 0)
152*38fd1498Szrj       return;
153*38fd1498Szrj 
154*38fd1498Szrj     /* Left or right?  If no child, then we're done.  */
155*38fd1498Szrj     if (cmp1 < 0)
156*38fd1498Szrj       c = n->left;
157*38fd1498Szrj     else
158*38fd1498Szrj       c = n->right;
159*38fd1498Szrj     if (!c)
160*38fd1498Szrj       return;
161*38fd1498Szrj 
162*38fd1498Szrj     /* Next one left or right?  If found or no child, we're done
163*38fd1498Szrj        after one rotation.  */
164*38fd1498Szrj     cmp2 = (*sp->comp) (key, c->key);
165*38fd1498Szrj     if (cmp2 == 0
166*38fd1498Szrj         || (cmp2 < 0 && !c->left)
167*38fd1498Szrj         || (cmp2 > 0 && !c->right))
168*38fd1498Szrj       {
169*38fd1498Szrj 	if (cmp1 < 0)
170*38fd1498Szrj 	  rotate_left (&sp->root, n, c);
171*38fd1498Szrj 	else
172*38fd1498Szrj 	  rotate_right (&sp->root, n, c);
173*38fd1498Szrj         return;
174*38fd1498Szrj       }
175*38fd1498Szrj 
176*38fd1498Szrj     /* Now we have the four cases of double-rotation.  */
177*38fd1498Szrj     if (cmp1 < 0 && cmp2 < 0)
178*38fd1498Szrj       {
179*38fd1498Szrj 	rotate_left (&n->left, c, c->left);
180*38fd1498Szrj 	rotate_left (&sp->root, n, n->left);
181*38fd1498Szrj       }
182*38fd1498Szrj     else if (cmp1 > 0 && cmp2 > 0)
183*38fd1498Szrj       {
184*38fd1498Szrj 	rotate_right (&n->right, c, c->right);
185*38fd1498Szrj 	rotate_right (&sp->root, n, n->right);
186*38fd1498Szrj       }
187*38fd1498Szrj     else if (cmp1 < 0 && cmp2 > 0)
188*38fd1498Szrj       {
189*38fd1498Szrj 	rotate_right (&n->left, c, c->right);
190*38fd1498Szrj 	rotate_left (&sp->root, n, n->left);
191*38fd1498Szrj       }
192*38fd1498Szrj     else if (cmp1 > 0 && cmp2 < 0)
193*38fd1498Szrj       {
194*38fd1498Szrj 	rotate_left (&n->right, c, c->left);
195*38fd1498Szrj 	rotate_right (&sp->root, n, n->right);
196*38fd1498Szrj       }
197*38fd1498Szrj   } while (1);
198*38fd1498Szrj }
199*38fd1498Szrj 
200*38fd1498Szrj /* Call FN, passing it the DATA, for every node below NODE, all of
201*38fd1498Szrj    which are from SP, following an in-order traversal.  If FN every
202*38fd1498Szrj    returns a non-zero value, the iteration ceases immediately, and the
203*38fd1498Szrj    value is returned.  Otherwise, this function returns 0.  */
204*38fd1498Szrj 
205*38fd1498Szrj static int
splay_tree_foreach_helper(splay_tree_node node,splay_tree_foreach_fn fn,void * data)206*38fd1498Szrj splay_tree_foreach_helper (splay_tree_node node,
207*38fd1498Szrj                            splay_tree_foreach_fn fn, void *data)
208*38fd1498Szrj {
209*38fd1498Szrj   int val;
210*38fd1498Szrj   splay_tree_node *stack;
211*38fd1498Szrj   int stack_ptr, stack_size;
212*38fd1498Szrj 
213*38fd1498Szrj   /* A non-recursive implementation is used to avoid filling the stack
214*38fd1498Szrj      for large trees.  Splay trees are worst case O(n) in the depth of
215*38fd1498Szrj      the tree.  */
216*38fd1498Szrj 
217*38fd1498Szrj #define INITIAL_STACK_SIZE 100
218*38fd1498Szrj   stack_size = INITIAL_STACK_SIZE;
219*38fd1498Szrj   stack_ptr = 0;
220*38fd1498Szrj   stack = XNEWVEC (splay_tree_node, stack_size);
221*38fd1498Szrj   val = 0;
222*38fd1498Szrj 
223*38fd1498Szrj   for (;;)
224*38fd1498Szrj     {
225*38fd1498Szrj       while (node != NULL)
226*38fd1498Szrj 	{
227*38fd1498Szrj 	  if (stack_ptr == stack_size)
228*38fd1498Szrj 	    {
229*38fd1498Szrj 	      stack_size *= 2;
230*38fd1498Szrj 	      stack = XRESIZEVEC (splay_tree_node, stack, stack_size);
231*38fd1498Szrj 	    }
232*38fd1498Szrj 	  stack[stack_ptr++] = node;
233*38fd1498Szrj 	  node = node->left;
234*38fd1498Szrj 	}
235*38fd1498Szrj 
236*38fd1498Szrj       if (stack_ptr == 0)
237*38fd1498Szrj 	break;
238*38fd1498Szrj 
239*38fd1498Szrj       node = stack[--stack_ptr];
240*38fd1498Szrj 
241*38fd1498Szrj       val = (*fn) (node, data);
242*38fd1498Szrj       if (val)
243*38fd1498Szrj 	break;
244*38fd1498Szrj 
245*38fd1498Szrj       node = node->right;
246*38fd1498Szrj     }
247*38fd1498Szrj 
248*38fd1498Szrj   XDELETEVEC (stack);
249*38fd1498Szrj   return val;
250*38fd1498Szrj }
251*38fd1498Szrj 
252*38fd1498Szrj /* An allocator and deallocator based on xmalloc.  */
253*38fd1498Szrj static void *
splay_tree_xmalloc_allocate(int size,void * data ATTRIBUTE_UNUSED)254*38fd1498Szrj splay_tree_xmalloc_allocate (int size, void *data ATTRIBUTE_UNUSED)
255*38fd1498Szrj {
256*38fd1498Szrj   return (void *) xmalloc (size);
257*38fd1498Szrj }
258*38fd1498Szrj 
259*38fd1498Szrj static void
splay_tree_xmalloc_deallocate(void * object,void * data ATTRIBUTE_UNUSED)260*38fd1498Szrj splay_tree_xmalloc_deallocate (void *object, void *data ATTRIBUTE_UNUSED)
261*38fd1498Szrj {
262*38fd1498Szrj   free (object);
263*38fd1498Szrj }
264*38fd1498Szrj 
265*38fd1498Szrj 
266*38fd1498Szrj /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
267*38fd1498Szrj    DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
268*38fd1498Szrj    values.  Use xmalloc to allocate the splay tree structure, and any
269*38fd1498Szrj    nodes added.  */
270*38fd1498Szrj 
271*38fd1498Szrj splay_tree
splay_tree_new(splay_tree_compare_fn compare_fn,splay_tree_delete_key_fn delete_key_fn,splay_tree_delete_value_fn delete_value_fn)272*38fd1498Szrj splay_tree_new (splay_tree_compare_fn compare_fn,
273*38fd1498Szrj                 splay_tree_delete_key_fn delete_key_fn,
274*38fd1498Szrj                 splay_tree_delete_value_fn delete_value_fn)
275*38fd1498Szrj {
276*38fd1498Szrj   return (splay_tree_new_with_allocator
277*38fd1498Szrj           (compare_fn, delete_key_fn, delete_value_fn,
278*38fd1498Szrj            splay_tree_xmalloc_allocate, splay_tree_xmalloc_deallocate, 0));
279*38fd1498Szrj }
280*38fd1498Szrj 
281*38fd1498Szrj 
282*38fd1498Szrj /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
283*38fd1498Szrj    DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
284*38fd1498Szrj    values.  */
285*38fd1498Szrj 
286*38fd1498Szrj splay_tree
splay_tree_new_with_allocator(splay_tree_compare_fn compare_fn,splay_tree_delete_key_fn delete_key_fn,splay_tree_delete_value_fn delete_value_fn,splay_tree_allocate_fn allocate_fn,splay_tree_deallocate_fn deallocate_fn,void * allocate_data)287*38fd1498Szrj splay_tree_new_with_allocator (splay_tree_compare_fn compare_fn,
288*38fd1498Szrj                                splay_tree_delete_key_fn delete_key_fn,
289*38fd1498Szrj                                splay_tree_delete_value_fn delete_value_fn,
290*38fd1498Szrj                                splay_tree_allocate_fn allocate_fn,
291*38fd1498Szrj                                splay_tree_deallocate_fn deallocate_fn,
292*38fd1498Szrj                                void *allocate_data)
293*38fd1498Szrj {
294*38fd1498Szrj   return
295*38fd1498Szrj     splay_tree_new_typed_alloc (compare_fn, delete_key_fn, delete_value_fn,
296*38fd1498Szrj 				allocate_fn, allocate_fn, deallocate_fn,
297*38fd1498Szrj 				allocate_data);
298*38fd1498Szrj }
299*38fd1498Szrj 
300*38fd1498Szrj /*
301*38fd1498Szrj 
302*38fd1498Szrj @deftypefn Supplemental splay_tree splay_tree_new_with_typed_alloc @
303*38fd1498Szrj (splay_tree_compare_fn @var{compare_fn}, @
304*38fd1498Szrj splay_tree_delete_key_fn @var{delete_key_fn}, @
305*38fd1498Szrj splay_tree_delete_value_fn @var{delete_value_fn}, @
306*38fd1498Szrj splay_tree_allocate_fn @var{tree_allocate_fn}, @
307*38fd1498Szrj splay_tree_allocate_fn @var{node_allocate_fn}, @
308*38fd1498Szrj splay_tree_deallocate_fn @var{deallocate_fn}, @
309*38fd1498Szrj void * @var{allocate_data})
310*38fd1498Szrj 
311*38fd1498Szrj This function creates a splay tree that uses two different allocators
312*38fd1498Szrj @var{tree_allocate_fn} and @var{node_allocate_fn} to use for allocating the
313*38fd1498Szrj tree itself and its nodes respectively.  This is useful when variables of
314*38fd1498Szrj different types need to be allocated with different allocators.
315*38fd1498Szrj 
316*38fd1498Szrj The splay tree will use @var{compare_fn} to compare nodes,
317*38fd1498Szrj @var{delete_key_fn} to deallocate keys, and @var{delete_value_fn} to
318*38fd1498Szrj deallocate values.
319*38fd1498Szrj 
320*38fd1498Szrj @end deftypefn
321*38fd1498Szrj 
322*38fd1498Szrj */
323*38fd1498Szrj 
324*38fd1498Szrj splay_tree
splay_tree_new_typed_alloc(splay_tree_compare_fn compare_fn,splay_tree_delete_key_fn delete_key_fn,splay_tree_delete_value_fn delete_value_fn,splay_tree_allocate_fn tree_allocate_fn,splay_tree_allocate_fn node_allocate_fn,splay_tree_deallocate_fn deallocate_fn,void * allocate_data)325*38fd1498Szrj splay_tree_new_typed_alloc (splay_tree_compare_fn compare_fn,
326*38fd1498Szrj 			    splay_tree_delete_key_fn delete_key_fn,
327*38fd1498Szrj 			    splay_tree_delete_value_fn delete_value_fn,
328*38fd1498Szrj 			    splay_tree_allocate_fn tree_allocate_fn,
329*38fd1498Szrj 			    splay_tree_allocate_fn node_allocate_fn,
330*38fd1498Szrj 			    splay_tree_deallocate_fn deallocate_fn,
331*38fd1498Szrj 			    void * allocate_data)
332*38fd1498Szrj {
333*38fd1498Szrj   splay_tree sp = (splay_tree) (*tree_allocate_fn)
334*38fd1498Szrj     (sizeof (struct splay_tree_s), allocate_data);
335*38fd1498Szrj 
336*38fd1498Szrj   sp->root = 0;
337*38fd1498Szrj   sp->comp = compare_fn;
338*38fd1498Szrj   sp->delete_key = delete_key_fn;
339*38fd1498Szrj   sp->delete_value = delete_value_fn;
340*38fd1498Szrj   sp->allocate = node_allocate_fn;
341*38fd1498Szrj   sp->deallocate = deallocate_fn;
342*38fd1498Szrj   sp->allocate_data = allocate_data;
343*38fd1498Szrj 
344*38fd1498Szrj   return sp;
345*38fd1498Szrj }
346*38fd1498Szrj 
347*38fd1498Szrj /* Deallocate SP.  */
348*38fd1498Szrj 
349*38fd1498Szrj void
splay_tree_delete(splay_tree sp)350*38fd1498Szrj splay_tree_delete (splay_tree sp)
351*38fd1498Szrj {
352*38fd1498Szrj   splay_tree_delete_helper (sp, sp->root);
353*38fd1498Szrj   (*sp->deallocate) ((char*) sp, sp->allocate_data);
354*38fd1498Szrj }
355*38fd1498Szrj 
356*38fd1498Szrj /* Insert a new node (associating KEY with DATA) into SP.  If a
357*38fd1498Szrj    previous node with the indicated KEY exists, its data is replaced
358*38fd1498Szrj    with the new value.  Returns the new node.  */
359*38fd1498Szrj 
360*38fd1498Szrj splay_tree_node
splay_tree_insert(splay_tree sp,splay_tree_key key,splay_tree_value value)361*38fd1498Szrj splay_tree_insert (splay_tree sp, splay_tree_key key, splay_tree_value value)
362*38fd1498Szrj {
363*38fd1498Szrj   int comparison = 0;
364*38fd1498Szrj 
365*38fd1498Szrj   splay_tree_splay (sp, key);
366*38fd1498Szrj 
367*38fd1498Szrj   if (sp->root)
368*38fd1498Szrj     comparison = (*sp->comp)(sp->root->key, key);
369*38fd1498Szrj 
370*38fd1498Szrj   if (sp->root && comparison == 0)
371*38fd1498Szrj     {
372*38fd1498Szrj       /* If the root of the tree already has the indicated KEY, just
373*38fd1498Szrj 	 replace the value with VALUE.  */
374*38fd1498Szrj       if (sp->delete_value)
375*38fd1498Szrj 	(*sp->delete_value)(sp->root->value);
376*38fd1498Szrj       sp->root->value = value;
377*38fd1498Szrj     }
378*38fd1498Szrj   else
379*38fd1498Szrj     {
380*38fd1498Szrj       /* Create a new node, and insert it at the root.  */
381*38fd1498Szrj       splay_tree_node node;
382*38fd1498Szrj 
383*38fd1498Szrj       node = ((splay_tree_node)
384*38fd1498Szrj 	      (*sp->allocate) (sizeof (struct splay_tree_node_s),
385*38fd1498Szrj 			       sp->allocate_data));
386*38fd1498Szrj       node->key = key;
387*38fd1498Szrj       node->value = value;
388*38fd1498Szrj 
389*38fd1498Szrj       if (!sp->root)
390*38fd1498Szrj 	node->left = node->right = 0;
391*38fd1498Szrj       else if (comparison < 0)
392*38fd1498Szrj 	{
393*38fd1498Szrj 	  node->left = sp->root;
394*38fd1498Szrj 	  node->right = node->left->right;
395*38fd1498Szrj 	  node->left->right = 0;
396*38fd1498Szrj 	}
397*38fd1498Szrj       else
398*38fd1498Szrj 	{
399*38fd1498Szrj 	  node->right = sp->root;
400*38fd1498Szrj 	  node->left = node->right->left;
401*38fd1498Szrj 	  node->right->left = 0;
402*38fd1498Szrj 	}
403*38fd1498Szrj 
404*38fd1498Szrj       sp->root = node;
405*38fd1498Szrj     }
406*38fd1498Szrj 
407*38fd1498Szrj   return sp->root;
408*38fd1498Szrj }
409*38fd1498Szrj 
410*38fd1498Szrj /* Remove KEY from SP.  It is not an error if it did not exist.  */
411*38fd1498Szrj 
412*38fd1498Szrj void
splay_tree_remove(splay_tree sp,splay_tree_key key)413*38fd1498Szrj splay_tree_remove (splay_tree sp, splay_tree_key key)
414*38fd1498Szrj {
415*38fd1498Szrj   splay_tree_splay (sp, key);
416*38fd1498Szrj 
417*38fd1498Szrj   if (sp->root && (*sp->comp) (sp->root->key, key) == 0)
418*38fd1498Szrj     {
419*38fd1498Szrj       splay_tree_node left, right;
420*38fd1498Szrj 
421*38fd1498Szrj       left = sp->root->left;
422*38fd1498Szrj       right = sp->root->right;
423*38fd1498Szrj 
424*38fd1498Szrj       /* Delete the root node itself.  */
425*38fd1498Szrj       if (sp->delete_value)
426*38fd1498Szrj 	(*sp->delete_value) (sp->root->value);
427*38fd1498Szrj       (*sp->deallocate) (sp->root, sp->allocate_data);
428*38fd1498Szrj 
429*38fd1498Szrj       /* One of the children is now the root.  Doesn't matter much
430*38fd1498Szrj 	 which, so long as we preserve the properties of the tree.  */
431*38fd1498Szrj       if (left)
432*38fd1498Szrj 	{
433*38fd1498Szrj 	  sp->root = left;
434*38fd1498Szrj 
435*38fd1498Szrj 	  /* If there was a right child as well, hang it off the
436*38fd1498Szrj 	     right-most leaf of the left child.  */
437*38fd1498Szrj 	  if (right)
438*38fd1498Szrj 	    {
439*38fd1498Szrj 	      while (left->right)
440*38fd1498Szrj 		left = left->right;
441*38fd1498Szrj 	      left->right = right;
442*38fd1498Szrj 	    }
443*38fd1498Szrj 	}
444*38fd1498Szrj       else
445*38fd1498Szrj 	sp->root = right;
446*38fd1498Szrj     }
447*38fd1498Szrj }
448*38fd1498Szrj 
449*38fd1498Szrj /* Lookup KEY in SP, returning VALUE if present, and NULL
450*38fd1498Szrj    otherwise.  */
451*38fd1498Szrj 
452*38fd1498Szrj splay_tree_node
splay_tree_lookup(splay_tree sp,splay_tree_key key)453*38fd1498Szrj splay_tree_lookup (splay_tree sp, splay_tree_key key)
454*38fd1498Szrj {
455*38fd1498Szrj   splay_tree_splay (sp, key);
456*38fd1498Szrj 
457*38fd1498Szrj   if (sp->root && (*sp->comp)(sp->root->key, key) == 0)
458*38fd1498Szrj     return sp->root;
459*38fd1498Szrj   else
460*38fd1498Szrj     return 0;
461*38fd1498Szrj }
462*38fd1498Szrj 
463*38fd1498Szrj /* Return the node in SP with the greatest key.  */
464*38fd1498Szrj 
465*38fd1498Szrj splay_tree_node
splay_tree_max(splay_tree sp)466*38fd1498Szrj splay_tree_max (splay_tree sp)
467*38fd1498Szrj {
468*38fd1498Szrj   splay_tree_node n = sp->root;
469*38fd1498Szrj 
470*38fd1498Szrj   if (!n)
471*38fd1498Szrj     return NULL;
472*38fd1498Szrj 
473*38fd1498Szrj   while (n->right)
474*38fd1498Szrj     n = n->right;
475*38fd1498Szrj 
476*38fd1498Szrj   return n;
477*38fd1498Szrj }
478*38fd1498Szrj 
479*38fd1498Szrj /* Return the node in SP with the smallest key.  */
480*38fd1498Szrj 
481*38fd1498Szrj splay_tree_node
splay_tree_min(splay_tree sp)482*38fd1498Szrj splay_tree_min (splay_tree sp)
483*38fd1498Szrj {
484*38fd1498Szrj   splay_tree_node n = sp->root;
485*38fd1498Szrj 
486*38fd1498Szrj   if (!n)
487*38fd1498Szrj     return NULL;
488*38fd1498Szrj 
489*38fd1498Szrj   while (n->left)
490*38fd1498Szrj     n = n->left;
491*38fd1498Szrj 
492*38fd1498Szrj   return n;
493*38fd1498Szrj }
494*38fd1498Szrj 
495*38fd1498Szrj /* Return the immediate predecessor KEY, or NULL if there is no
496*38fd1498Szrj    predecessor.  KEY need not be present in the tree.  */
497*38fd1498Szrj 
498*38fd1498Szrj splay_tree_node
splay_tree_predecessor(splay_tree sp,splay_tree_key key)499*38fd1498Szrj splay_tree_predecessor (splay_tree sp, splay_tree_key key)
500*38fd1498Szrj {
501*38fd1498Szrj   int comparison;
502*38fd1498Szrj   splay_tree_node node;
503*38fd1498Szrj 
504*38fd1498Szrj   /* If the tree is empty, there is certainly no predecessor.  */
505*38fd1498Szrj   if (!sp->root)
506*38fd1498Szrj     return NULL;
507*38fd1498Szrj 
508*38fd1498Szrj   /* Splay the tree around KEY.  That will leave either the KEY
509*38fd1498Szrj      itself, its predecessor, or its successor at the root.  */
510*38fd1498Szrj   splay_tree_splay (sp, key);
511*38fd1498Szrj   comparison = (*sp->comp)(sp->root->key, key);
512*38fd1498Szrj 
513*38fd1498Szrj   /* If the predecessor is at the root, just return it.  */
514*38fd1498Szrj   if (comparison < 0)
515*38fd1498Szrj     return sp->root;
516*38fd1498Szrj 
517*38fd1498Szrj   /* Otherwise, find the rightmost element of the left subtree.  */
518*38fd1498Szrj   node = sp->root->left;
519*38fd1498Szrj   if (node)
520*38fd1498Szrj     while (node->right)
521*38fd1498Szrj       node = node->right;
522*38fd1498Szrj 
523*38fd1498Szrj   return node;
524*38fd1498Szrj }
525*38fd1498Szrj 
526*38fd1498Szrj /* Return the immediate successor KEY, or NULL if there is no
527*38fd1498Szrj    successor.  KEY need not be present in the tree.  */
528*38fd1498Szrj 
529*38fd1498Szrj splay_tree_node
splay_tree_successor(splay_tree sp,splay_tree_key key)530*38fd1498Szrj splay_tree_successor (splay_tree sp, splay_tree_key key)
531*38fd1498Szrj {
532*38fd1498Szrj   int comparison;
533*38fd1498Szrj   splay_tree_node node;
534*38fd1498Szrj 
535*38fd1498Szrj   /* If the tree is empty, there is certainly no successor.  */
536*38fd1498Szrj   if (!sp->root)
537*38fd1498Szrj     return NULL;
538*38fd1498Szrj 
539*38fd1498Szrj   /* Splay the tree around KEY.  That will leave either the KEY
540*38fd1498Szrj      itself, its predecessor, or its successor at the root.  */
541*38fd1498Szrj   splay_tree_splay (sp, key);
542*38fd1498Szrj   comparison = (*sp->comp)(sp->root->key, key);
543*38fd1498Szrj 
544*38fd1498Szrj   /* If the successor is at the root, just return it.  */
545*38fd1498Szrj   if (comparison > 0)
546*38fd1498Szrj     return sp->root;
547*38fd1498Szrj 
548*38fd1498Szrj   /* Otherwise, find the leftmost element of the right subtree.  */
549*38fd1498Szrj   node = sp->root->right;
550*38fd1498Szrj   if (node)
551*38fd1498Szrj     while (node->left)
552*38fd1498Szrj       node = node->left;
553*38fd1498Szrj 
554*38fd1498Szrj   return node;
555*38fd1498Szrj }
556*38fd1498Szrj 
557*38fd1498Szrj /* Call FN, passing it the DATA, for every node in SP, following an
558*38fd1498Szrj    in-order traversal.  If FN every returns a non-zero value, the
559*38fd1498Szrj    iteration ceases immediately, and the value is returned.
560*38fd1498Szrj    Otherwise, this function returns 0.  */
561*38fd1498Szrj 
562*38fd1498Szrj int
splay_tree_foreach(splay_tree sp,splay_tree_foreach_fn fn,void * data)563*38fd1498Szrj splay_tree_foreach (splay_tree sp, splay_tree_foreach_fn fn, void *data)
564*38fd1498Szrj {
565*38fd1498Szrj   return splay_tree_foreach_helper (sp->root, fn, data);
566*38fd1498Szrj }
567*38fd1498Szrj 
568*38fd1498Szrj /* Splay-tree comparison function, treating the keys as ints.  */
569*38fd1498Szrj 
570*38fd1498Szrj int
splay_tree_compare_ints(splay_tree_key k1,splay_tree_key k2)571*38fd1498Szrj splay_tree_compare_ints (splay_tree_key k1, splay_tree_key k2)
572*38fd1498Szrj {
573*38fd1498Szrj   if ((int) k1 < (int) k2)
574*38fd1498Szrj     return -1;
575*38fd1498Szrj   else if ((int) k1 > (int) k2)
576*38fd1498Szrj     return 1;
577*38fd1498Szrj   else
578*38fd1498Szrj     return 0;
579*38fd1498Szrj }
580*38fd1498Szrj 
581*38fd1498Szrj /* Splay-tree comparison function, treating the keys as pointers.  */
582*38fd1498Szrj 
583*38fd1498Szrj int
splay_tree_compare_pointers(splay_tree_key k1,splay_tree_key k2)584*38fd1498Szrj splay_tree_compare_pointers (splay_tree_key k1, splay_tree_key k2)
585*38fd1498Szrj {
586*38fd1498Szrj   if ((char*) k1 < (char*) k2)
587*38fd1498Szrj     return -1;
588*38fd1498Szrj   else if ((char*) k1 > (char*) k2)
589*38fd1498Szrj     return 1;
590*38fd1498Szrj   else
591*38fd1498Szrj     return 0;
592*38fd1498Szrj }
593