| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 | 198 |
| 199 static int nesting() { return nesting_; } | 199 static int nesting() { return nesting_; } |
| 200 | 200 |
| 201 private: | 201 private: |
| 202 ZoneScopeMode mode_; | 202 ZoneScopeMode mode_; |
| 203 static int nesting_; | 203 static int nesting_; |
| 204 }; | 204 }; |
| 205 | 205 |
| 206 | 206 |
| 207 // A zone splay tree. The config type parameter encapsulates the | 207 // A zone splay tree. The config type parameter encapsulates the |
| 208 // different configurations of a concrete splay tree: | 208 // different configurations of a concrete splay tree (see splay-tree.h). |
| 209 // | 209 // The tree itself and all its elements are allocated in the Zone. |
| 210 // typedef Key: the key type | |
| 211 // typedef Value: the value type | |
| 212 // static const kNoKey: the dummy key used when no key is set | |
| 213 // static const kNoValue: the dummy value used to initialize nodes | |
| 214 // int (Compare)(Key& a, Key& b) -> {-1, 0, 1}: comparison function | |
| 215 // | |
| 216 template <typename Config> | 210 template <typename Config> |
| 217 class ZoneSplayTree : public ZoneObject { | 211 class ZoneSplayTree: public SplayTree<Config, ZoneListAllocationPolicy> { |
| 218 public: | 212 public: |
| 219 typedef typename Config::Key Key; | 213 ZoneSplayTree() |
| 220 typedef typename Config::Value Value; | 214 : SplayTree<Config, ZoneListAllocationPolicy>() {} |
| 221 | 215 ~ZoneSplayTree(); |
| 222 class Locator; | |
| 223 | |
| 224 ZoneSplayTree() : root_(NULL) { } | |
| 225 | |
| 226 // Inserts the given key in this tree with the given value. Returns | |
| 227 // true if a node was inserted, otherwise false. If found the locator | |
| 228 // is enabled and provides access to the mapping for the key. | |
| 229 bool Insert(const Key& key, Locator* locator); | |
| 230 | |
| 231 // Looks up the key in this tree and returns true if it was found, | |
| 232 // otherwise false. If the node is found the locator is enabled and | |
| 233 // provides access to the mapping for the key. | |
| 234 bool Find(const Key& key, Locator* locator); | |
| 235 | |
| 236 // Finds the mapping with the greatest key less than or equal to the | |
| 237 // given key. | |
| 238 bool FindGreatestLessThan(const Key& key, Locator* locator); | |
| 239 | |
| 240 // Find the mapping with the greatest key in this tree. | |
| 241 bool FindGreatest(Locator* locator); | |
| 242 | |
| 243 // Finds the mapping with the least key greater than or equal to the | |
| 244 // given key. | |
| 245 bool FindLeastGreaterThan(const Key& key, Locator* locator); | |
| 246 | |
| 247 // Find the mapping with the least key in this tree. | |
| 248 bool FindLeast(Locator* locator); | |
| 249 | |
| 250 // Remove the node with the given key from the tree. | |
| 251 bool Remove(const Key& key); | |
| 252 | |
| 253 bool is_empty() { return root_ == NULL; } | |
| 254 | |
| 255 // Perform the splay operation for the given key. Moves the node with | |
| 256 // the given key to the top of the tree. If no node has the given | |
| 257 // key, the last node on the search path is moved to the top of the | |
| 258 // tree. | |
| 259 void Splay(const Key& key); | |
| 260 | |
| 261 class Node : public ZoneObject { | |
| 262 public: | |
| 263 Node(const Key& key, const Value& value) | |
| 264 : key_(key), | |
| 265 value_(value), | |
| 266 left_(NULL), | |
| 267 right_(NULL) { } | |
| 268 Key key() { return key_; } | |
| 269 Value value() { return value_; } | |
| 270 Node* left() { return left_; } | |
| 271 Node* right() { return right_; } | |
| 272 private: | |
| 273 friend class ZoneSplayTree; | |
| 274 friend class Locator; | |
| 275 Key key_; | |
| 276 Value value_; | |
| 277 Node* left_; | |
| 278 Node* right_; | |
| 279 }; | |
| 280 | |
| 281 // A locator provides access to a node in the tree without actually | |
| 282 // exposing the node. | |
| 283 class Locator { | |
| 284 public: | |
| 285 explicit Locator(Node* node) : node_(node) { } | |
| 286 Locator() : node_(NULL) { } | |
| 287 const Key& key() { return node_->key_; } | |
| 288 Value& value() { return node_->value_; } | |
| 289 void set_value(const Value& value) { node_->value_ = value; } | |
| 290 inline void bind(Node* node) { node_ = node; } | |
| 291 private: | |
| 292 Node* node_; | |
| 293 }; | |
| 294 | |
| 295 template <class Callback> | |
| 296 void ForEach(Callback* callback); | |
| 297 | |
| 298 private: | |
| 299 Node* root_; | |
| 300 }; | 216 }; |
| 301 | 217 |
| 302 | 218 |
| 303 } } // namespace v8::internal | 219 } } // namespace v8::internal |
| 304 | 220 |
| 305 #endif // V8_ZONE_H_ | 221 #endif // V8_ZONE_H_ |
| OLD | NEW |