ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_SPLAY_TREE_H_ |
| 29 | #define V8_SPLAY_TREE_H_ |
| 30 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
| 32 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | |
| 37 | // A splay tree. The config type parameter encapsulates the different |
| 38 | // configurations of a concrete splay tree: |
| 39 | // |
| 40 | // typedef Key: the key type |
| 41 | // typedef Value: the value type |
| 42 | // static const kNoKey: the dummy key used when no key is set |
| 43 | // static const kNoValue: the dummy value used to initialize nodes |
| 44 | // int (Compare)(Key& a, Key& b) -> {-1, 0, 1}: comparison function |
| 45 | // |
| 46 | // The tree is also parameterized by an allocation policy |
| 47 | // (Allocator). The policy is used for allocating lists in the C free |
| 48 | // store or the zone; see zone.h. |
| 49 | |
| 50 | // Forward defined as |
| 51 | // template <typename Config, class Allocator = FreeStoreAllocationPolicy> |
| 52 | // class SplayTree; |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 53 | template <typename Config, class AllocationPolicy> |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 54 | class SplayTree { |
| 55 | public: |
| 56 | typedef typename Config::Key Key; |
| 57 | typedef typename Config::Value Value; |
| 58 | |
| 59 | class Locator; |
| 60 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 61 | SplayTree(AllocationPolicy allocator = AllocationPolicy()) |
| 62 | : root_(NULL), allocator_(allocator) { } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 63 | ~SplayTree(); |
| 64 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 65 | INLINE(void* operator new(size_t size, |
| 66 | AllocationPolicy allocator = AllocationPolicy())) { |
| 67 | return allocator.New(static_cast<int>(size)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 68 | } |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 69 | INLINE(void operator delete(void* p)) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 70 | AllocationPolicy::Delete(p); |
| 71 | } |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 72 | // Please the MSVC compiler. We should never have to execute this. |
| 73 | INLINE(void operator delete(void* p, AllocationPolicy policy)) { |
| 74 | UNREACHABLE(); |
| 75 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 76 | |
| 77 | // Inserts the given key in this tree with the given value. Returns |
| 78 | // true if a node was inserted, otherwise false. If found the locator |
| 79 | // is enabled and provides access to the mapping for the key. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 80 | bool Insert(const Key& key, Locator* locator); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 81 | |
| 82 | // Looks up the key in this tree and returns true if it was found, |
| 83 | // otherwise false. If the node is found the locator is enabled and |
| 84 | // provides access to the mapping for the key. |
| 85 | bool Find(const Key& key, Locator* locator); |
| 86 | |
| 87 | // Finds the mapping with the greatest key less than or equal to the |
| 88 | // given key. |
| 89 | bool FindGreatestLessThan(const Key& key, Locator* locator); |
| 90 | |
| 91 | // Find the mapping with the greatest key in this tree. |
| 92 | bool FindGreatest(Locator* locator); |
| 93 | |
| 94 | // Finds the mapping with the least key greater than or equal to the |
| 95 | // given key. |
| 96 | bool FindLeastGreaterThan(const Key& key, Locator* locator); |
| 97 | |
| 98 | // Find the mapping with the least key in this tree. |
| 99 | bool FindLeast(Locator* locator); |
| 100 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 101 | // Move the node from one key to another. |
| 102 | bool Move(const Key& old_key, const Key& new_key); |
| 103 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 104 | // Remove the node with the given key from the tree. |
| 105 | bool Remove(const Key& key); |
| 106 | |
| 107 | bool is_empty() { return root_ == NULL; } |
| 108 | |
| 109 | // Perform the splay operation for the given key. Moves the node with |
| 110 | // the given key to the top of the tree. If no node has the given |
| 111 | // key, the last node on the search path is moved to the top of the |
| 112 | // tree. |
| 113 | void Splay(const Key& key); |
| 114 | |
| 115 | class Node { |
| 116 | public: |
| 117 | Node(const Key& key, const Value& value) |
| 118 | : key_(key), |
| 119 | value_(value), |
| 120 | left_(NULL), |
| 121 | right_(NULL) { } |
| 122 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 123 | INLINE(void* operator new(size_t size, AllocationPolicy allocator)) { |
| 124 | return allocator.New(static_cast<int>(size)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 125 | } |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 126 | INLINE(void operator delete(void* p)) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 127 | return AllocationPolicy::Delete(p); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 128 | } |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 129 | // Please the MSVC compiler. We should never have to execute |
| 130 | // this. |
| 131 | INLINE(void operator delete(void* p, AllocationPolicy allocator)) { |
| 132 | UNREACHABLE(); |
| 133 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 134 | |
| 135 | Key key() { return key_; } |
| 136 | Value value() { return value_; } |
| 137 | Node* left() { return left_; } |
| 138 | Node* right() { return right_; } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 139 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 140 | private: |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 141 | friend class SplayTree; |
| 142 | friend class Locator; |
| 143 | Key key_; |
| 144 | Value value_; |
| 145 | Node* left_; |
| 146 | Node* right_; |
| 147 | }; |
| 148 | |
| 149 | // A locator provides access to a node in the tree without actually |
| 150 | // exposing the node. |
| 151 | class Locator BASE_EMBEDDED { |
| 152 | public: |
| 153 | explicit Locator(Node* node) : node_(node) { } |
| 154 | Locator() : node_(NULL) { } |
| 155 | const Key& key() { return node_->key_; } |
| 156 | Value& value() { return node_->value_; } |
| 157 | void set_value(const Value& value) { node_->value_ = value; } |
| 158 | inline void bind(Node* node) { node_ = node; } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 159 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 160 | private: |
| 161 | Node* node_; |
| 162 | }; |
| 163 | |
| 164 | template <class Callback> |
| 165 | void ForEach(Callback* callback); |
| 166 | |
| 167 | protected: |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 168 | // Resets tree root. Existing nodes become unreachable. |
| 169 | void ResetRoot() { root_ = NULL; } |
| 170 | |
| 171 | private: |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 172 | // Search for a node with a given key. If found, root_ points |
| 173 | // to the node. |
| 174 | bool FindInternal(const Key& key); |
| 175 | |
| 176 | // Inserts a node assuming that root_ is already set up. |
| 177 | void InsertInternal(int cmp, Node* node); |
| 178 | |
| 179 | // Removes root_ node. |
| 180 | void RemoveRootNode(const Key& key); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 181 | |
| 182 | template<class Callback> |
| 183 | class NodeToPairAdaptor BASE_EMBEDDED { |
| 184 | public: |
| 185 | explicit NodeToPairAdaptor(Callback* callback) |
| 186 | : callback_(callback) { } |
| 187 | void Call(Node* node) { |
| 188 | callback_->Call(node->key(), node->value()); |
| 189 | } |
| 190 | |
| 191 | private: |
| 192 | Callback* callback_; |
| 193 | |
| 194 | DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor); |
| 195 | }; |
| 196 | |
| 197 | class NodeDeleter BASE_EMBEDDED { |
| 198 | public: |
| 199 | NodeDeleter() { } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 200 | void Call(Node* node) { AllocationPolicy::Delete(node); } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 201 | |
| 202 | private: |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 203 | DISALLOW_COPY_AND_ASSIGN(NodeDeleter); |
| 204 | }; |
| 205 | |
| 206 | template <class Callback> |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 207 | void ForEachNode(Callback* callback); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 208 | |
| 209 | Node* root_; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 210 | AllocationPolicy allocator_; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 211 | |
| 212 | DISALLOW_COPY_AND_ASSIGN(SplayTree); |
| 213 | }; |
| 214 | |
| 215 | |
| 216 | } } // namespace v8::internal |
| 217 | |
| 218 | #endif // V8_SPLAY_TREE_H_ |