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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 = FreeStoreAllocationPolicy> |
| 52 // class SplayTree; | 52 // class SplayTree; |
| 53 template <typename Config, class Allocator> | 53 template <typename Config, class AllocationPolicy> |
| 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 typedef typename AllocationPolicy::Allocator Allocator; | |
| 60 typedef typename AllocationPolicy::Deallocator Deallocator; | |
| 61 | |
| 59 class Locator; | 62 class Locator; |
| 60 | 63 |
| 61 SplayTree() : root_(NULL) { } | 64 SplayTree(Allocator allocator = Allocator(), |
| 65 Deallocator deallocator = Deallocator()) | |
| 66 : root_(NULL), deallocator_(deallocator) { } | |
| 62 ~SplayTree(); | 67 ~SplayTree(); |
| 63 | 68 |
| 64 INLINE(void* operator new(size_t size)) { | 69 INLINE(void* operator new(size_t size, Allocator allocator = Allocator())) { |
| 65 return Allocator::New(static_cast<int>(size)); | 70 return allocator.New(static_cast<int>(size)); |
| 66 } | 71 } |
| 67 INLINE(void operator delete(void* p, size_t)) { return Allocator::Delete(p); } | 72 INLINE(void operator delete(void* ptr, size_t size)) { |
| 73 reinterpret_cast<SplayTree*>(ptr)->deallocator_.Delete(ptr); | |
| 74 } | |
| 68 | 75 |
| 69 // Inserts the given key in this tree with the given value. Returns | 76 // 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 | 77 // true if a node was inserted, otherwise false. If found the locator |
| 71 // is enabled and provides access to the mapping for the key. | 78 // is enabled and provides access to the mapping for the key. |
| 72 bool Insert(const Key& key, Locator* locator); | 79 bool Insert(const Key& key, Locator* locator, |
| 80 Allocator allocator = Allocator()); | |
| 73 | 81 |
| 74 // Looks up the key in this tree and returns true if it was found, | 82 // 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 | 83 // otherwise false. If the node is found the locator is enabled and |
| 76 // provides access to the mapping for the key. | 84 // provides access to the mapping for the key. |
| 77 bool Find(const Key& key, Locator* locator); | 85 bool Find(const Key& key, Locator* locator); |
| 78 | 86 |
| 79 // Finds the mapping with the greatest key less than or equal to the | 87 // Finds the mapping with the greatest key less than or equal to the |
| 80 // given key. | 88 // given key. |
| 81 bool FindGreatestLessThan(const Key& key, Locator* locator); | 89 bool FindGreatestLessThan(const Key& key, Locator* locator); |
| 82 | 90 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 105 void Splay(const Key& key); | 113 void Splay(const Key& key); |
| 106 | 114 |
| 107 class Node { | 115 class Node { |
| 108 public: | 116 public: |
| 109 Node(const Key& key, const Value& value) | 117 Node(const Key& key, const Value& value) |
| 110 : key_(key), | 118 : key_(key), |
| 111 value_(value), | 119 value_(value), |
| 112 left_(NULL), | 120 left_(NULL), |
| 113 right_(NULL) { } | 121 right_(NULL) { } |
| 114 | 122 |
| 115 INLINE(void* operator new(size_t size)) { | 123 INLINE(void* operator new(size_t size, Allocator allocator)) { |
| 116 return Allocator::New(static_cast<int>(size)); | 124 return allocator.New(static_cast<int>(size)); |
| 117 } | 125 } |
| 118 INLINE(void operator delete(void* p, size_t)) { | 126 INLINE(void operator delete(void* p, size_t)) { |
| 119 return Allocator::Delete(p); | 127 UNREACHABLE(); |
|
danno
2012/05/31 11:03:36
Hmmm. This still seems dangerous. It seems to intr
| |
| 120 } | 128 } |
| 121 | 129 |
| 122 Key key() { return key_; } | 130 Key key() { return key_; } |
| 123 Value value() { return value_; } | 131 Value value() { return value_; } |
| 124 Node* left() { return left_; } | 132 Node* left() { return left_; } |
| 125 Node* right() { return right_; } | 133 Node* right() { return right_; } |
| 126 | 134 |
| 127 private: | 135 private: |
| 128 friend class SplayTree; | 136 friend class SplayTree; |
| 129 friend class Locator; | 137 friend class Locator; |
| 130 Key key_; | 138 Key key_; |
| 131 Value value_; | 139 Value value_; |
| 132 Node* left_; | 140 Node* left_; |
| 133 Node* right_; | 141 Node* right_; |
|
danno
2012/05/31 11:03:36
I think the cleanest way is to make sure Deallocat
| |
| 134 }; | 142 }; |
| 135 | 143 |
| 136 // A locator provides access to a node in the tree without actually | 144 // A locator provides access to a node in the tree without actually |
| 137 // exposing the node. | 145 // exposing the node. |
| 138 class Locator BASE_EMBEDDED { | 146 class Locator BASE_EMBEDDED { |
| 139 public: | 147 public: |
| 140 explicit Locator(Node* node) : node_(node) { } | 148 explicit Locator(Node* node) : node_(node) { } |
| 141 Locator() : node_(NULL) { } | 149 Locator() : node_(NULL) { } |
| 142 const Key& key() { return node_->key_; } | 150 const Key& key() { return node_->key_; } |
| 143 Value& value() { return node_->value_; } | 151 Value& value() { return node_->value_; } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 } | 184 } |
| 177 | 185 |
| 178 private: | 186 private: |
| 179 Callback* callback_; | 187 Callback* callback_; |
| 180 | 188 |
| 181 DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor); | 189 DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor); |
| 182 }; | 190 }; |
| 183 | 191 |
| 184 class NodeDeleter BASE_EMBEDDED { | 192 class NodeDeleter BASE_EMBEDDED { |
| 185 public: | 193 public: |
| 186 NodeDeleter() { } | 194 explicit NodeDeleter(Deallocator deallocator) : deallocator_(deallocator) { |
| 187 void Call(Node* node) { delete node; } | 195 } |
| 196 void Call(Node* node) { deallocator_.Delete(node); } | |
| 188 | 197 |
| 189 private: | 198 private: |
| 199 Deallocator deallocator_; | |
| 200 | |
| 190 DISALLOW_COPY_AND_ASSIGN(NodeDeleter); | 201 DISALLOW_COPY_AND_ASSIGN(NodeDeleter); |
| 191 }; | 202 }; |
| 192 | 203 |
| 193 template <class Callback> | 204 template <class Callback> |
| 194 void ForEachNode(Callback* callback); | 205 void ForEachNode(Callback* callback, Allocator allocator = Allocator()); |
| 195 | 206 |
| 196 Node* root_; | 207 Node* root_; |
| 208 Deallocator deallocator_; | |
| 197 | 209 |
| 198 DISALLOW_COPY_AND_ASSIGN(SplayTree); | 210 DISALLOW_COPY_AND_ASSIGN(SplayTree); |
| 199 }; | 211 }; |
| 200 | 212 |
| 201 | 213 |
| 202 } } // namespace v8::internal | 214 } } // namespace v8::internal |
| 203 | 215 |
| 204 #endif // V8_SPLAY_TREE_H_ | 216 #endif // V8_SPLAY_TREE_H_ |
| OLD | NEW |