1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_SPLAY_TREE_H_
6 #define V8_SPLAY_TREE_H_
8 #include "src/allocation.h"
14 // A splay tree. The config type parameter encapsulates the different
15 // configurations of a concrete splay tree:
17 // typedef Key: the key type
18 // typedef Value: the value type
19 // static const Key kNoKey: the dummy key used when no key is set
20 // static Value kNoValue(): the dummy value used to initialize nodes
21 // static int (Compare)(Key& a, Key& b) -> {-1, 0, 1}: comparison function
23 // The tree is also parameterized by an allocation policy
24 // (Allocator). The policy is used for allocating lists in the C free
25 // store or the zone; see zone.h.
28 // template <typename Config, class Allocator = FreeStoreAllocationPolicy>
30 template <typename Config, class AllocationPolicy>
33 typedef typename Config::Key Key;
34 typedef typename Config::Value Value;
38 explicit SplayTree(AllocationPolicy allocator = AllocationPolicy())
39 : root_(NULL), allocator_(allocator) {}
42 INLINE(void* operator new(size_t size,
43 AllocationPolicy allocator = AllocationPolicy())) {
44 return allocator.New(static_cast<int>(size));
46 INLINE(void operator delete(void* p)) {
47 AllocationPolicy::Delete(p);
49 // Please the MSVC compiler. We should never have to execute this.
50 INLINE(void operator delete(void* p, AllocationPolicy policy)) {
54 AllocationPolicy allocator() { return allocator_; }
56 // Checks if there is a mapping for the key.
57 bool Contains(const Key& key);
59 // Inserts the given key in this tree with the given value. Returns
60 // true if a node was inserted, otherwise false. If found the locator
61 // is enabled and provides access to the mapping for the key.
62 bool Insert(const Key& key, Locator* locator);
64 // Looks up the key in this tree and returns true if it was found,
65 // otherwise false. If the node is found the locator is enabled and
66 // provides access to the mapping for the key.
67 bool Find(const Key& key, Locator* locator);
69 // Finds the mapping with the greatest key less than or equal to the
71 bool FindGreatestLessThan(const Key& key, Locator* locator);
73 // Find the mapping with the greatest key in this tree.
74 bool FindGreatest(Locator* locator);
76 // Finds the mapping with the least key greater than or equal to the
78 bool FindLeastGreaterThan(const Key& key, Locator* locator);
80 // Find the mapping with the least key in this tree.
81 bool FindLeast(Locator* locator);
83 // Move the node from one key to another.
84 bool Move(const Key& old_key, const Key& new_key);
86 // Remove the node with the given key from the tree.
87 bool Remove(const Key& key);
89 // Remove all keys from the tree.
90 void Clear() { ResetRoot(); }
92 bool is_empty() { return root_ == NULL; }
94 // Perform the splay operation for the given key. Moves the node with
95 // the given key to the top of the tree. If no node has the given
96 // key, the last node on the search path is moved to the top of the
98 void Splay(const Key& key);
102 Node(const Key& key, const Value& value)
108 INLINE(void* operator new(size_t size, AllocationPolicy allocator)) {
109 return allocator.New(static_cast<int>(size));
111 INLINE(void operator delete(void* p)) {
112 return AllocationPolicy::Delete(p);
114 // Please the MSVC compiler. We should never have to execute
116 INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
120 Key key() { return key_; }
121 Value value() { return value_; }
122 Node* left() { return left_; }
123 Node* right() { return right_; }
126 friend class SplayTree;
127 friend class Locator;
134 // A locator provides access to a node in the tree without actually
135 // exposing the node.
136 class Locator BASE_EMBEDDED {
138 explicit Locator(Node* node) : node_(node) { }
139 Locator() : node_(NULL) { }
140 const Key& key() { return node_->key_; }
141 Value& value() { return node_->value_; }
142 void set_value(const Value& value) { node_->value_ = value; }
143 inline void bind(Node* node) { node_ = node; }
149 template <class Callback>
150 void ForEach(Callback* callback);
153 // Resets tree root. Existing nodes become unreachable.
154 void ResetRoot() { root_ = NULL; }
157 // Search for a node with a given key. If found, root_ points
159 bool FindInternal(const Key& key);
161 // Inserts a node assuming that root_ is already set up.
162 void InsertInternal(int cmp, Node* node);
164 // Removes root_ node.
165 void RemoveRootNode(const Key& key);
167 template<class Callback>
168 class NodeToPairAdaptor BASE_EMBEDDED {
170 explicit NodeToPairAdaptor(Callback* callback)
171 : callback_(callback) { }
172 void Call(Node* node) {
173 callback_->Call(node->key(), node->value());
179 DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor);
182 class NodeDeleter BASE_EMBEDDED {
185 void Call(Node* node) { AllocationPolicy::Delete(node); }
188 DISALLOW_COPY_AND_ASSIGN(NodeDeleter);
191 template <class Callback>
192 void ForEachNode(Callback* callback);
195 AllocationPolicy allocator_;
197 DISALLOW_COPY_AND_ASSIGN(SplayTree);
201 } // namespace internal
204 #endif // V8_SPLAY_TREE_H_