V8 Project
splay-tree.h
Go to the documentation of this file.
1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_SPLAY_TREE_H_
6 #define V8_SPLAY_TREE_H_
7 
8 #include "src/allocation.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 
14 // A splay tree. The config type parameter encapsulates the different
15 // configurations of a concrete splay tree:
16 //
17 // typedef Key: the key type
18 // typedef Value: the value type
19 // static const Key kNoKey: the dummy key used when no key is set
20 // static Value kNoValue(): the dummy value used to initialize nodes
21 // static int (Compare)(Key& a, Key& b) -> {-1, 0, 1}: comparison function
22 //
23 // The tree is also parameterized by an allocation policy
24 // (Allocator). The policy is used for allocating lists in the C free
25 // store or the zone; see zone.h.
26 
27 // Forward defined as
28 // template <typename Config, class Allocator = FreeStoreAllocationPolicy>
29 // class SplayTree;
30 template <typename Config, class AllocationPolicy>
31 class SplayTree {
32  public:
33  typedef typename Config::Key Key;
34  typedef typename Config::Value Value;
35 
36  class Locator;
37 
40  ~SplayTree();
41 
42  INLINE(void* operator new(size_t size,
44  return allocator.New(static_cast<int>(size));
45  }
46  INLINE(void operator delete(void* p)) {
47  AllocationPolicy::Delete(p);
48  }
49  // Please the MSVC compiler. We should never have to execute this.
50  INLINE(void operator delete(void* p, AllocationPolicy policy)) {
51  UNREACHABLE();
52  }
53 
55 
56  // Checks if there is a mapping for the key.
57  bool Contains(const Key& key);
58 
59  // Inserts the given key in this tree with the given value. Returns
60  // true if a node was inserted, otherwise false. If found the locator
61  // is enabled and provides access to the mapping for the key.
62  bool Insert(const Key& key, Locator* locator);
63 
64  // Looks up the key in this tree and returns true if it was found,
65  // otherwise false. If the node is found the locator is enabled and
66  // provides access to the mapping for the key.
67  bool Find(const Key& key, Locator* locator);
68 
69  // Finds the mapping with the greatest key less than or equal to the
70  // given key.
71  bool FindGreatestLessThan(const Key& key, Locator* locator);
72 
73  // Find the mapping with the greatest key in this tree.
74  bool FindGreatest(Locator* locator);
75 
76  // Finds the mapping with the least key greater than or equal to the
77  // given key.
78  bool FindLeastGreaterThan(const Key& key, Locator* locator);
79 
80  // Find the mapping with the least key in this tree.
81  bool FindLeast(Locator* locator);
82 
83  // Move the node from one key to another.
84  bool Move(const Key& old_key, const Key& new_key);
85 
86  // Remove the node with the given key from the tree.
87  bool Remove(const Key& key);
88 
89  // Remove all keys from the tree.
90  void Clear() { ResetRoot(); }
91 
92  bool is_empty() { return root_ == NULL; }
93 
94  // Perform the splay operation for the given key. Moves the node with
95  // the given key to the top of the tree. If no node has the given
96  // key, the last node on the search path is moved to the top of the
97  // tree.
98  void Splay(const Key& key);
99 
100  class Node {
101  public:
102  Node(const Key& key, const Value& value)
103  : key_(key),
104  value_(value),
105  left_(NULL),
106  right_(NULL) { }
107 
108  INLINE(void* operator new(size_t size, AllocationPolicy allocator)) {
109  return allocator.New(static_cast<int>(size));
110  }
111  INLINE(void operator delete(void* p)) {
112  return AllocationPolicy::Delete(p);
113  }
114  // Please the MSVC compiler. We should never have to execute
115  // this.
116  INLINE(void operator delete(void* p, AllocationPolicy allocator)) {
117  UNREACHABLE();
118  }
119 
120  Key key() { return key_; }
121  Value value() { return value_; }
122  Node* left() { return left_; }
123  Node* right() { return right_; }
124 
125  private:
126  friend class SplayTree;
127  friend class Locator;
132  };
133 
134  // A locator provides access to a node in the tree without actually
135  // exposing the node.
136  class Locator BASE_EMBEDDED {
137  public:
138  explicit Locator(Node* node) : node_(node) { }
139  Locator() : node_(NULL) { }
140  const Key& key() { return node_->key_; }
141  Value& value() { return node_->value_; }
142  void set_value(const Value& value) { node_->value_ = value; }
143  inline void bind(Node* node) { node_ = node; }
144 
145  private:
147  };
148 
149  template <class Callback>
150  void ForEach(Callback* callback);
151 
152  protected:
153  // Resets tree root. Existing nodes become unreachable.
154  void ResetRoot() { root_ = NULL; }
155 
156  private:
157  // Search for a node with a given key. If found, root_ points
158  // to the node.
159  bool FindInternal(const Key& key);
160 
161  // Inserts a node assuming that root_ is already set up.
162  void InsertInternal(int cmp, Node* node);
163 
164  // Removes root_ node.
165  void RemoveRootNode(const Key& key);
166 
167  template<class Callback>
168  class NodeToPairAdaptor BASE_EMBEDDED {
169  public:
170  explicit NodeToPairAdaptor(Callback* callback)
171  : callback_(callback) { }
172  void Call(Node* node) {
173  callback_->Call(node->key(), node->value());
174  }
175 
176  private:
177  Callback* callback_;
178 
179  DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor);
180  };
181 
182  class NodeDeleter BASE_EMBEDDED {
183  public:
185  void Call(Node* node) { AllocationPolicy::Delete(node); }
186 
187  private:
189  };
190 
191  template <class Callback>
192  void ForEachNode(Callback* callback);
193 
196 
198 };
199 
200 
201 } } // namespace v8::internal
202 
203 #endif // V8_SPLAY_TREE_H_
void set_value(const Value &value)
Definition: splay-tree.h:142
NodeToPairAdaptor(Callback *callback)
Definition: splay-tree.h:170
DISALLOW_COPY_AND_ASSIGN(NodeToPairAdaptor)
INLINE(void operator delete(void *p, AllocationPolicy allocator))
Definition: splay-tree.h:116
INLINE(void *operator new(size_t size, AllocationPolicy allocator))
Definition: splay-tree.h:108
INLINE(void operator delete(void *p))
Definition: splay-tree.h:111
Node(const Key &key, const Value &value)
Definition: splay-tree.h:102
bool FindGreatestLessThan(const Key &key, Locator *locator)
bool FindLeast(Locator *locator)
bool Remove(const Key &key)
AllocationPolicy allocator()
Definition: splay-tree.h:54
bool Contains(const Key &key)
bool FindInternal(const Key &key)
SplayTree(AllocationPolicy allocator=AllocationPolicy())
Definition: splay-tree.h:38
bool Move(const Key &old_key, const Key &new_key)
AllocationPolicy allocator_
Definition: splay-tree.h:195
void RemoveRootNode(const Key &key)
void ForEach(Callback *callback)
bool FindLeastGreaterThan(const Key &key, Locator *locator)
INLINE(void *operator new(size_t size, AllocationPolicy allocator=AllocationPolicy()))
Definition: splay-tree.h:42
Config::Value Value
Definition: splay-tree.h:34
INLINE(void operator delete(void *p))
Definition: splay-tree.h:46
bool FindGreatest(Locator *locator)
void InsertInternal(int cmp, Node *node)
INLINE(void operator delete(void *p, AllocationPolicy policy))
Definition: splay-tree.h:50
DISALLOW_COPY_AND_ASSIGN(SplayTree)
bool Find(const Key &key, Locator *locator)
void ForEachNode(Callback *callback)
void Splay(const Key &key)
bool Insert(const Key &key, Locator *locator)
enable harmony numeric enable harmony object literal extensions Optimize object size
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
#define UNREACHABLE()
Definition: logging.h:30
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20