blob: 4eca71d1004f10ad49b9515c825fb4babc9e4e9a [file] [log] [blame]
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_SPLAY_TREE_INL_H_
29#define V8_SPLAY_TREE_INL_H_
30
31#include "splay-tree.h"
32
33namespace v8 {
34namespace internal {
35
36
37template<typename Config, class Allocator>
38SplayTree<Config, Allocator>::~SplayTree() {
39 NodeDeleter deleter;
40 ForEachNode(&deleter);
41}
42
43
44template<typename Config, class Allocator>
rossberg@chromium.org400388e2012-06-06 09:29:22 +000045bool SplayTree<Config, Allocator>::Insert(const Key& key,
mmassi@chromium.org7028c052012-06-13 11:51:58 +000046 Locator* locator) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000047 if (is_empty()) {
48 // If the tree is empty, insert the new node.
mmassi@chromium.org7028c052012-06-13 11:51:58 +000049 root_ = new(allocator_) Node(key, Config::NoValue());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000050 } else {
51 // Splay on the key to move the last node on the search path
52 // for the key to the root of the tree.
53 Splay(key);
54 // Ignore repeated insertions with the same key.
55 int cmp = Config::Compare(key, root_->key_);
56 if (cmp == 0) {
57 locator->bind(root_);
58 return false;
59 }
60 // Insert the new node.
mmassi@chromium.org7028c052012-06-13 11:51:58 +000061 Node* node = new(allocator_) Node(key, Config::NoValue());
fschneider@chromium.org086aac62010-03-17 13:18:24 +000062 InsertInternal(cmp, node);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000063 }
64 locator->bind(root_);
65 return true;
66}
67
68
69template<typename Config, class Allocator>
fschneider@chromium.org086aac62010-03-17 13:18:24 +000070void SplayTree<Config, Allocator>::InsertInternal(int cmp, Node* node) {
71 if (cmp > 0) {
72 node->left_ = root_;
73 node->right_ = root_->right_;
74 root_->right_ = NULL;
75 } else {
76 node->right_ = root_;
77 node->left_ = root_->left_;
78 root_->left_ = NULL;
79 }
80 root_ = node;
81}
82
83
84template<typename Config, class Allocator>
85bool SplayTree<Config, Allocator>::FindInternal(const Key& key) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000086 if (is_empty())
87 return false;
88 Splay(key);
fschneider@chromium.org086aac62010-03-17 13:18:24 +000089 return Config::Compare(key, root_->key_) == 0;
90}
91
92
93template<typename Config, class Allocator>
94bool SplayTree<Config, Allocator>::Find(const Key& key, Locator* locator) {
95 if (FindInternal(key)) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000096 locator->bind(root_);
97 return true;
98 } else {
99 return false;
100 }
101}
102
103
104template<typename Config, class Allocator>
105bool SplayTree<Config, Allocator>::FindGreatestLessThan(const Key& key,
106 Locator* locator) {
107 if (is_empty())
108 return false;
109 // Splay on the key to move the node with the given key or the last
110 // node on the search path to the top of the tree.
111 Splay(key);
112 // Now the result is either the root node or the greatest node in
113 // the left subtree.
114 int cmp = Config::Compare(root_->key_, key);
115 if (cmp <= 0) {
116 locator->bind(root_);
117 return true;
118 } else {
119 Node* temp = root_;
120 root_ = root_->left_;
121 bool result = FindGreatest(locator);
122 root_ = temp;
123 return result;
124 }
125}
126
127
128template<typename Config, class Allocator>
129bool SplayTree<Config, Allocator>::FindLeastGreaterThan(const Key& key,
130 Locator* locator) {
131 if (is_empty())
132 return false;
133 // Splay on the key to move the node with the given key or the last
134 // node on the search path to the top of the tree.
135 Splay(key);
136 // Now the result is either the root node or the least node in
137 // the right subtree.
138 int cmp = Config::Compare(root_->key_, key);
139 if (cmp >= 0) {
140 locator->bind(root_);
141 return true;
142 } else {
143 Node* temp = root_;
144 root_ = root_->right_;
145 bool result = FindLeast(locator);
146 root_ = temp;
147 return result;
148 }
149}
150
151
152template<typename Config, class Allocator>
153bool SplayTree<Config, Allocator>::FindGreatest(Locator* locator) {
154 if (is_empty())
155 return false;
156 Node* current = root_;
157 while (current->right_ != NULL)
158 current = current->right_;
159 locator->bind(current);
160 return true;
161}
162
163
164template<typename Config, class Allocator>
165bool SplayTree<Config, Allocator>::FindLeast(Locator* locator) {
166 if (is_empty())
167 return false;
168 Node* current = root_;
169 while (current->left_ != NULL)
170 current = current->left_;
171 locator->bind(current);
172 return true;
173}
174
175
176template<typename Config, class Allocator>
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000177bool SplayTree<Config, Allocator>::Move(const Key& old_key,
178 const Key& new_key) {
179 if (!FindInternal(old_key))
180 return false;
181 Node* node_to_move = root_;
182 RemoveRootNode(old_key);
183 Splay(new_key);
184 int cmp = Config::Compare(new_key, root_->key_);
185 if (cmp == 0) {
186 // A node with the target key already exists.
187 delete node_to_move;
188 return false;
189 }
190 node_to_move->key_ = new_key;
191 InsertInternal(cmp, node_to_move);
192 return true;
193}
194
195
196template<typename Config, class Allocator>
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000197bool SplayTree<Config, Allocator>::Remove(const Key& key) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000198 if (!FindInternal(key))
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000199 return false;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000200 Node* node_to_remove = root_;
201 RemoveRootNode(key);
202 delete node_to_remove;
203 return true;
204}
205
206
207template<typename Config, class Allocator>
208void SplayTree<Config, Allocator>::RemoveRootNode(const Key& key) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000209 if (root_->left_ == NULL) {
210 // No left child, so the new tree is just the right child.
211 root_ = root_->right_;
212 } else {
213 // Left child exists.
214 Node* right = root_->right_;
215 // Make the original left child the new root.
216 root_ = root_->left_;
217 // Splay to make sure that the new root has an empty right child.
218 Splay(key);
219 // Insert the original right child as the right child of the new
220 // root.
221 root_->right_ = right;
222 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000223}
224
225
226template<typename Config, class Allocator>
227void SplayTree<Config, Allocator>::Splay(const Key& key) {
228 if (is_empty())
229 return;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000230 Node dummy_node(Config::kNoKey, Config::NoValue());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000231 // Create a dummy node. The use of the dummy node is a bit
232 // counter-intuitive: The right child of the dummy node will hold
233 // the L tree of the algorithm. The left child of the dummy node
234 // will hold the R tree of the algorithm. Using a dummy node, left
235 // and right will always be nodes and we avoid special cases.
236 Node* dummy = &dummy_node;
237 Node* left = dummy;
238 Node* right = dummy;
239 Node* current = root_;
240 while (true) {
241 int cmp = Config::Compare(key, current->key_);
242 if (cmp < 0) {
243 if (current->left_ == NULL)
244 break;
245 if (Config::Compare(key, current->left_->key_) < 0) {
246 // Rotate right.
247 Node* temp = current->left_;
248 current->left_ = temp->right_;
249 temp->right_ = current;
250 current = temp;
251 if (current->left_ == NULL)
252 break;
253 }
254 // Link right.
255 right->left_ = current;
256 right = current;
257 current = current->left_;
258 } else if (cmp > 0) {
259 if (current->right_ == NULL)
260 break;
261 if (Config::Compare(key, current->right_->key_) > 0) {
262 // Rotate left.
263 Node* temp = current->right_;
264 current->right_ = temp->left_;
265 temp->left_ = current;
266 current = temp;
267 if (current->right_ == NULL)
268 break;
269 }
270 // Link left.
271 left->right_ = current;
272 left = current;
273 current = current->right_;
274 } else {
275 break;
276 }
277 }
278 // Assemble.
279 left->right_ = current->left_;
280 right->left_ = current->right_;
281 current->left_ = dummy->right_;
282 current->right_ = dummy->left_;
283 root_ = current;
284}
285
286
287template <typename Config, class Allocator> template <class Callback>
288void SplayTree<Config, Allocator>::ForEach(Callback* callback) {
289 NodeToPairAdaptor<Callback> callback_adaptor(callback);
290 ForEachNode(&callback_adaptor);
291}
292
293
294template <typename Config, class Allocator> template <class Callback>
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000295void SplayTree<Config, Allocator>::ForEachNode(Callback* callback) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000296 // Pre-allocate some space for tiny trees.
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000297 List<Node*, Allocator> nodes_to_visit(10, allocator_);
298 if (root_ != NULL) nodes_to_visit.Add(root_, allocator_);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000299 int pos = 0;
300 while (pos < nodes_to_visit.length()) {
301 Node* node = nodes_to_visit[pos++];
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000302 if (node->left() != NULL) nodes_to_visit.Add(node->left(), allocator_);
303 if (node->right() != NULL) nodes_to_visit.Add(node->right(), allocator_);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000304 callback->Call(node);
305 }
306}
307
308
309} } // namespace v8::internal
310
311#endif // V8_SPLAY_TREE_INL_H_