| OLD | NEW |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #define V8_SPLAY_TREE_INL_H_ | 29 #define V8_SPLAY_TREE_INL_H_ |
| 30 | 30 |
| 31 #include "splay-tree.h" | 31 #include "splay-tree.h" |
| 32 | 32 |
| 33 namespace v8 { | 33 namespace v8 { |
| 34 namespace internal { | 34 namespace internal { |
| 35 | 35 |
| 36 | 36 |
| 37 template<typename Config, class Allocator> | 37 template<typename Config, class Allocator> |
| 38 SplayTree<Config, Allocator>::~SplayTree() { | 38 SplayTree<Config, Allocator>::~SplayTree() { |
| 39 NodeDeleter deleter; | 39 NodeDeleter deleter(&allocator_); |
| 40 ForEachNode(&deleter); | 40 ForEachNode(&deleter); |
| 41 } | 41 } |
| 42 | 42 |
| 43 | 43 |
| 44 template<typename Config, class Allocator> | 44 template<typename Config, class Allocator> |
| 45 bool SplayTree<Config, Allocator>::Insert(const Key& key, Locator* locator) { | 45 bool SplayTree<Config, Allocator>::Insert(const Key& key, Locator* locator) { |
| 46 if (is_empty()) { | 46 if (is_empty()) { |
| 47 // If the tree is empty, insert the new node. | 47 // If the tree is empty, insert the new node. |
| 48 root_ = new Node(key, Config::kNoValue); | 48 root_ = new(&allocator_) Node(key, Config::kNoValue); |
| 49 } else { | 49 } else { |
| 50 // Splay on the key to move the last node on the search path | 50 // Splay on the key to move the last node on the search path |
| 51 // for the key to the root of the tree. | 51 // for the key to the root of the tree. |
| 52 Splay(key); | 52 Splay(key); |
| 53 // Ignore repeated insertions with the same key. | 53 // Ignore repeated insertions with the same key. |
| 54 int cmp = Config::Compare(key, root_->key_); | 54 int cmp = Config::Compare(key, root_->key_); |
| 55 if (cmp == 0) { | 55 if (cmp == 0) { |
| 56 locator->bind(root_); | 56 locator->bind(root_); |
| 57 return false; | 57 return false; |
| 58 } | 58 } |
| 59 // Insert the new node. | 59 // Insert the new node. |
| 60 Node* node = new Node(key, Config::kNoValue); | 60 Node* node = new(&allocator_) Node(key, Config::kNoValue); |
| 61 InsertInternal(cmp, node); | 61 InsertInternal(cmp, node); |
| 62 } | 62 } |
| 63 locator->bind(root_); | 63 locator->bind(root_); |
| 64 return true; | 64 return true; |
| 65 } | 65 } |
| 66 | 66 |
| 67 | 67 |
| 68 template<typename Config, class Allocator> | 68 template<typename Config, class Allocator> |
| 69 void SplayTree<Config, Allocator>::InsertInternal(int cmp, Node* node) { | 69 void SplayTree<Config, Allocator>::InsertInternal(int cmp, Node* node) { |
| 70 if (cmp > 0) { | 70 if (cmp > 0) { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 bool SplayTree<Config, Allocator>::Move(const Key& old_key, | 176 bool SplayTree<Config, Allocator>::Move(const Key& old_key, |
| 177 const Key& new_key) { | 177 const Key& new_key) { |
| 178 if (!FindInternal(old_key)) | 178 if (!FindInternal(old_key)) |
| 179 return false; | 179 return false; |
| 180 Node* node_to_move = root_; | 180 Node* node_to_move = root_; |
| 181 RemoveRootNode(old_key); | 181 RemoveRootNode(old_key); |
| 182 Splay(new_key); | 182 Splay(new_key); |
| 183 int cmp = Config::Compare(new_key, root_->key_); | 183 int cmp = Config::Compare(new_key, root_->key_); |
| 184 if (cmp == 0) { | 184 if (cmp == 0) { |
| 185 // A node with the target key already exists. | 185 // A node with the target key already exists. |
| 186 delete node_to_move; | 186 Node::Delete(&allocator_, node_to_move); |
| 187 return false; | 187 return false; |
| 188 } | 188 } |
| 189 node_to_move->key_ = new_key; | 189 node_to_move->key_ = new_key; |
| 190 InsertInternal(cmp, node_to_move); | 190 InsertInternal(cmp, node_to_move); |
| 191 return true; | 191 return true; |
| 192 } | 192 } |
| 193 | 193 |
| 194 | 194 |
| 195 template<typename Config, class Allocator> | 195 template<typename Config, class Allocator> |
| 196 bool SplayTree<Config, Allocator>::Remove(const Key& key) { | 196 bool SplayTree<Config, Allocator>::Remove(const Key& key) { |
| 197 if (!FindInternal(key)) | 197 if (!FindInternal(key)) |
| 198 return false; | 198 return false; |
| 199 Node* node_to_remove = root_; | 199 Node* node_to_remove = root_; |
| 200 RemoveRootNode(key); | 200 RemoveRootNode(key); |
| 201 delete node_to_remove; | 201 Node::Delete(&allocator_, node_to_remove); |
| 202 return true; | 202 return true; |
| 203 } | 203 } |
| 204 | 204 |
| 205 | 205 |
| 206 template<typename Config, class Allocator> | 206 template<typename Config, class Allocator> |
| 207 void SplayTree<Config, Allocator>::RemoveRootNode(const Key& key) { | 207 void SplayTree<Config, Allocator>::RemoveRootNode(const Key& key) { |
| 208 if (root_->left_ == NULL) { | 208 if (root_->left_ == NULL) { |
| 209 // No left child, so the new tree is just the right child. | 209 // No left child, so the new tree is just the right child. |
| 210 root_ = root_->right_; | 210 root_ = root_->right_; |
| 211 } else { | 211 } else { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 template <typename Config, class Allocator> template <class Callback> | 286 template <typename Config, class Allocator> template <class Callback> |
| 287 void SplayTree<Config, Allocator>::ForEach(Callback* callback) { | 287 void SplayTree<Config, Allocator>::ForEach(Callback* callback) { |
| 288 NodeToPairAdaptor<Callback> callback_adaptor(callback); | 288 NodeToPairAdaptor<Callback> callback_adaptor(callback); |
| 289 ForEachNode(&callback_adaptor); | 289 ForEachNode(&callback_adaptor); |
| 290 } | 290 } |
| 291 | 291 |
| 292 | 292 |
| 293 template <typename Config, class Allocator> template <class Callback> | 293 template <typename Config, class Allocator> template <class Callback> |
| 294 void SplayTree<Config, Allocator>::ForEachNode(Callback* callback) { | 294 void SplayTree<Config, Allocator>::ForEachNode(Callback* callback) { |
| 295 // Pre-allocate some space for tiny trees. | 295 // Pre-allocate some space for tiny trees. |
| 296 List<Node*, Allocator> nodes_to_visit(10); | 296 List<Node*, Allocator> nodes_to_visit(10, allocator_); |
| 297 if (root_ != NULL) nodes_to_visit.Add(root_); | 297 if (root_ != NULL) nodes_to_visit.Add(root_); |
| 298 int pos = 0; | 298 int pos = 0; |
| 299 while (pos < nodes_to_visit.length()) { | 299 while (pos < nodes_to_visit.length()) { |
| 300 Node* node = nodes_to_visit[pos++]; | 300 Node* node = nodes_to_visit[pos++]; |
| 301 if (node->left() != NULL) nodes_to_visit.Add(node->left()); | 301 if (node->left() != NULL) nodes_to_visit.Add(node->left()); |
| 302 if (node->right() != NULL) nodes_to_visit.Add(node->right()); | 302 if (node->right() != NULL) nodes_to_visit.Add(node->right()); |
| 303 callback->Call(node); | 303 callback->Call(node); |
| 304 } | 304 } |
| 305 } | 305 } |
| 306 | 306 |
| 307 | 307 |
| 308 } } // namespace v8::internal | 308 } } // namespace v8::internal |
| 309 | 309 |
| 310 #endif // V8_SPLAY_TREE_INL_H_ | 310 #endif // V8_SPLAY_TREE_INL_H_ |
| OLD | NEW |