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

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

Issue 910002: Start migrating profiles processing to C++. (Closed)
Patch Set: Comments addressed 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
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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 // for the key to the root of the tree. 51 // for the key to the root of the tree.
52 Splay(key); 52 Splay(key);
53 // Ignore repeated insertions with the same key. 53 // Ignore repeated insertions with the same key.
54 int cmp = Config::Compare(key, root_->key_); 54 int cmp = Config::Compare(key, root_->key_);
55 if (cmp == 0) { 55 if (cmp == 0) {
56 locator->bind(root_); 56 locator->bind(root_);
57 return false; 57 return false;
58 } 58 }
59 // Insert the new node. 59 // Insert the new node.
60 Node* node = new Node(key, Config::kNoValue); 60 Node* node = new Node(key, Config::kNoValue);
61 if (cmp > 0) { 61 InsertInternal(cmp, node);
62 node->left_ = root_;
63 node->right_ = root_->right_;
64 root_->right_ = NULL;
65 } else {
66 node->right_ = root_;
67 node->left_ = root_->left_;
68 root_->left_ = NULL;
69 }
70 root_ = node;
71 } 62 }
72 locator->bind(root_); 63 locator->bind(root_);
73 return true; 64 return true;
74 } 65 }
75 66
76 67
77 template<typename Config, class Allocator> 68 template<typename Config, class Allocator>
69 void SplayTree<Config, Allocator>::InsertInternal(int cmp, Node* node) {
70 if (cmp > 0) {
71 node->left_ = root_;
72 node->right_ = root_->right_;
73 root_->right_ = NULL;
74 } else {
75 node->right_ = root_;
76 node->left_ = root_->left_;
77 root_->left_ = NULL;
78 }
79 root_ = node;
80 }
81
82
83 template<typename Config, class Allocator>
84 bool SplayTree<Config, Allocator>::FindInternal(const Key& key) {
85 if (is_empty())
86 return false;
87 Splay(key);
88 return Config::Compare(key, root_->key_) == 0;
89 }
90
91
92 template<typename Config, class Allocator>
78 bool SplayTree<Config, Allocator>::Find(const Key& key, Locator* locator) { 93 bool SplayTree<Config, Allocator>::Find(const Key& key, Locator* locator) {
79 if (is_empty()) 94 if (FindInternal(key)) {
80 return false;
81 Splay(key);
82 if (Config::Compare(key, root_->key_) == 0) {
83 locator->bind(root_); 95 locator->bind(root_);
84 return true; 96 return true;
85 } else { 97 } else {
86 return false; 98 return false;
87 } 99 }
88 } 100 }
89 101
90 102
91 template<typename Config, class Allocator> 103 template<typename Config, class Allocator>
92 bool SplayTree<Config, Allocator>::FindGreatestLessThan(const Key& key, 104 bool SplayTree<Config, Allocator>::FindGreatestLessThan(const Key& key,
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 return false; 166 return false;
155 Node* current = root_; 167 Node* current = root_;
156 while (current->left_ != NULL) 168 while (current->left_ != NULL)
157 current = current->left_; 169 current = current->left_;
158 locator->bind(current); 170 locator->bind(current);
159 return true; 171 return true;
160 } 172 }
161 173
162 174
163 template<typename Config, class Allocator> 175 template<typename Config, class Allocator>
176 bool SplayTree<Config, Allocator>::Move(const Key& old_key,
177 const Key& new_key) {
178 if (!FindInternal(old_key))
179 return false;
180 Node* node_to_move = root_;
181 RemoveRootNode(old_key);
182 Splay(new_key);
183 int cmp = Config::Compare(new_key, root_->key_);
184 if (cmp == 0) {
185 // A node with the target key already exists.
186 delete node_to_move;
187 return false;
188 }
189 node_to_move->key_ = new_key;
190 InsertInternal(cmp, node_to_move);
191 return true;
192 }
193
194
195 template<typename Config, class Allocator>
164 bool SplayTree<Config, Allocator>::Remove(const Key& key) { 196 bool SplayTree<Config, Allocator>::Remove(const Key& key) {
165 // Bail if the tree is empty 197 if (!FindInternal(key))
166 if (is_empty())
167 return false; 198 return false;
168 // Splay on the key to move the node with the given key to the top. 199 Node* node_to_remove = root_;
169 Splay(key); 200 RemoveRootNode(key);
170 // Bail if the key is not in the tree 201 delete node_to_remove;
171 if (Config::Compare(key, root_->key_) != 0) 202 return true;
172 return false; 203 }
204
205
206 template<typename Config, class Allocator>
207 void SplayTree<Config, Allocator>::RemoveRootNode(const Key& key) {
173 if (root_->left_ == NULL) { 208 if (root_->left_ == NULL) {
174 // No left child, so the new tree is just the right child. 209 // No left child, so the new tree is just the right child.
175 root_ = root_->right_; 210 root_ = root_->right_;
176 } else { 211 } else {
177 // Left child exists. 212 // Left child exists.
178 Node* right = root_->right_; 213 Node* right = root_->right_;
179 // Make the original left child the new root. 214 // Make the original left child the new root.
180 root_ = root_->left_; 215 root_ = root_->left_;
181 // Splay to make sure that the new root has an empty right child. 216 // Splay to make sure that the new root has an empty right child.
182 Splay(key); 217 Splay(key);
183 // Insert the original right child as the right child of the new 218 // Insert the original right child as the right child of the new
184 // root. 219 // root.
185 root_->right_ = right; 220 root_->right_ = right;
186 } 221 }
187 return true;
188 } 222 }
189 223
190 224
191 template<typename Config, class Allocator> 225 template<typename Config, class Allocator>
192 void SplayTree<Config, Allocator>::Splay(const Key& key) { 226 void SplayTree<Config, Allocator>::Splay(const Key& key) {
193 if (is_empty()) 227 if (is_empty())
194 return; 228 return;
195 Node dummy_node(Config::kNoKey, Config::kNoValue); 229 Node dummy_node(Config::kNoKey, Config::kNoValue);
196 // Create a dummy node. The use of the dummy node is a bit 230 // Create a dummy node. The use of the dummy node is a bit
197 // counter-intuitive: The right child of the dummy node will hold 231 // counter-intuitive: The right child of the dummy node will hold
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 if (node->left() != NULL) nodes_to_visit.Add(node->left()); 301 if (node->left() != NULL) nodes_to_visit.Add(node->left());
268 if (node->right() != NULL) nodes_to_visit.Add(node->right()); 302 if (node->right() != NULL) nodes_to_visit.Add(node->right());
269 callback->Call(node); 303 callback->Call(node);
270 } 304 }
271 } 305 }
272 306
273 307
274 } } // namespace v8::internal 308 } } // namespace v8::internal
275 309
276 #endif // V8_SPLAY_TREE_INL_H_ 310 #endif // V8_SPLAY_TREE_INL_H_
OLDNEW
« src/profile-generator.cc ('K') | « src/splay-tree.h ('k') | test/cctest/SConscript » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698