Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
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 | #ifndef ds_SplayTree_h |
michael@0 | 8 | #define ds_SplayTree_h |
michael@0 | 9 | |
michael@0 | 10 | #include "ds/LifoAlloc.h" |
michael@0 | 11 | |
michael@0 | 12 | namespace js { |
michael@0 | 13 | |
michael@0 | 14 | /* |
michael@0 | 15 | * Class which represents a splay tree with nodes allocated from a LifoAlloc. |
michael@0 | 16 | * Splay trees are balanced binary search trees for which search, insert and |
michael@0 | 17 | * remove are all amortized O(log n). |
michael@0 | 18 | * |
michael@0 | 19 | * T indicates the type of tree elements, C must have a static |
michael@0 | 20 | * compare(const T&, const T&) method ordering the elements. As for LifoAlloc |
michael@0 | 21 | * objects, T objects stored in the tree will not be explicitly destroyed. |
michael@0 | 22 | */ |
michael@0 | 23 | template <class T, class C> |
michael@0 | 24 | class SplayTree |
michael@0 | 25 | { |
michael@0 | 26 | struct Node { |
michael@0 | 27 | T item; |
michael@0 | 28 | Node *left, *right, *parent; |
michael@0 | 29 | |
michael@0 | 30 | Node(const T &item) |
michael@0 | 31 | : item(item), left(nullptr), right(nullptr), parent(nullptr) |
michael@0 | 32 | {} |
michael@0 | 33 | }; |
michael@0 | 34 | |
michael@0 | 35 | LifoAlloc *alloc; |
michael@0 | 36 | Node *root, *freeList; |
michael@0 | 37 | |
michael@0 | 38 | SplayTree(const SplayTree &) MOZ_DELETE; |
michael@0 | 39 | SplayTree &operator=(const SplayTree &) MOZ_DELETE; |
michael@0 | 40 | |
michael@0 | 41 | public: |
michael@0 | 42 | |
michael@0 | 43 | SplayTree(LifoAlloc *alloc = nullptr) |
michael@0 | 44 | : alloc(alloc), root(nullptr), freeList(nullptr) |
michael@0 | 45 | {} |
michael@0 | 46 | |
michael@0 | 47 | void setAllocator(LifoAlloc *alloc) { |
michael@0 | 48 | this->alloc = alloc; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | bool empty() const { |
michael@0 | 52 | return !root; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | bool contains(const T &v, T *res) |
michael@0 | 56 | { |
michael@0 | 57 | if (!root) |
michael@0 | 58 | return false; |
michael@0 | 59 | Node *last = lookup(v); |
michael@0 | 60 | splay(last); |
michael@0 | 61 | checkCoherency(root, nullptr); |
michael@0 | 62 | if (C::compare(v, last->item) == 0) { |
michael@0 | 63 | *res = last->item; |
michael@0 | 64 | return true; |
michael@0 | 65 | } |
michael@0 | 66 | return false; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | bool insert(const T &v) |
michael@0 | 70 | { |
michael@0 | 71 | Node *element = allocateNode(v); |
michael@0 | 72 | if (!element) |
michael@0 | 73 | return false; |
michael@0 | 74 | |
michael@0 | 75 | if (!root) { |
michael@0 | 76 | root = element; |
michael@0 | 77 | return true; |
michael@0 | 78 | } |
michael@0 | 79 | Node *last = lookup(v); |
michael@0 | 80 | int cmp = C::compare(v, last->item); |
michael@0 | 81 | |
michael@0 | 82 | // Don't tolerate duplicate elements. |
michael@0 | 83 | JS_ASSERT(cmp); |
michael@0 | 84 | |
michael@0 | 85 | Node *&parentPointer = (cmp < 0) ? last->left : last->right; |
michael@0 | 86 | JS_ASSERT(!parentPointer); |
michael@0 | 87 | parentPointer = element; |
michael@0 | 88 | element->parent = last; |
michael@0 | 89 | |
michael@0 | 90 | splay(element); |
michael@0 | 91 | checkCoherency(root, nullptr); |
michael@0 | 92 | return true; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | void remove(const T &v) |
michael@0 | 96 | { |
michael@0 | 97 | Node *last = lookup(v); |
michael@0 | 98 | JS_ASSERT(last && C::compare(v, last->item) == 0); |
michael@0 | 99 | |
michael@0 | 100 | splay(last); |
michael@0 | 101 | JS_ASSERT(last == root); |
michael@0 | 102 | |
michael@0 | 103 | // Find another node which can be swapped in for the root: either the |
michael@0 | 104 | // rightmost child of the root's left, or the leftmost child of the |
michael@0 | 105 | // root's right. |
michael@0 | 106 | Node *swap, *swapChild; |
michael@0 | 107 | if (root->left) { |
michael@0 | 108 | swap = root->left; |
michael@0 | 109 | while (swap->right) |
michael@0 | 110 | swap = swap->right; |
michael@0 | 111 | swapChild = swap->left; |
michael@0 | 112 | } else if (root->right) { |
michael@0 | 113 | swap = root->right; |
michael@0 | 114 | while (swap->left) |
michael@0 | 115 | swap = swap->left; |
michael@0 | 116 | swapChild = swap->right; |
michael@0 | 117 | } else { |
michael@0 | 118 | freeNode(root); |
michael@0 | 119 | root = nullptr; |
michael@0 | 120 | return; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | // The selected node has at most one child, in swapChild. Detach it |
michael@0 | 124 | // from the subtree by replacing it with that child. |
michael@0 | 125 | if (swap == swap->parent->left) |
michael@0 | 126 | swap->parent->left = swapChild; |
michael@0 | 127 | else |
michael@0 | 128 | swap->parent->right = swapChild; |
michael@0 | 129 | if (swapChild) |
michael@0 | 130 | swapChild->parent = swap->parent; |
michael@0 | 131 | |
michael@0 | 132 | root->item = swap->item; |
michael@0 | 133 | freeNode(swap); |
michael@0 | 134 | |
michael@0 | 135 | checkCoherency(root, nullptr); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | template <class Op> |
michael@0 | 139 | void forEach(Op op) |
michael@0 | 140 | { |
michael@0 | 141 | forEachInner(op, root); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | private: |
michael@0 | 145 | |
michael@0 | 146 | Node *lookup(const T &v) |
michael@0 | 147 | { |
michael@0 | 148 | JS_ASSERT(root); |
michael@0 | 149 | Node *node = root, *parent; |
michael@0 | 150 | do { |
michael@0 | 151 | parent = node; |
michael@0 | 152 | int c = C::compare(v, node->item); |
michael@0 | 153 | if (c == 0) |
michael@0 | 154 | return node; |
michael@0 | 155 | else if (c < 0) |
michael@0 | 156 | node = node->left; |
michael@0 | 157 | else |
michael@0 | 158 | node = node->right; |
michael@0 | 159 | } while (node); |
michael@0 | 160 | return parent; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | Node *allocateNode(const T &v) |
michael@0 | 164 | { |
michael@0 | 165 | Node *node = freeList; |
michael@0 | 166 | if (node) { |
michael@0 | 167 | freeList = node->left; |
michael@0 | 168 | new(node) Node(v); |
michael@0 | 169 | return node; |
michael@0 | 170 | } |
michael@0 | 171 | return alloc->new_<Node>(v); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | void freeNode(Node *node) |
michael@0 | 175 | { |
michael@0 | 176 | node->left = freeList; |
michael@0 | 177 | freeList = node; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | void splay(Node *node) |
michael@0 | 181 | { |
michael@0 | 182 | // Rotate the element until it is at the root of the tree. Performing |
michael@0 | 183 | // the rotations in this fashion preserves the amortized balancing of |
michael@0 | 184 | // the tree. |
michael@0 | 185 | JS_ASSERT(node); |
michael@0 | 186 | while (node != root) { |
michael@0 | 187 | Node *parent = node->parent; |
michael@0 | 188 | if (parent == root) { |
michael@0 | 189 | // Zig rotation. |
michael@0 | 190 | rotate(node); |
michael@0 | 191 | JS_ASSERT(node == root); |
michael@0 | 192 | return; |
michael@0 | 193 | } |
michael@0 | 194 | Node *grandparent = parent->parent; |
michael@0 | 195 | if ((parent->left == node) == (grandparent->left == parent)) { |
michael@0 | 196 | // Zig-zig rotation. |
michael@0 | 197 | rotate(parent); |
michael@0 | 198 | rotate(node); |
michael@0 | 199 | } else { |
michael@0 | 200 | // Zig-zag rotation. |
michael@0 | 201 | rotate(node); |
michael@0 | 202 | rotate(node); |
michael@0 | 203 | } |
michael@0 | 204 | } |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | void rotate(Node *node) |
michael@0 | 208 | { |
michael@0 | 209 | // Rearrange nodes so that node becomes the parent of its current |
michael@0 | 210 | // parent, while preserving the sortedness of the tree. |
michael@0 | 211 | Node *parent = node->parent; |
michael@0 | 212 | if (parent->left == node) { |
michael@0 | 213 | // x y |
michael@0 | 214 | // y c ==> a x |
michael@0 | 215 | // a b b c |
michael@0 | 216 | parent->left = node->right; |
michael@0 | 217 | if (node->right) |
michael@0 | 218 | node->right->parent = parent; |
michael@0 | 219 | node->right = parent; |
michael@0 | 220 | } else { |
michael@0 | 221 | JS_ASSERT(parent->right == node); |
michael@0 | 222 | // x y |
michael@0 | 223 | // a y ==> x c |
michael@0 | 224 | // b c a b |
michael@0 | 225 | parent->right = node->left; |
michael@0 | 226 | if (node->left) |
michael@0 | 227 | node->left->parent = parent; |
michael@0 | 228 | node->left = parent; |
michael@0 | 229 | } |
michael@0 | 230 | node->parent = parent->parent; |
michael@0 | 231 | parent->parent = node; |
michael@0 | 232 | if (Node *grandparent = node->parent) { |
michael@0 | 233 | if (grandparent->left == parent) |
michael@0 | 234 | grandparent->left = node; |
michael@0 | 235 | else |
michael@0 | 236 | grandparent->right = node; |
michael@0 | 237 | } else { |
michael@0 | 238 | root = node; |
michael@0 | 239 | } |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | template <class Op> |
michael@0 | 243 | void forEachInner(Op op, Node *node) |
michael@0 | 244 | { |
michael@0 | 245 | if (!node) |
michael@0 | 246 | return; |
michael@0 | 247 | |
michael@0 | 248 | forEachInner(op, node->left); |
michael@0 | 249 | op(node->item); |
michael@0 | 250 | forEachInner(op, node->right); |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | Node *checkCoherency(Node *node, Node *minimum) |
michael@0 | 254 | { |
michael@0 | 255 | #ifdef DEBUG |
michael@0 | 256 | if (!node) { |
michael@0 | 257 | JS_ASSERT(!root); |
michael@0 | 258 | return nullptr; |
michael@0 | 259 | } |
michael@0 | 260 | JS_ASSERT_IF(!node->parent, node == root); |
michael@0 | 261 | JS_ASSERT_IF(minimum, C::compare(minimum->item, node->item) < 0); |
michael@0 | 262 | if (node->left) { |
michael@0 | 263 | JS_ASSERT(node->left->parent == node); |
michael@0 | 264 | Node *leftMaximum = checkCoherency(node->left, minimum); |
michael@0 | 265 | JS_ASSERT(C::compare(leftMaximum->item, node->item) < 0); |
michael@0 | 266 | } |
michael@0 | 267 | if (node->right) { |
michael@0 | 268 | JS_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 | |
michael@0 | 278 | } /* namespace js */ |
michael@0 | 279 | |
michael@0 | 280 | #endif /* ds_SplayTree_h */ |