1 // 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
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.
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.
28 #ifndef V8_SPLAY_TREE_INL_H_
29 #define V8_SPLAY_TREE_INL_H_
31 #include "splay-tree.h"
37 template<typename Config, class Allocator>
38 SplayTree<Config, Allocator>::~SplayTree() {
40 ForEachNode(&deleter);
44 template<typename Config, class Allocator>
45 bool SplayTree<Config, Allocator>::Insert(const Key& key, Locator* locator) {
47 // If the tree is empty, insert the new node.
48 root_ = new Node(key, Config::NoValue());
50 // Splay on the key to move the last node on the search path
51 // for the key to the root of the tree.
53 // Ignore repeated insertions with the same key.
54 int cmp = Config::Compare(key, root_->key_);
59 // Insert the new node.
60 Node* node = new Node(key, Config::NoValue());
61 InsertInternal(cmp, node);
68 template<typename Config, class Allocator>
69 void SplayTree<Config, Allocator>::InsertInternal(int cmp, Node* node) {
72 node->right_ = root_->right_;
76 node->left_ = root_->left_;
83 template<typename Config, class Allocator>
84 bool SplayTree<Config, Allocator>::FindInternal(const Key& key) {
88 return Config::Compare(key, root_->key_) == 0;
92 template<typename Config, class Allocator>
93 bool SplayTree<Config, Allocator>::Find(const Key& key, Locator* locator) {
94 if (FindInternal(key)) {
103 template<typename Config, class Allocator>
104 bool SplayTree<Config, Allocator>::FindGreatestLessThan(const Key& key,
108 // Splay on the key to move the node with the given key or the last
109 // node on the search path to the top of the tree.
111 // Now the result is either the root node or the greatest node in
113 int cmp = Config::Compare(root_->key_, key);
115 locator->bind(root_);
119 root_ = root_->left_;
120 bool result = FindGreatest(locator);
127 template<typename Config, class Allocator>
128 bool SplayTree<Config, Allocator>::FindLeastGreaterThan(const Key& key,
132 // Splay on the key to move the node with the given key or the last
133 // node on the search path to the top of the tree.
135 // Now the result is either the root node or the least node in
136 // the right subtree.
137 int cmp = Config::Compare(root_->key_, key);
139 locator->bind(root_);
143 root_ = root_->right_;
144 bool result = FindLeast(locator);
151 template<typename Config, class Allocator>
152 bool SplayTree<Config, Allocator>::FindGreatest(Locator* locator) {
155 Node* current = root_;
156 while (current->right_ != NULL)
157 current = current->right_;
158 locator->bind(current);
163 template<typename Config, class Allocator>
164 bool SplayTree<Config, Allocator>::FindLeast(Locator* locator) {
167 Node* current = root_;
168 while (current->left_ != NULL)
169 current = current->left_;
170 locator->bind(current);
175 template<typename Config, class Allocator>
176 bool SplayTree<Config, Allocator>::Move(const Key& old_key,
177 const Key& new_key) {
178 if (!FindInternal(old_key))
180 Node* node_to_move = root_;
181 RemoveRootNode(old_key);
183 int cmp = Config::Compare(new_key, root_->key_);
185 // A node with the target key already exists.
189 node_to_move->key_ = new_key;
190 InsertInternal(cmp, node_to_move);
195 template<typename Config, class Allocator>
196 bool SplayTree<Config, Allocator>::Remove(const Key& key) {
197 if (!FindInternal(key))
199 Node* node_to_remove = root_;
201 delete node_to_remove;
206 template<typename Config, class Allocator>
207 void SplayTree<Config, Allocator>::RemoveRootNode(const Key& key) {
208 if (root_->left_ == NULL) {
209 // No left child, so the new tree is just the right child.
210 root_ = root_->right_;
212 // Left child exists.
213 Node* right = root_->right_;
214 // Make the original left child the new root.
215 root_ = root_->left_;
216 // Splay to make sure that the new root has an empty right child.
218 // Insert the original right child as the right child of the new
220 root_->right_ = right;
225 template<typename Config, class Allocator>
226 void SplayTree<Config, Allocator>::Splay(const Key& key) {
229 Node dummy_node(Config::kNoKey, Config::NoValue());
230 // Create a dummy node. The use of the dummy node is a bit
231 // counter-intuitive: The right child of the dummy node will hold
232 // the L tree of the algorithm. The left child of the dummy node
233 // will hold the R tree of the algorithm. Using a dummy node, left
234 // and right will always be nodes and we avoid special cases.
235 Node* dummy = &dummy_node;
238 Node* current = root_;
240 int cmp = Config::Compare(key, current->key_);
242 if (current->left_ == NULL)
244 if (Config::Compare(key, current->left_->key_) < 0) {
246 Node* temp = current->left_;
247 current->left_ = temp->right_;
248 temp->right_ = current;
250 if (current->left_ == NULL)
254 right->left_ = current;
256 current = current->left_;
257 } else if (cmp > 0) {
258 if (current->right_ == NULL)
260 if (Config::Compare(key, current->right_->key_) > 0) {
262 Node* temp = current->right_;
263 current->right_ = temp->left_;
264 temp->left_ = current;
266 if (current->right_ == NULL)
270 left->right_ = current;
272 current = current->right_;
278 left->right_ = current->left_;
279 right->left_ = current->right_;
280 current->left_ = dummy->right_;
281 current->right_ = dummy->left_;
286 template <typename Config, class Allocator> template <class Callback>
287 void SplayTree<Config, Allocator>::ForEach(Callback* callback) {
288 NodeToPairAdaptor<Callback> callback_adaptor(callback);
289 ForEachNode(&callback_adaptor);
293 template <typename Config, class Allocator> template <class Callback>
294 void SplayTree<Config, Allocator>::ForEachNode(Callback* callback) {
295 // Pre-allocate some space for tiny trees.
296 List<Node*, Allocator> nodes_to_visit(10);
297 if (root_ != NULL) nodes_to_visit.Add(root_);
299 while (pos < nodes_to_visit.length()) {
300 Node* node = nodes_to_visit[pos++];
301 if (node->left() != NULL) nodes_to_visit.Add(node->left());
302 if (node->right() != NULL) nodes_to_visit.Add(node->right());
303 callback->Call(node);
308 } } // namespace v8::internal
310 #endif // V8_SPLAY_TREE_INL_H_