v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
splay-tree.h
Go to the documentation of this file.
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 #include "allocation.h"
32 
33 namespace v8 {
34 namespace internal {
35 
36 
37 // A splay tree. The config type parameter encapsulates the different
38 // configurations of a concrete splay tree:
39 //
40 // typedef Key: the key type
41 // typedef Value: the value type
42 // static const kNoKey: the dummy key used when no key is set
43 // static const kNoValue: the dummy value used to initialize nodes
44 // int (Compare)(Key& a, Key& b) -> {-1, 0, 1}: comparison function
45 //
46 // The tree is also parameterized by an allocation policy
47 // (Allocator). The policy is used for allocating lists in the C free
48 // store or the zone; see zone.h.
49 
50 // Forward defined as
51 // template <typename Config, class Allocator = FreeStoreAllocationPolicy>
52 // class SplayTree;
53 template <typename Config, class AllocationPolicy>
54 class SplayTree {
55  public:
56  typedef typename Config::Key Key;
57  typedef typename Config::Value Value;
58 
59  class Locator;
60 
61  SplayTree(AllocationPolicy allocator = AllocationPolicy())
62  : root_(NULL), allocator_(allocator) { }
63  ~SplayTree();
64 
65  INLINE(void* operator new(size_t size,
66  AllocationPolicy allocator = AllocationPolicy())) {
67  return allocator.New(static_cast<int>(size));
68  }
69  INLINE(void operator delete(void* p)) {
70  AllocationPolicy::Delete(p);
71  }
72  // Please the MSVC compiler. We should never have to execute this.
73  INLINE(void operator delete(void* p, AllocationPolicy policy)) {
74  UNREACHABLE();
75  }
76 
77  // Inserts the given key in this tree with the given value. Returns
78  // true if a node was inserted, otherwise false. If found the locator
79  // is enabled and provides access to the mapping for the key.
80  bool Insert(const Key& key, Locator* locator);
81 
82  // Looks up the key in this tree and returns true if it was found,
83  // otherwise false. If the node is found the locator is enabled and
84  // provides access to the mapping for the key.
85  bool Find(const Key& key, Locator* locator);
86 
87  // Finds the mapping with the greatest key less than or equal to the
88  // given key.
89  bool FindGreatestLessThan(const Key& key, Locator* locator);
90 
91  // Find the mapping with the greatest key in this tree.
92  bool FindGreatest(Locator* locator);
93 
94  // Finds the mapping with the least key greater than or equal to the
95  // given key.
96  bool FindLeastGreaterThan(const Key& key, Locator* locator);
97 
98  // Find the mapping with the least key in this tree.
99  bool FindLeast(Locator* locator);
100 
101  // Move the node from one key to another.
102  bool Move(const Key& old_key, const Key& new_key);
103 
104  // Remove the node with the given key from the tree.
105  bool Remove(const Key& key);
106 
107  bool is_empty() { return root_ == NULL; }
108 
109  // Perform the splay operation for the given key. Moves the node with
110  // the given key to the top of the tree. If no node has the given
111  // key, the last node on the search path is moved to the top of the
112  // tree.
113  void Splay(const Key& key);
114 
115  class Node {
116  public:
117  Node(const Key& key, const Value& value)
118  : key_(key),
119  value_(value),
120  left_(NULL),
121  right_(NULL) { }
122 
123  INLINE(void* operator new(size_t size, AllocationPolicy allocator)) {
124  return allocator.New(static_cast<int>(size));
125  }
126  INLINE(void operator delete(void* p)) {
127  return AllocationPolicy::Delete(p);
128  }
129  // Please the MSVC compiler. We should never have to execute
130  // this.
131  INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
132  UNREACHABLE();
133  }
134 
135  Key key() { return key_; }
136  Value value() { return value_; }
137  Node* left() { return left_; }
138  Node* right() { return right_; }
139 
140  private:
141  friend class SplayTree;
142  friend class Locator;
143  Key key_;
144  Value value_;
145  Node* left_;
146  Node* right_;
147  };
148 
149  // A locator provides access to a node in the tree without actually
150  // exposing the node.
151  class Locator BASE_EMBEDDED {
152  public:
153  explicit Locator(Node* node) : node_(node) { }
154  Locator() : node_(NULL) { }
155  const Key& key() { return node_->key_; }
156  Value& value() { return node_->value_; }
157  void set_value(const Value& value) { node_->value_ = value; }
158  inline void bind(Node* node) { node_ = node; }
159 
160  private:
161  Node* node_;
162  };
163 
164  template <class Callback>
165  void ForEach(Callback* callback);
166 
167  protected:
168  // Resets tree root. Existing nodes become unreachable.
169  void ResetRoot() { root_ = NULL; }
170 
171  private:
172  // Search for a node with a given key. If found, root_ points
173  // to the node.
174  bool FindInternal(const Key& key);
175 
176  // Inserts a node assuming that root_ is already set up.
177  void InsertInternal(int cmp, Node* node);
178 
179  // Removes root_ node.
180  void RemoveRootNode(const Key& key);
181 
182  template<class Callback>
183  class NodeToPairAdaptor BASE_EMBEDDED {
184  public:
185  explicit NodeToPairAdaptor(Callback* callback)
186  : callback_(callback) { }
187  void Call(Node* node) {
188  callback_->Call(node->key(), node->value());
189  }
190 
191  private:
192  Callback* callback_;
193 
194  DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor);
195  };
196 
197  class NodeDeleter BASE_EMBEDDED {
198  public:
200  void Call(Node* node) { AllocationPolicy::Delete(node); }
201 
202  private:
203  DISALLOW_COPY_AND_ASSIGN(NodeDeleter);
204  };
205 
206  template <class Callback>
207  void ForEachNode(Callback* callback);
208 
209  Node* root_;
210  AllocationPolicy allocator_;
211 
212  DISALLOW_COPY_AND_ASSIGN(SplayTree);
213 };
214 
215 
216 } } // namespace v8::internal
217 
218 #endif // V8_SPLAY_TREE_H_
bool Find(const Key &key, Locator *locator)
Config::Value Value
Definition: splay-tree.h:57
bool Insert(const Key &key, Locator *locator)
INLINE(void operator delete(void *p, AllocationPolicy policy))
Definition: splay-tree.h:73
bool FindGreatest(Locator *locator)
void Splay(const Key &key)
INLINE(void operator delete(void *p))
Definition: splay-tree.h:126
void ForEach(Callback *callback)
Node(const Key &key, const Value &value)
Definition: splay-tree.h:117
INLINE(void *operator new(size_t size, AllocationPolicy allocator))
Definition: splay-tree.h:123
INLINE(void *operator new(size_t size, AllocationPolicy allocator=AllocationPolicy()))
Definition: splay-tree.h:65
NodeToPairAdaptor(Callback *callback)
Definition: splay-tree.h:185
#define UNREACHABLE()
Definition: checks.h:50
bool FindLeast(Locator *locator)
bool Move(const Key &old_key, const Key &new_key)
INLINE(void operator delete(void *p))
Definition: splay-tree.h:69
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:307
bool FindLeastGreaterThan(const Key &key, Locator *locator)
bool Remove(const Key &key)
bool FindGreatestLessThan(const Key &key, Locator *locator)
INLINE(void operator delete(void *p, AllocationPolicy allocator))
Definition: splay-tree.h:131
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if NULL
Definition: flags.cc:301
void set_value(const Value &value)
Definition: splay-tree.h:157
SplayTree(AllocationPolicy allocator=AllocationPolicy())
Definition: splay-tree.h:61