1 /* A splay-tree datatype.
3 Free Software Foundation, Inc.
4 Contributed by Mark Mitchell (mark@markmitchell.com).
6 This file is part of the GNU OpenMP Library (libgomp).
8 Libgomp 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 3, or (at your option)
13 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 /* The splay tree code copied from include/splay-tree.h and adjusted,
28 so that all the data lives directly in splay_tree_node_s structure
29 and no extra allocations are needed.
31 Files including this header should before including it add:
32 typedef struct splay_tree_node_s *splay_tree_node;
33 typedef struct splay_tree_s *splay_tree;
34 typedef struct splay_tree_key_s *splay_tree_key;
35 define splay_tree_key_s structure, and define
36 splay_compare inline function. */
38 /* For an easily readable description of splay-trees, see:
40 Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
41 Algorithms. Harper-Collins, Inc. 1991.
43 The major feature of splay trees is that all basic tree operations
44 are amortized O(log n) time for a tree with n nodes. */
47 #include "splay-tree.h"
49 extern int splay_compare (splay_tree_key
, splay_tree_key
);
51 /* Rotate the edge joining the left child N with its parent P. PP is the
52 grandparents' pointer to P. */
55 rotate_left (splay_tree_node
*pp
, splay_tree_node p
, splay_tree_node n
)
64 /* Rotate the edge joining the right child N with its parent P. PP is the
65 grandparents' pointer to P. */
68 rotate_right (splay_tree_node
*pp
, splay_tree_node p
, splay_tree_node n
)
77 /* Bottom up splay of KEY. */
80 splay_tree_splay (splay_tree sp
, splay_tree_key key
)
90 cmp1
= splay_compare (key
, &n
->key
);
96 /* Left or right? If no child, then we're done. */
104 /* Next one left or right? If found or no child, we're done
105 after one rotation. */
106 cmp2
= splay_compare (key
, &c
->key
);
108 || (cmp2
< 0 && !c
->left
)
109 || (cmp2
> 0 && !c
->right
))
112 rotate_left (&sp
->root
, n
, c
);
114 rotate_right (&sp
->root
, n
, c
);
118 /* Now we have the four cases of double-rotation. */
119 if (cmp1
< 0 && cmp2
< 0)
121 rotate_left (&n
->left
, c
, c
->left
);
122 rotate_left (&sp
->root
, n
, n
->left
);
124 else if (cmp1
> 0 && cmp2
> 0)
126 rotate_right (&n
->right
, c
, c
->right
);
127 rotate_right (&sp
->root
, n
, n
->right
);
129 else if (cmp1
< 0 && cmp2
> 0)
131 rotate_right (&n
->left
, c
, c
->right
);
132 rotate_left (&sp
->root
, n
, n
->left
);
134 else if (cmp1
> 0 && cmp2
< 0)
136 rotate_left (&n
->right
, c
, c
->left
);
137 rotate_right (&sp
->root
, n
, n
->right
);
142 /* Insert a new NODE into SP. The NODE shouldn't exist in the tree. */
144 attribute_hidden
void
145 splay_tree_insert (splay_tree sp
, splay_tree_node node
)
149 splay_tree_splay (sp
, &node
->key
);
152 comparison
= splay_compare (&sp
->root
->key
, &node
->key
);
154 if (sp
->root
&& comparison
== 0)
155 gomp_fatal ("Duplicate node");
158 /* Insert it at the root. */
159 if (sp
->root
== NULL
)
160 node
->left
= node
->right
= NULL
;
161 else if (comparison
< 0)
163 node
->left
= sp
->root
;
164 node
->right
= node
->left
->right
;
165 node
->left
->right
= NULL
;
169 node
->right
= sp
->root
;
170 node
->left
= node
->right
->left
;
171 node
->right
->left
= NULL
;
178 /* Remove node with KEY from SP. It is not an error if it did not exist. */
180 attribute_hidden
void
181 splay_tree_remove (splay_tree sp
, splay_tree_key key
)
183 splay_tree_splay (sp
, key
);
185 if (sp
->root
&& splay_compare (&sp
->root
->key
, key
) == 0)
187 splay_tree_node left
, right
;
189 left
= sp
->root
->left
;
190 right
= sp
->root
->right
;
192 /* One of the children is now the root. Doesn't matter much
193 which, so long as we preserve the properties of the tree. */
198 /* If there was a right child as well, hang it off the
199 right-most leaf of the left child. */
212 /* Lookup KEY in SP, returning NODE if present, and NULL
215 attribute_hidden splay_tree_key
216 splay_tree_lookup (splay_tree sp
, splay_tree_key key
)
218 splay_tree_splay (sp
, key
);
220 if (sp
->root
&& splay_compare (&sp
->root
->key
, key
) == 0)
221 return &sp
->root
->key
;