Beispiel #1
0
        private static bool IsContinuationFor(RangeBoundaryTo <T> lastBoundary, Range <T, TKey> nextRange)
        {
            DebugCode.BugIf(lastBoundary.IsEmpty || nextRange.IsEmpty, "No empty ranges expected.");

            return(lastBoundary.IsPositiveInfinity ||
                   lastBoundary.GetComplementation() >= nextRange.From);
        }
Beispiel #2
0
        /// <summary>Creates instance of <seealso cref="Range{T}"/></summary>
        /// <param name="from">Boundary From.</param>
        /// <param name="to">Boundary To.</param>
        public Range(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to)
        {
            bool fromEmpty = from.IsEmpty;
            bool toEmpty   = to.IsEmpty;

            if (fromEmpty != toEmpty)
            {
                throw CodeExceptions.Argument(
                          nameof(from),
                          $"Both {nameof(from)} and {nameof(to)} args should be either empty or non-empty.");
            }

            if (!fromEmpty)
            {
                if (to < from)
                {
                    throw CodeExceptions.Argument(
                              nameof(to),
                              $"Invalid range {from.ToInvariantString()}..{to.ToInvariantString()}.");
                }
            }

            _from = from;
            _to   = to;
        }
Beispiel #3
0
        public static void TestRangeEquality()
        {
            int?value1 = 1;
            int?value2 = 2;
            int?empty  = null;
            var key    = "Hello!";
            var key2   = "Hello2!";

            var eFrom = new RangeBoundaryFrom <int?>();
            var eTo   = new RangeBoundaryTo <int?>();

            AreEqual(Range <int?> .Empty, Range.Create(eFrom, eTo));
            AreEqual(Range <int?> .Infinite, Range.Create(empty, empty));
            AreNotEqual(Range <double?> .Infinite, Range.Create(empty, empty));

            AreEqual(
                Range.CreateExclusiveFrom(value1, value2).From,
                Range.BoundaryFromExclusive(value1));

            AreEqual(
                Range.CreateExclusiveFrom(value1, value2).To,
                Range.BoundaryTo(value2));

            AreNotEqual(
                Range.CreateExclusiveFrom(value1, value2),
                Range.Create(value1, value2));

            IsTrue(Range.Create(value1, value2) == Range.Create <int?>(1, 2));
            IsTrue(Range.Create(value1, value2) != Range.Create(value1, 1));
            IsTrue(Range.Create(value1, value2, key) == Range.Create <int?, string>(1, 2, key));
            IsTrue(Range.Create(value1, value2, key) != Range.Create <int?, string>(1, 2, key2));
            IsTrue(Range.Create(value1, value2, key) != Range.Create <int?, string>(1, 1, key));
        }
Beispiel #4
0
        /// <summary>Creates instance of <seealso cref="Range{T}"/></summary>
        /// <param name="from">Boundary From.</param>
        /// <param name="to">Boundary To.</param>
        /// <param name="key">The value of the range key.</param>
        public Range(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to, TKey key)
        {
            bool fromEmpty = from.IsEmpty;
            bool toEmpty   = to.IsEmpty;

            if (fromEmpty != toEmpty)
            {
                throw CodeExceptions.Argument(
                          nameof(from),
                          $"Both {nameof(from)} and {nameof(to)} args should be either empty or non-empty.");
            }

            if (!fromEmpty)
            {
                if (to < from)
                {
                    throw CodeExceptions.Argument(
                              nameof(to),
                              $"The boundary {nameof(to)} should be greater than or equal to boundary {nameof(from)}.");
                }
            }

            _from = from;
            _to   = to;
            _key  = key;
        }
Beispiel #5
0
 private static Range <T> TryCreateCore <T>(
     T from, RangeBoundaryFromKind fromKind,
     T to, RangeBoundaryToKind toKind) =>
 RangeBoundaryFrom <T> .IsValid(from) && RangeBoundaryTo <T> .IsValid(to)
                                 ? TryCreate(
     RangeBoundaryFrom <T> .AdjustAndCreate(from, fromKind),
     RangeBoundaryTo <T> .AdjustAndCreate(to, toKind))
                                 : Range <T> .Empty;
