Flutter Engine
The Flutter Engine
Classes | Public Types | Public Member Functions | Protected Member Functions | List of all members
dart::SplayTree< Config, B, Allocator > Class Template Reference

#include <splay-tree.h>

Inheritance diagram for dart::SplayTree< Config, B, Allocator >:
B A

Classes

class  Locator
 
class  Node
 

Public Types

typedef Config::Key Key
 
typedef Config::Value Value
 

Public Member Functions

 SplayTree (Allocator *allocator)
 
 ~SplayTree ()
 
Allocatorallocator ()
 
bool Contains (const Key &key)
 
bool Insert (const Key &key, Locator *locator)
 
bool Find (const Key &key, Locator *locator)
 
bool FindGreatestLessThan (const Key &key, Locator *locator)
 
bool FindGreatest (Locator *locator)
 
bool FindLeastGreaterThan (const Key &key, Locator *locator)
 
bool FindLeast (Locator *locator)
 
bool Move (const Key &old_key, const Key &new_key)
 
bool Remove (const Key &key)
 
void Clear ()
 
bool is_empty ()
 
void Splay (const Key &key)
 
template<class Callback >
void ForEach (Callback *callback)
 
- Public Member Functions inherited from B
 B ()
 
void setValues (int v) override
 
bool checkValues (int v) override
 
- Public Member Functions inherited from A
 A ()
 
virtual void setValues (int v)
 
virtual bool checkValues (int v)
 
virtual ~A ()
 
void * operator new (size_t size)
 
void operator delete (void *p)
 

Protected Member Functions

void ResetRoot ()
 

Additional Inherited Members

- Static Public Member Functions inherited from A
static ACreate (SkRandom *r)
 
static void SetAllocator (size_t preallocSize, size_t minAllocSize)
 
static void ResetAllocator ()
 
static void ValidatePool ()
 

Detailed Description

template<typename Config, class B, class Allocator>
class dart::SplayTree< Config, B, Allocator >

Definition at line 29 of file splay-tree.h.

Member Typedef Documentation

◆ Key

template<typename Config , class B , class Allocator >
typedef Config::Key dart::SplayTree< Config, B, Allocator >::Key

Definition at line 31 of file splay-tree.h.

◆ Value

template<typename Config , class B , class Allocator >
typedef Config::Value dart::SplayTree< Config, B, Allocator >::Value

Definition at line 32 of file splay-tree.h.

Constructor & Destructor Documentation

◆ SplayTree()

template<typename Config , class B , class Allocator >
dart::SplayTree< Config, B, Allocator >::SplayTree ( Allocator allocator)
inlineexplicit

Definition at line 36 of file splay-tree.h.

37 : root_(nullptr), allocator_(allocator) {}
Allocator * allocator()
Definition: splay-tree.h:40

◆ ~SplayTree()

template<typename Config , class B , class Allocator >
dart::SplayTree< Config, B, Allocator >::~SplayTree

Definition at line 18 of file splay-tree-inl.h.

18 {
19 NodeDeleter deleter;
20 ForEachNode(&deleter);
21}

Member Function Documentation

◆ allocator()

template<typename Config , class B , class Allocator >
Allocator * dart::SplayTree< Config, B, Allocator >::allocator ( )
inline

Definition at line 40 of file splay-tree.h.

40{ return allocator_; }

◆ Clear()

template<typename Config , class B , class Allocator >
void dart::SplayTree< Config, B, Allocator >::Clear ( )
inline

Definition at line 76 of file splay-tree.h.

76{ ResetRoot(); }
void ResetRoot()
Definition: splay-tree.h:125

◆ Contains()

template<typename Config , class B , class Allocator >
bool dart::SplayTree< Config, B, Allocator >::Contains ( const Key key)

Definition at line 68 of file splay-tree-inl.h.

68 {
69 return FindInternal(key);
70}

◆ Find()

