| 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 30 matching lines...) Expand all Loading... |
| 41 // typedef Value: the value type | 41 // typedef Value: the value type |
| 42 // static const kNoKey: the dummy key used when no key is set | 42 // static const kNoKey: the dummy key used when no key is set |
| 43 // static const kNoValue: the dummy value used to initialize nodes | 43 // static const kNoValue: the dummy value used to initialize nodes |
| 44 // int (Compare)(Key& a, Key& b) -> {-1, 0, 1}: comparison function | 44 // int (Compare)(Key& a, Key& b) -> {-1, 0, 1}: comparison function |
| 45 // | 45 // |
| 46 // The tree is also parameterized by an allocation policy | 46 // The tree is also parameterized by an allocation policy |
| 47 // (Allocator). The policy is used for allocating lists in the C free | 47 // (Allocator). The policy is used for allocating lists in the C free |
| 48 // store or the zone; see zone.h. | 48 // store or the zone; see zone.h. |
| 49 | 49 |
| 50 // Forward defined as | 50 // Forward defined as |
| 51 // template <typename Config, class Allocator = FreeStoreAllocationPolicy> | 51 // template <typename Config, class Allocator = FreeStoreAllocator> |
| 52 // class SplayTree; | 52 // class SplayTree; |
| 53 template <typename Config, class Allocator> | 53 template <typename Config, class Allocator> |
| 54 class SplayTree { | 54 class SplayTree { |
| 55 public: | 55 public: |
| 56 typedef typename Config::Key Key; | 56 typedef typename Config::Key Key; |
| 57 typedef typename Config::Value Value; | 57 typedef typename Config::Value Value; |
| 58 | 58 |
| 59 class Locator; | 59 class Locator; |
| 60 | 60 |
| 61 SplayTree() : root_(NULL) { } | 61 explicit SplayTree(const Allocator& allocator = Allocator()) |
| 62 : allocator_(allocator), root_(NULL) { } |
| 62 ~SplayTree(); | 63 ~SplayTree(); |
| 63 | 64 |
| 64 INLINE(void* operator new(size_t size)) { | 65 INLINE(void* operator new(size_t size)) { |
| 65 return Allocator::New(static_cast<int>(size)); | 66 return Allocator().New(static_cast<int>(size)); |
| 66 } | 67 } |
| 67 INLINE(void operator delete(void* p, size_t)) { return Allocator::Delete(p); } | 68 INLINE(void operator delete(void* p, size_t)) { |
| 69 return Allocator().Delete(p); |
| 70 } |
| 68 | 71 |
| 69 // Inserts the given key in this tree with the given value. Returns | 72 // Inserts the given key in this tree with the given value. Returns |
| 70 // true if a node was inserted, otherwise false. If found the locator | 73 // true if a node was inserted, otherwise false. If found the locator |
| 71 // is enabled and provides access to the mapping for the key. | 74 // is enabled and provides access to the mapping for the key. |
| 72 bool Insert(const Key& key, Locator* locator); | 75 bool Insert(const Key& key, Locator* locator); |
| 73 | 76 |
| 74 // Looks up the key in this tree and returns true if it was found, | 77 // Looks up the key in this tree and returns true if it was found, |
| 75 // otherwise false. If the node is found the locator is enabled and | 78 // otherwise false. If the node is found the locator is enabled and |
| 76 // provides access to the mapping for the key. | 79 // provides access to the mapping for the key. |
| 77 bool Find(const Key& key, Locator* locator); | 80 bool Find(const Key& key, Locator* locator); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 105 void Splay(const Key& key); | 108 void Splay(const Key& key); |
| 106 | 109 |
| 107 class Node { | 110 class Node { |
| 108 public: | 111 public: |
| 109 Node(const Key& key, const Value& value) | 112 Node(const Key& key, const Value& value) |
| 110 : key_(key), | 113 : key_(key), |
| 111 value_(value), | 114 value_(value), |
| 112 left_(NULL), | 115 left_(NULL), |
| 113 right_(NULL) { } | 116 right_(NULL) { } |
| 114 | 117 |
| 115 INLINE(void* operator new(size_t size)) { | 118 INLINE(void* operator new(size_t size, Allocator* allocator)) { |
| 116 return Allocator::New(static_cast<int>(size)); | 119 return allocator->New(static_cast<int>(size)); |
| 117 } | 120 } |
| 118 INLINE(void operator delete(void* p, size_t)) { | 121 static void Delete(Allocator* allocator, Node* node) { |
| 119 return Allocator::Delete(p); | 122 node->~Node(); |
| 123 allocator->Delete(node); |
| 120 } | 124 } |
| 121 | 125 |
| 122 Key key() { return key_; } | 126 Key key() { return key_; } |
| 123 Value value() { return value_; } | 127 Value value() { return value_; } |
| 124 Node* left() { return left_; } | 128 Node* left() { return left_; } |
| 125 Node* right() { return right_; } | 129 Node* right() { return right_; } |
| 126 private: | 130 private: |
| 131 void operator delete(void* p, size_t); |
| 127 | 132 |
| 128 friend class SplayTree; | 133 friend class SplayTree; |
| 129 friend class Locator; | 134 friend class Locator; |
| 130 Key key_; | 135 Key key_; |
| 131 Value value_; | 136 Value value_; |
| 132 Node* left_; | 137 Node* left_; |
| 133 Node* right_; | 138 Node* right_; |
| 134 }; | 139 }; |
| 135 | 140 |
| 136 // A locator provides access to a node in the tree without actually | 141 // A locator provides access to a node in the tree without actually |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } | 181 } |
| 177 | 182 |
| 178 private: | 183 private: |
| 179 Callback* callback_; | 184 Callback* callback_; |
| 180 | 185 |
| 181 DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor); | 186 DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor); |
| 182 }; | 187 }; |
| 183 | 188 |
| 184 class NodeDeleter BASE_EMBEDDED { | 189 class NodeDeleter BASE_EMBEDDED { |
| 185 public: | 190 public: |
| 186 NodeDeleter() { } | 191 explicit NodeDeleter(Allocator* allocator) : allocator_(allocator) { } |
| 187 void Call(Node* node) { delete node; } | 192 void Call(Node* node) { Node::Delete(allocator_, node); } |
| 188 | 193 |
| 189 private: | 194 private: |
| 195 Allocator* allocator_; |
| 190 | 196 |
| 191 DISALLOW_COPY_AND_ASSIGN(NodeDeleter); | 197 DISALLOW_COPY_AND_ASSIGN(NodeDeleter); |
| 192 }; | 198 }; |
| 193 | 199 |
| 194 template <class Callback> | 200 template <class Callback> |
| 195 void ForEachNode(Callback* callback); | 201 void ForEachNode(Callback* callback); |
| 196 | 202 |
| 203 Allocator allocator_; |
| 197 Node* root_; | 204 Node* root_; |
| 198 | 205 |
| 199 DISALLOW_COPY_AND_ASSIGN(SplayTree); | 206 DISALLOW_COPY_AND_ASSIGN(SplayTree); |
| 200 }; | 207 }; |
| 201 | 208 |
| 202 | 209 |
| 203 } } // namespace v8::internal | 210 } } // namespace v8::internal |
| 204 | 211 |
| 205 #endif // V8_SPLAY_TREE_H_ | 212 #endif // V8_SPLAY_TREE_H_ |
| OLD | NEW |