// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Text; using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.Shared { internal class NormalizedTextSpanCollection : ReadOnlyCollection { /// /// Initializes a new instance of that is /// empty. /// public NormalizedTextSpanCollection() : base(new List(0)) { } /// /// Initializes a new instance of that contains the specified span. /// /// TextSpan contained by the span set. public NormalizedTextSpanCollection(TextSpan span) : base(ListFromSpan(span)) { } /// /// Initializes a new instance of that contains the specified list of spans. /// /// The spans to be added. /// /// The list of spans will be sorted and normalized (overlapping and adjoining spans will be combined). /// This constructor runs in O(N log N) time, where N = spans.Count. /// is null. public NormalizedTextSpanCollection(IEnumerable spans) : base(NormalizedTextSpanCollection.NormalizeSpans(spans)) { // NormalizeSpans will throw if spans == null. } /// /// Finds the union of two span sets. /// /// /// The first span set. /// /// /// The second span set. /// /// /// The new span set that corresponds to the union of and . /// /// This operator runs in O(N+M) time where N = left.Count, M = right.Count. /// Either or is null. public static NormalizedTextSpanCollection Union(NormalizedTextSpanCollection left, NormalizedTextSpanCollection right) { if (left == null) { throw new ArgumentNullException(nameof(left)); } if (right == null) { throw new ArgumentNullException(nameof(right)); } if (left.Count == 0) { return right; } if (right.Count == 0) { return left; } OrderedSpanList spans = new OrderedSpanList(); int index1 = 0; int index2 = 0; int start = -1; int end = int.MaxValue; while ((index1 < left.Count) && (index2 < right.Count)) { TextSpan span1 = left[index1]; TextSpan span2 = right[index2]; if (span1.Start < span2.Start) { NormalizedTextSpanCollection.UpdateSpanUnion(span1, spans, ref start, ref end); ++index1; } else { NormalizedTextSpanCollection.UpdateSpanUnion(span2, spans, ref start, ref end); ++index2; } } while (index1 < left.Count) { NormalizedTextSpanCollection.UpdateSpanUnion(left[index1], spans, ref start, ref end); ++index1; } while (index2 < right.Count) { NormalizedTextSpanCollection.UpdateSpanUnion(right[index2], spans, ref start, ref end); ++index2; } if (end != int.MaxValue) { spans.Add(TextSpan.FromBounds(start, end)); } return new NormalizedTextSpanCollection(spans); } /// /// Finds the overlap of two span sets. /// /// The first span set. /// The second span set. /// The new span set that corresponds to the overlap of and . /// This operator runs in O(N+M) time where N = left.Count, M = right.Count. /// or is null. public static NormalizedTextSpanCollection Overlap(NormalizedTextSpanCollection left, NormalizedTextSpanCollection right) { if (left == null) { throw new ArgumentNullException(nameof(left)); } if (right == null) { throw new ArgumentNullException(nameof(right)); } if (left.Count == 0) { return left; } if (right.Count == 0) { return right; } OrderedSpanList spans = new OrderedSpanList(); for (int index1 = 0, index2 = 0; (index1 < left.Count) && (index2 < right.Count);) { TextSpan span1 = left[index1]; TextSpan span2 = right[index2]; if (span1.OverlapsWith(span2)) { spans.Add(span1.Overlap(span2).Value); } if (span1.End < span2.End) { ++index1; } else if (span1.End == span2.End) { ++index1; ++index2; } else { ++index2; } } return new NormalizedTextSpanCollection(spans); } /// /// Finds the intersection of two span sets. /// /// The first span set. /// The second span set. /// The new span set that corresponds to the intersection of and . /// This operator runs in O(N+M) time where N = left.Count, M = right.Count. /// is null. /// is null. public static NormalizedTextSpanCollection Intersection(NormalizedTextSpanCollection left, NormalizedTextSpanCollection right) { if (left == null) { throw new ArgumentNullException(nameof(left)); } if (right == null) { throw new ArgumentNullException(nameof(right)); } if (left.Count == 0) { return left; } if (right.Count == 0) { return right; } OrderedSpanList spans = new OrderedSpanList(); for (int index1 = 0, index2 = 0; (index1 < left.Count) && (index2 < right.Count);) { TextSpan span1 = left[index1]; TextSpan span2 = right[index2]; if (span1.IntersectsWith(span2)) { spans.Add(span1.Intersection(span2).Value); } if (span1.End < span2.End) { ++index1; } else { ++index2; } } return new NormalizedTextSpanCollection(spans); } /// /// Finds the difference between two sets. The difference is defined as everything in the first span set that is not in the second span set. /// /// The first span set. /// The second span set. /// The new span set that corresponds to the difference between and . /// /// Empty spans in the second set do not affect the first set at all. This method returns empty spans in the first set that are not contained by any set in /// the second set. /// /// is null. /// is null. public static NormalizedTextSpanCollection Difference(NormalizedTextSpanCollection left, NormalizedTextSpanCollection right) { if (left == null) { throw new ArgumentNullException(nameof(left)); } if (right == null) { throw new ArgumentNullException(nameof(right)); } if (left.Count == 0) { return left; } if (right.Count == 0) { return left; } OrderedSpanList spans = new OrderedSpanList(); int index1 = 0; int index2 = 0; int lastEnd = -1; do { TextSpan span1 = left[index1]; TextSpan span2 = right[index2]; if ((span2.Length == 0) || (span1.Start >= span2.End)) { ++index2; } else if (span1.End <= span2.Start) { // lastEnd is set to the end of the previously encountered intersecting span // from right when it ended before the end of span1 (so it must still be less // than the end of span1). Debug.Assert(lastEnd < span1.End); spans.Add(TextSpan.FromBounds(Math.Max(lastEnd, span1.Start), span1.End)); ++index1; } else { // The spans intersect, so add anything from span1 that extends to the left of span2. if (span1.Start < span2.Start) { // lastEnd is set to the end of the previously encountered intersecting span // on span2, so it must be less than the start of the current span on span2. Debug.Assert(lastEnd < span2.Start); spans.Add(TextSpan.FromBounds(Math.Max(lastEnd, span1.Start), span2.Start)); } if (span1.End < span2.End) { ++index1; } else if (span1.End == span2.End) { // Both spans ended at the same place so we're done with both. ++index1; ++index2; } else { // span2 ends before span1, so keep track of where it ended so that we don't // try to add the excluded portion the next time we add a span. lastEnd = span2.End; ++index2; } } } while ((index1 < left.Count) && (index2 < right.Count)); while (index1 < left.Count) { TextSpan span1 = left[index1++]; spans.Add(TextSpan.FromBounds(Math.Max(lastEnd, span1.Start), span1.End)); } return new NormalizedTextSpanCollection(spans); } /// /// Determines whether two span sets are the same. /// /// The first set. /// The second set. /// true if the two sets are equivalent, otherwise false. public static bool operator ==(NormalizedTextSpanCollection left, NormalizedTextSpanCollection right) { if (object.ReferenceEquals(left, right)) { return true; } if (object.ReferenceEquals(left, null) || object.ReferenceEquals(right, null)) { return false; } if (left.Count != right.Count) { return false; } for (int i = 0; i < left.Count; ++i) { if (left[i] != right[i]) { return false; } } return true; } /// /// Determines whether two span sets are not the same. /// /// The first set. /// The second set. /// true if the two sets are not equivalent, otherwise false. public static bool operator !=(NormalizedTextSpanCollection left, NormalizedTextSpanCollection right) { return !(left == right); } /// /// Determines whether this span set overlaps with another span set. /// /// The span set to test. /// true if the span sets overlap, otherwise false. /// is null. public bool OverlapsWith(NormalizedTextSpanCollection set) { if (set == null) { throw new ArgumentNullException(nameof(set)); } for (int index1 = 0, index2 = 0; (index1 < this.Count) && (index2 < set.Count);) { TextSpan span1 = this[index1]; TextSpan span2 = set[index2]; if (span1.OverlapsWith(span2)) { return true; } if (span1.End < span2.End) { ++index1; } else if (span1.End == span2.End) { ++index1; ++index2; } else { ++index2; } } return false; } /// /// Determines whether this span set overlaps with another span. /// /// The span to test. /// true if this span set overlaps with the given span, otherwise false. public bool OverlapsWith(TextSpan span) { // TODO: binary search for (int index = 0; index < this.Count; ++index) { if (this[index].OverlapsWith(span)) { return true; } } return false; } /// /// Determines whether this span set intersects with another span set. /// /// Set to test. /// true if the span sets intersect, otherwise false. /// is null. public bool IntersectsWith(NormalizedTextSpanCollection set) { if (set == null) { throw new ArgumentNullException(nameof(set)); } for (int index1 = 0, index2 = 0; (index1 < this.Count) && (index2 < set.Count);) { TextSpan span1 = this[index1]; TextSpan span2 = set[index2]; if (span1.IntersectsWith(span2)) { return true; } if (span1.End < span2.End) { ++index1; } else { ++index2; } } return false; } /// /// Determines whether this span set intersects with another span. /// /// true if this span set intersects with the given span, otherwise false. public bool IntersectsWith(TextSpan span) { // TODO: binary search for (int index = 0; index < this.Count; ++index) { if (this[index].IntersectsWith(span)) { return true; } } return false; } #region Overridden methods and operators /// /// Gets a unique hash code for the span set. /// /// A 32-bit hash code associated with the set. public override int GetHashCode() { int hc = 0; foreach (TextSpan s in this) { hc ^= s.GetHashCode(); } return hc; } /// /// Determines whether this span set is the same as another object. /// /// The object to test. /// true if the two objects are equal, otherwise false. public override bool Equals(object obj) { NormalizedTextSpanCollection set = obj as NormalizedTextSpanCollection; return this == set; } /// /// Provides a string representation of the set. /// /// The string representation of the set. public override string ToString() { StringBuilder value = new StringBuilder("{"); foreach (TextSpan s in this) { value.Append(s.ToString()); } value.Append("}"); return value.ToString(); } #endregion // Overridden methods and operators #region Private Helpers private static IList ListFromSpan(TextSpan span) { IList list = new List(1); list.Add(span); return list; } /// /// Private constructor for use when the span list is already normalized. /// /// An already normalized span list. private NormalizedTextSpanCollection(OrderedSpanList normalizedSpans) : base(normalizedSpans) { } private static void UpdateSpanUnion(TextSpan span, IList spans, ref int start, ref int end) { if (end < span.Start) { spans.Add(TextSpan.FromBounds(start, end)); start = -1; end = int.MaxValue; } if (end == int.MaxValue) { start = span.Start; end = span.End; } else { end = Math.Max(end, span.End); } } private static IList NormalizeSpans(IEnumerable spans) { if (spans == null) { throw new ArgumentNullException(nameof(spans)); } List sorted = new List(spans); if (sorted.Count <= 1) { return sorted; } else { sorted.Sort(delegate (TextSpan s1, TextSpan s2) { return s1.Start.CompareTo(s2.Start); }); IList normalized = new List(sorted.Count); int oldStart = sorted[0].Start; int oldEnd = sorted[0].End; for (int i = 1; i < sorted.Count; ++i) { int newStart = sorted[i].Start; int newEnd = sorted[i].End; if (oldEnd < newStart) { normalized.Add(TextSpan.FromBounds(oldStart, oldEnd)); oldStart = newStart; oldEnd = newEnd; } else { oldEnd = Math.Max(oldEnd, newEnd); } } normalized.Add(TextSpan.FromBounds(oldStart, oldEnd)); return normalized; } } private class OrderedSpanList : List { } #endregion // Private Helpers } }