diff --git a/src/Workspaces/Core/Portable/Formatting/ContextIntervalTree.cs b/src/Workspaces/Core/Portable/Formatting/ContextIntervalTree.cs index ae265776427c23b391653f2b541b8b0fbc57737e..7e8ab62feea4a02223cebbc461c0b561e67b4793 100644 --- a/src/Workspaces/Core/Portable/Formatting/ContextIntervalTree.cs +++ b/src/Workspaces/Core/Portable/Formatting/ContextIntervalTree.cs @@ -32,7 +32,7 @@ public ContextIntervalTree(IIntervalIntrospector introspector) public void AddIntervalInPlace(T value) { var newNode = new Node(Introspector, value); - this.root = Insert(root, newNode, Introspector, inPlace: true); + this.root = Insert(root, newNode, Introspector); } public T GetSmallestEdgeExclusivelyContainingInterval(int start, int length) diff --git a/src/Workspaces/Core/Portable/Shared/Collections/IntervalTree`1.Node.cs b/src/Workspaces/Core/Portable/Shared/Collections/IntervalTree`1.Node.cs index d34484c3da78dbeccc6f7e4bb2d68c07c6eb1704..b4c3be6731b03416c57bf0fd6150b6641005b274 100644 --- a/src/Workspaces/Core/Portable/Shared/Collections/IntervalTree`1.Node.cs +++ b/src/Workspaces/Core/Portable/Shared/Collections/IntervalTree`1.Node.cs @@ -61,11 +61,6 @@ internal void SetLeftRight(Node left, Node right, IIntervalIntrospector intro } } - internal Node With(Node left, Node right, IIntervalIntrospector introspector) - { - return new Node(introspector, this.Value, left, right); - } - // Sample: // 1 2 // / \ / \ @@ -74,25 +69,13 @@ internal Node With(Node left, Node right, IIntervalIntrospector introspector) // 3 c a b c d // / \ // a b - internal Node RightRotation(bool inPlace, IIntervalIntrospector introspector) + internal Node RightRotation(IIntervalIntrospector introspector) { - if (inPlace) - { - var oldLeft = this.Left; - this.SetLeftRight(this.Left.Right, this.Right, introspector); - oldLeft.SetLeftRight(oldLeft.Left, this, introspector); + var oldLeft = this.Left; + this.SetLeftRight(this.Left.Right, this.Right, introspector); + oldLeft.SetLeftRight(oldLeft.Left, this, introspector); - return oldLeft; - } - else - { - var newLeft = this.Left.Left; - var newRight = this.With( - this.Left.Right, - this.Right, - introspector); - return this.Left.With(newLeft, newRight, introspector); - } + return oldLeft; } // Sample: @@ -103,24 +86,12 @@ internal Node RightRotation(bool inPlace, IIntervalIntrospector introspector) // b 3 a b c d // / \ // c d - internal Node LeftRotation(bool inPlace, IIntervalIntrospector introspector) + internal Node LeftRotation(IIntervalIntrospector introspector) { - if (inPlace) - { - var oldRight = this.Right; - this.SetLeftRight(this.Left, this.Right.Left, introspector); - oldRight.SetLeftRight(this, oldRight.Right, introspector); - return oldRight; - } - else - { - var newLeft = this.With( - this.Left, - this.Right.Left, - introspector); - var newRight = this.Right.Right; - return this.Right.With(newLeft, newRight, introspector); - } + var oldRight = this.Right; + this.SetLeftRight(this.Left, this.Right.Left, introspector); + oldRight.SetLeftRight(this, oldRight.Right, introspector); + return oldRight; } // Sample: @@ -131,27 +102,16 @@ internal Node LeftRotation(bool inPlace, IIntervalIntrospector introspector) // 3 d b 2 a b c d // / \ / \ // b c c d - internal Node InnerRightOuterLeftRotation(bool inPlace, IIntervalIntrospector introspector) + internal Node InnerRightOuterLeftRotation(IIntervalIntrospector introspector) { - if (inPlace) - { - var newTop = this.Right.Left; - var oldRight = this.Right; + var newTop = this.Right.Left; + var oldRight = this.Right; - this.SetLeftRight(this.Left, this.Right.Left.Left, introspector); - oldRight.SetLeftRight(oldRight.Left.Right, oldRight.Right, introspector); - newTop.SetLeftRight(this, oldRight, introspector); + this.SetLeftRight(this.Left, this.Right.Left.Left, introspector); + oldRight.SetLeftRight(oldRight.Left.Right, oldRight.Right, introspector); + newTop.SetLeftRight(this, oldRight, introspector); - return newTop; - } - else - { - var temp = this.With( - this.Left, - this.Right.RightRotation(inPlace, introspector), - introspector); - return temp.LeftRotation(inPlace, introspector); - } + return newTop; } // Sample: @@ -162,27 +122,16 @@ internal Node InnerRightOuterLeftRotation(bool inPlace, IIntervalIntrospector // a 3 2 c a b c d // / \ / \ // b c a b - internal Node InnerLeftOuterRightRotation(bool inPlace, IIntervalIntrospector introspector) + internal Node InnerLeftOuterRightRotation(IIntervalIntrospector introspector) { - if (inPlace) - { - var newTop = this.Left.Right; - var oldLeft = this.Left; + var newTop = this.Left.Right; + var oldLeft = this.Left; - this.SetLeftRight(this.Left.Right.Right, this.Right, introspector); - oldLeft.SetLeftRight(oldLeft.Left, oldLeft.Right.Left, introspector); - newTop.SetLeftRight(oldLeft, this, introspector); + this.SetLeftRight(this.Left.Right.Right, this.Right, introspector); + oldLeft.SetLeftRight(oldLeft.Left, oldLeft.Right.Left, introspector); + newTop.SetLeftRight(oldLeft, this, introspector); - return newTop; - } - else - { - var temp = this.With( - this.Left.LeftRotation(inPlace, introspector), - this.Right, - introspector); - return temp.RightRotation(inPlace, introspector); - } + return newTop; } } } diff --git a/src/Workspaces/Core/Portable/Shared/Collections/IntervalTree`1.cs b/src/Workspaces/Core/Portable/Shared/Collections/IntervalTree`1.cs index 4cc93fea84127277e3fa2573211f8ad975f72828..351da71aaec2cde6aafaff5aa7f2f1cb161a4b55 100644 --- a/src/Workspaces/Core/Portable/Shared/Collections/IntervalTree`1.cs +++ b/src/Workspaces/Core/Portable/Shared/Collections/IntervalTree`1.cs @@ -38,7 +38,7 @@ public IntervalTree(IIntervalIntrospector introspector, IEnumerable values { foreach (var value in values) { - root = Insert(root, new Node(introspector, value), introspector, inPlace: true); + root = Insert(root, new Node(introspector, value), introspector); } } @@ -223,13 +223,13 @@ public bool IsEmpty() return this.root == null; } - protected static Node Insert(Node root, Node newNode, IIntervalIntrospector introspector, bool inPlace) + protected static Node Insert(Node root, Node newNode, IIntervalIntrospector introspector) { var newNodeStart = introspector.GetStart(newNode.Value); - return Insert(root, newNode, newNodeStart, introspector, inPlace); + return Insert(root, newNode, newNodeStart, introspector); } - private static Node Insert(Node root, Node newNode, int newNodeStart, IIntervalIntrospector introspector, bool inPlace) + private static Node Insert(Node root, Node newNode, int newNodeStart, IIntervalIntrospector introspector) { if (root == null) { @@ -240,30 +240,22 @@ private static Node Insert(Node root, Node newNode, int newNodeStart, IIntervalI if (newNodeStart < introspector.GetStart(root.Value)) { - newLeft = Insert(root.Left, newNode, newNodeStart, introspector, inPlace); + newLeft = Insert(root.Left, newNode, newNodeStart, introspector); newRight = root.Right; } else { newLeft = root.Left; - newRight = Insert(root.Right, newNode, newNodeStart, introspector, inPlace); + newRight = Insert(root.Right, newNode, newNodeStart, introspector); } - Node newRoot; - if (inPlace) - { - root.SetLeftRight(newLeft, newRight, introspector); - newRoot = root; - } - else - { - newRoot = new Node(introspector, root.Value, newLeft, newRight); - } + root.SetLeftRight(newLeft, newRight, introspector); + var newRoot = root; - return Balance(newRoot, inPlace, introspector); + return Balance(newRoot, introspector); } - private static Node Balance(Node node, bool inPlace, IIntervalIntrospector introspector) + private static Node Balance(Node node, IIntervalIntrospector introspector) { int balanceFactor = BalanceFactor(node); if (balanceFactor == -2) @@ -271,12 +263,12 @@ private static Node Balance(Node node, bool inPlace, IIntervalIntrospector in int rightBalance = BalanceFactor(node.Right); if (rightBalance == -1) { - return node.LeftRotation(inPlace, introspector); + return node.LeftRotation(introspector); } else { Contract.Requires(rightBalance == 1); - return node.InnerRightOuterLeftRotation(inPlace, introspector); + return node.InnerRightOuterLeftRotation(introspector); } } else if (balanceFactor == 2) @@ -284,12 +276,12 @@ private static Node Balance(Node node, bool inPlace, IIntervalIntrospector in int leftBalance = BalanceFactor(node.Left); if (leftBalance == 1) { - return node.RightRotation(inPlace, introspector); + return node.RightRotation(introspector); } else { Contract.Requires(leftBalance == -1); - return node.InnerLeftOuterRightRotation(inPlace, introspector); + return node.InnerLeftOuterRightRotation(introspector); } } diff --git a/src/Workspaces/Core/Portable/Shared/Collections/SimpleIntervalTree`1.cs b/src/Workspaces/Core/Portable/Shared/Collections/SimpleIntervalTree`1.cs index 4b2c24b81e58f17ab5d71e48f95f56a2343cc7af..1be48a7cd7ec44213078f2ec26410b3f11d242b8 100644 --- a/src/Workspaces/Core/Portable/Shared/Collections/SimpleIntervalTree`1.cs +++ b/src/Workspaces/Core/Portable/Shared/Collections/SimpleIntervalTree`1.cs @@ -32,7 +32,7 @@ public SimpleIntervalTree(IIntervalIntrospector introspector, IEnumerable { foreach (var value in values) { - root = Insert(root, new Node(introspector, value), introspector, inPlace: true); + root = Insert(root, new Node(introspector, value), introspector); } } }