template<typename Config , class B , class Allocator >
bool dart::SplayTree< Config, B, Allocator >::Find ( const Key key,
Locator locator 
)

Definition at line 73 of file splay-tree-inl.h.

73 {
74 if (FindInternal(key)) {
75 locator->bind(root_);
76 return true;
77 } else {
78 return false;
79 }
80}

◆ FindGreatest()

template<typename Config , class B , class Allocator >
bool dart::SplayTree< Config, B, Allocator >::FindGreatest ( Locator locator)

Definition at line 127 of file splay-tree-inl.h.

127 {
128 if (is_empty()) return false;
129 Node* current = root_;
130 while (current->right_ != nullptr)
131 current = current->right_;
132 locator->bind(current);
133 return true;
134}
bool is_empty()
Definition: splay-tree.h:78
Definition: dart.idl:29

◆ FindGreatestLessThan()

template<typename Config , class B , class Allocator >
bool dart::SplayTree< Config, B, Allocator >::FindGreatestLessThan ( const Key key,
Locator locator 
)

Definition at line 83 of file splay-tree-inl.h.

84 {
85 if (is_empty()) return false;
86 // Splay on the key to move the node with the given key or the last
87 // node on the search path to the top of the tree.
88 Splay(key);
89 // Now the result is either the root node or the greatest node in
90 // the left subtree.
91 int cmp = Config::Compare(root_->key_, key);
92 if (cmp <= 0) {
93 locator->bind(root_);
94 return true;
95 } else {
96 Node* temp = root_;
97 root_ = root_->left_;
98 bool result = FindGreatest(locator);
99 root_ = temp;
100 return result;
101 }
102}
void Splay(const Key &key)
bool FindGreatest(Locator *locator)
GAsyncResult * result

◆ FindLeast()

template<typename Config , class B , class Allocator >
bool dart::SplayTree< Config, B, Allocator >::FindLeast ( Locator locator)

Definition at line 137 of file splay-tree-inl.h.

137 {
138 if (is_empty()) return false;
139 Node* current = root_;
140 while (current->left_ != nullptr)
141 current = current->left_;
142 locator->bind(current);
143 return true;
144}

◆ FindLeastGreaterThan()

template<typename Config , class B , class Allocator >
bool dart::SplayTree< Config, B, Allocator >::FindLeastGreaterThan ( const Key key,
Locator locator 
)

Definition at line 105 of file splay-tree-inl.h.

106 {
107 if (is_empty()) return false;
108 // Splay on the key to move the node with the given key or the last
109 // node on the search path to the top of the tree.
110 Splay(key);
111 // Now the result is either the root node or the least node in
112 // the right subtree.
113 int cmp = Config::Compare(root_->key_, key);
114 if (cmp >= 0) {
115 locator->bind(root_);
116 return true;
117 } else {
118 Node* temp = root_;
119 root_ = root_->right_;
120 bool result = FindLeast(locator);
121 root_ = temp;
122 return result;
123 }
124}
bool FindLeast(Locator *locator)

◆ ForEach()

template<typename Config , class B , class Allocator >
template<class Callback >
void dart::SplayTree< Config, B, Allocator >::ForEach ( Callback *  callback)

Definition at line 248 of file splay-tree-inl.h.

248 {
249 NodeToPairAdaptor<Callback> callback_adaptor(callback);
250 ForEachNode(&callback_adaptor);
251}
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback

◆ Insert()

template<typename Config , class B , class Allocator >
bool dart::SplayTree< Config, B, Allocator >::Insert ( const Key key,
Locator locator 
)

Definition at line 24 of file splay-tree-inl.h.

