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 P> |
|
danno
2012/05/25 11:03:37
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 P::Alloc Allocator; | |
| 60 typedef typename P::Dealloc 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* p, size_t, |
| 73 Deallocator deallocator = Deallocator())) { | |
|
danno
2012/05/25 11:03:37
Adding the deallocator here does something differe
| |
| 74 return deallocator.Delete(p); | |
| 75 } | |
| 68 | 76 |
| 69 // Inserts the given key in this tree with the given value. Returns | 77 // 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 | 78 // true if a node was inserted, otherwise false. If found the locator |
| 71 // is enabled and provides access to the mapping for the key. | 79 // is enabled and provides access to the mapping for the key. |
| 72 bool Insert(const Key& key, Locator* locator); | 80 bool Insert(const Key& key, Locator* locator, |
| 81 Allocator allocator = Allocator()); | |
| 73 | 82 |
| 74 // Looks up the key in this tree and returns true if it was found, | 83 // 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 | 84 // otherwise false. If the node is found the locator is enabled and |
| 76 // provides access to the mapping for the key. | 85 // provides access to the mapping for the key. |
| 77 bool Find(const Key& key, Locator* locator); | 86 bool Find(const Key& key, Locator* locator); |
| 78 | 87 |
| 79 // Finds the mapping with the greatest key less than or equal to the | 88 // Finds the mapping with the greatest key less than or equal to the |
| 80 // given key. | 89 // given key. |
| 81 bool FindGreatestLessThan(const Key& key, Locator* locator); | 90 bool FindGreatestLessThan(const Key& key, Locator* locator); |
| 82 | 91 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 105 void Splay(const Key& key); | 114 void Splay(const Key& key); |
| 106 | 115 |
| 107 class Node { | 116 class Node { |
| 108 public: | 117 public: |
| 109 Node(const Key& key, const Value& value) | 118 Node(const Key& key, const Value& value) |
| 110 : key_(key), | 119 : key_(key), |
| 111 value_(value), | 120 value_(value), |
| 112 left_(NULL), | 121 left_(NULL), |
| 113 right_(NULL) { } | 122 right_(NULL) { } |
| 114 | 123 |
| 115 INLINE(void* operator new(size_t size)) { | 124 INLINE(void* operator new(size_t size, Allocator allocator)) { |
| 116 return Allocator::New(static_cast<int>(size)); | 125 return allocator.New(static_cast<int>(size)); |
| 117 } | 126 } |
| 118 INLINE(void operator delete(void* p, size_t)) { | 127 INLINE(void operator delete(void* p, size_t)) { |
| 119 return Allocator::Delete(p); | 128 UNREACHABLE(); |
|
danno
2012/05/25 11:03:37
Why unreachable? Because nodes should only be allo
sanjoy1
2012/05/25 20:32:44
The nodes are deleted using Deallocator::Delete, w
| |
| 120 } | 129 } |
| 121 | 130 |
| 122 Key key() { return key_; } | 131 Key key() { return key_; } |
| 123 Value value() { return value_; } | 132 Value value() { return value_; } |
| 124 Node* left() { return left_; } | 133 Node* left() { return left_; } |
| 125 Node* right() { return right_; } | 134 Node* right() { return right_; } |
| 126 | 135 |
| 127 private: | 136 private: |
| 128 friend class SplayTree; | 137 friend class SplayTree; |
| 129 friend class Locator; | 138 friend class Locator; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 } | 185 } |
| 177 | 186 |
| 178 private: | 187 private: |
| 179 Callback* callback_; | 188 Callback* callback_; |
| 180 | 189 |
| 181 DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor); | 190 DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor); |
| 182 }; | 191 }; |
| 183 | 192 |
| 184 class NodeDeleter BASE_EMBEDDED { | 193 class NodeDeleter BASE_EMBEDDED { |
| 185 public: | 194 public: |
| 186 NodeDeleter() { } | 195 explicit NodeDeleter(Deallocator deallocator) : deallocator_(deallocator) { |
| 187 void Call(Node* node) { delete node; } | 196 } |
| 197 void Call(Node* node) { deallocator_.Delete(node); } | |
| 188 | 198 |
| 189 private: | 199 private: |
| 200 Deallocator deallocator_; | |
| 201 | |
| 190 DISALLOW_COPY_AND_ASSIGN(NodeDeleter); | 202 DISALLOW_COPY_AND_ASSIGN(NodeDeleter); |
| 191 }; | 203 }; |
| 192 | 204 |
| 193 template <class Callback> | 205 template <class Callback> |
| 194 void ForEachNode(Callback* callback); | 206 void ForEachNode(Callback* callback, Allocator allocator = Allocator()); |
| 195 | 207 |
| 196 Node* root_; | 208 Node* root_; |
| 209 Deallocator deallocator_; | |
|
danno
2012/05/25 11:03:37
I don't think you ever need to store the Deallocat
sanjoy1
2012/05/25 20:32:44
I need to store it in case the we're using the Pre
| |
| 197 | 210 |
| 198 DISALLOW_COPY_AND_ASSIGN(SplayTree); | 211 DISALLOW_COPY_AND_ASSIGN(SplayTree); |
| 199 }; | 212 }; |
| 200 | 213 |
| 201 | 214 |
| 202 } } // namespace v8::internal | 215 } } // namespace v8::internal |
| 203 | 216 |
| 204 #endif // V8_SPLAY_TREE_H_ | 217 #endif // V8_SPLAY_TREE_H_ |
| OLD | NEW |