Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(244)

Side by Side Diff: src/splay-tree.h

Issue 660280: Parametrize C++ splay tree with allocator. (Closed)
Patch Set: P -> Allocator Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/heap.h ('k') | src/splay-tree-inl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
31 namespace v8 {
32 namespace internal {
33
34
35 // A splay tree. The config type parameter encapsulates the different
36 // configurations of a concrete splay tree:
37 //
38 // typedef Key: the key type
39 // typedef Value: the value type
40 // static const kNoKey: the dummy key used when no key is set
41 // static const kNoValue: the dummy value used to initialize nodes
42 // int (Compare)(Key& a, Key& b) -> {-1, 0, 1}: comparison function
43 //
44 // The tree is also parameterized by an allocation policy
45 // (Allocator). The policy is used for allocating lists in the C free
46 // store or the zone; see zone.h.
47
48 // Forward defined as
49 // template <typename Config, class Allocator = FreeStoreAllocationPolicy>
50 // class SplayTree;
51 template <typename Config, class Allocator>
52 class SplayTree {
53 public:
54 typedef typename Config::Key Key;
55 typedef typename Config::Value Value;
56
57 class Locator;
58
59 SplayTree() : root_(NULL) { }
60 ~SplayTree();
61
62 INLINE(void* operator new(size_t size)) {
63 return Allocator::New(static_cast<int>(size));
64 }
65 INLINE(void operator delete(void* p, size_t)) { return Allocator::Delete(p); }
66
67 // Inserts the given key in this tree with the given value. Returns
68 // true if a node was inserted, otherwise false. If found the locator
69 // is enabled and provides access to the mapping for the key.
70 bool Insert(const Key& key, Locator* locator);
71
72 // Looks up the key in this tree and returns true if it was found,
73 // otherwise false. If the node is found the locator is enabled and
74 // provides access to the mapping for the key.
75 bool Find(const Key& key, Locator* locator);
76
77 // Finds the mapping with the greatest key less than or equal to the
78 // given key.
79 bool FindGreatestLessThan(const Key& key, Locator* locator);
80
81 // Find the mapping with the greatest key in this tree.
82 bool FindGreatest(Locator* locator);
83
84 // Finds the mapping with the least key greater than or equal to the
85 // given key.
86 bool FindLeastGreaterThan(const Key& key, Locator* locator);
87
88 // Find the mapping with the least key in this tree.
89 bool FindLeast(Locator* locator);
90
91 // Remove the node with the given key from the tree.
92 bool Remove(const Key& key);
93
94 bool is_empty() { return root_ == NULL; }
95
96 // Perform the splay operation for the given key. Moves the node with
97 // the given key to the top of the tree. If no node has the given
98 // key, the last node on the search path is moved to the top of the
99 // tree.
100 void Splay(const Key& key);
101
102 class Node {
103 public:
104 Node(const Key& key, const Value& value)
105 : key_(key),
106 value_(value),
107 left_(NULL),
108 right_(NULL) { }
109
110 INLINE(void* operator new(size_t size)) {
111 return Allocator::New(static_cast<int>(size));
112 }
113 INLINE(void operator delete(void* p, size_t)) {
114 return Allocator::Delete(p);
115 }
116
117 Key key() { return key_; }
118 Value value() { return value_; }
119 Node* left() { return left_; }
120 Node* right() { return right_; }
121 private:
122
123 friend class SplayTree;
124 friend class Locator;
125 Key key_;
126 Value value_;
127 Node* left_;
128 Node* right_;
129 };
130
131 // A locator provides access to a node in the tree without actually
132 // exposing the node.
133 class Locator BASE_EMBEDDED {
134 public:
135 explicit Locator(Node* node) : node_(node) { }
136 Locator() : node_(NULL) { }
137 const Key& key() { return node_->key_; }
138 Value& value() { return node_->value_; }
139 void set_value(const Value& value) { node_->value_ = value; }
140 inline void bind(Node* node) { node_ = node; }
141 private:
142 Node* node_;
143 };
144
145 template <class Callback>
146 void ForEach(Callback* callback);
147
148 protected:
149
150 // Resets tree root. Existing nodes become unreachable.
151 void ResetRoot() { root_ = NULL; }
152
153 private:
154
155 template<class Callback>
156 class NodeToPairAdaptor BASE_EMBEDDED {
157 public:
158 explicit NodeToPairAdaptor(Callback* callback)
159 : callback_(callback) { }
160 void Call(Node* node) {
161 callback_->Call(node->key(), node->value());
162 }
163
164 private:
165 Callback* callback_;
166
167 DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor);
168 };
169
170 class NodeDeleter BASE_EMBEDDED {
171 public:
172 NodeDeleter() { }
173 void Call(Node* node) { delete node; }
174
175 private:
176
177 DISALLOW_COPY_AND_ASSIGN(NodeDeleter);
178 };
179
180 template <class Callback>
181 void ForEachNode(Callback* callback);
182
183 Node* root_;
184
185 DISALLOW_COPY_AND_ASSIGN(SplayTree);
186 };
187
188
189 } } // namespace v8::internal
190
191 #endif // V8_SPLAY_TREE_H_
OLDNEW
« no previous file with comments | « src/heap.h ('k') | src/splay-tree-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698