Beispiel #6
0
 public static bool Contains <T>(this Range <T> range, RangeBoundaryTo <T> other)
 {
     if (range.IsEmpty)
     {
         return(other.IsEmpty);
     }
     return(other.IsNotEmpty && range.From <= other && range.To >= other);
 }
 public bool Contains(RangeBoundaryTo <T> other)
 {
     if (IsEmpty)
     {
         return(other.IsEmpty);
     }
     return(other.IsNotEmpty && From <= other && To >= other);
 }
        public static void TestBoundaryToProperties()
        {
            int?value1 = 1;
            int?value2 = 2;
            int?empty  = null;

            var a = new RangeBoundaryTo <int?>();

            AreEqual(a.Kind, RangeBoundaryToKind.Empty);
            AreEqual(a.GetValueOrDefault(), empty);
            Throws <InvalidOperationException>(() => a.Value.ToString());

            IsTrue(a.IsEmpty);
            IsFalse(a.IsNotEmpty);
            IsFalse(a.HasValue);
            IsFalse(a.IsPositiveInfinity);
            IsFalse(a.IsInclusiveBoundary);
            IsFalse(a.IsExclusiveBoundary);

            a = Range.BoundaryToInfinity <int?>();
            AreEqual(a.Kind, RangeBoundaryToKind.Infinite);
            AreEqual(a.GetValueOrDefault(), empty);
            Throws <InvalidOperationException>(() => a.Value.ToString());

            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsFalse(a.HasValue);
            IsTrue(a.IsPositiveInfinity);
            IsFalse(a.IsInclusiveBoundary);
            IsFalse(a.IsExclusiveBoundary);

            a = Range.BoundaryTo(value1);
            AreEqual(a.Kind, RangeBoundaryToKind.Inclusive);
            AreEqual(a.Value, value1);
            AreNotEqual(a.Value, value2);

            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.HasValue);
            IsFalse(a.IsPositiveInfinity);
            IsTrue(a.IsInclusiveBoundary);
            IsFalse(a.IsExclusiveBoundary);

            a = Range.BoundaryToExclusive(value1);
            AreEqual(a.Kind, RangeBoundaryToKind.Exclusive);
            AreEqual(a.Value, value1);
            AreNotEqual(a.Value, value2);

            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.HasValue);
            IsFalse(a.IsPositiveInfinity);
            IsFalse(a.IsInclusiveBoundary);
            IsTrue(a.IsExclusiveBoundary);
        }
Beispiel #9
0
        public static Range <T> ExtendTo <T>(this Range <T> range, RangeBoundaryTo <T> to)
        {
            if (range.IsEmpty || to.IsEmpty)
            {
                return(range);
            }

            return(range.To >= to
                                ? range
                                : range.TryCreateRange(range.From, to));
        }
        public Range <T, TKey> ExtendTo(RangeBoundaryTo <T> to)
        {
            if (IsEmpty || to.IsEmpty)
            {
                return(this);
            }

            return(To >= to
                                ? this
                                : TryCreateRange(From, to));
        }
Beispiel #11
0
        private static Range <T> TryCreateCore <T>(
            T from, RangeBoundaryFromKind fromKind,
            T to, RangeBoundaryToKind toKind) =>
        IsValid(from, to)
#pragma warning disable 618 // Validation not required: IsValid() called.
                                        ? new Range <T>(
            RangeBoundaryFrom <T> .AdjustAndCreate(from, fromKind),
            RangeBoundaryTo <T> .AdjustAndCreate(to, toKind),
            SkipsArgValidation)
#pragma warning restore 618
                                        : Range <T> .Empty;
        public CompositeRange <T> ExtendTo(RangeBoundaryTo <T> to)
        {
            if (IsEmpty || to.IsEmpty || to <= ContainingRange.To)
            {
                return(this);
            }

            var ranges = SubRanges.ToArray();

            for (var i = ranges.Length - 1; i >= 0; i--)
            {
                if (ranges[i].To != ContainingRange.To)
                {
                    break;
                }

                ranges[i] = ranges[i].ExtendTo(to);
            }
            return(new CompositeRange <T>(ranges, UnsafeOverload.RangesAlreadySorted));
        }
        public static void TestBoundaryToEquality()
        {
            int?value1 = 1;
            int?value2 = 2;
            int?empty  = null;

            var e   = new RangeBoundaryTo <int?>();
            var inf = Range.BoundaryTo(empty);
            var a1  = Range.BoundaryTo(value1);
            var a12 = Range.BoundaryTo(value1);
            var a2  = Range.BoundaryTo(value2);
            var b1  = Range.BoundaryFrom(value1);

            AreEqual(e, RangeBoundaryTo <int?> .Empty);
            IsTrue(e == RangeBoundaryTo <int?> .Empty);
            IsFalse(e != RangeBoundaryTo <int?> .Empty);

            AreEqual(inf, RangeBoundaryTo <int?> .PositiveInfinity);
            IsTrue(inf == RangeBoundaryTo <int?> .PositiveInfinity);

            AreNotEqual(a1, empty);
            AreNotEqual(a1, inf);

            AreEqual(a1, a12);
            IsTrue(a1 == a12);
            IsFalse(a1 != a12);

            AreNotEqual(a1, a2);
            IsFalse(a1 == a2);
            IsTrue(a1 != a2);

            AreEqual(a1.Value, value1);
            AreNotEqual(a1.Value, value2);
            AreNotEqual(a1, value1);
            AreNotEqual(a1, value2);

            AreNotEqual(a1, b1);
            AreEqual(b1.Value, value1);
        }
