Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | * A sorted tree with optimal access times, where recently-accessed elements |
michael@0 | 9 | * are faster to access again. |
michael@0 | 10 | */ |
michael@0 | 11 | |
michael@0 | 12 | #ifndef mozilla_SplayTree_h |
michael@0 | 13 | #define mozilla_SplayTree_h |
michael@0 | 14 | |
michael@0 | 15 | #include "mozilla/Assertions.h" |
michael@0 | 16 | #include "mozilla/NullPtr.h" |
michael@0 | 17 | |
michael@0 | 18 | namespace mozilla { |
michael@0 | 19 | |
michael@0 | 20 | template<class T, class C> |
michael@0 | 21 | class SplayTree; |
michael@0 | 22 | |
michael@0 | 23 | template<typename T> |
michael@0 | 24 | class SplayTreeNode |
michael@0 | 25 | { |
michael@0 | 26 | public: |
michael@0 | 27 | template<class A, class B> |
michael@0 | 28 | friend class SplayTree; |
michael@0 | 29 | |
michael@0 | 30 | SplayTreeNode() |
michael@0 | 31 | : left(nullptr), right(nullptr), parent(nullptr) |
michael@0 | 32 | {} |
michael@0 | 33 | |
michael@0 | 34 | private: |
michael@0 | 35 | T* left; |
michael@0 | 36 | T* right; |
michael@0 | 37 | T* parent; |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | |
michael@0 | 41 | /** |
michael@0 | 42 | * Class which represents a splay tree. |
michael@0 | 43 | * Splay trees are balanced binary search trees for which search, insert and |
michael@0 | 44 | * remove are all amortized O(log n), but where accessing a node makes it |
michael@0 | 45 | * faster to access that node in the future. |
michael@0 | 46 | * |
michael@0 | 47 | * T indicates the type of tree elements, Comparator must have a static |
michael@0 | 48 | * compare(const T&, const T&) method ordering the elements. The compare |
michael@0 | 49 | * method must be free from side effects. |
michael@0 | 50 | */ |
michael@0 | 51 | template<typename T, class Comparator> |
michael@0 | 52 | class SplayTree |
michael@0 | 53 | { |
michael@0 | 54 | T* root; |
michael@0 | 55 | |
michael@0 | 56 | public: |
michael@0 | 57 | SplayTree() |
michael@0 | 58 | : root(nullptr) |
michael@0 | 59 | {} |
michael@0 | 60 | |
michael@0 | 61 | bool empty() const { |
michael@0 | 62 | return !root; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | T* find(const T& v) |
michael@0 | 66 | { |
michael@0 | 67 | if (empty()) |
michael@0 | 68 | return nullptr; |
michael@0 | 69 | |
michael@0 | 70 | T* last = lookup(v); |
michael@0 | 71 | splay(last); |
michael@0 | 72 | checkCoherency(root, nullptr); |
michael@0 | 73 | return Comparator::compare(v, *last) == 0 ? last : nullptr; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | bool insert(T* v) |
michael@0 | 77 | { |
michael@0 | 78 | MOZ_ASSERT(!find(*v), "Duplicate elements are not allowed."); |
michael@0 | 79 | |
michael@0 | 80 | if (!root) { |
michael@0 | 81 | root = v; |
michael@0 | 82 | return true; |
michael@0 | 83 | } |
michael@0 | 84 | T* last = lookup(*v); |
michael@0 | 85 | int cmp = Comparator::compare(*v, *last); |
michael@0 | 86 | |
michael@0 | 87 | T** parentPointer = (cmp < 0) ? &last->left : &last->right; |
michael@0 | 88 | MOZ_ASSERT(!*parentPointer); |
michael@0 | 89 | *parentPointer = v; |
michael@0 | 90 | v->parent = last; |
michael@0 | 91 | |
michael@0 | 92 | splay(v); |
michael@0 | 93 | checkCoherency(root, nullptr); |
michael@0 | 94 | return true; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | T* remove(const T& v) |
michael@0 | 98 | { |
michael@0 | 99 | T* last = lookup(v); |
michael@0 | 100 | MOZ_ASSERT(last, "This tree must contain the element being removed."); |
michael@0 | 101 | MOZ_ASSERT(Comparator::compare(v, *last) == 0); |
michael@0 | 102 | |
michael@0 | 103 | // Splay the tree so that the item to remove is the root. |
michael@0 | 104 | splay(last); |
michael@0 | 105 | MOZ_ASSERT(last == root); |
michael@0 | 106 | |
michael@0 | 107 | // Find another node which can be swapped in for the root: either the |
michael@0 | 108 | // rightmost child of the root's left, or the leftmost child of the |
michael@0 | 109 | // root's right. |
michael@0 | 110 | T* swap; |
michael@0 | 111 | T* swapChild; |
michael@0 | 112 | if (root->left) { |
michael@0 | 113 | swap = root->left; |
michael@0 | 114 | while (swap->right) |
michael@0 | 115 | swap = swap->right; |
michael@0 | 116 | swapChild = swap->left; |
michael@0 | 117 | } else if (root->right) { |
michael@0 | 118 | swap = root->right; |
michael@0 | 119 | while (swap->left) |
michael@0 | 120 | swap = swap->left; |
michael@0 | 121 | swapChild = swap->right; |
michael@0 | 122 | } else { |
michael@0 | 123 | T* result = root; |
michael@0 | 124 | root = nullptr; |
michael@0 | 125 | return result; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | // The selected node has at most one child, in swapChild. Detach it |
michael@0 | 129 | // from the subtree by replacing it with that child. |
michael@0 | 130 | if (swap == swap->parent->left) |
michael@0 | 131 | swap->parent->left = swapChild; |
michael@0 | 132 | else |
michael@0 | 133 | swap->parent->right = swapChild; |
michael@0 | 134 | if (swapChild) |
michael@0 | 135 | swapChild->parent = swap->parent; |
michael@0 | 136 | |
michael@0 | 137 | // Make the selected node the new root. |
michael@0 | 138 | root = swap; |
michael@0 | 139 | root->parent = nullptr; |
michael@0 | 140 | root->left = last->left; |
michael@0 | 141 | root->right = last->right; |
michael@0 | 142 | if (root->left) { |
michael@0 | 143 | root->left->parent = root; |
michael@0 | 144 | } |
michael@0 | 145 | if (root->right) { |
michael@0 | 146 | root->right->parent = root; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | checkCoherency(root, nullptr); |
michael@0 | 150 | return last; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | T* removeMin() |
michael@0 | 154 | { |
michael@0 | 155 | MOZ_ASSERT(root, "No min to remove!"); |
michael@0 | 156 | |
michael@0 | 157 | T* min = root; |
michael@0 | 158 | while (min->left) |
michael@0 | 159 | min = min->left; |
michael@0 | 160 | return remove(*min); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | private: |
michael@0 | 164 | /** |
michael@0 | 165 | * Returns the node in this comparing equal to |v|, or a node just greater or |
michael@0 | 166 | * just less than |v| if there is no such node. |
michael@0 | 167 | */ |
michael@0 | 168 | T* lookup(const T& v) |
michael@0 | 169 | { |
michael@0 | 170 | MOZ_ASSERT(!empty()); |
michael@0 | 171 | |
michael@0 | 172 | T* node = root; |
michael@0 | 173 | T* parent; |
michael@0 | 174 | do { |
michael@0 | 175 | parent = node; |
michael@0 | 176 | int c = Comparator::compare(v, *node); |
michael@0 | 177 | if (c == 0) |
michael@0 | 178 | return node; |
michael@0 | 179 | else if (c < 0) |
michael@0 | 180 | node = node->left; |
michael@0 | 181 | else |
michael@0 | 182 | node = node->right; |
michael@0 | 183 | } while (node); |
michael@0 | 184 | return parent; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | /** |
michael@0 | 188 | * Rotate the tree until |node| is at the root of the tree. Performing |
michael@0 | 189 | * the rotations in this fashion preserves the amortized balancing of |
michael@0 | 190 | * the tree. |
michael@0 | 191 | */ |
michael@0 | 192 | void splay(T* node) |
michael@0 | 193 | { |
michael@0 | 194 | MOZ_ASSERT(node); |
michael@0 | 195 | |
michael@0 | 196 | while (node != root) { |
michael@0 | 197 | T* parent = node->parent; |
michael@0 | 198 | if (parent == root) { |
michael@0 | 199 | // Zig rotation. |
michael@0 | 200 | rotate(node); |
michael@0 | 201 | MOZ_ASSERT(node == root); |
michael@0 | 202 | return; |
michael@0 | 203 | } |
michael@0 | 204 | T* grandparent = parent->parent; |
michael@0 | 205 | if ((parent->left == node) == (grandparent->left == parent)) { |
michael@0 | 206 | // Zig-zig rotation. |
michael@0 | 207 | rotate(parent); |
michael@0 | 208 | rotate(node); |
michael@0 | 209 | } else { |
michael@0 | 210 | // Zig-zag rotation. |
michael@0 | 211 | rotate(node); |
michael@0 | 212 | rotate(node); |
michael@0 | 213 | } |
michael@0 | 214 | } |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | void rotate(T* node) |
michael@0 | 218 | { |
michael@0 | 219 | // Rearrange nodes so that node becomes the parent of its current |
michael@0 | 220 | // parent, while preserving the sortedness of the tree. |
michael@0 | 221 | T* parent = node->parent; |
michael@0 | 222 | if (parent->left == node) { |
michael@0 | 223 | // x y |
michael@0 | 224 | // y c ==> a x |
michael@0 | 225 | // a b b c |
michael@0 | 226 | parent->left = node->right; |
michael@0 | 227 | if (node->right) |
michael@0 | 228 | node->right->parent = parent; |
michael@0 | 229 | node->right = parent; |
michael@0 | 230 | } else { |
michael@0 | 231 | MOZ_ASSERT(parent->right == node); |
michael@0 | 232 | // x y |
michael@0 | 233 | // a y ==> x c |
michael@0 | 234 | // b c a b |
michael@0 | 235 | parent->right = node->left; |
michael@0 | 236 | if (node->left) |
michael@0 | 237 | node->left->parent = parent; |
michael@0 | 238 | node->left = parent; |
michael@0 | 239 | } |
michael@0 | 240 | node->parent = parent->parent; |
michael@0 | 241 | parent->parent = node; |
michael@0 | 242 | if (T* grandparent = node->parent) { |
michael@0 | 243 | if (grandparent->left == parent) |
michael@0 | 244 | grandparent->left = node; |
michael@0 | 245 | else |
michael@0 | 246 | grandparent->right = node; |
michael@0 | 247 | } else { |
michael@0 | 248 | root = node; |
michael@0 | 249 | } |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | T* checkCoherency(T* node, T* minimum) |
michael@0 | 253 | { |
michael@0 | 254 | #ifdef DEBUG |
michael@0 | 255 | MOZ_ASSERT_IF(root, !root->parent); |
michael@0 | 256 | if (!node) { |
michael@0 | 257 | MOZ_ASSERT(!root); |
michael@0 | 258 | return nullptr; |
michael@0 | 259 | } |
michael@0 | 260 | MOZ_ASSERT_IF(!node->parent, node == root); |
michael@0 | 261 | MOZ_ASSERT_IF(minimum, Comparator::compare(*minimum, *node) < 0); |
michael@0 | 262 | if (node->left) { |
michael@0 | 263 | MOZ_ASSERT(node->left->parent == node); |
michael@0 | 264 | T* leftMaximum = checkCoherency(node->left, minimum); |
michael@0 | 265 | MOZ_ASSERT(Comparator::compare(*leftMaximum, *node) < 0); |
michael@0 | 266 | } |
michael@0 | 267 | if (node->right) { |
michael@0 | 268 | MOZ_ASSERT(node->right->parent == node); |
michael@0 | 269 | return checkCoherency(node->right, node); |
michael@0 | 270 | } |
michael@0 | 271 | return node; |
michael@0 | 272 | #else |
michael@0 | 273 | return nullptr; |
michael@0 | 274 | #endif |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | SplayTree(const SplayTree&) MOZ_DELETE; |
michael@0 | 278 | void operator=(const SplayTree&) MOZ_DELETE; |
michael@0 | 279 | }; |
michael@0 | 280 | |
michael@0 | 281 | } /* namespace mozilla */ |
michael@0 | 282 | |
michael@0 | 283 | #endif /* mozilla_SplayTree_h */ |