Chromium Code Reviews| 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 16 matching lines...) Expand all Loading... | |
| 27 | 27 |
| 28 #ifndef V8_SPLAY_TREE_INL_H_ | 28 #ifndef V8_SPLAY_TREE_INL_H_ |
| 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 P> |
|
danno
2012/05/25 11:03:37
AllocatorPolicy, or Policy (P is not very descript
| |
| 38 SplayTree<Config, Allocator>::~SplayTree() { | 38 SplayTree<Config, P>::~SplayTree() { |
| 39 NodeDeleter deleter; | 39 NodeDeleter deleter(deallocator_); |
| 40 ForEachNode(&deleter); | 40 ForEachNode(&deleter); |
| 41 } | 41 } |
| 42 | 42 |
| 43 | 43 |
| 44 template<typename Config, class Allocator> | 44 template<typename Config, class P> |
| 45 bool SplayTree<Config, Allocator>::Insert(const Key& key, Locator* locator) { | 45 bool SplayTree<Config, P>::Insert(const Key& key, Locator* locator, |
| 46 Allocator allocator) { | |
| 46 if (is_empty()) { | 47 if (is_empty()) { |
| 47 // If the tree is empty, insert the new node. | 48 // If the tree is empty, insert the new node. |
| 48 root_ = new Node(key, Config::NoValue()); | 49 root_ = new(allocator) Node(key, Config::NoValue()); |
| 49 } else { | 50 } else { |
| 50 // Splay on the key to move the last node on the search path | 51 // Splay on the key to move the last node on the search path |
| 51 // for the key to the root of the tree. | 52 // for the key to the root of the tree. |
| 52 Splay(key); | 53 Splay(key); |
| 53 // Ignore repeated insertions with the same key. | 54 // Ignore repeated insertions with the same key. |
| 54 int cmp = Config::Compare(key, root_->key_); | 55 int cmp = Config::Compare(key, root_->key_); |
| 55 if (cmp == 0) { | 56 if (cmp == 0) { |
| 56 locator->bind(root_); | 57 locator->bind(root_); |
| 57 return false; | 58 return false; |
| 58 } | 59 } |
| 59 // Insert the new node. | 60 // Insert the new node. |
| 60 Node* node = new Node(key, Config::NoValue()); | 61 Node* node = new(allocator) Node(key, Config::NoValue()); |
| 61 InsertInternal(cmp, node); | 62 InsertInternal(cmp, node); |
| 62 } | 63 } |
| 63 locator->bind(root_); | 64 locator->bind(root_); |
| 64 return true; | 65 return true; |
| 65 } | 66 } |
| 66 | 67 |
| 67 | 68 |
| 68 template<typename Config, class Allocator> | 69 template<typename Config, class P> |
| 69 void SplayTree<Config, Allocator>::InsertInternal(int cmp, Node* node) { | 70 void SplayTree<Config, P>::InsertInternal(int cmp, Node* node) { |
| 70 if (cmp > 0) { | 71 if (cmp > 0) { |
| 71 node->left_ = root_; | 72 node->left_ = root_; |
| 72 node->right_ = root_->right_; | 73 node->right_ = root_->right_; |
| 73 root_->right_ = NULL; | 74 root_->right_ = NULL; |
| 74 } else { | 75 } else { |
| 75 node->right_ = root_; | 76 node->right_ = root_; |
| 76 node->left_ = root_->left_; | 77 node->left_ = root_->left_; |
| 77 root_->left_ = NULL; | 78 root_->left_ = NULL; |
| 78 } | 79 } |
| 79 root_ = node; | 80 root_ = node; |
| 80 } | 81 } |
| 81 | 82 |
| 82 | 83 |
| 83 template<typename Config, class Allocator> | 84 template<typename Config, class P> |
| 84 bool SplayTree<Config, Allocator>::FindInternal(const Key& key) { | 85 bool SplayTree<Config, P>::FindInternal(const Key& key) { |
| 85 if (is_empty()) | 86 if (is_empty()) |
| 86 return false; | 87 return false; |
| 87 Splay(key); | 88 Splay(key); |
| 88 return Config::Compare(key, root_->key_) == 0; | 89 return Config::Compare(key, root_->key_) == 0; |
| 89 } | 90 } |
| 90 | 91 |
| 91 | 92 |
| 92 template<typename Config, class Allocator> | 93 template<typename Config, class P> |
| 93 bool SplayTree<Config, Allocator>::Find(const Key& key, Locator* locator) { | 94 bool SplayTree<Config, P>::Find(const Key& key, Locator* locator) { |
| 94 if (FindInternal(key)) { | 95 if (FindInternal(key)) { |
| 95 locator->bind(root_); | 96 locator->bind(root_); |
| 96 return true; | 97 return true; |
| 97 } else { | 98 } else { |
| 98 return false; | 99 return false; |
| 99 } | 100 } |
| 100 } | 101 } |
| 101 | 102 |
| 102 | 103 |
| 103 template<typename Config, class Allocator> | 104 template<typename Config, class P> |
| 104 bool SplayTree<Config, Allocator>::FindGreatestLessThan(const Key& key, | 105 bool SplayTree<Config, P>::FindGreatestLessThan(const Key& key, |
| 105 Locator* locator) { | 106 Locator* locator) { |
| 106 if (is_empty()) | 107 if (is_empty()) |
| 107 return false; | 108 return false; |
| 108 // Splay on the key to move the node with the given key or the last | 109 // 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. | 110 // node on the search path to the top of the tree. |
| 110 Splay(key); | 111 Splay(key); |
| 111 // Now the result is either the root node or the greatest node in | 112 // Now the result is either the root node or the greatest node in |
| 112 // the left subtree. | 113 // the left subtree. |
| 113 int cmp = Config::Compare(root_->key_, key); | 114 int cmp = Config::Compare(root_->key_, key); |
| 114 if (cmp <= 0) { | 115 if (cmp <= 0) { |
| 115 locator->bind(root_); | 116 locator->bind(root_); |
| 116 return true; | 117 return true; |
| 117 } else { | 118 } else { |
| 118 Node* temp = root_; | 119 Node* temp = root_; |
| 119 root_ = root_->left_; | 120 root_ = root_->left_; |
| 120 bool result = FindGreatest(locator); | 121 bool result = FindGreatest(locator); |
| 121 root_ = temp; | 122 root_ = temp; |
| 122 return result; | 123 return result; |
| 123 } | 124 } |
| 124 } | 125 } |
| 125 | 126 |
| 126 | 127 |
| 127 template<typename Config, class Allocator> | 128 template<typename Config, class P> |
| 128 bool SplayTree<Config, Allocator>::FindLeastGreaterThan(const Key& key, | 129 bool SplayTree<Config, P>::FindLeastGreaterThan(const Key& key, |
| 129 Locator* locator) { | 130 Locator* locator) { |
| 130 if (is_empty()) | 131 if (is_empty()) |
| 131 return false; | 132 return false; |
| 132 // Splay on the key to move the node with the given key or the last | 133 // 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. | 134 // node on the search path to the top of the tree. |
| 134 Splay(key); | 135 Splay(key); |
| 135 // Now the result is either the root node or the least node in | 136 // Now the result is either the root node or the least node in |
| 136 // the right subtree. | 137 // the right subtree. |
| 137 int cmp = Config::Compare(root_->key_, key); | 138 int cmp = Config::Compare(root_->key_, key); |
| 138 if (cmp >= 0) { | 139 if (cmp >= 0) { |
| 139 locator->bind(root_); | 140 locator->bind(root_); |
| 140 return true; | 141 return true; |
| 141 } else { | 142 } else { |
| 142 Node* temp = root_; | 143 Node* temp = root_; |
| 143 root_ = root_->right_; | 144 root_ = root_->right_; |
| 144 bool result = FindLeast(locator); | 145 bool result = FindLeast(locator); |
| 145 root_ = temp; | 146 root_ = temp; |
| 146 return result; | 147 return result; |
| 147 } | 148 } |
| 148 } | 149 } |
| 149 | 150 |
| 150 | 151 |
| 151 template<typename Config, class Allocator> | 152 template<typename Config, class P> |
| 152 bool SplayTree<Config, Allocator>::FindGreatest(Locator* locator) { | 153 bool SplayTree<Config, P>::FindGreatest(Locator* locator) { |
| 153 if (is_empty()) | 154 if (is_empty()) |
| 154 return false; | 155 return false; |
| 155 Node* current = root_; | 156 Node* current = root_; |
| 156 while (current->right_ != NULL) | 157 while (current->right_ != NULL) |
| 157 current = current->right_; | 158 current = current->right_; |
| 158 locator->bind(current); | 159 locator->bind(current); |
| 159 return true; | 160 return true; |
| 160 } | 161 } |
| 161 | 162 |
| 162 | 163 |
| 163 template<typename Config, class Allocator> | 164 template<typename Config, class P> |
| 164 bool SplayTree<Config, Allocator>::FindLeast(Locator* locator) { | 165 bool SplayTree<Config, P>::FindLeast(Locator* locator) { |
| 165 if (is_empty()) | 166 if (is_empty()) |
| 166 return false; | 167 return false; |
| 167 Node* current = root_; | 168 Node* current = root_; |
| 168 while (current->left_ != NULL) | 169 while (current->left_ != NULL) |
| 169 current = current->left_; | 170 current = current->left_; |
| 170 locator->bind(current); | 171 locator->bind(current); |
| 171 return true; | 172 return true; |
| 172 } | 173 } |
| 173 | 174 |
| 174 | 175 |
| 175 template<typename Config, class Allocator> | 176 template<typename Config, class P> |
| 176 bool SplayTree<Config, Allocator>::Move(const Key& old_key, | 177 bool SplayTree<Config, P>::Move(const Key& old_key, 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 deallocator_.Delete(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 P> |
| 196 bool SplayTree<Config, Allocator>::Remove(const Key& key) { | 196 bool SplayTree<Config, P>::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 delete 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 P> |
| 207 void SplayTree<Config, Allocator>::RemoveRootNode(const Key& key) { | 207 void SplayTree<Config, P>::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 { |
| 212 // Left child exists. | 212 // Left child exists. |
| 213 Node* right = root_->right_; | 213 Node* right = root_->right_; |
| 214 // Make the original left child the new root. | 214 // Make the original left child the new root. |
| 215 root_ = root_->left_; | 215 root_ = root_->left_; |
| 216 // Splay to make sure that the new root has an empty right child. | 216 // Splay to make sure that the new root has an empty right child. |
| 217 Splay(key); | 217 Splay(key); |
| 218 // Insert the original right child as the right child of the new | 218 // Insert the original right child as the right child of the new |
| 219 // root. | 219 // root. |
| 220 root_->right_ = right; | 220 root_->right_ = right; |
| 221 } | 221 } |
| 222 } | 222 } |
| 223 | 223 |
| 224 | 224 |
| 225 template<typename Config, class Allocator> | 225 template<typename Config, class P> |
| 226 void SplayTree<Config, Allocator>::Splay(const Key& key) { | 226 void SplayTree<Config, P>::Splay(const Key& key) { |
| 227 if (is_empty()) | 227 if (is_empty()) |
| 228 return; | 228 return; |
| 229 Node dummy_node(Config::kNoKey, Config::NoValue()); | 229 Node dummy_node(Config::kNoKey, Config::NoValue()); |
| 230 // Create a dummy node. The use of the dummy node is a bit | 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 | 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 | 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 | 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. | 234 // and right will always be nodes and we avoid special cases. |
| 235 Node* dummy = &dummy_node; | 235 Node* dummy = &dummy_node; |
| 236 Node* left = dummy; | 236 Node* left = dummy; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 } | 276 } |
| 277 // Assemble. | 277 // Assemble. |
| 278 left->right_ = current->left_; | 278 left->right_ = current->left_; |
| 279 right->left_ = current->right_; | 279 right->left_ = current->right_; |
| 280 current->left_ = dummy->right_; | 280 current->left_ = dummy->right_; |
| 281 current->right_ = dummy->left_; | 281 current->right_ = dummy->left_; |
| 282 root_ = current; | 282 root_ = current; |
| 283 } | 283 } |
| 284 | 284 |
| 285 | 285 |
| 286 template <typename Config, class Allocator> template <class Callback> | 286 template <typename Config, class P> template <class Callback> |
| 287 void SplayTree<Config, Allocator>::ForEach(Callback* callback) { | 287 void SplayTree<Config, P>::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 P> template <class Callback> |
| 294 void SplayTree<Config, Allocator>::ForEachNode(Callback* callback) { | 294 void SplayTree<Config, P>::ForEachNode(Callback* callback, |
| 295 Allocator allocator) { | |
| 295 // Pre-allocate some space for tiny trees. | 296 // Pre-allocate some space for tiny trees. |
| 296 List<Node*, Allocator> nodes_to_visit(10); | 297 List<Node*, P> nodes_to_visit(10); |
| 297 if (root_ != NULL) nodes_to_visit.Add(root_); | 298 if (root_ != NULL) nodes_to_visit.Add(root_, allocator); |
| 298 int pos = 0; | 299 int pos = 0; |
| 299 while (pos < nodes_to_visit.length()) { | 300 while (pos < nodes_to_visit.length()) { |
| 300 Node* node = nodes_to_visit[pos++]; | 301 Node* node = nodes_to_visit[pos++]; |
| 301 if (node->left() != NULL) nodes_to_visit.Add(node->left()); | 302 if (node->left() != NULL) nodes_to_visit.Add(node->left(), allocator); |
| 302 if (node->right() != NULL) nodes_to_visit.Add(node->right()); | 303 if (node->right() != NULL) nodes_to_visit.Add(node->right(), allocator); |
| 303 callback->Call(node); | 304 callback->Call(node); |
| 304 } | 305 } |
| 305 } | 306 } |
| 306 | 307 |
| 307 | 308 |
| 308 } } // namespace v8::internal | 309 } } // namespace v8::internal |
| 309 | 310 |
| 310 #endif // V8_SPLAY_TREE_INL_H_ | 311 #endif // V8_SPLAY_TREE_INL_H_ |
| OLD | NEW |