Bug 1507750 - Compare the flexbox state for any changes before updating on reflows...
[gecko.git] / mfbt / SplayTree.h
blob1555d135df4cec296d3d788f77c1f414cd3593dc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /**
8 * A sorted tree with optimal access times, where recently-accessed elements
9 * are faster to access again.
12 #ifndef mozilla_SplayTree_h
13 #define mozilla_SplayTree_h
15 #include "mozilla/Assertions.h"
16 #include "mozilla/Attributes.h"
18 namespace mozilla {
20 template<class T, class C>
21 class SplayTree;
23 template<typename T>
24 class SplayTreeNode
26 public:
27 template<class A, class B>
28 friend class SplayTree;
30 SplayTreeNode()
31 : mLeft(nullptr)
32 , mRight(nullptr)
33 , mParent(nullptr)
36 private:
37 T* mLeft;
38 T* mRight;
39 T* mParent;
43 /**
44 * Class which represents a splay tree.
45 * Splay trees are balanced binary search trees for which search, insert and
46 * remove are all amortized O(log n), but where accessing a node makes it
47 * faster to access that node in the future.
49 * T indicates the type of tree elements, Comparator must have a static
50 * compare(const T&, const T&) method ordering the elements. The compare
51 * method must be free from side effects.
53 template<typename T, class Comparator>
54 class SplayTree
56 T* mRoot;
58 public:
59 constexpr SplayTree()
60 : mRoot(nullptr)
63 bool empty() const
65 return !mRoot;
68 T* find(const T& aValue)
70 if (empty()) {
71 return nullptr;
74 T* last = lookup(aValue);
75 splay(last);
76 return Comparator::compare(aValue, *last) == 0 ? last : nullptr;
79 void insert(T* aValue)
81 MOZ_ASSERT(!find(*aValue), "Duplicate elements are not allowed.");
83 if (!mRoot) {
84 mRoot = aValue;
85 return;
87 T* last = lookup(*aValue);
88 int cmp = Comparator::compare(*aValue, *last);
90 finishInsertion(last, cmp, aValue);
93 T* findOrInsert(const T& aValue);
95 T* remove(const T& aValue)
97 T* last = lookup(aValue);
98 MOZ_ASSERT(last, "This tree must contain the element being removed.");
99 MOZ_ASSERT(Comparator::compare(aValue, *last) == 0);
101 // Splay the tree so that the item to remove is the root.
102 splay(last);
103 MOZ_ASSERT(last == mRoot);
105 // Find another node which can be swapped in for the root: either the
106 // rightmost child of the root's left, or the leftmost child of the
107 // root's right.
108 T* swap;
109 T* swapChild;
110 if (mRoot->mLeft) {
111 swap = mRoot->mLeft;
112 while (swap->mRight) {
113 swap = swap->mRight;
115 swapChild = swap->mLeft;
116 } else if (mRoot->mRight) {
117 swap = mRoot->mRight;
118 while (swap->mLeft) {
119 swap = swap->mLeft;
121 swapChild = swap->mRight;
122 } else {
123 T* result = mRoot;
124 mRoot = nullptr;
125 return result;
128 // The selected node has at most one child, in swapChild. Detach it
129 // from the subtree by replacing it with that child.
130 if (swap == swap->mParent->mLeft) {
131 swap->mParent->mLeft = swapChild;
132 } else {
133 swap->mParent->mRight = swapChild;
135 if (swapChild) {
136 swapChild->mParent = swap->mParent;
139 // Make the selected node the new root.
140 mRoot = swap;
141 mRoot->mParent = nullptr;
142 mRoot->mLeft = last->mLeft;
143 mRoot->mRight = last->mRight;
144 if (mRoot->mLeft) {
145 mRoot->mLeft->mParent = mRoot;
147 if (mRoot->mRight) {
148 mRoot->mRight->mParent = mRoot;
151 last->mLeft = nullptr;
152 last->mRight = nullptr;
153 return last;
156 T* removeMin()
158 MOZ_ASSERT(mRoot, "No min to remove!");
160 T* min = mRoot;
161 while (min->mLeft) {
162 min = min->mLeft;
164 return remove(*min);
167 // For testing purposes only.
168 void checkCoherency()
170 checkCoherency(mRoot, nullptr);
173 private:
175 * Returns the node in this comparing equal to |aValue|, or a node just
176 * greater or just less than |aValue| if there is no such node.
178 T* lookup(const T& aValue)
180 MOZ_ASSERT(!empty());
182 T* node = mRoot;
183 T* parent;
184 do {
185 parent = node;
186 int c = Comparator::compare(aValue, *node);
187 if (c == 0) {
188 return node;
189 } else if (c < 0) {
190 node = node->mLeft;
191 } else {
192 node = node->mRight;
194 } while (node);
195 return parent;
198 void finishInsertion(T* aLast, int32_t aCmp, T* aNew)
200 MOZ_ASSERT(aCmp, "Nodes shouldn't be equal!");
202 T** parentPointer = (aCmp < 0) ? &aLast->mLeft : &aLast->mRight;
203 MOZ_ASSERT(!*parentPointer);
204 *parentPointer = aNew;
205 aNew->mParent = aLast;
207 splay(aNew);
211 * Rotate the tree until |node| is at the root of the tree. Performing
212 * the rotations in this fashion preserves the amortized balancing of
213 * the tree.
215 void splay(T* aNode)
217 MOZ_ASSERT(aNode);
219 while (aNode != mRoot) {
220 T* parent = aNode->mParent;
221 if (parent == mRoot) {
222 // Zig rotation.
223 rotate(aNode);
224 MOZ_ASSERT(aNode == mRoot);
225 return;
227 T* grandparent = parent->mParent;
228 if ((parent->mLeft == aNode) == (grandparent->mLeft == parent)) {
229 // Zig-zig rotation.
230 rotate(parent);
231 rotate(aNode);
232 } else {
233 // Zig-zag rotation.
234 rotate(aNode);
235 rotate(aNode);
240 void rotate(T* aNode)
242 // Rearrange nodes so that aNode becomes the parent of its current
243 // parent, while preserving the sortedness of the tree.
244 T* parent = aNode->mParent;
245 if (parent->mLeft == aNode) {
246 // x y
247 // y c ==> a x
248 // a b b c
249 parent->mLeft = aNode->mRight;
250 if (aNode->mRight) {
251 aNode->mRight->mParent = parent;
253 aNode->mRight = parent;
254 } else {
255 MOZ_ASSERT(parent->mRight == aNode);
256 // x y
257 // a y ==> x c
258 // b c a b
259 parent->mRight = aNode->mLeft;
260 if (aNode->mLeft) {
261 aNode->mLeft->mParent = parent;
263 aNode->mLeft = parent;
265 aNode->mParent = parent->mParent;
266 parent->mParent = aNode;
267 if (T* grandparent = aNode->mParent) {
268 if (grandparent->mLeft == parent) {
269 grandparent->mLeft = aNode;
270 } else {
271 grandparent->mRight = aNode;
273 } else {
274 mRoot = aNode;
278 T* checkCoherency(T* aNode, T* aMinimum)
280 if (mRoot) {
281 MOZ_RELEASE_ASSERT(!mRoot->mParent);
283 if (!aNode) {
284 MOZ_RELEASE_ASSERT(!mRoot);
285 return nullptr;
287 if (!aNode->mParent) {
288 MOZ_RELEASE_ASSERT(aNode == mRoot);
290 if (aMinimum) {
291 MOZ_RELEASE_ASSERT(Comparator::compare(*aMinimum, *aNode) < 0);
293 if (aNode->mLeft) {
294 MOZ_RELEASE_ASSERT(aNode->mLeft->mParent == aNode);
295 T* leftMaximum = checkCoherency(aNode->mLeft, aMinimum);
296 MOZ_RELEASE_ASSERT(Comparator::compare(*leftMaximum, *aNode) < 0);
298 if (aNode->mRight) {
299 MOZ_RELEASE_ASSERT(aNode->mRight->mParent == aNode);
300 return checkCoherency(aNode->mRight, aNode);
302 return aNode;
305 SplayTree(const SplayTree&) = delete;
306 void operator=(const SplayTree&) = delete;
309 template<typename T, class Comparator>
311 SplayTree<T, Comparator>::findOrInsert(const T& aValue)
313 if (!mRoot) {
314 mRoot = new T(aValue);
315 return mRoot;
318 T* last = lookup(aValue);
319 int cmp = Comparator::compare(aValue, *last);
320 if (!cmp) {
321 return last;
324 T* t = new T(aValue);
325 finishInsertion(last, cmp, t);
326 return t;
329 } /* namespace mozilla */
331 #endif /* mozilla_SplayTree_h */