2017-09-02 03:05:13 +03: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
|
|
|
|
* 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/. */
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Portions of this file were originally under the following license:
|
|
|
|
//
|
|
|
|
// Copyright (C) 2008 Jason Evans <jasone@FreeBSD.org>.
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions
|
|
|
|
// are met:
|
|
|
|
// 1. Redistributions of source code must retain the above copyright
|
|
|
|
// notice(s), this list of conditions and the following disclaimer
|
|
|
|
// unmodified other than the allowable addition of one or more
|
|
|
|
// copyright notices.
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
// notice(s), this list of conditions and the following disclaimer in
|
|
|
|
// the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
|
|
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
|
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// ****************************************************************************
|
|
|
|
//
|
|
|
|
// C++ template implementation of left-leaning red-black trees.
|
|
|
|
//
|
|
|
|
// All operations are done non-recursively. Parent pointers are not used, and
|
|
|
|
// color bits are stored in the least significant bit of right-child pointers,
|
|
|
|
// thus making node linkage as compact as is possible for red-black trees.
|
|
|
|
//
|
|
|
|
// The RedBlackTree template expects two type arguments: the type of the nodes,
|
|
|
|
// containing a RedBlackTreeNode, and a trait providing two methods:
|
|
|
|
// - a GetTreeNode method that returns a reference to the RedBlackTreeNode
|
|
|
|
// corresponding to a given node with the following signature:
|
|
|
|
// static RedBlackTreeNode<T>& GetTreeNode(T*)
|
|
|
|
// - a Compare function with the following signature:
|
2017-11-21 03:11:54 +03:00
|
|
|
// static Order Compare(T* aNode, T* aOther)
|
|
|
|
// ^^^^^
|
|
|
|
// or aKey
|
2017-11-01 11:15:12 +03:00
|
|
|
//
|
|
|
|
// Interpretation of comparision function return values:
|
|
|
|
//
|
2017-11-21 03:11:54 +03:00
|
|
|
// Order::eLess: aNode < aOther
|
|
|
|
// Order::eEqual: aNode == aOther
|
|
|
|
// Order::eGreater: aNode > aOther
|
2017-11-01 11:15:12 +03:00
|
|
|
//
|
|
|
|
// In all cases, the aNode or aKey argument is the first argument to the
|
|
|
|
// comparison function, which makes it possible to write comparison functions
|
|
|
|
// that treat the first argument specially.
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
2008-06-20 21:34:42 +04:00
|
|
|
|
|
|
|
#ifndef RB_H_
|
2017-09-25 00:59:30 +03:00
|
|
|
#define RB_H_
|
2008-06-20 21:34:42 +04:00
|
|
|
|
2017-10-30 03:55:18 +03:00
|
|
|
#include "mozilla/Alignment.h"
|
Bug 1438427 - Fix wrong change from bug 1412722 in RedBlackTree::Remove. r=njn
Before bug 1412722, which removed the sentinels, the code looked like:
if (rbp_r_c->Right()->Left()->IsBlack()) {
At that point in the code, rbp_r_c is the root node of the tree. If
rbp_r_c->Right() was the sentinel, ->Right()->Left() would be the
sentinel too, and the sentinel is black. Which means the condition would
be true.
The code after was:
if (rbp_r_c->Right() && (!rbp_r_c->Right()->Left() ||
rbp_r_c->Right()->Left()->IsBlack())) {
The second half correctly deals with the case of
rbp_r_c->Right()->Left() being the sentinel. But the first half now
makes things different: ->Right() being null would correspond to the
previous case where it was the sentinel, and the test would not return
true in that case when it would have before. When ->Right() is not null,
things are normal again.
The correct check is to make the branch taken when ->Right() is null.
Now, looking under which conditions we may get in that branch wrongly:
- The root node's right link must be empty, which means a very small tree.
- The comparison between the removed key and the root node must indicate
the key is greater than the value of the root node.
- There's another case where the comparison result (rbp_r_cmp) can be
eGreater, when it is reassigned under one of the branches under the
eEqual test, and that branch is only taken when ->Right() on the root
node was non-null, which was the non-broken case.
So it would seem we can't reach that code when rbp_r_c->Right() is null
anyways, so it /should/ practically make no difference. Better safe than
sorry, though. It's hard to tell anything from crash stats, because
since the templatization in bug 1403444, all crashes fit in one bucket,
when there used to be 5 functions before :(
While here, add a missing include in rb.h.
--HG--
extra : rebase_source : 2ebcb84345ad52059b0c081b9e2e1af1d0bbb7bc
2018-02-15 08:38:52 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2017-10-30 05:43:10 +03:00
|
|
|
#include "Utils.h"
|
|
|
|
|
2017-09-02 14:26:09 +03:00
|
|
|
enum NodeColor {
|
|
|
|
Black = 0,
|
|
|
|
Red = 1,
|
|
|
|
};
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Node structure.
|
2017-11-01 11:20:54 +03:00
|
|
|
template <typename T>
|
2017-09-02 14:26:09 +03:00
|
|
|
class RedBlackTreeNode {
|
|
|
|
T* mLeft;
|
2017-11-01 11:15:12 +03:00
|
|
|
// The lowest bit is the color
|
2017-09-02 14:26:09 +03:00
|
|
|
T* mRightAndColor;
|
|
|
|
|
|
|
|
public:
|
2017-11-01 11:20:54 +03:00
|
|
|
T* Left() { return mLeft; }
|
2017-09-02 14:26:09 +03:00
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
void SetLeft(T* aValue) { mLeft = aValue; }
|
2017-09-02 14:26:09 +03:00
|
|
|
|
|
|
|
T* Right() {
|
2017-11-01 11:20:54 +03:00
|
|
|
return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(mRightAndColor) &
|
|
|
|
uintptr_t(~1));
|
2017-09-02 14:26:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetRight(T* aValue) {
|
|
|
|
mRightAndColor = reinterpret_cast<T*>(
|
|
|
|
(reinterpret_cast<uintptr_t>(aValue) & uintptr_t(~1)) | Color());
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeColor Color() {
|
2017-11-01 11:20:54 +03:00
|
|
|
return static_cast<NodeColor>(reinterpret_cast<uintptr_t>(mRightAndColor) &
|
|
|
|
1);
|
2017-09-02 14:26:09 +03:00
|
|
|
}
|
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
bool IsBlack() { return Color() == NodeColor::Black; }
|
2017-09-27 02:54:49 +03:00
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
bool IsRed() { return Color() == NodeColor::Red; }
|
2017-09-02 14:26:09 +03:00
|
|
|
|
|
|
|
void SetColor(NodeColor aColor) {
|
|
|
|
mRightAndColor = reinterpret_cast<T*>(
|
|
|
|
(reinterpret_cast<uintptr_t>(mRightAndColor) & uintptr_t(~1)) | aColor);
|
|
|
|
}
|
2017-09-02 03:05:13 +03:00
|
|
|
};
|
2008-06-20 21:34:42 +04:00
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Tree structure.
|
2017-09-26 09:06:00 +03:00
|
|
|
template <typename T, typename Trait>
|
2017-09-27 10:07:22 +03:00
|
|
|
class RedBlackTree {
|
|
|
|
public:
|
2018-02-20 14:39:28 +03:00
|
|
|
void Init() { mRoot = nullptr; }
|
2017-09-26 09:06:00 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
T* First(T* aStart = nullptr) { return First(TreeNode(aStart)).Get(); }
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
T* Last(T* aStart = nullptr) { return Last(TreeNode(aStart)).Get(); }
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
T* Next(T* aNode) { return Next(TreeNode(aNode)).Get(); }
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
T* Prev(T* aNode) { return Prev(TreeNode(aNode)).Get(); }
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
T* Search(T* aKey) { return Search(TreeNode(aKey)).Get(); }
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Find a match if it exists. Otherwise, find the next greater node, if one
|
|
|
|
// exists.
|
2018-02-15 14:15:00 +03:00
|
|
|
T* SearchOrNext(T* aKey) { return SearchOrNext(TreeNode(aKey)).Get(); }
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
void Insert(T* aNode) { Insert(TreeNode(aNode)); }
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
void Remove(T* aNode) { Remove(TreeNode(aNode)); }
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Helper class to avoid having all the tree traversal code further below
|
2018-02-15 14:20:11 +03:00
|
|
|
// have to use Trait::GetTreeNode and do manual null pointer checks, adding
|
|
|
|
// visual noise. Practically speaking TreeNode(nullptr) acts as a virtual
|
|
|
|
// sentinel, that loops back to itself for Left() and Right() and is always
|
|
|
|
// black.
|
2018-02-15 14:15:00 +03:00
|
|
|
class TreeNode {
|
|
|
|
public:
|
|
|
|
constexpr TreeNode() : mNode(nullptr) {}
|
|
|
|
|
|
|
|
MOZ_IMPLICIT TreeNode(T* aNode) : mNode(aNode) {}
|
2018-02-15 14:15:00 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode& operator=(TreeNode aOther) {
|
|
|
|
mNode = aOther.mNode;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-02-15 14:20:11 +03:00
|
|
|
TreeNode Left() {
|
|
|
|
return TreeNode(mNode ? Trait::GetTreeNode(mNode).Left() : nullptr);
|
|
|
|
}
|
2018-02-15 14:15:00 +03:00
|
|
|
|
|
|
|
void SetLeft(TreeNode aNode) {
|
2018-02-15 14:20:11 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mNode);
|
2018-02-15 14:15:00 +03:00
|
|
|
Trait::GetTreeNode(mNode).SetLeft(aNode.mNode);
|
|
|
|
}
|
2018-02-15 14:15:00 +03:00
|
|
|
|
2018-02-15 14:20:11 +03:00
|
|
|
TreeNode Right() {
|
|
|
|
return TreeNode(mNode ? Trait::GetTreeNode(mNode).Right() : nullptr);
|
|
|
|
}
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
void SetRight(TreeNode aNode) {
|
2018-02-15 14:20:11 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mNode);
|
2018-02-15 14:15:00 +03:00
|
|
|
Trait::GetTreeNode(mNode).SetRight(aNode.mNode);
|
|
|
|
}
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2018-02-15 14:20:11 +03:00
|
|
|
NodeColor Color() {
|
|
|
|
return mNode ? Trait::GetTreeNode(mNode).Color() : NodeColor::Black;
|
|
|
|
}
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2018-02-15 14:20:11 +03:00
|
|
|
bool IsRed() { return Color() == NodeColor::Red; }
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2018-02-15 14:20:11 +03:00
|
|
|
bool IsBlack() { return Color() == NodeColor::Black; }
|
2017-09-27 10:07:22 +03:00
|
|
|
|
|
|
|
void SetColor(NodeColor aColor) {
|
2018-02-15 14:20:11 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mNode);
|
2018-02-15 14:15:00 +03:00
|
|
|
Trait::GetTreeNode(mNode).SetColor(aColor);
|
2017-09-27 10:07:22 +03:00
|
|
|
}
|
2018-02-15 14:15:00 +03:00
|
|
|
|
|
|
|
T* Get() { return mNode; }
|
|
|
|
|
|
|
|
MOZ_IMPLICIT operator bool() { return !!mNode; }
|
|
|
|
|
|
|
|
bool operator==(TreeNode& aOther) { return mNode == aOther.mNode; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* mNode;
|
2017-09-27 10:07:22 +03:00
|
|
|
};
|
|
|
|
|
2017-10-31 01:13:39 +03:00
|
|
|
private:
|
2018-02-15 14:15:00 +03:00
|
|
|
// Ideally we'd use a TreeNode for mRoot, but we need RedBlackTree to stay
|
|
|
|
// a POD type to avoid a static initializer for gArenas.
|
|
|
|
T* mRoot;
|
2017-09-27 10:07:22 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode First(TreeNode aStart) {
|
|
|
|
TreeNode ret;
|
2018-02-15 14:25:57 +03:00
|
|
|
for (ret = aStart ? aStart : mRoot; ret.Left(); ret = ret.Left()) {
|
2017-09-26 10:59:02 +03:00
|
|
|
}
|
2017-10-28 02:36:32 +03:00
|
|
|
return ret;
|
2017-09-26 09:06:00 +03:00
|
|
|
}
|
2008-06-20 21:34:42 +04:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode Last(TreeNode aStart) {
|
|
|
|
TreeNode ret;
|
2018-02-15 14:25:57 +03:00
|
|
|
for (ret = aStart ? aStart : mRoot; ret.Right(); ret = ret.Right()) {
|
2017-09-26 10:59:02 +03:00
|
|
|
}
|
2017-10-28 02:36:32 +03:00
|
|
|
return ret;
|
2017-09-26 09:06:00 +03:00
|
|
|
}
|
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode Next(TreeNode aNode) {
|
|
|
|
TreeNode ret;
|
|
|
|
if (aNode.Right()) {
|
|
|
|
ret = First(aNode.Right());
|
2017-09-26 10:39:03 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode rbp_n_t = mRoot;
|
2017-10-28 02:36:32 +03:00
|
|
|
MOZ_ASSERT(rbp_n_t);
|
|
|
|
ret = nullptr;
|
2017-09-26 10:39:03 +03:00
|
|
|
while (true) {
|
2018-02-15 14:15:00 +03:00
|
|
|
Order rbp_n_cmp = Trait::Compare(aNode.Get(), rbp_n_t.Get());
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_n_cmp == Order::eLess) {
|
2017-09-26 10:39:03 +03:00
|
|
|
ret = rbp_n_t;
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_n_t = rbp_n_t.Left();
|
2017-11-21 03:11:54 +03:00
|
|
|
} else if (rbp_n_cmp == Order::eGreater) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_n_t = rbp_n_t.Right();
|
2017-09-26 10:39:03 +03:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2017-10-28 02:36:32 +03:00
|
|
|
MOZ_ASSERT(rbp_n_t);
|
2017-09-26 10:39:03 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-28 02:36:32 +03:00
|
|
|
return ret;
|
2017-09-26 09:06:00 +03:00
|
|
|
}
|
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode Prev(TreeNode aNode) {
|
|
|
|
TreeNode ret;
|
|
|
|
if (aNode.Left()) {
|
|
|
|
ret = Last(aNode.Left());
|
2017-09-26 10:39:03 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode rbp_p_t = mRoot;
|
2017-10-28 02:36:32 +03:00
|
|
|
MOZ_ASSERT(rbp_p_t);
|
|
|
|
ret = nullptr;
|
2017-09-26 10:39:03 +03:00
|
|
|
while (true) {
|
2018-02-15 14:15:00 +03:00
|
|
|
Order rbp_p_cmp = Trait::Compare(aNode.Get(), rbp_p_t.Get());
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_p_cmp == Order::eLess) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_p_t = rbp_p_t.Left();
|
2017-11-21 03:11:54 +03:00
|
|
|
} else if (rbp_p_cmp == Order::eGreater) {
|
2017-09-26 10:39:03 +03:00
|
|
|
ret = rbp_p_t;
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_p_t = rbp_p_t.Right();
|
2017-09-26 10:39:03 +03:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2017-10-28 02:36:32 +03:00
|
|
|
MOZ_ASSERT(rbp_p_t);
|
2017-09-26 10:39:03 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-28 02:36:32 +03:00
|
|
|
return ret;
|
2017-09-26 09:06:00 +03:00
|
|
|
}
|
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode Search(TreeNode aKey) {
|
|
|
|
TreeNode ret = mRoot;
|
2017-11-21 03:11:54 +03:00
|
|
|
Order rbp_se_cmp;
|
2018-02-15 14:15:00 +03:00
|
|
|
while (ret && (rbp_se_cmp = Trait::Compare(aKey.Get(), ret.Get())) !=
|
|
|
|
Order::eEqual) {
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_se_cmp == Order::eLess) {
|
2018-02-15 14:15:00 +03:00
|
|
|
ret = ret.Left();
|
2017-09-26 10:46:43 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
ret = ret.Right();
|
2017-09-26 10:46:43 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-28 02:36:32 +03:00
|
|
|
return ret;
|
2017-09-25 00:59:30 +03:00
|
|
|
}
|
2008-06-20 21:34:42 +04:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode SearchOrNext(TreeNode aKey) {
|
|
|
|
TreeNode ret = nullptr;
|
|
|
|
TreeNode rbp_ns_t = mRoot;
|
2017-10-28 02:36:32 +03:00
|
|
|
while (rbp_ns_t) {
|
2018-02-15 14:15:00 +03:00
|
|
|
Order rbp_ns_cmp = Trait::Compare(aKey.Get(), rbp_ns_t.Get());
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_ns_cmp == Order::eLess) {
|
2017-09-26 10:46:43 +03:00
|
|
|
ret = rbp_ns_t;
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_ns_t = rbp_ns_t.Left();
|
2017-11-21 03:11:54 +03:00
|
|
|
} else if (rbp_ns_cmp == Order::eGreater) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_ns_t = rbp_ns_t.Right();
|
2017-09-26 10:46:43 +03:00
|
|
|
} else {
|
|
|
|
ret = rbp_ns_t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 09:06:00 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
void Insert(TreeNode aNode) {
|
2017-10-30 03:55:18 +03:00
|
|
|
// rbp_i_s is only used as a placeholder for its RedBlackTreeNode. Use
|
|
|
|
// AlignedStorage2 to avoid running the TreeNode base class constructor.
|
2018-02-15 14:15:00 +03:00
|
|
|
mozilla::AlignedStorage2<T> rbp_i_s;
|
|
|
|
TreeNode rbp_i_g, rbp_i_p, rbp_i_c, rbp_i_t, rbp_i_u;
|
2017-11-21 03:11:54 +03:00
|
|
|
Order rbp_i_cmp = Order::eEqual;
|
2017-10-28 02:36:32 +03:00
|
|
|
rbp_i_g = nullptr;
|
2017-10-30 03:55:18 +03:00
|
|
|
rbp_i_p = rbp_i_s.addr();
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_i_p.SetLeft(mRoot);
|
|
|
|
rbp_i_p.SetRight(nullptr);
|
|
|
|
rbp_i_p.SetColor(NodeColor::Black);
|
2017-09-27 10:11:11 +03:00
|
|
|
rbp_i_c = mRoot;
|
2017-11-01 11:15:12 +03:00
|
|
|
// Iteratively search down the tree for the insertion point,
|
|
|
|
// splitting 4-nodes as they are encountered. At the end of each
|
|
|
|
// iteration, rbp_i_g->rbp_i_p->rbp_i_c is a 3-level path down
|
|
|
|
// the tree, assuming a sufficiently deep tree.
|
2017-10-28 02:36:32 +03:00
|
|
|
while (rbp_i_c) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_i_t = rbp_i_c.Left();
|
2018-02-15 14:25:57 +03:00
|
|
|
rbp_i_u = rbp_i_t.Left();
|
|
|
|
if (rbp_i_t.IsRed() && rbp_i_u.IsRed()) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// rbp_i_c is the top of a logical 4-node, so split it.
|
|
|
|
// This iteration does not move down the tree, due to the
|
|
|
|
// disruptiveness of node splitting.
|
|
|
|
//
|
|
|
|
// Rotate right.
|
2017-09-26 15:08:29 +03:00
|
|
|
rbp_i_t = RotateRight(rbp_i_c);
|
2017-11-01 11:15:12 +03:00
|
|
|
// Pass red links up one level.
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_i_u = rbp_i_t.Left();
|
|
|
|
rbp_i_u.SetColor(NodeColor::Black);
|
|
|
|
if (rbp_i_p.Left() == rbp_i_c) {
|
|
|
|
rbp_i_p.SetLeft(rbp_i_t);
|
2017-09-26 11:07:35 +03:00
|
|
|
rbp_i_c = rbp_i_t;
|
|
|
|
} else {
|
2017-11-01 11:15:12 +03:00
|
|
|
// rbp_i_c was the right child of rbp_i_p, so rotate
|
|
|
|
// left in order to maintain the left-leaning invariant.
|
2018-02-15 14:15:00 +03:00
|
|
|
MOZ_ASSERT(rbp_i_p.Right() == rbp_i_c);
|
|
|
|
rbp_i_p.SetRight(rbp_i_t);
|
2017-09-26 15:14:12 +03:00
|
|
|
rbp_i_u = LeanLeft(rbp_i_p);
|
2018-02-15 14:15:00 +03:00
|
|
|
if (rbp_i_g.Left() == rbp_i_p) {
|
|
|
|
rbp_i_g.SetLeft(rbp_i_u);
|
2017-09-26 11:07:35 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
MOZ_ASSERT(rbp_i_g.Right() == rbp_i_p);
|
|
|
|
rbp_i_g.SetRight(rbp_i_u);
|
2017-09-26 11:07:35 +03:00
|
|
|
}
|
|
|
|
rbp_i_p = rbp_i_u;
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_i_cmp = Trait::Compare(aNode.Get(), rbp_i_p.Get());
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_i_cmp == Order::eLess) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_i_c = rbp_i_p.Left();
|
2017-09-26 11:07:35 +03:00
|
|
|
} else {
|
2017-11-21 03:11:54 +03:00
|
|
|
MOZ_ASSERT(rbp_i_cmp == Order::eGreater);
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_i_c = rbp_i_p.Right();
|
2017-09-26 11:07:35 +03:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rbp_i_g = rbp_i_p;
|
|
|
|
rbp_i_p = rbp_i_c;
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_i_cmp = Trait::Compare(aNode.Get(), rbp_i_c.Get());
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_i_cmp == Order::eLess) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_i_c = rbp_i_c.Left();
|
2017-09-26 11:07:35 +03:00
|
|
|
} else {
|
2017-11-21 03:11:54 +03:00
|
|
|
MOZ_ASSERT(rbp_i_cmp == Order::eGreater);
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_i_c = rbp_i_c.Right();
|
2017-09-26 11:07:35 +03:00
|
|
|
}
|
|
|
|
}
|
2017-11-01 11:15:12 +03:00
|
|
|
// rbp_i_p now refers to the node under which to insert.
|
2018-02-15 14:15:00 +03:00
|
|
|
aNode.SetLeft(nullptr);
|
|
|
|
aNode.SetRight(nullptr);
|
|
|
|
aNode.SetColor(NodeColor::Red);
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_i_cmp == Order::eGreater) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_i_p.SetRight(aNode);
|
2017-09-26 15:14:12 +03:00
|
|
|
rbp_i_t = LeanLeft(rbp_i_p);
|
2018-02-15 14:15:00 +03:00
|
|
|
if (rbp_i_g.Left() == rbp_i_p) {
|
|
|
|
rbp_i_g.SetLeft(rbp_i_t);
|
|
|
|
} else if (rbp_i_g.Right() == rbp_i_p) {
|
|
|
|
rbp_i_g.SetRight(rbp_i_t);
|
2017-09-26 11:07:35 +03:00
|
|
|
}
|
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_i_p.SetLeft(aNode);
|
2017-09-26 11:07:35 +03:00
|
|
|
}
|
2017-11-01 11:15:12 +03:00
|
|
|
// Update the root and make sure that it is black.
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode root = TreeNode(rbp_i_s.addr()).Left();
|
|
|
|
root.SetColor(NodeColor::Black);
|
|
|
|
mRoot = root.Get();
|
2017-09-26 09:06:00 +03:00
|
|
|
}
|
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
void Remove(TreeNode aNode) {
|
2017-10-30 03:55:18 +03:00
|
|
|
// rbp_r_s is only used as a placeholder for its RedBlackTreeNode. Use
|
|
|
|
// AlignedStorage2 to avoid running the TreeNode base class constructor.
|
2018-02-15 14:15:00 +03:00
|
|
|
mozilla::AlignedStorage2<T> rbp_r_s;
|
|
|
|
TreeNode rbp_r_p, rbp_r_c, rbp_r_xp, rbp_r_t, rbp_r_u;
|
2017-11-21 03:11:54 +03:00
|
|
|
Order rbp_r_cmp;
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_p = TreeNode(rbp_r_s.addr());
|
|
|
|
rbp_r_p.SetLeft(mRoot);
|
|
|
|
rbp_r_p.SetRight(nullptr);
|
|
|
|
rbp_r_p.SetColor(NodeColor::Black);
|
2017-09-27 10:11:11 +03:00
|
|
|
rbp_r_c = mRoot;
|
2017-10-28 02:36:32 +03:00
|
|
|
rbp_r_xp = nullptr;
|
2017-11-01 11:15:12 +03:00
|
|
|
// Iterate down the tree, but always transform 2-nodes to 3- or
|
|
|
|
// 4-nodes in order to maintain the invariant that the current
|
|
|
|
// node is not a 2-node. This allows simple deletion once a leaf
|
|
|
|
// is reached. Handle the root specially though, since there may
|
|
|
|
// be no way to convert it from a 2-node to a 3-node.
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_cmp = Trait::Compare(aNode.Get(), rbp_r_c.Get());
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_r_cmp == Order::eLess) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_t = rbp_r_c.Left();
|
2018-02-15 14:25:57 +03:00
|
|
|
rbp_r_u = rbp_r_t.Left();
|
|
|
|
if (rbp_r_t.IsBlack() && rbp_r_u.IsBlack()) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Apply standard transform to prepare for left move.
|
2017-09-26 15:21:13 +03:00
|
|
|
rbp_r_t = MoveRedLeft(rbp_r_c);
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_t.SetColor(NodeColor::Black);
|
|
|
|
rbp_r_p.SetLeft(rbp_r_t);
|
2017-09-26 11:13:53 +03:00
|
|
|
rbp_r_c = rbp_r_t;
|
|
|
|
} else {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Move left.
|
2017-09-26 11:13:53 +03:00
|
|
|
rbp_r_p = rbp_r_c;
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_c = rbp_r_c.Left();
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_r_cmp == Order::eEqual) {
|
2017-09-26 11:13:53 +03:00
|
|
|
MOZ_ASSERT(aNode == rbp_r_c);
|
2018-02-15 14:15:00 +03:00
|
|
|
if (!rbp_r_c.Right()) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Delete root node (which is also a leaf node).
|
2018-02-15 14:15:00 +03:00
|
|
|
if (rbp_r_c.Left()) {
|
2017-09-26 15:14:12 +03:00
|
|
|
rbp_r_t = LeanRight(rbp_r_c);
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_t.SetRight(nullptr);
|
2017-09-26 11:13:53 +03:00
|
|
|
} else {
|
2017-10-28 02:36:32 +03:00
|
|
|
rbp_r_t = nullptr;
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_p.SetLeft(rbp_r_t);
|
2017-09-26 11:13:53 +03:00
|
|
|
} else {
|
2017-11-01 11:15:12 +03:00
|
|
|
// This is the node we want to delete, but we will
|
|
|
|
// instead swap it with its successor and delete the
|
|
|
|
// successor. Record enough information to do the
|
|
|
|
// swap later. rbp_r_xp is the aNode's parent.
|
2017-09-26 11:13:53 +03:00
|
|
|
rbp_r_xp = rbp_r_p;
|
2017-11-21 03:11:54 +03:00
|
|
|
rbp_r_cmp = Order::eGreater; // Note that deletion is incomplete.
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
|
|
|
}
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_r_cmp == Order::eGreater) {
|
2018-02-15 14:25:57 +03:00
|
|
|
if (rbp_r_c.Right().Left().IsBlack()) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_t = rbp_r_c.Left();
|
|
|
|
if (rbp_r_t.IsRed()) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Standard transform.
|
2017-09-26 15:21:13 +03:00
|
|
|
rbp_r_t = MoveRedRight(rbp_r_c);
|
2017-09-26 11:13:53 +03:00
|
|
|
} else {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Root-specific transform.
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_c.SetColor(NodeColor::Red);
|
|
|
|
rbp_r_u = rbp_r_t.Left();
|
2018-02-15 14:25:57 +03:00
|
|
|
if (rbp_r_u.IsRed()) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_u.SetColor(NodeColor::Black);
|
2017-09-26 15:08:29 +03:00
|
|
|
rbp_r_t = RotateRight(rbp_r_c);
|
|
|
|
rbp_r_u = RotateLeft(rbp_r_c);
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_t.SetRight(rbp_r_u);
|
2017-09-26 11:13:53 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_t.SetColor(NodeColor::Red);
|
2017-09-26 15:08:29 +03:00
|
|
|
rbp_r_t = RotateLeft(rbp_r_c);
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_p.SetLeft(rbp_r_t);
|
2017-09-26 11:13:53 +03:00
|
|
|
rbp_r_c = rbp_r_t;
|
|
|
|
} else {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Move right.
|
2017-09-26 11:13:53 +03:00
|
|
|
rbp_r_p = rbp_r_c;
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_c = rbp_r_c.Right();
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_r_cmp != Order::eEqual) {
|
2017-09-26 11:13:53 +03:00
|
|
|
while (true) {
|
2017-10-28 02:36:32 +03:00
|
|
|
MOZ_ASSERT(rbp_r_p);
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_cmp = Trait::Compare(aNode.Get(), rbp_r_c.Get());
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_r_cmp == Order::eLess) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_t = rbp_r_c.Left();
|
2017-10-28 02:36:32 +03:00
|
|
|
if (!rbp_r_t) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// rbp_r_c now refers to the successor node to
|
|
|
|
// relocate, and rbp_r_xp/aNode refer to the
|
|
|
|
// context for the relocation.
|
2018-02-15 14:15:00 +03:00
|
|
|
if (rbp_r_xp.Left() == aNode) {
|
|
|
|
rbp_r_xp.SetLeft(rbp_r_c);
|
2017-09-26 11:13:53 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
MOZ_ASSERT(rbp_r_xp.Right() == (aNode));
|
|
|
|
rbp_r_xp.SetRight(rbp_r_c);
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_c.SetLeft(aNode.Left());
|
|
|
|
rbp_r_c.SetRight(aNode.Right());
|
|
|
|
rbp_r_c.SetColor(aNode.Color());
|
|
|
|
if (rbp_r_p.Left() == rbp_r_c) {
|
|
|
|
rbp_r_p.SetLeft(nullptr);
|
2017-09-26 11:13:53 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
MOZ_ASSERT(rbp_r_p.Right() == rbp_r_c);
|
|
|
|
rbp_r_p.SetRight(nullptr);
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_u = rbp_r_t.Left();
|
2018-02-15 14:25:57 +03:00
|
|
|
if (rbp_r_t.IsBlack() && rbp_r_u.IsBlack()) {
|
2017-09-26 15:21:13 +03:00
|
|
|
rbp_r_t = MoveRedLeft(rbp_r_c);
|
2018-02-15 14:15:00 +03:00
|
|
|
if (rbp_r_p.Left() == rbp_r_c) {
|
|
|
|
rbp_r_p.SetLeft(rbp_r_t);
|
2017-09-26 11:13:53 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_p.SetRight(rbp_r_t);
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
|
|
|
rbp_r_c = rbp_r_t;
|
|
|
|
} else {
|
|
|
|
rbp_r_p = rbp_r_c;
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_c = rbp_r_c.Left();
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Check whether to delete this node (it has to be
|
|
|
|
// the correct node and a leaf node).
|
2017-11-21 03:11:54 +03:00
|
|
|
if (rbp_r_cmp == Order::eEqual) {
|
2017-09-26 11:13:53 +03:00
|
|
|
MOZ_ASSERT(aNode == rbp_r_c);
|
2018-02-15 14:15:00 +03:00
|
|
|
if (!rbp_r_c.Right()) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Delete leaf node.
|
2018-02-15 14:15:00 +03:00
|
|
|
if (rbp_r_c.Left()) {
|
2017-09-26 15:14:12 +03:00
|
|
|
rbp_r_t = LeanRight(rbp_r_c);
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_t.SetRight(nullptr);
|
2017-09-26 11:13:53 +03:00
|
|
|
} else {
|
2017-10-28 02:36:32 +03:00
|
|
|
rbp_r_t = nullptr;
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
2018-02-15 14:15:00 +03:00
|
|
|
if (rbp_r_p.Left() == rbp_r_c) {
|
|
|
|
rbp_r_p.SetLeft(rbp_r_t);
|
2017-09-26 11:13:53 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_p.SetRight(rbp_r_t);
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-11-01 11:15:12 +03:00
|
|
|
// This is the node we want to delete, but we
|
|
|
|
// will instead swap it with its successor
|
|
|
|
// and delete the successor. Record enough
|
|
|
|
// information to do the swap later.
|
|
|
|
// rbp_r_xp is aNode's parent.
|
2017-09-28 15:01:00 +03:00
|
|
|
rbp_r_xp = rbp_r_p;
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_t = rbp_r_c.Right();
|
|
|
|
rbp_r_u = rbp_r_t.Left();
|
2018-02-15 14:25:57 +03:00
|
|
|
if (rbp_r_u.IsBlack()) {
|
2017-09-26 15:21:13 +03:00
|
|
|
rbp_r_t = MoveRedRight(rbp_r_c);
|
2018-02-15 14:15:00 +03:00
|
|
|
if (rbp_r_p.Left() == rbp_r_c) {
|
|
|
|
rbp_r_p.SetLeft(rbp_r_t);
|
2017-09-26 11:13:53 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_p.SetRight(rbp_r_t);
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
|
|
|
rbp_r_c = rbp_r_t;
|
|
|
|
} else {
|
|
|
|
rbp_r_p = rbp_r_c;
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_r_c = rbp_r_c.Right();
|
2017-09-26 11:13:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-01 11:15:12 +03:00
|
|
|
// Update root.
|
2018-02-15 14:15:00 +03:00
|
|
|
mRoot = TreeNode(rbp_r_s.addr()).Left().Get();
|
2017-09-26 09:06:00 +03:00
|
|
|
}
|
2017-09-26 15:08:29 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode RotateLeft(TreeNode aNode) {
|
|
|
|
TreeNode node = aNode.Right();
|
|
|
|
aNode.SetRight(node.Left());
|
|
|
|
node.SetLeft(aNode);
|
2017-09-26 15:08:29 +03:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode RotateRight(TreeNode aNode) {
|
|
|
|
TreeNode node = aNode.Left();
|
|
|
|
aNode.SetLeft(node.Right());
|
|
|
|
node.SetRight(aNode);
|
2017-09-26 15:08:29 +03:00
|
|
|
return node;
|
|
|
|
}
|
2017-09-26 15:14:12 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode LeanLeft(TreeNode aNode) {
|
|
|
|
TreeNode node = RotateLeft(aNode);
|
|
|
|
NodeColor color = aNode.Color();
|
|
|
|
node.SetColor(color);
|
|
|
|
aNode.SetColor(NodeColor::Red);
|
2017-09-26 15:14:12 +03:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode LeanRight(TreeNode aNode) {
|
|
|
|
TreeNode node = RotateRight(aNode);
|
|
|
|
NodeColor color = aNode.Color();
|
|
|
|
node.SetColor(color);
|
|
|
|
aNode.SetColor(NodeColor::Red);
|
2017-09-26 15:14:12 +03:00
|
|
|
return node;
|
|
|
|
}
|
2017-09-26 15:21:13 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode MoveRedLeft(TreeNode aNode) {
|
|
|
|
TreeNode node;
|
|
|
|
TreeNode rbp_mrl_t, rbp_mrl_u;
|
|
|
|
rbp_mrl_t = aNode.Left();
|
|
|
|
rbp_mrl_t.SetColor(NodeColor::Red);
|
|
|
|
rbp_mrl_t = aNode.Right();
|
2018-02-15 14:25:57 +03:00
|
|
|
rbp_mrl_u = rbp_mrl_t.Left();
|
|
|
|
if (rbp_mrl_u.IsRed()) {
|
2017-09-26 15:21:13 +03:00
|
|
|
rbp_mrl_u = RotateRight(rbp_mrl_t);
|
2018-02-15 14:15:00 +03:00
|
|
|
aNode.SetRight(rbp_mrl_u);
|
2017-09-26 15:21:13 +03:00
|
|
|
node = RotateLeft(aNode);
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_mrl_t = aNode.Right();
|
2018-02-15 14:25:57 +03:00
|
|
|
if (rbp_mrl_t.IsRed()) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_mrl_t.SetColor(NodeColor::Black);
|
|
|
|
aNode.SetColor(NodeColor::Red);
|
2017-09-26 15:21:13 +03:00
|
|
|
rbp_mrl_t = RotateLeft(aNode);
|
2018-02-15 14:15:00 +03:00
|
|
|
node.SetLeft(rbp_mrl_t);
|
2017-09-26 15:21:13 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
aNode.SetColor(NodeColor::Black);
|
2017-09-26 15:21:13 +03:00
|
|
|
}
|
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
aNode.SetColor(NodeColor::Red);
|
2017-09-26 15:21:13 +03:00
|
|
|
node = RotateLeft(aNode);
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode MoveRedRight(TreeNode aNode) {
|
|
|
|
TreeNode node;
|
|
|
|
TreeNode rbp_mrr_t;
|
|
|
|
rbp_mrr_t = aNode.Left();
|
2018-02-15 14:25:57 +03:00
|
|
|
if (rbp_mrr_t.IsRed()) {
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode rbp_mrr_u, rbp_mrr_v;
|
|
|
|
rbp_mrr_u = rbp_mrr_t.Right();
|
2018-02-15 14:25:57 +03:00
|
|
|
rbp_mrr_v = rbp_mrr_u.Left();
|
|
|
|
if (rbp_mrr_v.IsRed()) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_mrr_u.SetColor(aNode.Color());
|
|
|
|
rbp_mrr_v.SetColor(NodeColor::Black);
|
2017-09-26 15:21:13 +03:00
|
|
|
rbp_mrr_u = RotateLeft(rbp_mrr_t);
|
2018-02-15 14:15:00 +03:00
|
|
|
aNode.SetLeft(rbp_mrr_u);
|
2017-09-26 15:21:13 +03:00
|
|
|
node = RotateRight(aNode);
|
|
|
|
rbp_mrr_t = RotateLeft(aNode);
|
2018-02-15 14:15:00 +03:00
|
|
|
node.SetRight(rbp_mrr_t);
|
2017-09-26 15:21:13 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_mrr_t.SetColor(aNode.Color());
|
|
|
|
rbp_mrr_u.SetColor(NodeColor::Red);
|
2017-09-26 15:21:13 +03:00
|
|
|
node = RotateRight(aNode);
|
|
|
|
rbp_mrr_t = RotateLeft(aNode);
|
2018-02-15 14:15:00 +03:00
|
|
|
node.SetRight(rbp_mrr_t);
|
2017-09-26 15:21:13 +03:00
|
|
|
}
|
2018-02-15 14:15:00 +03:00
|
|
|
aNode.SetColor(NodeColor::Red);
|
2017-09-26 15:21:13 +03:00
|
|
|
} else {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_mrr_t.SetColor(NodeColor::Red);
|
|
|
|
rbp_mrr_t = rbp_mrr_t.Left();
|
2018-02-15 14:25:57 +03:00
|
|
|
if (rbp_mrr_t.IsRed()) {
|
2018-02-15 14:15:00 +03:00
|
|
|
rbp_mrr_t.SetColor(NodeColor::Black);
|
2017-09-26 15:21:13 +03:00
|
|
|
node = RotateRight(aNode);
|
|
|
|
rbp_mrr_t = RotateLeft(aNode);
|
2018-02-15 14:15:00 +03:00
|
|
|
node.SetRight(rbp_mrr_t);
|
2017-09-26 15:21:13 +03:00
|
|
|
} else {
|
|
|
|
node = RotateLeft(aNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
2017-09-26 09:06:00 +03:00
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// The iterator simulates recursion via an array of pointers that store the
|
|
|
|
// current path. This is critical to performance, since a series of calls to
|
|
|
|
// rb_{next,prev}() would require time proportional to (n lg n), whereas this
|
|
|
|
// implementation only requires time proportional to (n).
|
|
|
|
//
|
|
|
|
// Since the iterator caches a path down the tree, any tree modification may
|
|
|
|
// cause the cached path to become invalid. Don't modify the tree during an
|
|
|
|
// iteration.
|
|
|
|
|
|
|
|
// Size the path arrays such that they are always large enough, even if a
|
|
|
|
// tree consumes all of memory. Since each node must contain a minimum of
|
|
|
|
// two pointers, there can never be more nodes than:
|
|
|
|
//
|
|
|
|
// 1 << ((sizeof(void*)<<3) - (log2(sizeof(void*))+1))
|
|
|
|
//
|
|
|
|
// Since the depth of a tree is limited to 3*lg(#nodes), the maximum depth
|
|
|
|
// is:
|
|
|
|
//
|
|
|
|
// (3 * ((sizeof(void*)<<3) - (log2(sizeof(void*))+1)))
|
|
|
|
//
|
|
|
|
// This works out to a maximum depth of 87 and 180 for 32- and 64-bit
|
|
|
|
// systems, respectively (approximately 348 and 1440 bytes, respectively).
|
2017-09-27 09:25:19 +03:00
|
|
|
public:
|
|
|
|
class Iterator {
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode mPath[3 * ((sizeof(void*) << 3) - (LOG2(sizeof(void*)) + 1))];
|
2017-09-27 09:25:19 +03:00
|
|
|
unsigned mDepth;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Iterator(RedBlackTree<T, Trait>* aTree) : mDepth(0) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Initialize the path to contain the left spine.
|
2017-10-28 02:36:32 +03:00
|
|
|
if (aTree->mRoot) {
|
2018-02-15 14:15:00 +03:00
|
|
|
TreeNode node;
|
2017-09-27 10:11:11 +03:00
|
|
|
mPath[mDepth++] = aTree->mRoot;
|
2018-02-15 14:15:00 +03:00
|
|
|
while ((node = mPath[mDepth - 1].Left())) {
|
2017-09-27 09:25:19 +03:00
|
|
|
mPath[mDepth++] = node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-20 21:34:42 +04:00
|
|
|
|
2017-10-31 01:13:39 +03:00
|
|
|
template <typename Iterator>
|
2017-09-27 09:25:19 +03:00
|
|
|
class Item {
|
|
|
|
Iterator* mIterator;
|
|
|
|
T* mItem;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Item(Iterator* aIterator, T* aItem)
|
|
|
|
: mIterator(aIterator), mItem(aItem) {}
|
|
|
|
|
|
|
|
bool operator!=(const Item& aOther) const {
|
|
|
|
return (mIterator != aOther.mIterator) || (mItem != aOther.mItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator*() const { return mItem; }
|
|
|
|
|
|
|
|
const Item& operator++() {
|
|
|
|
mItem = mIterator->Next();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-31 01:13:39 +03:00
|
|
|
Item<Iterator> begin() {
|
2018-02-15 14:15:00 +03:00
|
|
|
return Item<Iterator>(this,
|
|
|
|
mDepth > 0 ? mPath[mDepth - 1].Get() : nullptr);
|
2017-09-27 09:25:19 +03:00
|
|
|
}
|
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
Item<Iterator> end() { return Item<Iterator>(this, nullptr); }
|
2017-09-27 09:25:19 +03:00
|
|
|
|
2018-02-15 14:15:00 +03:00
|
|
|
T* Next() {
|
|
|
|
TreeNode node;
|
|
|
|
if ((node = mPath[mDepth - 1].Right())) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// The successor is the left-most node in the right subtree.
|
2017-09-27 09:25:19 +03:00
|
|
|
mPath[mDepth++] = node;
|
2018-02-15 14:15:00 +03:00
|
|
|
while ((node = mPath[mDepth - 1].Left())) {
|
2017-09-27 09:25:19 +03:00
|
|
|
mPath[mDepth++] = node;
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-01 11:15:12 +03:00
|
|
|
// The successor is above the current node. Unwind until a
|
|
|
|
// left-leaning edge is removed from the path, of the path is empty.
|
2017-09-27 09:25:19 +03:00
|
|
|
for (mDepth--; mDepth > 0; mDepth--) {
|
2018-02-15 14:15:00 +03:00
|
|
|
if (mPath[mDepth - 1].Left() == mPath[mDepth]) {
|
2017-09-27 09:25:19 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-15 14:15:00 +03:00
|
|
|
return mDepth > 0 ? mPath[mDepth - 1].Get() : nullptr;
|
2017-09-27 09:25:19 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Iterator iter() { return Iterator(this); }
|
|
|
|
};
|
2008-06-20 21:34:42 +04:00
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
#endif // RB_H_
|