using System;
namespace Ryujinx.Common.Collections
{
///
/// Tree that provides the ability for O(logN) lookups for keys that exist in the tree, and O(logN) lookups for keys immediately greater than or less than a specified key.
///
/// Derived node type
public class IntrusiveRedBlackTreeImpl where T : IntrusiveRedBlackTreeNode
{
protected const bool Black = true;
protected const bool Red = false;
protected T Root;
internal T RootNode => Root;
///
/// Number of nodes on the tree.
///
public int Count { get; protected set; }
///
/// Removes all nodes on the tree.
///
public void Clear()
{
Root = null;
Count = 0;
}
///
/// Finds the node whose key is immediately greater than .
///
/// Node to find the successor of
/// Successor of
internal static T SuccessorOf(T node)
{
if (node.Right != null)
{
return Minimum(node.Right);
}
T parent = node.Parent;
while (parent != null && node == parent.Right)
{
node = parent;
parent = parent.Parent;
}
return parent;
}
///
/// Finds the node whose key is immediately less than .
///
/// Node to find the predecessor of
/// Predecessor of
internal static T PredecessorOf(T node)
{
if (node.Left != null)
{
return Maximum(node.Left);
}
T parent = node.Parent;
while (parent != null && node == parent.Left)
{
node = parent;
parent = parent.Parent;
}
return parent;
}
///
/// Returns the node with the largest key where is considered the root node.
///
/// Root node
/// Node with the maximum key in the tree of
protected static T Maximum(T node)
{
T tmp = node;
while (tmp.Right != null)
{
tmp = tmp.Right;
}
return tmp;
}
///
/// Returns the node with the smallest key where is considered the root node.
///
/// Root node
/// Node with the minimum key in the tree of
/// is null
protected static T Minimum(T node)
{
ArgumentNullException.ThrowIfNull(node);
T tmp = node;
while (tmp.Left != null)
{
tmp = tmp.Left;
}
return tmp;
}
protected void RestoreBalanceAfterRemoval(T balanceNode)
{
T ptr = balanceNode;
while (ptr != Root && ColorOf(ptr) == Black)
{
if (ptr == LeftOf(ParentOf(ptr)))
{
T sibling = RightOf(ParentOf(ptr));
if (ColorOf(sibling) == Red)
{
SetColor(sibling, Black);
SetColor(ParentOf(ptr), Red);
RotateLeft(ParentOf(ptr));
sibling = RightOf(ParentOf(ptr));
}
if (ColorOf(LeftOf(sibling)) == Black && ColorOf(RightOf(sibling)) == Black)
{
SetColor(sibling, Red);
ptr = ParentOf(ptr);
}
else
{
if (ColorOf(RightOf(sibling)) == Black)
{
SetColor(LeftOf(sibling), Black);
SetColor(sibling, Red);
RotateRight(sibling);
sibling = RightOf(ParentOf(ptr));
}
SetColor(sibling, ColorOf(ParentOf(ptr)));
SetColor(ParentOf(ptr), Black);
SetColor(RightOf(sibling), Black);
RotateLeft(ParentOf(ptr));
ptr = Root;
}
}
else
{
T sibling = LeftOf(ParentOf(ptr));
if (ColorOf(sibling) == Red)
{
SetColor(sibling, Black);
SetColor(ParentOf(ptr), Red);
RotateRight(ParentOf(ptr));
sibling = LeftOf(ParentOf(ptr));
}
if (ColorOf(RightOf(sibling)) == Black && ColorOf(LeftOf(sibling)) == Black)
{
SetColor(sibling, Red);
ptr = ParentOf(ptr);
}
else
{
if (ColorOf(LeftOf(sibling)) == Black)
{
SetColor(RightOf(sibling), Black);
SetColor(sibling, Red);
RotateLeft(sibling);
sibling = LeftOf(ParentOf(ptr));
}
SetColor(sibling, ColorOf(ParentOf(ptr)));
SetColor(ParentOf(ptr), Black);
SetColor(LeftOf(sibling), Black);
RotateRight(ParentOf(ptr));
ptr = Root;
}
}
}
SetColor(ptr, Black);
}
protected void RestoreBalanceAfterInsertion(T balanceNode)
{
SetColor(balanceNode, Red);
while (balanceNode != null && balanceNode != Root && ColorOf(ParentOf(balanceNode)) == Red)
{
if (ParentOf(balanceNode) == LeftOf(ParentOf(ParentOf(balanceNode))))
{
T sibling = RightOf(ParentOf(ParentOf(balanceNode)));
if (ColorOf(sibling) == Red)
{
SetColor(ParentOf(balanceNode), Black);
SetColor(sibling, Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
balanceNode = ParentOf(ParentOf(balanceNode));
}
else
{
if (balanceNode == RightOf(ParentOf(balanceNode)))
{
balanceNode = ParentOf(balanceNode);
RotateLeft(balanceNode);
}
SetColor(ParentOf(balanceNode), Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
RotateRight(ParentOf(ParentOf(balanceNode)));
}
}
else
{
T sibling = LeftOf(ParentOf(ParentOf(balanceNode)));
if (ColorOf(sibling) == Red)
{
SetColor(ParentOf(balanceNode), Black);
SetColor(sibling, Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
balanceNode = ParentOf(ParentOf(balanceNode));
}
else
{
if (balanceNode == LeftOf(ParentOf(balanceNode)))
{
balanceNode = ParentOf(balanceNode);
RotateRight(balanceNode);
}
SetColor(ParentOf(balanceNode), Black);
SetColor(ParentOf(ParentOf(balanceNode)), Red);
RotateLeft(ParentOf(ParentOf(balanceNode)));
}
}
}
SetColor(Root, Black);
}
protected virtual void RotateLeft(T node)
{
if (node != null)
{
T right = RightOf(node);
node.Right = LeftOf(right);
if (node.Right != null)
{
node.Right.Parent = node;
}
T nodeParent = ParentOf(node);
right.Parent = nodeParent;
if (nodeParent == null)
{
Root = right;
}
else if (node == LeftOf(nodeParent))
{
nodeParent.Left = right;
}
else
{
nodeParent.Right = right;
}
right.Left = node;
node.Parent = right;
}
}
protected virtual void RotateRight(T node)
{
if (node != null)
{
T left = LeftOf(node);
node.Left = RightOf(left);
if (node.Left != null)
{
node.Left.Parent = node;
}
T nodeParent = ParentOf(node);
left.Parent = nodeParent;
if (nodeParent == null)
{
Root = left;
}
else if (node == RightOf(nodeParent))
{
nodeParent.Right = left;
}
else
{
nodeParent.Left = left;
}
left.Right = node;
node.Parent = left;
}
}
#region Safety-Methods
// These methods save memory by allowing us to forego sentinel nil nodes, as well as serve as protection against NullReferenceExceptions.
///
/// Returns the color of , or Black if it is null.
///
/// Node
/// The boolean color of , or black if null
protected static bool ColorOf(T node)
{
return node == null || node.Color;
}
///
/// Sets the color of node to .
///
/// This method does nothing if is null.
///
/// Node to set the color of
/// Color (Boolean)
protected static void SetColor(T node, bool color)
{
if (node != null)
{
node.Color = color;
}
}
///
/// This method returns the left node of , or null if is null.
///
/// Node to retrieve the left child from
/// Left child of
protected static T LeftOf(T node)
{
return node?.Left;
}
///
/// This method returns the right node of , or null if is null.
///
/// Node to retrieve the right child from
/// Right child of
protected static T RightOf(T node)
{
return node?.Right;
}
///
/// Returns the parent node of , or null if is null.
///
/// Node to retrieve the parent from
/// Parent of
protected static T ParentOf(T node)
{
return node?.Parent;
}
#endregion
}
}