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. */
46 /* The nodes in the splay tree. */
47 struct splay_tree_node_s
{
48 struct splay_tree_key_s key
;
49 /* The left and right children, respectively. */
51 splay_tree_node right
;
59 /* Rotate the edge joining the left child N with its parent P. PP is the
60 grandparents' pointer to P. */
63 rotate_left (splay_tree_node
*pp
, splay_tree_node p
, splay_tree_node n
)
72 /* Rotate the edge joining the right child N with its parent P. PP is the
73 grandparents' pointer to P. */
76 rotate_right (splay_tree_node
*pp
, splay_tree_node p
, splay_tree_node n
)
85 /* Bottom up splay of KEY. */
88 splay_tree_splay (splay_tree sp
, splay_tree_key key
)
98 cmp1
= splay_compare (key
, &n
->key
);
104 /* Left or right? If no child, then we're done. */
112 /* Next one left or right? If found or no child, we're done
113 after one rotation. */
114 cmp2
= splay_compare (key
, &c
->key
);
116 || (cmp2
< 0 && !c
->left
)
117 || (cmp2
> 0 && !c
->right
))
120 rotate_left (&sp
->root
, n
, c
);
122 rotate_right (&sp
->root
, n
, c
);
126 /* Now we have the four cases of double-rotation. */
127 if (cmp1
< 0 && cmp2
< 0)
129 rotate_left (&n
->left
, c
, c
->left
);
130 rotate_left (&sp
->root
, n
, n
->left
);
132 else if (cmp1
> 0 && cmp2
> 0)
134 rotate_right (&n
->right
, c
, c
->right
);
135 rotate_right (&sp
->root
, n
, n
->right
);
137 else if (cmp1
< 0 && cmp2
> 0)
139 rotate_right (&n
->left
, c
, c
->right
);
140 rotate_left (&sp
->root
, n
, n
->left
);
142 else if (cmp1
> 0 && cmp2
< 0)
144 rotate_left (&n
->right
, c
, c
->left
);
145 rotate_right (&sp
->root
, n
, n
->right
);
150 /* Insert a new NODE into SP. The NODE shouldn't exist in the tree. */
153 splay_tree_insert (splay_tree sp
, splay_tree_node node
)
157 splay_tree_splay (sp
, &node
->key
);
160 comparison
= splay_compare (&sp
->root
->key
, &node
->key
);
162 if (sp
->root
&& comparison
== 0)
166 /* Insert it at the root. */
167 if (sp
->root
== NULL
)
168 node
->left
= node
->right
= NULL
;
169 else if (comparison
< 0)
171 node
->left
= sp
->root
;
172 node
->right
= node
->left
->right
;
173 node
->left
->right
= NULL
;
177 node
->right
= sp
->root
;
178 node
->left
= node
->right
->left
;
179 node
->right
->left
= NULL
;
186 /* Remove node with KEY from SP. It is not an error if it did not exist. */
189 splay_tree_remove (splay_tree sp
, splay_tree_key key
)
191 splay_tree_splay (sp
, key
);
193 if (sp
->root
&& splay_compare (&sp
->root
->key
, key
) == 0)
195 splay_tree_node left
, right
;
197 left
= sp
->root
->left
;
198 right
= sp
->root
->right
;
200 /* One of the children is now the root. Doesn't matter much
201 which, so long as we preserve the properties of the tree. */
206 /* If there was a right child as well, hang it off the
207 right-most leaf of the left child. */
220 /* Lookup KEY in SP, returning NODE if present, and NULL
223 static splay_tree_key
224 splay_tree_lookup (splay_tree sp
, splay_tree_key key
)
226 splay_tree_splay (sp
, key
);
228 if (sp
->root
&& splay_compare (&sp
->root
->key
, key
) == 0)
229 return &sp
->root
->key
;