Exemplo n.º 1
0
        /// <summary>Determines whether the composite range has intersection with another range.</summary>
        /// <typeparam name="T">The type of the range values.</typeparam>
        /// <typeparam name="TCompositeRange">The type of another range.</typeparam>
        /// <param name="compositeRange">The source range.</param>
        /// <param name="other">The range to check.</param>
        /// <returns><c>true</c>, if the composite range has intersection with another range.</returns>
        public static bool HasIntersection <T, TCompositeRange>(
            this CompositeRange <T> compositeRange, TCompositeRange other)
            where TCompositeRange : ICompositeRange <T>
        {
            if (compositeRange.IsEmpty && other.IsEmpty)
            {
                return(true);
            }
            if (!compositeRange.ContainingRange.HasIntersection(other.ContainingRange))
            {
                return(false);
            }

            bool result = false;

            using (var containingRanges = compositeRange.GetMergedRanges().GetEnumerator())
            {
                bool hasContainingRange = containingRanges.MoveNext();
                foreach (var otherRange in other.GetMergedRanges())
                {
                    while (hasContainingRange && containingRanges.Current.EndsBefore(otherRange))
                    {
                        hasContainingRange = containingRanges.MoveNext();
                    }

                    if (!hasContainingRange || containingRanges.Current.HasIntersection(otherRange))
                    {
                        result = hasContainingRange;
                        break;
                    }
                }
            }

            return(result);
        }
        public CompositeRange <T> Intersect <TKey2>(CompositeRange <T, TKey2> other)
        {
            if (IsEmpty)
            {
                return(this);
            }
            if (other.IsEmpty || !ContainingRange.HasIntersection(other.ContainingRange))
            {
                return(Empty);
            }

            var intersectionResult = new List <Range <T> >(SubRanges.Count);

            var rangesToIntersect = new List <Range <T> >(SubRanges);

            foreach (var otherRange in other.GetMergedRanges())
            {
                for (var i = 0; i < rangesToIntersect.Count; i++)
                {
                    var intersectionRange = rangesToIntersect[i];
                    if (intersectionRange.StartsAfter(otherRange))
                    {
                        break;
                    }

                    intersectionRange = intersectionRange.Intersect(otherRange);
                    if (intersectionRange.IsEmpty)
                    {
                        rangesToIntersect.RemoveAt(i);
                        i--;
                    }
                    else
                    {
                        intersectionResult.Add(intersectionRange);
                    }
                }

                if (rangesToIntersect.Count == 0)
                {
                    break;
                }
            }

            CompositeRange <T> result;

            if (intersectionResult.Count == 0)
            {
                result = Empty;
            }
            else
            {
                var overload = IsMerged
                                        ? UnsafeOverload.NoEmptyRangesAlreadySortedAndMerged
                                        : UnsafeOverload.RangesAlreadySorted;

                result = new CompositeRange <T>(intersectionResult, overload);
            }

            return(result);
        }
        public bool HasIntersection <TKey2>(CompositeRange <T, TKey2> other)
        {
            if (IsEmpty && other.IsEmpty)
            {
                return(true);
            }
            if (!ContainingRange.HasIntersection(other.ContainingRange))
            {
                return(false);
            }

            var result = false;

            using (var containingRanges = GetMergedRanges().GetEnumerator())
            {
                var hasContainingRange = containingRanges.MoveNext();
                foreach (var otherRange in other.GetMergedRanges())
                {
                    while (hasContainingRange && containingRanges.Current.EndsBefore(otherRange))
                    {
                        hasContainingRange = containingRanges.MoveNext();
                    }

                    if (!hasContainingRange || containingRanges.Current.HasIntersection(otherRange))
                    {
                        result = hasContainingRange;
                        break;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>Determines whether the composite range contains another range.</summary>
        /// <typeparam name="T">The type of the range values.</typeparam>
        /// <param name="compositeRange">The source range.</param>
        /// <param name="other">The range to check.</param>
        /// <returns><c>true</c>, if the composite range contains another range.</returns>
        public static bool Contains <T>(
            this CompositeRange <T> compositeRange,

            #region T4-dont-replace
            Range <T> other
            #endregion

            ) =>
        compositeRange.ContainingRange.Contains(other) &&
        compositeRange.GetMergedRanges().Any(r => r.Contains(other));
        public bool Contains(

            #region T4-dont-replace
            CompositeRange <T> other
            #endregion

            )
        {
            if (IsEmpty && other.IsEmpty)
            {
                return(true);
            }
            if (!ContainingRange.Contains(other.ContainingRange))
            {
                return(false);
            }

            var result = true;

            using (var containingRanges = GetMergedRanges().GetEnumerator())
            {
                var hasContainingRange = containingRanges.MoveNext();
                foreach (var otherRange in other.GetMergedRanges())
                {
                    while (hasContainingRange && containingRanges.Current.EndsBefore(otherRange))
                    {
                        hasContainingRange = containingRanges.MoveNext();
                    }

                    if (!hasContainingRange || !containingRanges.Current.Contains(otherRange))
                    {
                        result = false;
                        break;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 6
0
 /// <summary>Determines whether the composite range contains another range.</summary>
 /// <typeparam name="T">The type of the range values.</typeparam>
 /// <typeparam name="TKey2">The type of the other range key</typeparam>
 /// <param name="compositeRange">The source range.</param>
 /// <param name="other">The range to check.</param>
 /// <returns><c>true</c>, if the composite range contains another range.</returns>
 public static bool Contains <T, TKey2>(
     this CompositeRange <T> compositeRange, Range <T, TKey2> other) =>
 compositeRange.ContainingRange.Contains(other) &&
 compositeRange.GetMergedRanges().Any(r => r.Contains(other));