diff options
Diffstat (limited to 'Ryujinx.Common/Collections/IntervalTree.cs')
-rw-r--r-- | Ryujinx.Common/Collections/IntervalTree.cs | 331 |
1 files changed, 23 insertions, 308 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; |