| 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 24 matching lines...) Expand all Loading... |
| 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; |
| 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, |
| 46 Locator* locator, |
| 47 Allocator allocator) { |
| 46 if (is_empty()) { | 48 if (is_empty()) { |
| 47 // If the tree is empty, insert the new node. | 49 // If the tree is empty, insert the new node. |
| 48 root_ = new Node(key, Config::NoValue()); | 50 root_ = new(allocator) Node(key, Config::NoValue()); |
| 49 } else { | 51 } else { |
| 50 // Splay on the key to move the last node on the search path | 52 // Splay on the key to move the last node on the search path |
| 51 // for the key to the root of the tree. | 53 // for the key to the root of the tree. |
| 52 Splay(key); | 54 Splay(key); |
| 53 // Ignore repeated insertions with the same key. | 55 // Ignore repeated insertions with the same key. |
| 54 int cmp = Config::Compare(key, root_->key_); | 56 int cmp = Config::Compare(key, root_->key_); |
| 55 if (cmp == 0) { | 57 if (cmp == 0) { |
| 56 locator->bind(root_); | 58 locator->bind(root_); |
| 57 return false; | 59 return false; |
| 58 } | 60 } |
| 59 // Insert the new node. | 61 // Insert the new node. |
| 60 Node* node = new Node(key, Config::NoValue()); | 62 Node* node = new(allocator) Node(key, Config::NoValue()); |
| 61 InsertInternal(cmp, node); | 63 InsertInternal(cmp, node); |
| 62 } | 64 } |
| 63 locator->bind(root_); | 65 locator->bind(root_); |
| 64 return true; | 66 return true; |
| 65 } | 67 } |
| 66 | 68 |
| 67 | 69 |
| 68 template<typename Config, class Allocator> | 70 template<typename Config, class Allocator> |
| 69 void SplayTree<Config, Allocator>::InsertInternal(int cmp, Node* node) { | 71 void SplayTree<Config, Allocator>::InsertInternal(int cmp, Node* node) { |
| 70 if (cmp > 0) { | 72 if (cmp > 0) { |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 | 286 |
| 285 | 287 |
| 286 template <typename Config, class Allocator> template <class Callback> | 288 template <typename Config, class Allocator> template <class Callback> |
| 287 void SplayTree<Config, Allocator>::ForEach(Callback* callback) { | 289 void SplayTree<Config, Allocator>::ForEach(Callback* callback) { |
| 288 NodeToPairAdaptor<Callback> callback_adaptor(callback); | 290 NodeToPairAdaptor<Callback> callback_adaptor(callback); |
| 289 ForEachNode(&callback_adaptor); | 291 ForEachNode(&callback_adaptor); |
| 290 } | 292 } |
| 291 | 293 |
| 292 | 294 |
| 293 template <typename Config, class Allocator> template <class Callback> | 295 template <typename Config, class Allocator> template <class Callback> |
| 294 void SplayTree<Config, Allocator>::ForEachNode(Callback* callback) { | 296 void SplayTree<Config, Allocator>::ForEachNode(Callback* callback, |
| 297 Allocator allocator) { |
| 295 // Pre-allocate some space for tiny trees. | 298 // Pre-allocate some space for tiny trees. |
| 296 List<Node*, Allocator> nodes_to_visit(10); | 299 List<Node*, Allocator> nodes_to_visit(10); |
| 297 if (root_ != NULL) nodes_to_visit.Add(root_); | 300 if (root_ != NULL) nodes_to_visit.Add(root_, allocator); |
| 298 int pos = 0; | 301 int pos = 0; |
| 299 while (pos < nodes_to_visit.length()) { | 302 while (pos < nodes_to_visit.length()) { |
| 300 Node* node = nodes_to_visit[pos++]; | 303 Node* node = nodes_to_visit[pos++]; |
| 301 if (node->left() != NULL) nodes_to_visit.Add(node->left()); | 304 if (node->left() != NULL) nodes_to_visit.Add(node->left(), allocator); |
| 302 if (node->right() != NULL) nodes_to_visit.Add(node->right()); | 305 if (node->right() != NULL) nodes_to_visit.Add(node->right(), allocator); |
| 303 callback->Call(node); | 306 callback->Call(node); |
| 304 } | 307 } |
| 305 } | 308 } |
| 306 | 309 |
| 307 | 310 |
| 308 } } // namespace v8::internal | 311 } } // namespace v8::internal |
| 309 | 312 |
| 310 #endif // V8_SPLAY_TREE_INL_H_ | 313 #endif // V8_SPLAY_TREE_INL_H_ |
| OLD | NEW |