2013-07-24 07:41:39 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-01-15 23:12:05 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A sorted tree with optimal access times, where recently-accessed elements
|
|
|
|
* are faster to access again.
|
|
|
|
*/
|
|
|
|
|
2013-07-24 07:41:39 +00:00
|
|
|
#ifndef mozilla_SplayTree_h
|
|
|
|
#define mozilla_SplayTree_h
|
2013-01-15 23:12:05 +00:00
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
2015-04-28 19:55:29 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-01-15 23:12:05 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template<class T, class C>
|
|
|
|
class SplayTree;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class SplayTreeNode
|
|
|
|
{
|
2014-03-14 05:35:45 +00:00
|
|
|
public:
|
|
|
|
template<class A, class B>
|
|
|
|
friend class SplayTree;
|
|
|
|
|
|
|
|
SplayTreeNode()
|
|
|
|
: mLeft(nullptr)
|
|
|
|
, mRight(nullptr)
|
|
|
|
, mParent(nullptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* mLeft;
|
|
|
|
T* mRight;
|
|
|
|
T* mParent;
|
2013-01-15 23:12:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class which represents a splay tree.
|
|
|
|
* Splay trees are balanced binary search trees for which search, insert and
|
|
|
|
* remove are all amortized O(log n), but where accessing a node makes it
|
|
|
|
* faster to access that node in the future.
|
|
|
|
*
|
|
|
|
* T indicates the type of tree elements, Comparator must have a static
|
|
|
|
* compare(const T&, const T&) method ordering the elements. The compare
|
|
|
|
* method must be free from side effects.
|
|
|
|
*/
|
|
|
|
template<typename T, class Comparator>
|
|
|
|
class SplayTree
|
|
|
|
{
|
2014-03-14 05:35:45 +00:00
|
|
|
T* mRoot;
|
2013-01-15 23:12:05 +00:00
|
|
|
|
2014-03-14 05:35:45 +00:00
|
|
|
public:
|
2016-07-08 21:39:53 +00:00
|
|
|
constexpr SplayTree()
|
2014-03-14 05:35:45 +00:00
|
|
|
: mRoot(nullptr)
|
|
|
|
{}
|
2013-01-15 23:12:05 +00:00
|
|
|
|
2014-03-14 05:35:45 +00:00
|
|
|
bool empty() const
|
|
|
|
{
|
|
|
|
return !mRoot;
|
|
|
|
}
|
2013-01-15 23:12:05 +00:00
|
|
|
|
2014-03-14 05:35:45 +00:00
|
|
|
T* find(const T& aValue)
|
|
|
|
{
|
|
|
|
if (empty()) {
|
|
|
|
return nullptr;
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
|
|
|
|
2014-03-14 05:35:45 +00:00
|
|
|
T* last = lookup(aValue);
|
|
|
|
splay(last);
|
|
|
|
return Comparator::compare(aValue, *last) == 0 ? last : nullptr;
|
|
|
|
}
|
2013-01-15 23:12:05 +00:00
|
|
|
|
2016-04-29 03:14:34 +00:00
|
|
|
void insert(T* aValue)
|
2014-03-14 05:35:45 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!find(*aValue), "Duplicate elements are not allowed.");
|
2013-01-15 23:12:05 +00:00
|
|
|
|
2014-03-14 05:35:45 +00:00
|
|
|
if (!mRoot) {
|
|
|
|
mRoot = aValue;
|
2016-04-29 03:14:34 +00:00
|
|
|
return;
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
2014-03-14 05:35:45 +00:00
|
|
|
T* last = lookup(*aValue);
|
|
|
|
int cmp = Comparator::compare(*aValue, *last);
|
|
|
|
|
2015-04-28 19:55:29 +00:00
|
|
|
finishInsertion(last, cmp, aValue);
|
2016-04-29 03:14:34 +00:00
|
|
|
return;
|
2014-03-14 05:35:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 19:55:29 +00:00
|
|
|
T* findOrInsert(const T& aValue);
|
|
|
|
|
2014-03-14 05:35:45 +00:00
|
|
|
T* remove(const T& aValue)
|
|
|
|
{
|
|
|
|
T* last = lookup(aValue);
|
|
|
|
MOZ_ASSERT(last, "This tree must contain the element being removed.");
|
|
|
|
MOZ_ASSERT(Comparator::compare(aValue, *last) == 0);
|
|
|
|
|
|
|
|
// Splay the tree so that the item to remove is the root.
|
|
|
|
splay(last);
|
|
|
|
MOZ_ASSERT(last == mRoot);
|
|
|
|
|
|
|
|
// Find another node which can be swapped in for the root: either the
|
|
|
|
// rightmost child of the root's left, or the leftmost child of the
|
|
|
|
// root's right.
|
|
|
|
T* swap;
|
|
|
|
T* swapChild;
|
|
|
|
if (mRoot->mLeft) {
|
|
|
|
swap = mRoot->mLeft;
|
|
|
|
while (swap->mRight) {
|
|
|
|
swap = swap->mRight;
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
2014-03-14 05:35:45 +00:00
|
|
|
swapChild = swap->mLeft;
|
|
|
|
} else if (mRoot->mRight) {
|
|
|
|
swap = mRoot->mRight;
|
|
|
|
while (swap->mLeft) {
|
|
|
|
swap = swap->mLeft;
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
2014-03-14 05:35:45 +00:00
|
|
|
swapChild = swap->mRight;
|
|
|
|
} else {
|
|
|
|
T* result = mRoot;
|
|
|
|
mRoot = nullptr;
|
|
|
|
return result;
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
|
|
|
|
2014-03-14 05:35:45 +00:00
|
|
|
// The selected node has at most one child, in swapChild. Detach it
|
|
|
|
// from the subtree by replacing it with that child.
|
|
|
|
if (swap == swap->mParent->mLeft) {
|
|
|
|
swap->mParent->mLeft = swapChild;
|
|
|
|
} else {
|
|
|
|
swap->mParent->mRight = swapChild;
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
2014-03-14 05:35:45 +00:00
|
|
|
if (swapChild) {
|
|
|
|
swapChild->mParent = swap->mParent;
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
|
|
|
|
2014-03-14 05:35:45 +00:00
|
|
|
// Make the selected node the new root.
|
|
|
|
mRoot = swap;
|
|
|
|
mRoot->mParent = nullptr;
|
|
|
|
mRoot->mLeft = last->mLeft;
|
|
|
|
mRoot->mRight = last->mRight;
|
|
|
|
if (mRoot->mLeft) {
|
|
|
|
mRoot->mLeft->mParent = mRoot;
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
2014-03-14 05:35:45 +00:00
|
|
|
if (mRoot->mRight) {
|
|
|
|
mRoot->mRight->mParent = mRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
return last;
|
|
|
|
}
|
2013-01-15 23:12:05 +00:00
|
|
|
|
2014-03-14 05:35:45 +00:00
|
|
|
T* removeMin()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mRoot, "No min to remove!");
|
|
|
|
|
|
|
|
T* min = mRoot;
|
|
|
|
while (min->mLeft) {
|
|
|
|
min = min->mLeft;
|
|
|
|
}
|
|
|
|
return remove(*min);
|
|
|
|
}
|
|
|
|
|
2014-07-30 22:53:40 +00:00
|
|
|
// For testing purposes only.
|
|
|
|
void checkCoherency()
|
|
|
|
{
|
|
|
|
checkCoherency(mRoot, nullptr);
|
|
|
|
}
|
|
|
|
|
2014-03-14 05:35:45 +00:00
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Returns the node in this comparing equal to |aValue|, or a node just
|
|
|
|
* greater or just less than |aValue| if there is no such node.
|
|
|
|
*/
|
|
|
|
T* lookup(const T& aValue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
|
|
|
|
T* node = mRoot;
|
|
|
|
T* parent;
|
|
|
|
do {
|
|
|
|
parent = node;
|
|
|
|
int c = Comparator::compare(aValue, *node);
|
|
|
|
if (c == 0) {
|
|
|
|
return node;
|
|
|
|
} else if (c < 0) {
|
|
|
|
node = node->mLeft;
|
2013-01-15 23:12:05 +00:00
|
|
|
} else {
|
2014-03-14 05:35:45 +00:00
|
|
|
node = node->mRight;
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
2014-03-14 05:35:45 +00:00
|
|
|
} while (node);
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:14:34 +00:00
|
|
|
void finishInsertion(T* aLast, int32_t aCmp, T* aNew)
|
2015-04-28 19:55:29 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCmp, "Nodes shouldn't be equal!");
|
|
|
|
|
|
|
|
T** parentPointer = (aCmp < 0) ? &aLast->mLeft : &aLast->mRight;
|
|
|
|
MOZ_ASSERT(!*parentPointer);
|
|
|
|
*parentPointer = aNew;
|
|
|
|
aNew->mParent = aLast;
|
|
|
|
|
|
|
|
splay(aNew);
|
|
|
|
}
|
|
|
|
|
2014-03-14 05:35:45 +00:00
|
|
|
/**
|
|
|
|
* Rotate the tree until |node| is at the root of the tree. Performing
|
|
|
|
* the rotations in this fashion preserves the amortized balancing of
|
|
|
|
* the tree.
|
|
|
|
*/
|
|
|
|
void splay(T* aNode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
|
|
|
|
while (aNode != mRoot) {
|
|
|
|
T* parent = aNode->mParent;
|
|
|
|
if (parent == mRoot) {
|
|
|
|
// Zig rotation.
|
|
|
|
rotate(aNode);
|
|
|
|
MOZ_ASSERT(aNode == mRoot);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
T* grandparent = parent->mParent;
|
|
|
|
if ((parent->mLeft == aNode) == (grandparent->mLeft == parent)) {
|
|
|
|
// Zig-zig rotation.
|
|
|
|
rotate(parent);
|
|
|
|
rotate(aNode);
|
2013-01-15 23:12:05 +00:00
|
|
|
} else {
|
2014-03-14 05:35:45 +00:00
|
|
|
// Zig-zag rotation.
|
|
|
|
rotate(aNode);
|
|
|
|
rotate(aNode);
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-14 05:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rotate(T* aNode)
|
|
|
|
{
|
|
|
|
// Rearrange nodes so that aNode becomes the parent of its current
|
|
|
|
// parent, while preserving the sortedness of the tree.
|
|
|
|
T* parent = aNode->mParent;
|
|
|
|
if (parent->mLeft == aNode) {
|
|
|
|
// x y
|
|
|
|
// y c ==> a x
|
|
|
|
// a b b c
|
|
|
|
parent->mLeft = aNode->mRight;
|
|
|
|
if (aNode->mRight) {
|
|
|
|
aNode->mRight->mParent = parent;
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
2014-03-14 05:35:45 +00:00
|
|
|
aNode->mRight = parent;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(parent->mRight == aNode);
|
|
|
|
// x y
|
|
|
|
// a y ==> x c
|
|
|
|
// b c a b
|
|
|
|
parent->mRight = aNode->mLeft;
|
|
|
|
if (aNode->mLeft) {
|
|
|
|
aNode->mLeft->mParent = parent;
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
2014-03-14 05:35:45 +00:00
|
|
|
aNode->mLeft = parent;
|
|
|
|
}
|
|
|
|
aNode->mParent = parent->mParent;
|
|
|
|
parent->mParent = aNode;
|
|
|
|
if (T* grandparent = aNode->mParent) {
|
|
|
|
if (grandparent->mLeft == parent) {
|
|
|
|
grandparent->mLeft = aNode;
|
|
|
|
} else {
|
|
|
|
grandparent->mRight = aNode;
|
2013-01-15 23:12:05 +00:00
|
|
|
}
|
2014-03-14 05:35:45 +00:00
|
|
|
} else {
|
|
|
|
mRoot = aNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T* checkCoherency(T* aNode, T* aMinimum)
|
|
|
|
{
|
2014-07-30 23:21:06 +00:00
|
|
|
if (mRoot) {
|
|
|
|
MOZ_RELEASE_ASSERT(!mRoot->mParent);
|
|
|
|
}
|
2014-03-14 05:35:45 +00:00
|
|
|
if (!aNode) {
|
2014-07-30 23:21:06 +00:00
|
|
|
MOZ_RELEASE_ASSERT(!mRoot);
|
2013-01-15 23:12:05 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-30 23:21:06 +00:00
|
|
|
if (!aNode->mParent) {
|
|
|
|
MOZ_RELEASE_ASSERT(aNode == mRoot);
|
|
|
|
}
|
|
|
|
if (aMinimum) {
|
|
|
|
MOZ_RELEASE_ASSERT(Comparator::compare(*aMinimum, *aNode) < 0);
|
|
|
|
}
|
2014-03-14 05:35:45 +00:00
|
|
|
if (aNode->mLeft) {
|
2014-07-30 23:21:06 +00:00
|
|
|
MOZ_RELEASE_ASSERT(aNode->mLeft->mParent == aNode);
|
2014-03-14 05:35:45 +00:00
|
|
|
T* leftMaximum = checkCoherency(aNode->mLeft, aMinimum);
|
2014-07-30 23:21:06 +00:00
|
|
|
MOZ_RELEASE_ASSERT(Comparator::compare(*leftMaximum, *aNode) < 0);
|
2014-03-14 05:35:45 +00:00
|
|
|
}
|
|
|
|
if (aNode->mRight) {
|
2014-07-30 23:21:06 +00:00
|
|
|
MOZ_RELEASE_ASSERT(aNode->mRight->mParent == aNode);
|
2014-03-14 05:35:45 +00:00
|
|
|
return checkCoherency(aNode->mRight, aNode);
|
|
|
|
}
|
|
|
|
return aNode;
|
|
|
|
}
|
2013-01-15 23:12:05 +00:00
|
|
|
|
2015-01-06 23:35:02 +00:00
|
|
|
SplayTree(const SplayTree&) = delete;
|
|
|
|
void operator=(const SplayTree&) = delete;
|
2013-01-15 23:12:05 +00:00
|
|
|
};
|
|
|
|
|
2015-04-28 19:55:29 +00:00
|
|
|
template<typename T, class Comparator>
|
|
|
|
T*
|
|
|
|
SplayTree<T, Comparator>::findOrInsert(const T& aValue)
|
|
|
|
{
|
|
|
|
if (!mRoot) {
|
|
|
|
mRoot = new T(aValue);
|
|
|
|
return mRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* last = lookup(aValue);
|
|
|
|
int cmp = Comparator::compare(aValue, *last);
|
|
|
|
if (!cmp) {
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:14:34 +00:00
|
|
|
T* t = new T(aValue);
|
|
|
|
finishInsertion(last, cmp, t);
|
|
|
|
return t;
|
2015-04-28 19:55:29 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 23:12:05 +00:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
2013-07-24 07:41:39 +00:00
|
|
|
#endif /* mozilla_SplayTree_h */
|