Esempio n. 1
0
 private static Range <T, TKey> TryCreateCore <T, TKey>(
     T from, RangeBoundaryFromKind fromKind,
     T to, RangeBoundaryToKind toKind,
     TKey key) =>
 RangeBoundaryFrom <T> .IsValid(from) && RangeBoundaryTo <T> .IsValid(to)
                                 ? TryCreate(
     RangeBoundaryFrom <T> .AdjustAndCreate(from, fromKind),
     RangeBoundaryTo <T> .AdjustAndCreate(to, toKind),
     key)
                                 : Range <T, TKey> .Empty;
        public RangeBoundaryFrom <int> Test00Validated()
        {
            var result = RangeBoundaryFrom <int> .Empty;

            for (var i = 0; i < Count; i++)
            {
                result = new RangeBoundaryFrom <int>(i, RangeBoundaryFromKind.Inclusive);
            }
            return(result);
        }
Esempio n. 3
0
        public void TestBoundaryFromProperties()
        {
            int?value1 = 1;
            int?value2 = 2;
            int?empty  = null;

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

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

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

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

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

            a = Range.BoundaryFrom(value1);
            AreEqual(a.Kind, RangeBoundaryFromKind.Inclusive);
            AreEqual(a.Value, value1);
            AreNotEqual(a.Value, value2);

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

            a = Range.BoundaryFromExclusive(value1);
            AreEqual(a.Kind, RangeBoundaryFromKind.Exclusive);
            AreEqual(a.Value, value1);
            AreNotEqual(a.Value, value2);

            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.HasValue);
            IsFalse(a.IsNegativeInfinity);
            IsFalse(a.IsInclusiveBoundary);
            IsTrue(a.IsExclusiveBoundary);
        }
Esempio n. 4
0
        public static Range <T> ExtendFrom <T>(this Range <T> range, RangeBoundaryFrom <T> from)
        {
            if (range.IsEmpty || from.IsEmpty)
            {
                return(range);
            }

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

            return(From <= from
                                ? this
                                : TryCreateRange(from, To));
        }
Esempio n. 6
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;
Esempio n. 7
0
        public static void TestRangeEquality()
        {
            int?value1 = 1;
            int?value2 = 2;
            int?value3 = 3;
            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));

            IsTrue(Range.TryCreate(value1, value2) == Range.Create <int?>(1, 2));
            IsTrue(Range.TryCreate(value1, value2) != Range.Create(value1, 1));
            IsTrue(Range.TryCreate(value1, value2, key) == Range.Create <int?, string>(1, 2, key));
            IsTrue(Range.TryCreate(value1, value2, key) != Range.Create <int?, string>(1, 2, key2));
            IsTrue(Range.TryCreate(value1, value2, key) != Range.Create <int?, string>(1, 1, key));

            IsTrue(Range.TryCreateExclusiveFrom(value1, value2) == Range.CreateExclusiveFrom <int?>(1, 2));
            IsTrue(Range.TryCreateExclusiveFrom(value1, value3) == Range.CreateExclusiveFrom(value1, 3));
            IsTrue(Range.TryCreateExclusiveFrom(value1, value3) != Range.CreateExclusiveFrom(value1, 4));
            IsTrue(Range.TryCreateExclusiveFrom(value1, value2, key) == Range.CreateExclusiveFrom <int?, string>(1, 2, key));
            IsTrue(Range.TryCreateExclusiveFrom(value1, value2, key) != Range.CreateExclusiveFrom <int?, string>(1, 2, key2));

            IsTrue(Range.TryCreateExclusiveTo(value1, value2) == Range.CreateExclusiveTo <int?>(1, 2));
            IsTrue(Range.TryCreateExclusiveTo(value1, value3) == Range.CreateExclusiveTo(value1, 3));
            IsTrue(Range.TryCreateExclusiveTo(value1, value3) != Range.CreateExclusiveTo(value1, 4));
            IsTrue(Range.TryCreateExclusiveTo(value1, value2, key) == Range.CreateExclusiveTo <int?, string>(1, 2, key));
            IsTrue(Range.TryCreateExclusiveTo(value1, value2, key) != Range.CreateExclusiveTo <int?, string>(1, 2, key2));
        }
        public CompositeRange <T> ExtendFrom(RangeBoundaryFrom <T> from)
        {
            if (IsEmpty || from.IsEmpty || from >= ContainingRange.From)
            {
                return(this);
            }

            var ranges = SubRanges.ToArray();

            for (var i = 0; i < ranges.Length; i++)
            {
                if (ranges[i].From != ContainingRange.From)
                {
                    break;
                }

                ranges[i] = ranges[i].ExtendFrom(from);
            }
            return(new CompositeRange <T>(ranges, UnsafeOverload.RangesAlreadySorted));
        }
