* reload1.c (delete_output_reload): Avoid ambigous else.
[official-gcc.git] / libiberty / splay-tree.c
blob68b9f995f673cc47780be62b356a18acde2b884c
1 /* A splay-tree datatype.
2 Copyright (C) 1998 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)
10 any later version.
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 the Free
19 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 For an easily readable description of splay-trees, see:
23 Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
24 Algorithms. Harper-Collins, Inc. 1991. */
26 #if defined (IN_GCC) || defined (HAVE_CONFIG_H)
27 #include "config.h"
28 #endif
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
34 #ifndef IN_GCC
35 #include "libiberty.h"
36 #else /* IN_GCC */
37 extern char* xmalloc ();
38 #endif /* IN_GCC */
39 #include "splay-tree.h"
41 static void splay_tree_delete_helper PARAMS((splay_tree,
42 splay_tree_node));
43 static void splay_tree_splay PARAMS((splay_tree,
44 splay_tree_key));
45 static splay_tree_node splay_tree_splay_helper
46 PARAMS((splay_tree,
47 splay_tree_key,
48 splay_tree_node*,
49 splay_tree_node*,
50 splay_tree_node*));
51 static int splay_tree_foreach_helper PARAMS((splay_tree,
52 splay_tree_node,
53 splay_tree_foreach_fn,
54 void*));
56 /* Deallocate NODE (a member of SP), and all its sub-trees. */
58 static void
59 splay_tree_delete_helper (sp, node)
60 splay_tree sp;
61 splay_tree_node node;
63 if (!node)
64 return;
66 splay_tree_delete_helper (sp, node->left);
67 splay_tree_delete_helper (sp, node->right);
69 if (sp->delete_key)
70 (*sp->delete_key)(node->key);
71 if (sp->delete_value)
72 (*sp->delete_value)(node->value);
74 free ((char*) node);
77 /* Help splay SP around KEY. PARENT and GRANDPARENT are the parent
78 and grandparent, respectively, of NODE. */
80 static splay_tree_node
81 splay_tree_splay_helper (sp, key, node, parent, grandparent)
82 splay_tree sp;
83 splay_tree_key key;
84 splay_tree_node *node;
85 splay_tree_node *parent;
86 splay_tree_node *grandparent;
88 splay_tree_node *next;
89 splay_tree_node n;
90 int comparison;
92 n = *node;
94 if (!n)
95 return *parent;
97 comparison = (*sp->comp) (key, n->key);
99 if (comparison == 0)
100 /* We've found the target. */
101 next = 0;
102 else if (comparison < 0)
103 /* The target is to the left. */
104 next = &n->left;
105 else
106 /* The target is to the right. */
107 next = &n->right;
109 if (next)
111 /* Continue down the tree. */
112 n = splay_tree_splay_helper (sp, key, next, node, parent);
114 /* The recursive call will change the place to which NODE
115 points. */
116 if (*node != n)
117 return n;
120 if (!parent)
121 /* NODE is the root. We are done. */
122 return n;
124 /* First, handle the case where there is no grandparent (i.e.,
125 *PARENT is the root of the tree.) */
126 if (!grandparent)
128 if (n == (*parent)->left)
130 *node = n->right;
131 n->right = *parent;
133 else
135 *node = n->left;
136 n->left = *parent;
138 *parent = n;
139 return n;
142 /* Next handle the cases where both N and *PARENT are left children,
143 or where both are right children. */
144 if (n == (*parent)->left && *parent == (*grandparent)->left)
146 splay_tree_node p = *parent;
148 (*grandparent)->left = p->right;
149 p->right = *grandparent;
150 p->left = n->right;
151 n->right = p;
152 *grandparent = n;
153 return n;
155 else if (n == (*parent)->right && *parent == (*grandparent)->right)
157 splay_tree_node p = *parent;
159 (*grandparent)->right = p->left;
160 p->left = *grandparent;
161 p->right = n->left;
162 n->left = p;
163 *grandparent = n;
164 return n;
167 /* Finally, deal with the case where N is a left child, but *PARENT
168 is a right child, or vice versa. */
169 if (n == (*parent)->left)
171 (*parent)->left = n->right;
172 n->right = *parent;
173 (*grandparent)->right = n->left;
174 n->left = *grandparent;
175 *grandparent = n;
176 return n;
178 else
180 (*parent)->right = n->left;
181 n->left = *parent;
182 (*grandparent)->left = n->right;
183 n->right = *grandparent;
184 *grandparent = n;
185 return n;
189 /* Splay SP around KEY. */
191 static void
192 splay_tree_splay (sp, key)
193 splay_tree sp;
194 splay_tree_key key;
196 if (sp->root == 0)
197 return;
199 splay_tree_splay_helper (sp, key, &sp->root,
200 /*grandparent=*/0, /*parent=*/0);
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. */
208 static int
209 splay_tree_foreach_helper (sp, node, fn, data)
210 splay_tree sp;
211 splay_tree_node node;
212 splay_tree_foreach_fn fn;
213 void* data;
215 int val;
217 if (!node)
218 return 0;
220 val = splay_tree_foreach_helper (sp, node->left, fn, data);
221 if (val)
222 return val;
224 val = (*fn)(node, data);
225 if (val)
226 return val;
228 return splay_tree_foreach_helper (sp, node->right, fn, data);
231 /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
232 DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
233 values. */
235 splay_tree
236 splay_tree_new (compare_fn, delete_key_fn, delete_value_fn)
237 splay_tree_compare_fn compare_fn;
238 splay_tree_delete_key_fn delete_key_fn;
239 splay_tree_delete_value_fn delete_value_fn;
241 splay_tree sp = (splay_tree) xmalloc (sizeof (struct splay_tree));
242 sp->root = 0;
243 sp->comp = compare_fn;
244 sp->delete_key = delete_key_fn;
245 sp->delete_value = delete_value_fn;
247 return sp;
250 /* Deallocate SP. */
252 void
253 splay_tree_delete (sp)
254 splay_tree sp;
256 splay_tree_delete_helper (sp, sp->root);
257 free ((char*) sp);
260 /* Insert a new node (associating KEY with DATA) into SP. If a
261 previous node with the indicated KEY exists, its data is replaced
262 with the new value. */
264 void
265 splay_tree_insert (sp, key, value)
266 splay_tree sp;
267 splay_tree_key key;
268 splay_tree_value value;
270 int comparison;
272 splay_tree_splay (sp, key);
274 if (sp->root)
275 comparison = (*sp->comp)(sp->root->key, key);
277 if (sp->root && comparison == 0)
279 /* If the root of the tree already has the indicated KEY, just
280 replace the value with VALUE. */
281 if (sp->delete_value)
282 (*sp->delete_value)(sp->root->value);
283 sp->root->value = value;
285 else
287 /* Create a new node, and insert it at the root. */
288 splay_tree_node node;
290 node = (splay_tree_node) xmalloc (sizeof (struct splay_tree_node));
291 node->key = key;
292 node->value = value;
294 if (!sp->root)
295 node->left = node->right = 0;
296 else if (comparison < 0)
298 node->left = sp->root;
299 node->right = node->left->right;
300 node->left->right = 0;
302 else
304 node->right = sp->root;
305 node->left = node->right->left;
306 node->right->left = 0;
309 sp->root = node;
313 /* Lookup KEY in SP, returning VALUE if present, and NULL
314 otherwise. */
316 splay_tree_node
317 splay_tree_lookup (sp, key)
318 splay_tree sp;
319 splay_tree_key key;
321 splay_tree_splay (sp, key);
323 if (sp->root && (*sp->comp)(sp->root->key, key) == 0)
324 return sp->root;
325 else
326 return 0;
329 /* Call FN, passing it the DATA, for every node in SP, following an
330 in-order traversal. If FN every returns a non-zero value, the
331 iteration ceases immediately, and the value is returned.
332 Otherwise, this function returns 0. */
335 splay_tree_foreach (sp, fn, data)
336 splay_tree sp;
337 splay_tree_foreach_fn fn;
338 void *data;
340 return splay_tree_foreach_helper (sp, sp->root, fn, data);