Beispiel #14
0
 public static bool EndsBefore <T>(this Range <T> range, T value) =>
 range.IsNotEmpty && RangeBoundaryTo <T> .IsValid(value) && range.To < Range.BoundaryTo(value);
Beispiel #15
0
 public static bool EndsBefore <T>(this Range <T> range, RangeBoundaryTo <T> other) =>
 range.IsNotEmpty && other.IsNotEmpty && range.To < other;
Beispiel #16
0
 Range <T, TKey> IRangeFactory <T, Range <T, TKey> > .TryCreateRange(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to) =>
 Range.TryCreate(from, to, _key);
Beispiel #17
0
 public static bool StartsAfter <T>(this Range <T> range, RangeBoundaryTo <T> other) =>
 other.IsNotEmpty && range.From > other;
Beispiel #18
0
        internal Range(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to, UnsafeOverload skipsArgValidation)
#if DEBUG
            : this(from, to)
        {
        }
Beispiel #19
0
 public static Range <T> Create <T>(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to) =>
 new Range <T>(from, to);
Beispiel #20
0
 /// <summary>Trims the range from the right.</summary>
 /// <typeparam name="T">The type of the range values.</typeparam>
 /// <param name="compositeRange">The source range.</param>
 /// <param name="to">A new boundary To.</param>
 /// <returns>A range trimmed with a new To boundary.</returns>
 public static CompositeRange <T> TrimTo <T>(this CompositeRange <T> compositeRange, RangeBoundaryTo <T> to) =>
 compositeRange.Intersect(Range.TryCreate(RangeBoundaryFrom <T> .NegativeInfinity, to));
Beispiel #21
0
 private Range <T2> CreateRange <T2>(RangeBoundaryFrom <T2> from, RangeBoundaryTo <T2> to) =>
 new Range <T2>(from, to);
Beispiel #22
0
 /// <summary>Determines whether the composite range contains the specified range boundary.</summary>
 /// <typeparam name="T">The type of the range values.</typeparam>
 /// <param name="compositeRange">The source range.</param>
 /// <param name="other">The boundary to check.</param>
 /// <returns><c>true</c>, if the composite range contains the boundary.</returns>
 public static bool Contains <T>(this CompositeRange <T> compositeRange, RangeBoundaryTo <T> other) =>
 compositeRange.ContainingRange.Contains(other) &&
 compositeRange.SubRanges.Any(r => r.Contains(other));
Beispiel #23
0
        /// <summary>Extends the range from the right.</summary>
        /// <typeparam name="T">The type of the range values.</typeparam>
        /// <param name="compositeRange">The source range.</param>
        /// <param name="to">A new boundary To.</param>
        /// <returns>
        /// A range with a new To boundary or the source fange if the new boundary is less than original.
        /// </returns>
        public static CompositeRange <T> ExtendTo <T>(this CompositeRange <T> compositeRange, RangeBoundaryTo <T> to)
        {
            if (compositeRange.IsEmpty || to.IsEmpty || to <= compositeRange.ContainingRange.To)
            {
                return(compositeRange);
            }

            var ranges = compositeRange.SubRanges.ToArray();

            for (int i = ranges.Length - 1; i >= 0; i--)
            {
                if (ranges[i].To != compositeRange.ContainingRange.To)
                {
                    break;
                }

                ranges[i] = ranges[i].ExtendTo(to);
            }
            return(new CompositeRange <T>(ranges, UnsafeOverload.RangesAlreadySorted));
        }
Beispiel #24
0
 private static RangeIntersection <T> GetRangeIntersection <T>(
     RangeBoundaryFrom <T> intersectionFrom, RangeBoundaryTo <T> intersectionTo,
     [NotNull] IEnumerable <Range <T> > intersectionRanges) =>
 new RangeIntersection <T>(
     Range.Create(intersectionFrom, intersectionTo),
     intersectionRanges.ToArray());
Beispiel #25
0
 private static TRange TryCreateRange <T, TRange>(
     this TRange range,
     RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to)
     where TRange : IRangeFactory <T, TRange> =>
 range.TryCreateRange(from, to);
Beispiel #26
0
 private Range <T> CreateUnsafe(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to) =>
 new Range <T>(from, to, UnsafeOverload.SkipsArgValidation);
Beispiel #27
0
 Range <T, TKey> IRangeFactory <T, Range <T, TKey> > .CreateRange(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to) =>
 new Range <T, TKey>(from, to, _key);
Beispiel #28
0
 private Range <T> CreateRange(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to) =>
 new Range <T>(from, to);
Beispiel #29
0
 public static Range <T> TrimTo <T>(this Range <T> range, RangeBoundaryTo <T> to) =>
 to.IsNotEmpty && range.To <= to
                         ? range
                         : range.TryCreateRange(range.From, to);
Beispiel #30
0
 private Range <T2> TryCreateRange <T2>(RangeBoundaryFrom <T2> from, RangeBoundaryTo <T2> to) =>
 Range.TryCreate(from, to);