Esempio n. 9
0
        public void TestBoundaryFromEquality()
        {
            double?value1 = 1;
            double?value2 = 2;
            double?empty  = null;

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

            AreEqual(e, RangeBoundaryFrom <double?> .Empty);
            IsTrue(e == RangeBoundaryFrom <double?> .Empty);
            IsFalse(e != RangeBoundaryFrom <double?> .Empty);

            AreEqual(inf, RangeBoundaryFrom <double?> .NegativeInfinity);
            IsTrue(inf == RangeBoundaryFrom <double?> .NegativeInfinity);

            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);
        }
Esempio n. 10
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;
		}
Esempio n. 11
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, RangeBoundaryFrom <T> other) =>
 compositeRange.ContainingRange.Contains(other) &&
 compositeRange.SubRanges.Any(r => r.Contains(other));
Esempio n. 12
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());
Esempio n. 13
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);
        public static CompositeRange <T, TKey> ExtendFrom <T, TKey>(this CompositeRange <T, TKey> compositeRange, RangeBoundaryFrom <T> from)
        {
            if (compositeRange.IsEmpty || from.IsEmpty || from >= compositeRange.ContainingRange.From)
            {
                return(compositeRange);
            }

            var ranges = compositeRange.SubRanges.ToArray();

            for (int i = 0; i < ranges.Length; i++)
            {
                if (ranges[i].From != compositeRange.ContainingRange.From)
                {
                    break;
                }

                ranges[i] = ranges[i].ExtendFrom(from);
            }
            return(new CompositeRange <T, TKey>(ranges, UnsafeOverload.RangesAlreadySorted));
        }
Esempio n. 15
0
 private Range <T2> TryCreateRange <T2>(RangeBoundaryFrom <T2> from, RangeBoundaryTo <T2> to) =>
 Range.TryCreate(from, to);
Esempio n. 16
0
 public static bool Contains <T>(this Range <T> range, T value) =>
 RangeBoundaryFrom <T> .IsValid(value)
                         ? Contains(range, Range.BoundaryFrom(value))
                         : Contains(range, Range.BoundaryTo(value));
Esempio n. 17
0
 public static bool StartsAfter <T>(this Range <T> range, RangeBoundaryFrom <T> other) =>
 other.IsNotEmpty && range.From > other;
Esempio n. 18
0
 public static Range <T> Create <T>(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to) =>
 new Range <T>(from, to);
Esempio n. 19
0
 Range <T, TKey> IRangeFactory <T, Range <T, TKey> > .TryCreateRange(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to) =>
 Range.TryCreate(from, to, _key);
Esempio n. 20
0
 Range <T, TKey> IRangeFactory <T, Range <T, TKey> > .CreateRange(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to) =>
 new Range <T, TKey>(from, to, _key);
 public Range <T, TKey> TrimFrom(RangeBoundaryFrom <T> from) =>
 from.IsNotEmpty && From >= from
                         ? this
                         : TryCreateRange(from, To);
Esempio n. 22
0
 /// <summary>Trims the range from the left.</summary>
 /// <typeparam name="T">The type of the range values.</typeparam>
 /// <param name="compositeRange">The source range.</param>
 /// <param name="from">A new boundary From.</param>
 /// <returns>A range trimmed with a new From boundary.</returns>
 public static CompositeRange <T> TrimFrom <T>(this CompositeRange <T> compositeRange, RangeBoundaryFrom <T> from) =>
 compositeRange.Intersect(Range.TryCreate(from, RangeBoundaryTo <T> .PositiveInfinity));
Esempio n. 23
0
        internal Range(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to, UnsafeOverload skipsArgValidation)
#if DEBUG
            : this(from, to)
        {
        }
Esempio n. 24
0
 private Range <T2> CreateRange <T2>(RangeBoundaryFrom <T2> from, RangeBoundaryTo <T2> to) =>
 new Range <T2>(from, to);
Esempio n. 25
0
 public static bool StartsAfter <T>(this Range <T> range, T value) =>
 RangeBoundaryFrom <T> .IsValid(value) && range.From > Range.BoundaryFrom(value);
 public bool EndsBefore(RangeBoundaryFrom <T> other) =>
 IsNotEmpty && other.IsNotEmpty && To < other;
Esempio n. 27
0
 public static bool EndsBefore <T>(this Range <T> range, RangeBoundaryFrom <T> other) =>
 range.IsNotEmpty && other.IsNotEmpty && range.To < other;
Esempio n. 28
0
 private Range <T> CreateRange(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to) =>
 new Range <T>(from, to);
Esempio n. 29
0
 public static Range <T> TrimFrom <T>(this Range <T> range, RangeBoundaryFrom <T> from) =>
 from.IsNotEmpty && range.From >= from
                         ? range
                         : range.TryCreateRange(from, range.To);
Esempio n. 30
0
 private Range <T> CreateUnsafe(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to) =>
 new Range <T>(from, to, UnsafeOverload.SkipsArgValidation);