24 {
25 if (is_empty()) {
26 // If the tree is empty, insert the new node.
27 root_ = new (allocator_) Node(key, Config::NoValue());
28 } else {
29 // Splay on the key to move the last node on the search path
30 // for the key to the root of the tree.
31 Splay(key);
32 // Ignore repeated insertions with the same key.
33 int cmp = Config::Compare(key, root_->key_);
34 if (cmp == 0) {
35 locator->bind(root_);
36 return false;
37 }
38 // Insert the new node.
39 Node* node = new (allocator_) Node(key, Config::NoValue());
40 InsertInternal(cmp, node);
41 }
42 locator->bind(root_);
43 return true;
44}

◆ is_empty()

template<typename Config , class B , class Allocator >
bool dart::SplayTree< Config, B, Allocator >::is_empty ( )
inline

Definition at line 78 of file splay-tree.h.

78{ return root_ == nullptr; }

◆ Move()

template<typename Config , class B , class Allocator >
bool dart::SplayTree< Config, B, Allocator >::Move ( const Key old_key,
const Key new_key 
)

Definition at line 147 of file splay-tree-inl.h.

148 {
149 if (!FindInternal(old_key)) return false;
150 Node* node_to_move = root_;
151 RemoveRootNode(old_key);
152 Splay(new_key);
153 int cmp = Config::Compare(new_key, root_->key_);
154 if (cmp == 0) {
155 // A node with the target key already exists.
156 delete node_to_move;
157 return false;
158 }
159 node_to_move->key_ = new_key;
160 InsertInternal(cmp, node_to_move);
161 return true;
162}

◆ Remove()

template<typename Config , class B , class Allocator >
bool dart::SplayTree< Config, B, Allocator >::Remove ( const Key key)

Definition at line 165 of file splay-tree-inl.h.

165 {
166 if (!FindInternal(key)) return false;
167 Node* node_to_remove = root_;
168 RemoveRootNode(key);
169 delete node_to_remove;
170 return true;
171}

◆ ResetRoot()

template<typename Config , class B , class Allocator >
void dart::SplayTree< Config, B, Allocator >::ResetRoot ( )
inlineprotected

Definition at line 125 of file splay-tree.h.

125{ root_ = nullptr; }

◆ Splay()

template<typename Config , class B , class Allocator >
void dart::SplayTree< Config, B, Allocator >::Splay ( const Key key)

Definition at line 192 of file splay-tree-inl.h.

192 {
193 if (is_empty()) return;
194 Node dummy_node(Config::kNoKey, Config::NoValue());
195 // Create a dummy node. The use of the dummy node is a bit
196 // counter-intuitive: The right child of the dummy node will hold
197 // the L tree of the algorithm. The left child of the dummy node
198 // will hold the R tree of the algorithm. Using a dummy node, left
199 // and right will always be nodes and we avoid special cases.
200 Node* dummy = &dummy_node;
201 Node* left = dummy;
202 Node* right = dummy;
203 Node* current = root_;
204 while (true) {
205 int cmp = Config::Compare(key, current->key_);
206 if (cmp < 0) {
207 if (current->left_ == nullptr) break;
208 if (Config::Compare(key, current->left_->key_) < 0) {
209 // Rotate right.
210 Node* temp = current->left_;
211 current->left_ = temp->right_;
212 temp->right_ = current;
213 current = temp;
214 if (current->left_ == nullptr) break;
215 }
216 // Link right.
217 right->left_ = current;
218 right = current;
219 current = current->left_;
220 } else if (cmp > 0) {
221 if (current->right_ == nullptr) break;
222 if (Config::Compare(key, current->right_->key_) > 0) {
223 // Rotate left.
224 Node* temp = current->right_;
225 current->right_ = temp->left_;
226 temp->left_ = current;
227 current = temp;
228 if (current->right_ == nullptr) break;
229 }
230 // Link left.
231 left->right_ = current;
232 left = current;
233 current = current->right_;
234 } else {
235 break;
236 }
237 }
238 // Assemble.
239 left->right_ = current->left_;
240 right->left_ = current->right_;
241 current->left_ = dummy->right_;
242 current->right_ = dummy->left_;
243 root_ = current;
244}

The documentation for this class was generated from the following files: