aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/Collections
diff options
context:
space:
mode:
authorgdkchan <gab.dark.100@gmail.com>2022-08-26 15:21:48 -0300
committerGitHub <noreply@github.com>2022-08-26 18:21:48 +0000
commit6922862db8677fb8067a3e35d2433b1f12f8329c (patch)
tree8c14b28e8648b2bb43b928a29a5c73727795f07e /Ryujinx.Common/Collections
parent6592d64751d31bccc1b20eace538464b488e4be9 (diff)
Optimize kernel memory block lookup and consolidate RBTree implementations (#3410)
* Implement intrusive red-black tree, use it for HLE kernel block manager * Implement TreeDictionary using IntrusiveRedBlackTree * Implement IntervalTree using IntrusiveRedBlackTree * Implement IntervalTree (on Ryujinx.Memory) using IntrusiveRedBlackTree * Make PredecessorOf and SuccessorOf internal, expose Predecessor and Successor properties on the node itself * Allocation free tree node lookup
Diffstat (limited to 'Ryujinx.Common/Collections')
-rw-r--r--Ryujinx.Common/Collections/IntervalTree.cs331
-rw-r--r--Ryujinx.Common/Collections/IntrusiveRedBlackTree.cs293
-rw-r--r--Ryujinx.Common/Collections/IntrusiveRedBlackTreeImpl.cs356
-rw-r--r--Ryujinx.Common/Collections/IntrusiveRedBlackTreeNode.cs16
-rw-r--r--Ryujinx.Common/Collections/TreeDictionary.cs400
5 files changed, 716 insertions, 680 deletions
diff --git a/Ryujinx.Common/Collections/IntervalTree.cs b/Ryujinx.Common/Collections/IntervalTree.cs
index 514fd534..c829caba 100644
--- a/Ryujinx.Common/Collections/IntervalTree.cs
+++ b/Ryujinx.Common/Collections/IntervalTree.cs
@@ -9,19 +9,10 @@ namespace Ryujinx.Common.Collections
/// </summary>
/// <typeparam name="K">Key</typeparam>
/// <typeparam name="V">Value</typeparam>
- public class IntervalTree<K, V> where K : IComparable<K>
+ public class IntervalTree<K, V> : IntrusiveRedBlackTreeImpl<IntervalTreeNode<K, V>> where K : IComparable<K>
{
private const int ArrayGrowthSize = 32;
- private const bool Black = true;
- private const bool Red = false;
- private IntervalTreeNode<K, V> _root = null;
- private int _count = 0;
-
- public int Count => _count;
-
- public IntervalTree() { }
-
#region Public Methods
/// <summary>
@@ -80,7 +71,7 @@ namespace Ryujinx.Common.Collections
throw new ArgumentNullException(nameof(end));
}
- GetValues(_root, start, end, ref overlaps, ref overlapCount);
+ GetValues(Root, start, end, ref overlaps, ref overlapCount);
return overlapCount;
}
@@ -128,7 +119,7 @@ namespace Ryujinx.Common.Collections
int removed = Delete(key, value);
- _count -= removed;
+ Count -= removed;
return removed;
}
@@ -141,7 +132,7 @@ namespace Ryujinx.Common.Collections
{
List<RangeNode<K, V>> list = new List<RangeNode<K, V>>();
- AddToList(_root, list);
+ AddToList(Root, list);
return list;
}
@@ -182,7 +173,7 @@ namespace Ryujinx.Common.Collections
throw new ArgumentNullException(nameof(key));
}
- IntervalTreeNode<K, V> node = _root;
+ IntervalTreeNode<K, V> node = Root;
while (node != null)
{
int cmp = key.CompareTo(node.Start);
@@ -317,7 +308,7 @@ namespace Ryujinx.Common.Collections
private IntervalTreeNode<K, V> BSTInsert(K start, K end, V value)
{
IntervalTreeNode<K, V> parent = null;
- IntervalTreeNode<K, V> node = _root;
+ IntervalTreeNode<K, V> node = Root;
while (node != null)
{
@@ -345,14 +336,14 @@ namespace Ryujinx.Common.Collections
}
}
- _count++;
+ Count++;
return node;
}
}
IntervalTreeNode<K, V> newNode = new IntervalTreeNode<K, V>(start, end, value, parent);
if (newNode.Parent == null)
{
- _root = newNode;
+ Root = newNode;
}
else if (start.CompareTo(parent.Start) < 0)
{
@@ -364,7 +355,7 @@ namespace Ryujinx.Common.Collections
}
PropagateIncrease(newNode);
- _count++;
+ Count++;
return newNode;
}
@@ -418,7 +409,7 @@ namespace Ryujinx.Common.Collections
if (ParentOf(replacementNode) == null)
{
- _root = tmp;
+ Root = tmp;
}
else if (replacementNode == LeftOf(ParentOf(replacementNode)))
{
@@ -447,295 +438,27 @@ namespace Ryujinx.Common.Collections
return removed;
}
- /// <summary>
- /// Returns the node with the largest key where <paramref name="node"/> is considered the root node.
- /// </summary>
- /// <param name="node">Root Node</param>
- /// <returns>Node with the maximum key in the tree of <paramref name="node"/></returns>
- private static IntervalTreeNode<K, V> Maximum(IntervalTreeNode<K, V> node)
- {
- IntervalTreeNode<K, V> tmp = node;
- while (tmp.Right != null)
- {
- tmp = tmp.Right;
- }
-
- return tmp;
- }
-
- /// <summary>
- /// Finds the node whose key is immediately less than <paramref name="node"/>.
- /// </summary>
- /// <param name="node">Node to find the predecessor of</param>
- /// <returns>Predecessor of <paramref name="node"/></returns>
- private static IntervalTreeNode<K, V> PredecessorOf(IntervalTreeNode<K, V> node)
- {
- if (node.Left != null)
- {
- return Maximum(node.Left);
- }
- IntervalTreeNode<K, V> parent = node.Parent;
- while (parent != null && node == parent.Left)
- {
- node = parent;
- parent = parent.Parent;
- }
- return parent;
- }
#endregion
- #region Private Methods (RBL)
- private void RestoreBalanceAfterRemoval(IntervalTreeNode<K, V> balanceNode)
- {
- IntervalTreeNode<K, V> ptr = balanceNode;
-
- while (ptr != _root && ColorOf(ptr) == Black)
- {
- if (ptr == LeftOf(ParentOf(ptr)))
- {
- IntervalTreeNode<K, V> 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
- {
- IntervalTreeNode<K, V> 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);
- }
-
- private void RestoreBalanceAfterInsertion(IntervalTreeNode<K, V> balanceNode)
- {
- SetColor(balanceNode, Red);
- while (balanceNode != null && balanceNode != _root && ColorOf(ParentOf(balanceNode)) == Red)
- {
- if (ParentOf(balanceNode) == LeftOf(ParentOf(ParentOf(balanceNode))))
- {
- IntervalTreeNode<K, V> 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
- {
- IntervalTreeNode<K, V> 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);
- }
-
- private void RotateLeft(IntervalTreeNode<K, V> node)
+ protected override void RotateLeft(IntervalTreeNode<K, V> node)
{
if (node != null)
{
- IntervalTreeNode<K, V> right = RightOf(node);
- node.Right = LeftOf(right);
- if (node.Right != null)
- {
- node.Right.Parent = node;
- }
- IntervalTreeNode<K, V> 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;
+ base.RotateLeft(node);
PropagateFull(node);
}
}
- private void RotateRight(IntervalTreeNode<K, V> node)
+ protected override void RotateRight(IntervalTreeNode<K, V> node)
{
if (node != null)
{
- IntervalTreeNode<K, V> left = LeftOf(node);
- node.Left = RightOf(left);
- if (node.Left != null)
- {
- node.Left.Parent = node;
- }
- IntervalTreeNode<K, V> 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;
+ base.RotateRight(node);
PropagateFull(node);
}
}
- #endregion
-
- #region Safety-Methods
-
- // These methods save memory by allowing us to forego sentinel nil nodes, as well as serve as protection against NullReferenceExceptions.
-
- /// <summary>
- /// Returns the color of <paramref name="node"/>, or Black if it is null.
- /// </summary>
- /// <param name="node">Node</param>
- /// <returns>The boolean color of <paramref name="node"/>, or black if null</returns>
- private static bool ColorOf(IntervalTreeNode<K, V> node)
- {
- return node == null || node.Color;
- }
-
- /// <summary>
- /// Sets the color of <paramref name="node"/> node to <paramref name="color"/>.
- /// <br></br>
- /// This method does nothing if <paramref name="node"/> is null.
- /// </summary>
- /// <param name="node">Node to set the color of</param>
- /// <param name="color">Color (Boolean)</param>
- private static void SetColor(IntervalTreeNode<K, V> node, bool color)
- {
- if (node != null)
- {
- node.Color = color;
- }
- }
-
- /// <summary>
- /// This method returns the left node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
- /// </summary>
- /// <param name="node">Node to retrieve the left child from</param>
- /// <returns>Left child of <paramref name="node"/></returns>
- private static IntervalTreeNode<K, V> LeftOf(IntervalTreeNode<K, V> node)
- {
- return node?.Left;
- }
-
- /// <summary>
- /// This method returns the right node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
- /// </summary>
- /// <param name="node">Node to retrieve the right child from</param>
- /// <returns>Right child of <paramref name="node"/></returns>
- private static IntervalTreeNode<K, V> RightOf(IntervalTreeNode<K, V> node)
- {
- return node?.Right;
- }
-
- /// <summary>
- /// Returns the parent node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
- /// </summary>
- /// <param name="node">Node to retrieve the parent from</param>
- /// <returns>Parent of <paramref name="node"/></returns>
- private static IntervalTreeNode<K, V> ParentOf(IntervalTreeNode<K, V> node)
- {
- return node?.Parent;
- }
- #endregion
public bool ContainsKey(K key)
{
@@ -745,12 +468,6 @@ namespace Ryujinx.Common.Collections
}
return GetNode(key) != null;
}
-
- public void Clear()
- {
- _root = null;
- _count = 0;
- }
}
/// <summary>
@@ -777,31 +494,29 @@ namespace Ryujinx.Common.Collections
/// </summary>
/// <typeparam name="K">Key type of the node</typeparam>
/// <typeparam name="V">Value type of the node</typeparam>
- class IntervalTreeNode<K, V>
+ public class IntervalTreeNode<K, V> : IntrusiveRedBlackTreeNode<IntervalTreeNode<K, V>>
{
- public bool Color = true;
- public IntervalTreeNode<K, V> Left = null;
- public IntervalTreeNode<K, V> Right = null;
- public IntervalTreeNode<K, V> Parent = null;
-
/// <summary>
/// The start of the range.
/// </summary>
- public K Start;
+ internal K Start;
/// <summary>
/// The end of the range - maximum of all in the Values list.
/// </summary>
- public K End;
+ internal K End;
/// <summary>
/// The maximum end value of this node and all its children.
/// </summary>
- public K Max;
+ internal K Max;
- public List<RangeNode<K, V>> Values;
+ /// <summary>
+ /// Values contained on the node that shares a common Start value.
+ /// </summary>
+ internal List<RangeNode<K, V>> Values;
- public IntervalTreeNode(K start, K end, V value, IntervalTreeNode<K, V> parent)
+ internal IntervalTreeNode(K start, K end, V value, IntervalTreeNode<K, V> parent)
{
Start = start;
End = end;
diff --git a/Ryujinx.Common/Collections/IntrusiveRedBlackTree.cs b/Ryujinx.Common/Collections/IntrusiveRedBlackTree.cs
new file mode 100644
index 00000000..970cab87
--- /dev/null
+++ b/Ryujinx.Common/Collections/IntrusiveRedBlackTree.cs
@@ -0,0 +1,293 @@
+using System;
+
+namespace Ryujinx.Common.Collections
+{
+ /// <summary>
+ /// 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.
+ /// </summary>
+ /// <typeparam name="T">Derived node type</typeparam>
+ public class IntrusiveRedBlackTree<T> : IntrusiveRedBlackTreeImpl<T> where T : IntrusiveRedBlackTreeNode<T>, IComparable<T>
+ {
+ #region Public Methods
+
+ /// <summary>
+ /// Adds a new node into the tree.
+ /// </summary>
+ /// <param name="node">Node to be added</param>
+ /// <exception cref="ArgumentNullException"><paramref name="node"/> is null</exception>
+ public void Add(T node)
+ {
+ if (node == null)
+ {
+ throw new ArgumentNullException(nameof(node));
+ }
+
+ Insert(node);
+ }
+
+ /// <summary>
+ /// Removes a node from the tree.
+ /// </summary>
+ /// <param name="node">Note to be removed</param>
+ /// <exception cref="ArgumentNullException"><paramref name="node"/> is null</exception>
+ public void Remove(T node)
+ {
+ if (node == null)
+ {
+ throw new ArgumentNullException(nameof(node));
+ }
+ if (Delete(node) != null)
+ {
+ Count--;
+ }
+ }
+
+ /// <summary>
+ /// Retrieve the node that is considered equal to the specified node by the comparator.
+ /// </summary>
+ /// <param name="searchNode">Node to compare with</param>
+ /// <returns>Node that is equal to <paramref name="searchNode"/></returns>
+ /// <exception cref="ArgumentNullException"><paramref name="searchNode"/> is null</exception>
+ public T GetNode(T searchNode)
+ {
+ if (searchNode == null)
+ {
+ throw new ArgumentNullException(nameof(searchNode));
+ }
+
+ T node = Root;
+ while (node != null)
+ {
+ int cmp = searchNode.CompareTo(node);
+ if (cmp < 0)
+ {
+ node = node.Left;
+ }
+ else if (cmp > 0)
+ {
+ node = node.Right;
+ }
+ else
+ {
+ return node;
+ }
+ }
+ return null;
+ }
+
+ #endregion
+
+ #region Private Methods (BST)
+
+ /// <summary>
+ /// Inserts a new node into the tree.
+ /// </summary>
+ /// <param name="node">Node to be inserted</param>
+ private void Insert(T node)
+ {
+ T newNode = BSTInsert(node);
+ RestoreBalanceAfterInsertion(newNode);
+ }
+
+ /// <summary>
+ /// Insertion Mechanism for a Binary Search Tree (BST).
+ /// <br></br>
+ /// Iterates the tree starting from the root and inserts a new node
+ /// where all children in the left subtree are less than <paramref name="newNode"/>,
+ /// and all children in the right subtree are greater than <paramref name="newNode"/>.
+ /// </summary>
+ /// <param name="newNode">Node to be inserted</param>
+ /// <returns>The inserted Node</returns>
+ private T BSTInsert(T newNode)
+ {
+ T parent = null;
+ T node = Root;
+
+ while (node != null)
+ {
+ parent = node;
+ int cmp = newNode.CompareTo(node);
+ if (cmp < 0)
+ {
+ node = node.Left;
+ }
+ else if (cmp > 0)
+ {
+ node = node.Right;
+ }
+ else
+ {
+ return node;
+ }
+ }
+ newNode.Parent = parent;
+ if (parent == null)
+ {
+ Root = newNode;
+ }
+ else if (newNode.CompareTo(parent) < 0)
+ {
+ parent.Left = newNode;
+ }
+ else
+ {
+ parent.Right = newNode;
+ }
+ Count++;
+ return newNode;
+ }
+
+ /// <summary>
+ /// Removes <paramref name="nodeToDelete"/> from the tree, if it exists.
+ /// </summary>
+ /// <param name="nodeToDelete">Node to be removed</param>
+ /// <returns>The deleted Node</returns>
+ private T Delete(T nodeToDelete)
+ {
+ if (nodeToDelete == null)
+ {
+ return null;
+ }
+
+ T old = nodeToDelete;
+ T child;
+ T parent;
+ bool color;
+
+ if (LeftOf(nodeToDelete) == null)
+ {
+ child = RightOf(nodeToDelete);
+ }
+ else if (RightOf(nodeToDelete) == null)
+ {
+ child = LeftOf(nodeToDelete);
+ }
+ else
+ {
+ T element = Minimum(RightOf(nodeToDelete));
+
+ child = RightOf(element);
+ parent = ParentOf(element);
+ color = ColorOf(element);
+
+ if (child != null)
+ {
+ child.Parent = parent;
+ }
+
+ if (parent == null)
+ {
+ Root = child;
+ }
+ else if (element == LeftOf(parent))
+ {
+ parent.Left = child;
+ }
+ else
+ {
+ parent.Right = child;
+ }
+
+ if (ParentOf(element) == old)
+ {
+ parent = element;
+ }
+
+ element.Color = old.Color;
+ element.Left = old.Left;
+ element.Right = old.Right;
+ element.Parent = old.Parent;
+
+ if (ParentOf(old) == null)
+ {
+ Root = element;
+ }
+ else if (old == LeftOf(ParentOf(old)))
+ {
+ ParentOf(old).Left = element;
+ }
+ else
+ {
+ ParentOf(old).Right = element;
+ }
+
+ LeftOf(old).Parent = element;
+
+ if (RightOf(old) != null)
+ {
+ RightOf(old).Parent = element;
+ }
+
+ if (child != null && color == Black)
+ {
+ RestoreBalanceAfterRemoval(child);
+ }
+
+ return old;
+ }
+
+ parent = ParentOf(nodeToDelete);
+ color = ColorOf(nodeToDelete);
+
+ if (child != null)
+ {
+ child.Parent = parent;
+ }
+
+ if (parent == null)
+ {
+ Root = child;
+ }
+ else if (nodeToDelete == LeftOf(parent))
+ {
+ parent.Left = child;
+ }
+ else
+ {
+ parent.Right = child;
+ }
+
+ if (child != null && color == Black)
+ {
+ RestoreBalanceAfterRemoval(child);
+ }
+
+ return old;
+ }
+
+ #endregion
+ }
+
+ public static class IntrusiveRedBlackTreeExtensions
+ {
+ /// <summary>
+ /// Retrieve the node that is considered equal to the key by the comparator.
+ /// </summary>
+ /// <param name="tree">Tree to search at</param>
+ /// <param name="key">Key of the node to be found</param>
+ /// <returns>Node that is equal to <paramref name="key"/></returns>
+ public static N GetNodeByKey<N, K>(this IntrusiveRedBlackTree<N> tree, K key)
+ where N : IntrusiveRedBlackTreeNode<N>, IComparable<N>, IComparable<K>
+ where K : struct
+ {
+ N node = tree.RootNode;
+ while (node != null)
+ {
+ int cmp = node.CompareTo(key);
+ if (cmp < 0)
+ {
+ node = node.Right;
+ }
+ else if (cmp > 0)
+ {
+ node = node.Left;
+ }
+ else
+ {
+ return node;
+ }
+ }
+ return null;
+ }
+ }
+}
diff --git a/Ryujinx.Common/Collections/IntrusiveRedBlackTreeImpl.cs b/Ryujinx.Common/Collections/IntrusiveRedBlackTreeImpl.cs
new file mode 100644
index 00000000..267aeec3
--- /dev/null
+++ b/Ryujinx.Common/Collections/IntrusiveRedBlackTreeImpl.cs
@@ -0,0 +1,356 @@
+using System;
+
+namespace Ryujinx.Common.Collections
+{
+ /// <summary>
+ /// 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.
+ /// </summary>
+ /// <typeparam name="T">Derived node type</typeparam>
+ public class IntrusiveRedBlackTreeImpl<T> where T : IntrusiveRedBlackTreeNode<T>
+ {
+ protected const bool Black = true;
+ protected const bool Red = false;
+ protected T Root = null;
+
+ internal T RootNode => Root;
+
+ /// <summary>
+ /// Number of nodes on the tree.
+ /// </summary>
+ public int Count { get; protected set; }
+
+ /// <summary>
+ /// Removes all nodes on the tree.
+ /// </summary>
+ public void Clear()
+ {
+ Root = null;
+ Count = 0;
+ }
+
+ /// <summary>
+ /// Finds the node whose key is immediately greater than <paramref name="node"/>.
+ /// </summary>
+ /// <param name="node">Node to find the successor of</param>
+ /// <returns>Successor of <paramref name="node"/></returns>
+ 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;
+ }
+
+ /// <summary>
+ /// Finds the node whose key is immediately less than <paramref name="node"/>.
+ /// </summary>
+ /// <param name="node">Node to find the predecessor of</param>
+ /// <returns>Predecessor of <paramref name="node"/></returns>
+ 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;
+ }
+
+ /// <summary>
+ /// Returns the node with the largest key where <paramref name="node"/> is considered the root node.
+ /// </summary>
+ /// <param name="node">Root node</param>
+ /// <returns>Node with the maximum key in the tree of <paramref name="node"/></returns>
+ protected static T Maximum(T node)
+ {
+ T tmp = node;
+ while (tmp.Right != null)
+ {
+ tmp = tmp.Right;
+ }
+
+ return tmp;
+ }
+
+ /// <summary>
+ /// Returns the node with the smallest key where <paramref name="node"/> is considered the root node.
+ /// </summary>
+ /// <param name="node">Root node</param>
+ /// <returns>Node with the minimum key in the tree of <paramref name="node"/></returns>
+ /// <exception cref="ArgumentNullException"><paramref name="node"/> is null</exception>
+ protected static T Minimum(T node)
+ {
+ if (node == null)
+ {
+ throw new ArgumentNullException(nameof(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.
+
+ /// <summary>
+ /// Returns the color of <paramref name="node"/>, or Black if it is null.
+ /// </summary>
+ /// <param name="node">Node</param>
+ /// <returns>The boolean color of <paramref name="node"/>, or black if null</returns>
+ protected static bool ColorOf(T node)
+ {
+ return node == null || node.Color;
+ }
+
+ /// <summary>
+ /// Sets the color of <paramref name="node"/> node to <paramref name="color"/>.
+ /// <br></br>
+ /// This method does nothing if <paramref name="node"/> is null.
+ /// </summary>
+ /// <param name="node">Node to set the color of</param>
+ /// <param name="color">Color (Boolean)</param>
+ protected static void SetColor(T node, bool color)
+ {
+ if (node != null)
+ {
+ node.Color = color;
+ }
+ }
+
+ /// <summary>
+ /// This method returns the left node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
+ /// </summary>
+ /// <param name="node">Node to retrieve the left child from</param>
+ /// <returns>Left child of <paramref name="node"/></returns>
+ protected static T LeftOf(T node)
+ {
+ return node?.Left;
+ }
+
+ /// <summary>
+ /// This method returns the right node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
+ /// </summary>
+ /// <param name="node">Node to retrieve the right child from</param>
+ /// <returns>Right child of <paramref name="node"/></returns>
+ protected static T RightOf(T node)
+ {
+ return node?.Right;
+ }
+
+ /// <summary>
+ /// Returns the parent node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
+ /// </summary>
+ /// <param name="node">Node to retrieve the parent from</param>
+ /// <returns>Parent of <paramref name="node"/></returns>
+ protected static T ParentOf(T node)
+ {
+ return node?.Parent;
+ }
+
+ #endregion
+ }
+}
diff --git a/Ryujinx.Common/Collections/IntrusiveRedBlackTreeNode.cs b/Ryujinx.Common/Collections/IntrusiveRedBlackTreeNode.cs
new file mode 100644
index 00000000..c6c01a06
--- /dev/null
+++ b/Ryujinx.Common/Collections/IntrusiveRedBlackTreeNode.cs
@@ -0,0 +1,16 @@
+namespace Ryujinx.Common.Collections
+{
+ /// <summary>
+ /// Represents a node in the Red-Black Tree.
+ /// </summary>
+ public class IntrusiveRedBlackTreeNode<T> where T : IntrusiveRedBlackTreeNode<T>
+ {
+ public bool Color = true;
+ public T Left;
+ public T Right;
+ public T Parent;
+
+ public T Predecessor => IntrusiveRedBlackTreeImpl<T>.PredecessorOf((T)this);
+ public T Successor => IntrusiveRedBlackTreeImpl<T>.SuccessorOf((T)this);
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.Common/Collections/TreeDictionary.cs b/Ryujinx.Common/Collections/TreeDictionary.cs
index 108fa773..a5a3b818 100644
--- a/Ryujinx.Common/Collections/TreeDictionary.cs
+++ b/Ryujinx.Common/Collections/TreeDictionary.cs
@@ -10,14 +10,8 @@ namespace Ryujinx.Common.Collections
/// </summary>
/// <typeparam name="K">Key</typeparam>
/// <typeparam name="V">Value</typeparam>
- public class TreeDictionary<K, V> : IDictionary<K, V> where K : IComparable<K>
+ public class TreeDictionary<K, V> : IntrusiveRedBlackTreeImpl<Node<K, V>>, IDictionary<K, V> where K : IComparable<K>
{
- private const bool Black = true;
- private const bool Red = false;
- private Node<K, V> _root = null;
- private int _count = 0;
- public TreeDictionary() { }
-
#region Public Methods
/// <summary>
@@ -57,7 +51,7 @@ namespace Ryujinx.Common.Collections
{
throw new ArgumentNullException(nameof(key));
}
- if (null == value)
+ if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
@@ -78,7 +72,7 @@ namespace Ryujinx.Common.Collections
}
if (Delete(key) != null)
{
- _count--;
+ Count--;
}
}
@@ -160,13 +154,12 @@ namespace Ryujinx.Common.Collections
Queue<Node<K, V>> nodes = new Queue<Node<K, V>>();
- if (this._root != null)
+ if (this.Root != null)
{
- nodes.Enqueue(this._root);
+ nodes.Enqueue(this.Root);
}
- while (nodes.Count > 0)
+ while (nodes.TryDequeue(out Node<K, V> node))
{
- Node<K, V> node = nodes.Dequeue();
list.Add(new KeyValuePair<K, V>(node.Key, node.Value));
if (node.Left != null)
{
@@ -188,7 +181,7 @@ namespace Ryujinx.Common.Collections
{
List<KeyValuePair<K, V>> list = new List<KeyValuePair<K, V>>();
- AddToList(_root, list);
+ AddToList(Root, list);
return list;
}
@@ -229,7 +222,7 @@ namespace Ryujinx.Common.Collections
throw new ArgumentNullException(nameof(key));
}
- Node<K, V> node = _root;
+ Node<K, V> node = Root;
while (node != null)
{
int cmp = key.CompareTo(node.Key);
@@ -275,7 +268,7 @@ namespace Ryujinx.Common.Collections
private Node<K, V> BSTInsert(K key, V value)
{
Node<K, V> parent = null;
- Node<K, V> node = _root;
+ Node<K, V> node = Root;
while (node != null)
{
@@ -298,7 +291,7 @@ namespace Ryujinx.Common.Collections
Node<K, V> newNode = new Node<K, V>(key, value, parent);
if (newNode.Parent == null)
{
- _root = newNode;
+ Root = newNode;
}
else if (key.CompareTo(parent.Key) < 0)
{
@@ -308,7 +301,7 @@ namespace Ryujinx.Common.Collections
{
parent.Right = newNode;
}
- _count++;
+ Count++;
return newNode;
}
@@ -344,9 +337,8 @@ namespace Ryujinx.Common.Collections
if (ParentOf(replacementNode) == null)
{
- _root = tmp;
+ Root = tmp;
}
-
else if (replacementNode == LeftOf(ParentOf(replacementNode)))
{
ParentOf(replacementNode).Left = tmp;
@@ -371,43 +363,6 @@ namespace Ryujinx.Common.Collections
}
/// <summary>
- /// Returns the node with the largest key where <paramref name="node"/> is considered the root node.
- /// </summary>
- /// <param name="node">Root Node</param>
- /// <returns>Node with the maximum key in the tree of <paramref name="node"/></returns>
- private static Node<K, V> Maximum(Node<K, V> node)
- {
- Node<K, V> tmp = node;
- while (tmp.Right != null)
- {
- tmp = tmp.Right;
- }
-
- return tmp;
- }
-
- /// <summary>
- /// Returns the node with the smallest key where <paramref name="node"/> is considered the root node.
- /// </summary>
- /// <param name="node">Root Node</param>
- /// <returns>Node with the minimum key in the tree of <paramref name="node"/></returns>
- ///<exception cref="ArgumentNullException"><paramref name="node"/> is null</exception>
- private static Node<K, V> Minimum(Node<K, V> node)
- {
- if (node == null)
- {
- throw new ArgumentNullException(nameof(node));
- }
- Node<K, V> tmp = node;
- while (tmp.Left != null)
- {
- tmp = tmp.Left;
- }
-
- return tmp;
- }
-
- /// <summary>
/// Returns the node whose key immediately less than or equal to <paramref name="key"/>.
/// </summary>
/// <param name="key">Key for which to find the floor node of</param>
@@ -419,7 +374,7 @@ namespace Ryujinx.Common.Collections
{
throw new ArgumentNullException(nameof(key));
}
- Node<K, V> tmp = _root;
+ Node<K, V> tmp = Root;
while (tmp != null)
{
@@ -473,7 +428,7 @@ namespace Ryujinx.Common.Collections
{
throw new ArgumentNullException(nameof(key));
}
- Node<K, V> tmp = _root;
+ Node<K, V> tmp = Root;
while (tmp != null)
{
@@ -515,294 +470,6 @@ namespace Ryujinx.Common.Collections
return null;
}
- /// <summary>
- /// Finds the node with the key is immediately greater than <paramref name="node"/>.
- /// </summary>
- /// <param name="node">Node to find the successor of</param>
- /// <returns>Successor of <paramref name="node"/></returns>
- private static Node<K, V> SuccessorOf(Node<K, V> node)
- {
- if (node.Right != null)
- {
- return Minimum(node.Right);
- }
- Node<K, V> parent = node.Parent;
- while (parent != null && node == parent.Right)
- {
- node = parent;
- parent = parent.Parent;
- }
- return parent;
- }
-
- /// <summary>
- /// Finds the node whose key is immediately less than <paramref name="node"/>.
- /// </summary>
- /// <param name="node">Node to find the predecessor of</param>
- /// <returns>Predecessor of <paramref name="node"/></returns>
- private static Node<K, V> PredecessorOf(Node<K, V> node)
- {
- if (node.Left != null)
- {
- return Maximum(node.Left);
- }
- Node<K, V> parent = node.Parent;
- while (parent != null && node == parent.Left)
- {
- node = parent;
- parent = parent.Parent;
- }
- return parent;
- }
-
- #endregion
-
- #region Private Methods (RBL)
-
- private void RestoreBalanceAfterRemoval(Node<K, V> balanceNode)
- {
- Node<K, V> ptr = balanceNode;
-
- while (ptr != _root && ColorOf(ptr) == Black)
- {
- if (ptr == LeftOf(ParentOf(ptr)))
- {
- Node<K, V> 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
- {
- Node<K, V> 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);
- }
-
- private void RestoreBalanceAfterInsertion(Node<K, V> balanceNode)
- {
- SetColor(balanceNode, Red);
- while (balanceNode != null && balanceNode != _root && ColorOf(ParentOf(balanceNode)) == Red)
- {
- if (ParentOf(balanceNode) == LeftOf(ParentOf(ParentOf(balanceNode))))
- {
- Node<K, V> 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
- {
- Node<K, V> 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);
- }
-
- private void RotateLeft(Node<K, V> node)
- {
- if (node != null)
- {
- Node<K, V> right = RightOf(node);
- node.Right = LeftOf(right);
- if (LeftOf(right) != null)
- {
- LeftOf(right).Parent = node;
- }
- right.Parent = ParentOf(node);
- if (ParentOf(node) == null)
- {
- _root = right;
- }
- else if (node == LeftOf(ParentOf(node)))
- {
- ParentOf(node).Left = right;
- }
- else
- {
- ParentOf(node).Right = right;
- }
- right.Left = node;
- node.Parent = right;
- }
- }
-
- private void RotateRight(Node<K, V> node)
- {
- if (node != null)
- {
- Node<K, V> left = LeftOf(node);
- node.Left = RightOf(left);
- if (RightOf(left) != null)
- {
- RightOf(left).Parent = node;
- }
- left.Parent = node.Parent;
- if (ParentOf(node) == null)
- {
- _root = left;
- }
- else if (node == RightOf(ParentOf(node)))
- {
- ParentOf(node).Right = left;
- }
- else
- {
- ParentOf(node).Left = left;
- }
- left.Right = node;
- node.Parent = left;
- }
- }
- #endregion
-
- #region Safety-Methods
-
- // These methods save memory by allowing us to forego sentinel nil nodes, as well as serve as protection against NullReferenceExceptions.
-
- /// <summary>
- /// Returns the color of <paramref name="node"/>, or Black if it is null.
- /// </summary>
- /// <param name="node">Node</param>
- /// <returns>The boolean color of <paramref name="node"/>, or black if null</returns>
- private static bool ColorOf(Node<K, V> node)
- {
- return node == null || node.Color;
- }
-
- /// <summary>
- /// Sets the color of <paramref name="node"/> node to <paramref name="color"/>.
- /// <br></br>
- /// This method does nothing if <paramref name="node"/> is null.
- /// </summary>
- /// <param name="node">Node to set the color of</param>
- /// <param name="color">Color (Boolean)</param>
- private static void SetColor(Node<K, V> node, bool color)
- {
- if (node != null)
- {
- node.Color = color;
- }
- }
-
- /// <summary>
- /// This method returns the left node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
- /// </summary>
- /// <param name="node">Node to retrieve the left child from</param>
- /// <returns>Left child of <paramref name="node"/></returns>
- private static Node<K, V> LeftOf(Node<K, V> node)
- {
- return node?.Left;
- }
-
- /// <summary>
- /// This method returns the right node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
- /// </summary>
- /// <param name="node">Node to retrieve the right child from</param>
- /// <returns>Right child of <paramref name="node"/></returns>
- private static Node<K, V> RightOf(Node<K, V> node)
- {
- return node?.Right;
- }
-
- /// <summary>
- /// Returns the parent node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
- /// </summary>
- /// <param name="node">Node to retrieve the parent from</param>
- /// <returns>Parent of <paramref name="node"/></returns>
- private static Node<K, V> ParentOf(Node<K, V> node)
- {
- return node?.Parent;
- }
#endregion
#region Interface Implementations
@@ -819,9 +486,9 @@ namespace Ryujinx.Common.Collections
bool IDictionary<K, V>.Remove(K key)
{
- int count = _count;
+ int count = Count;
Remove(key);
- return count > _count;
+ return count > Count;
}
public bool TryGetValue(K key, [MaybeNullWhen(false)] out V value)
@@ -845,12 +512,6 @@ namespace Ryujinx.Common.Collections
Add(item.Key, item.Value);
}
- public void Clear()
- {
- _root = null;
- _count = 0;
- }
-
public bool Contains(KeyValuePair<K, V> item)
{
if (item.Key == null)
@@ -895,9 +556,9 @@ namespace Ryujinx.Common.Collections
if (node.Value.Equals(item.Value))
{
- int count = _count;
+ int count = Count;
Remove(item.Key);
- return count > _count;
+ return count > Count;
}
return false;
@@ -913,8 +574,6 @@ namespace Ryujinx.Common.Collections
return GetKeyValues().GetEnumerator();
}
- public int Count => _count;
-
public ICollection<K> Keys => GetKeyValues().Keys;
public ICollection<V> Values => GetKeyValues().Values;
@@ -928,6 +587,7 @@ namespace Ryujinx.Common.Collections
}
#endregion
+
#region Private Interface Helper Methods
/// <summary>
@@ -938,14 +598,13 @@ namespace Ryujinx.Common.Collections
{
SortedList<K, V> set = new SortedList<K, V>();
Queue<Node<K, V>> queue = new Queue<Node<K, V>>();
- if (_root != null)
+ if (Root != null)
{
- queue.Enqueue(_root);
+ queue.Enqueue(Root);
}
- while (queue.Count > 0)
+ while (queue.TryDequeue(out Node<K, V> node))
{
- Node<K, V> node = queue.Dequeue();
set.Add(node.Key, node.Value);
if (null != node.Left)
{
@@ -959,6 +618,7 @@ namespace Ryujinx.Common.Collections
return set;
}
+
#endregion
}
@@ -967,16 +627,12 @@ namespace Ryujinx.Common.Collections
/// </summary>
/// <typeparam name="K">Key of the node</typeparam>
/// <typeparam name="V">Value of the node</typeparam>
- class Node<K, V>
+ public class Node<K, V> : IntrusiveRedBlackTreeNode<Node<K, V>> where K : IComparable<K>
{
- public bool Color = true;
- public Node<K, V> Left = null;
- public Node<K, V> Right = null;
- public Node<K, V> Parent = null;
- public K Key;
- public V Value;
-
- public Node(K key, V value, Node<K, V> parent)
+ internal K Key;
+ internal V Value;
+
+ internal Node(K key, V value, Node<K, V> parent)
{
Key = key;
Value = value;