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

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

Issue 10448007: Split an allocation policy into an allocator and a deallocator. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fixed the issues pointed out. Created 8 years, 6 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 | Annotate | Revision Log
OLDNEW
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 16 matching lines...) Expand all
27 27
28 #ifndef V8_SPLAY_TREE_INL_H_ 28 #ifndef V8_SPLAY_TREE_INL_H_
29 #define V8_SPLAY_TREE_INL_H_ 29 #define V8_SPLAY_TREE_INL_H_
30 30
31 #include "splay-tree.h" 31 #include "splay-tree.h"
32 32
33 namespace v8 { 33 namespace v8 {
34 namespace internal { 34 namespace internal {
35 35
36 36
37 template<typename Config, class Allocator> 37 template<typename Config, class AllocationPolicy>
38 SplayTree<Config, Allocator>::~SplayTree() { 38 SplayTree<Config, AllocationPolicy>::~SplayTree() {
39 NodeDeleter deleter; 39 NodeDeleter deleter(deallocator_);
40 ForEachNode(&deleter); 40 ForEachNode(&deleter);
41 } 41 }
42 42
43 43
44 template<typename Config, class Allocator> 44 template<typename Config, class AllocationPolicy>
45 bool SplayTree<Config, Allocator>::Insert(const Key& key, Locator* locator) { 45 bool SplayTree<Config, AllocationPolicy>::Insert(const Key& key,
46 Locator* locator,
47 Allocator allocator) {
46 if (is_empty()) { 48 if (is_empty()) {
47 // If the tree is empty, insert the new node. 49 // If the tree is empty, insert the new node.
48 root_ = new Node(key, Config::NoValue()); 50 root_ = new(allocator) Node(key, Config::NoValue());
49 } else { 51 } else {
50 // Splay on the key to move the last node on the search path 52 // Splay on the key to move the last node on the search path
51 // for the key to the root of the tree. 53 // for the key to the root of the tree.
52 Splay(key); 54 Splay(key);
53 // Ignore repeated insertions with the same key. 55 // Ignore repeated insertions with the same key.
54 int cmp = Config::Compare(key, root_->key_); 56 int cmp = Config::Compare(key, root_->key_);
55 if (cmp == 0) { 57 if (cmp == 0) {
56 locator->bind(root_); 58 locator->bind(root_);
57 return false; 59 return false;
58 } 60 }
59 // Insert the new node. 61 // Insert the new node.
60 Node* node = new Node(key, Config::NoValue()); 62 Node* node = new(allocator) Node(key, Config::NoValue());
61 InsertInternal(cmp, node); 63 InsertInternal(cmp, node);
62 } 64 }
63 locator->bind(root_); 65 locator->bind(root_);
64 return true; 66 return true;
65 } 67 }
66 68
67 69
68 template<typename Config, class Allocator> 70 template<typename Config, class AllocationPolicy>
69 void SplayTree<Config, Allocator>::InsertInternal(int cmp, Node* node) { 71 void SplayTree<Config, AllocationPolicy>::InsertInternal(int cmp, Node* node) {
70 if (cmp > 0) { 72 if (cmp > 0) {
71 node->left_ = root_; 73 node->left_ = root_;
72 node->right_ = root_->right_; 74 node->right_ = root_->right_;
73 root_->right_ = NULL; 75 root_->right_ = NULL;
74 } else { 76 } else {
75 node->right_ = root_; 77 node->right_ = root_;
76 node->left_ = root_->left_; 78 node->left_ = root_->left_;
77 root_->left_ = NULL; 79 root_->left_ = NULL;
78 } 80 }
79 root_ = node; 81 root_ = node;
80 } 82 }
81 83
82 84
83 template<typename Config, class Allocator> 85 template<typename Config, class AllocationPolicy>
84 bool SplayTree<Config, Allocator>::FindInternal(const Key& key) { 86 bool SplayTree<Config, AllocationPolicy>::FindInternal(const Key& key) {
85 if (is_empty()) 87 if (is_empty())
86 return false; 88 return false;
87 Splay(key); 89 Splay(key);
88 return Config::Compare(key, root_->key_) == 0; 90 return Config::Compare(key, root_->key_) == 0;
89 } 91 }
90 92
91 93
92 template<typename Config, class Allocator> 94 template<typename Config, class AllocationPolicy>
93 bool SplayTree<Config, Allocator>::Find(const Key& key, Locator* locator) { 95 bool SplayTree<Config, AllocationPolicy>::Find(const Key& key,
96 Locator* locator) {
94 if (FindInternal(key)) { 97 if (FindInternal(key)) {
95 locator->bind(root_); 98 locator->bind(root_);
96 return true; 99 return true;
97 } else { 100 } else {
98 return false; 101 return false;
99 } 102 }
100 } 103 }
101 104
102 105
103 template<typename Config, class Allocator> 106 template<typename Config, class AllocationPolicy>
104 bool SplayTree<Config, Allocator>::FindGreatestLessThan(const Key& key, 107 bool SplayTree<Config, AllocationPolicy>::FindGreatestLessThan(
105 Locator* locator) { 108 const Key& key, Locator* locator) {
106 if (is_empty()) 109 if (is_empty())
107 return false; 110 return false;
108 // Splay on the key to move the node with the given key or the last 111 // Splay on the key to move the node with the given key or the last
109 // node on the search path to the top of the tree. 112 // node on the search path to the top of the tree.
110 Splay(key); 113 Splay(key);
111 // Now the result is either the root node or the greatest node in 114 // Now the result is either the root node or the greatest node in
112 // the left subtree. 115 // the left subtree.
113 int cmp = Config::Compare(root_->key_, key); 116 int cmp = Config::Compare(root_->key_, key);
114 if (cmp <= 0) { 117 if (cmp <= 0) {
115 locator->bind(root_); 118 locator->bind(root_);
116 return true; 119 return true;
117 } else { 120 } else {
118 Node* temp = root_; 121 Node* temp = root_;
119 root_ = root_->left_; 122 root_ = root_->left_;
120 bool result = FindGreatest(locator); 123 bool result = FindGreatest(locator);
121 root_ = temp; 124 root_ = temp;
122 return result; 125 return result;
123 } 126 }
124 } 127 }
125 128
126 129
127 template<typename Config, class Allocator> 130 template<typename Config, class AllocationPolicy>
128 bool SplayTree<Config, Allocator>::FindLeastGreaterThan(const Key& key, 131 bool SplayTree<Config, AllocationPolicy>::FindLeastGreaterThan(
129 Locator* locator) { 132 const Key& key, Locator* locator) {
130 if (is_empty()) 133 if (is_empty())
131 return false; 134 return false;
132 // Splay on the key to move the node with the given key or the last 135 // Splay on the key to move the node with the given key or the last
133 // node on the search path to the top of the tree. 136 // node on the search path to the top of the tree.
134 Splay(key); 137 Splay(key);
135 // Now the result is either the root node or the least node in 138 // Now the result is either the root node or the least node in
136 // the right subtree. 139 // the right subtree.
137 int cmp = Config::Compare(root_->key_, key); 140 int cmp = Config::Compare(root_->key_, key);
138 if (cmp >= 0) { 141 if (cmp >= 0) {
139 locator->bind(root_); 142 locator->bind(root_);
140 return true; 143 return true;
141 } else { 144 } else {
142 Node* temp = root_; 145 Node* temp = root_;
143 root_ = root_->right_; 146 root_ = root_->right_;
144 bool result = FindLeast(locator); 147 bool result = FindLeast(locator);
145 root_ = temp; 148 root_ = temp;
146 return result; 149 return result;
147 } 150 }
148 } 151 }
149 152
150 153
151 template<typename Config, class Allocator> 154 template<typename Config, class AllocationPolicy>
152 bool SplayTree<Config, Allocator>::FindGreatest(Locator* locator) { 155 bool SplayTree<Config, AllocationPolicy>::FindGreatest(Locator* locator) {
153 if (is_empty()) 156 if (is_empty())
154 return false; 157 return false;
155 Node* current = root_; 158 Node* current = root_;
156 while (current->right_ != NULL) 159 while (current->right_ != NULL)
157 current = current->right_; 160 current = current->right_;
158 locator->bind(current); 161 locator->bind(current);
159 return true; 162 return true;
160 } 163 }
161 164
162 165
163 template<typename Config, class Allocator> 166 template<typename Config, class AllocationPolicy>
164 bool SplayTree<Config, Allocator>::FindLeast(Locator* locator) { 167 bool SplayTree<Config, AllocationPolicy>::FindLeast(Locator* locator) {
165 if (is_empty()) 168 if (is_empty())
166 return false; 169 return false;
167 Node* current = root_; 170 Node* current = root_;
168 while (current->left_ != NULL) 171 while (current->left_ != NULL)
169 current = current->left_; 172 current = current->left_;
170 locator->bind(current); 173 locator->bind(current);
171 return true; 174 return true;
172 } 175 }
173 176
174 177
175 template<typename Config, class Allocator> 178 template<typename Config, class AllocationPolicy>
176 bool SplayTree<Config, Allocator>::Move(const Key& old_key, 179 bool SplayTree<Config, AllocationPolicy>::Move(const Key& old_key,
177 const Key& new_key) { 180 const Key& new_key) {
178 if (!FindInternal(old_key)) 181 if (!FindInternal(old_key))
179 return false; 182 return false;
180 Node* node_to_move = root_; 183 Node* node_to_move = root_;
181 RemoveRootNode(old_key); 184 RemoveRootNode(old_key);
182 Splay(new_key); 185 Splay(new_key);
183 int cmp = Config::Compare(new_key, root_->key_); 186 int cmp = Config::Compare(new_key, root_->key_);
184 if (cmp == 0) { 187 if (cmp == 0) {
185 // A node with the target key already exists. 188 // A node with the target key already exists.
186 delete node_to_move; 189 deallocator_.Delete(node_to_move);
danno 2012/05/31 11:03:36 Yikes! You never call the node's destructor this w
187 return false; 190 return false;
188 } 191 }
189 node_to_move->key_ = new_key; 192 node_to_move->key_ = new_key;
190 InsertInternal(cmp, node_to_move); 193 InsertInternal(cmp, node_to_move);
191 return true; 194 return true;
192 } 195 }
193 196
194 197
195 template<typename Config, class Allocator> 198 template<typename Config, class AllocationPolicy>
196 bool SplayTree<Config, Allocator>::Remove(const Key& key) { 199 bool SplayTree<Config, AllocationPolicy>::Remove(const Key& key) {
197 if (!FindInternal(key)) 200 if (!FindInternal(key))
198 return false; 201 return false;
199 Node* node_to_remove = root_; 202 Node* node_to_remove = root_;
200 RemoveRootNode(key); 203 RemoveRootNode(key);
201 delete node_to_remove; 204 delete node_to_remove;
202 return true; 205 return true;
203 } 206 }
204 207
205 208
206 template<typename Config, class Allocator> 209 template<typename Config, class AllocationPolicy>
207 void SplayTree<Config, Allocator>::RemoveRootNode(const Key& key) { 210 void SplayTree<Config, AllocationPolicy>::RemoveRootNode(const Key& key) {
208 if (root_->left_ == NULL) { 211 if (root_->left_ == NULL) {
209 // No left child, so the new tree is just the right child. 212 // No left child, so the new tree is just the right child.
210 root_ = root_->right_; 213 root_ = root_->right_;
211 } else { 214 } else {
212 // Left child exists. 215 // Left child exists.
213 Node* right = root_->right_; 216 Node* right = root_->right_;
214 // Make the original left child the new root. 217 // Make the original left child the new root.
215 root_ = root_->left_; 218 root_ = root_->left_;
216 // Splay to make sure that the new root has an empty right child. 219 // Splay to make sure that the new root has an empty right child.
217 Splay(key); 220 Splay(key);
218 // Insert the original right child as the right child of the new 221 // Insert the original right child as the right child of the new
219 // root. 222 // root.
220 root_->right_ = right; 223 root_->right_ = right;
221 } 224 }
222 } 225 }
223 226
224 227
225 template<typename Config, class Allocator> 228 template<typename Config, class AllocationPolicy>
226 void SplayTree<Config, Allocator>::Splay(const Key& key) { 229 void SplayTree<Config, AllocationPolicy>::Splay(const Key& key) {
227 if (is_empty()) 230 if (is_empty())
228 return; 231 return;
229 Node dummy_node(Config::kNoKey, Config::NoValue()); 232 Node dummy_node(Config::kNoKey, Config::NoValue());
230 // Create a dummy node. The use of the dummy node is a bit 233 // Create a dummy node. The use of the dummy node is a bit
231 // counter-intuitive: The right child of the dummy node will hold 234 // counter-intuitive: The right child of the dummy node will hold
232 // the L tree of the algorithm. The left child of the dummy node 235 // the L tree of the algorithm. The left child of the dummy node
233 // will hold the R tree of the algorithm. Using a dummy node, left 236 // will hold the R tree of the algorithm. Using a dummy node, left
234 // and right will always be nodes and we avoid special cases. 237 // and right will always be nodes and we avoid special cases.
235 Node* dummy = &dummy_node; 238 Node* dummy = &dummy_node;
236 Node* left = dummy; 239 Node* left = dummy;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 } 279 }
277 // Assemble. 280 // Assemble.
278 left->right_ = current->left_; 281 left->right_ = current->left_;
279 right->left_ = current->right_; 282 right->left_ = current->right_;
280 current->left_ = dummy->right_; 283 current->left_ = dummy->right_;
281 current->right_ = dummy->left_; 284 current->right_ = dummy->left_;
282 root_ = current; 285 root_ = current;
283 } 286 }
284 287
285 288
286 template <typename Config, class Allocator> template <class Callback> 289 template <typename Config, class AllocationPolicy> template <class Callback>
287 void SplayTree<Config, Allocator>::ForEach(Callback* callback) { 290 void SplayTree<Config, AllocationPolicy>::ForEach(Callback* callback) {
288 NodeToPairAdaptor<Callback> callback_adaptor(callback); 291 NodeToPairAdaptor<Callback> callback_adaptor(callback);
289 ForEachNode(&callback_adaptor); 292 ForEachNode(&callback_adaptor);
290 } 293 }
291 294
292 295
293 template <typename Config, class Allocator> template <class Callback> 296 template <typename Config, class AllocationPolicy> template <class Callback>
294 void SplayTree<Config, Allocator>::ForEachNode(Callback* callback) { 297 void SplayTree<Config, AllocationPolicy>::ForEachNode(Callback* callback,
298 Allocator allocator) {
295 // Pre-allocate some space for tiny trees. 299 // Pre-allocate some space for tiny trees.
296 List<Node*, Allocator> nodes_to_visit(10); 300 List<Node*, AllocationPolicy> nodes_to_visit(10);
297 if (root_ != NULL) nodes_to_visit.Add(root_); 301 if (root_ != NULL) nodes_to_visit.Add(root_, allocator);
298 int pos = 0; 302 int pos = 0;
299 while (pos < nodes_to_visit.length()) { 303 while (pos < nodes_to_visit.length()) {
300 Node* node = nodes_to_visit[pos++]; 304 Node* node = nodes_to_visit[pos++];
301 if (node->left() != NULL) nodes_to_visit.Add(node->left()); 305 if (node->left() != NULL) nodes_to_visit.Add(node->left(), allocator);
302 if (node->right() != NULL) nodes_to_visit.Add(node->right()); 306 if (node->right() != NULL) nodes_to_visit.Add(node->right(), allocator);
303 callback->Call(node); 307 callback->Call(node);
304 } 308 }
305 } 309 }
306 310
307 311
308 } } // namespace v8::internal 312 } } // namespace v8::internal
309 313
310 #endif // V8_SPLAY_TREE_INL_H_ 314 #endif // V8_SPLAY_TREE_INL_H_
OLDNEW
« src/splay-tree.h ('K') | « src/splay-tree.h ('k') | src/string-stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698