v8  3.11.10(node0.8.26)
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, size_t)) {
70  AllocationPolicy::Delete(p);
71  }
72 
73  // Inserts the given key in this tree with the given value. Returns
74  // true if a node was inserted, otherwise false. If found the locator
75  // is enabled and provides access to the mapping for the key.
76  bool Insert(const Key& key, Locator* locator);
77 
78  // Looks up the key in this tree and returns true if it was found,
79  // otherwise false. If the node is found the locator is enabled and
80  // provides access to the mapping for the key.
81  bool Find(const Key& key, Locator* locator);
82 
83  // Finds the mapping with the greatest key less than or equal to the
84  // given key.
85  bool FindGreatestLessThan(const Key& key, Locator* locator);
86 
87  // Find the mapping with the greatest key in this tree.
88  bool FindGreatest(Locator* locator);
89 
90  // Finds the mapping with the least key greater than or equal to the
91  // given key.
92  bool FindLeastGreaterThan(const Key& key, Locator* locator);
93 
94  // Find the mapping with the least key in this tree.
95  bool FindLeast(Locator* locator);
96 
97  // Move the node from one key to another.
98  bool Move(const Key& old_key, const Key& new_key);
99 
100  // Remove the node with the given key from the tree.
101  bool Remove(const Key& key);
102 
103  bool is_empty() { return root_ == NULL; }
104 
105  // Perform the splay operation for the given key. Moves the node with
106  // the given key to the top of the tree. If no node has the given
107  // key, the last node on the search path is moved to the top of the
108  // tree.
109  void Splay(const Key& key);
110 
111  class Node {
112  public:
113  Node(const Key& key, const Value& value)
114  : key_(key),
115  value_(value),
116  left_(NULL),
117  right_(NULL) { }
118 
119  INLINE(void* operator new(size_t size, AllocationPolicy allocator)) {
120  return allocator.New(static_cast<int>(size));
121  }
122  INLINE(void operator delete(void* p, size_t)) {
123  return AllocationPolicy::Delete(p);
124  }
125 
126  Key key() { return key_; }
127  Value value() { return value_; }
128  Node* left() { return left_; }
129  Node* right() { return right_; }
130 
131  private:
132  friend class SplayTree;
133  friend class Locator;
134  Key key_;
135  Value value_;
136  Node* left_;
137  Node* right_;
138  };
139 
140  // A locator provides access to a node in the tree without actually
141  // exposing the node.
142  class Locator BASE_EMBEDDED {
143  public:
144  explicit Locator(Node* node) : node_(node) { }
145  Locator() : node_(NULL) { }
146  const Key& key() { return node_->key_; }
147  Value& value() { return node_->value_; }
148  void set_value(const Value& value) { node_->value_ = value; }
149  inline void bind(Node* node) { node_ = node; }
150 
151  private:
152  Node* node_;
153  };
154 
155  template <class Callback>
156  void ForEach(Callback* callback);
157 
158  protected:
159  // Resets tree root. Existing nodes become unreachable.
160  void ResetRoot() { root_ = NULL; }
161 
162  private:
163  // Search for a node with a given key. If found, root_ points
164  // to the node.
165  bool FindInternal(const Key& key);
166 
167  // Inserts a node assuming that root_ is already set up.
168  void InsertInternal(int cmp, Node* node);
169 
170  // Removes root_ node.
171  void RemoveRootNode(const Key& key);
172 
173  template<class Callback>
174  class NodeToPairAdaptor BASE_EMBEDDED {
175  public:
176  explicit NodeToPairAdaptor(Callback* callback)
177  : callback_(callback) { }
178  void Call(Node* node) {
179  callback_->Call(node->key(), node->value());
180  }
181 
182  private:
183  Callback* callback_;
184 
185  DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor);
186  };
187 
188  class NodeDeleter BASE_EMBEDDED {
189  public:
191  void Call(Node* node) { AllocationPolicy::Delete(node); }
192 
193  private:
194  DISALLOW_COPY_AND_ASSIGN(NodeDeleter);
195  };
196 
197  template <class Callback>
198  void ForEachNode(Callback* callback);
199 
200  Node* root_;
201  AllocationPolicy allocator_;
202 
203  DISALLOW_COPY_AND_ASSIGN(SplayTree);
204 };
205 
206 
207 } } // namespace v8::internal
208 
209 #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)
bool FindGreatest(Locator *locator)
void Splay(const Key &key)
INLINE(void operator delete(void *p, size_t))
Definition: splay-tree.h:122
void ForEach(Callback *callback)
Node(const Key &key, const Value &value)
Definition: splay-tree.h:113
INLINE(void *operator new(size_t size, AllocationPolicy allocator))
Definition: splay-tree.h:119
INLINE(void operator delete(void *p, size_t))
Definition: splay-tree.h:69
INLINE(void *operator new(size_t size, AllocationPolicy allocator=AllocationPolicy()))
Definition: splay-tree.h:65
NodeToPairAdaptor(Callback *callback)
Definition: splay-tree.h:176
bool FindLeast(Locator *locator)
bool Move(const Key &old_key, const Key &new_key)
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:321
bool FindLeastGreaterThan(const Key &key, Locator *locator)
bool Remove(const Key &key)
bool FindGreatestLessThan(const Key &key, Locator *locator)
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 trace on stack replacement optimize closures functions with arguments object optimize functions containing for in loops profiler considers IC stability primitive functions trigger their own optimization re try self optimization if it failed insert an interrupt check at function exit execution budget before interrupt is triggered call count before self optimization self_optimization count_based_interrupts weighted_back_edges trace_opt 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 enable use of ARMv7 instructions if enable use of MIPS FPU instructions if NULL
Definition: flags.cc:274
void set_value(const Value &value)
Definition: splay-tree.h:148
SplayTree(AllocationPolicy allocator=AllocationPolicy())
Definition: splay-tree.h:61