/// <summary> /// Determines whether the specified index is within the specified range of indices. /// </summary> /// <typeparam name="TIndex">The type of the index.</typeparam> /// <param name="index">The index.</param> /// <param name="minimum">The minimum index.</param> /// <param name="maximum">The maximum index.</param> /// <param name="rangeOptions">The inclusive/exclusive range options.</param> /// <returns>True if the value is within range, false otherwise.</returns> public static bool IsIn <TIndex>( TIndex index, TIndex minimum, TIndex maximum, RangeClusivity rangeOptions = RangeClusivity.Inclusive) where TIndex : IIndex { Contracts.Requires.That(index != null); Contracts.Requires.That(minimum != null); Contracts.Requires.That(maximum != null); Contracts.Requires.That(index.Rank == minimum.Rank); Contracts.Requires.That(minimum.Rank == maximum.Rank); Contracts.Requires.That( Enumerable.Range(0, index.Rank).All(dimension => minimum[dimension] <= maximum[dimension])); for (int dimension = 0; dimension < index.Rank; dimension++) { if (!index[dimension].IsIn(Range.New(minimum[dimension], maximum[dimension], rangeOptions))) { return(false); } } return(true); }
public Range(T min, T max, RangeClusivity bounds = RangeClusivity.Inclusive) { Contracts.Requires.That(min != null); Contracts.Requires.That(max != null); Contracts.Requires.That(min.IsLessThanOrEqual(max)); Contracts.Requires.That(bounds.IsValidEnumValue()); this.Min = min; this.Max = max; this.Bounds = bounds; }
public static Clusivity GetMinClusivity(this RangeClusivity clusivity) { switch (clusivity) { case RangeClusivity.Inclusive: return(Clusivity.Inclusive); case RangeClusivity.Exclusive: return(Clusivity.Exclusive); case RangeClusivity.InclusiveMin: return(Clusivity.Inclusive); case RangeClusivity.InclusiveMax: return(Clusivity.Exclusive); default: throw InvalidEnumArgument.CreateException(nameof(clusivity), clusivity); } }
/// <summary> /// Calculates the volume as a long between the two indices. /// </summary> /// <typeparam name="TIndex">The type of the index.</typeparam> /// <param name="index">The first index defining the bounds of the volume.</param> /// <param name="otherIndex">The second index defining the bounds of the volume.</param> /// <param name="rangeOptions">The inclusive/exclusive range options of the volume.</param> /// <returns>The total volume of indices between the two indices.</returns> public static long CalculateLongVolume <TIndex>( this TIndex index, TIndex otherIndex, RangeClusivity rangeOptions = RangeClusivity.Inclusive) where TIndex : IIndex { Contracts.Requires.That(index != null); Contracts.Requires.That(otherIndex != null); Contracts.Requires.That(index.Rank == otherIndex.Rank); long result = 1; for (int dimension = 0; dimension < index.Rank; dimension++) { result *= CalculateLongDistance(index[dimension], otherIndex[dimension], rangeOptions); } return(result); }
/// <summary> /// Calculates the distance as a long between two long values. /// </summary> /// <param name="a">The first value.</param> /// <param name="b">The second value.</param> /// <param name="rangeOptions">The range options.</param> /// <returns>The distance as a long between the two long values.</returns> private static long CalculateLongDistance(long a, long b, RangeClusivity rangeOptions) { long result = Math.Abs(a - b); switch (rangeOptions) { case RangeClusivity.Exclusive: return((result - 1).ClampLower(0)); case RangeClusivity.InclusiveMin: return(result); case RangeClusivity.InclusiveMax: return(result); case RangeClusivity.Inclusive: return(result + 1); default: throw InvalidEnumArgument.CreateException(nameof(rangeOptions), rangeOptions); } }
public static Range <long> FromLength( long length, RangeClusivity bounds = RangeClusivity.InclusiveMin) => new Range <long>(0, length, bounds);
public static Range <uint> FromLength( uint length, RangeClusivity bounds = RangeClusivity.InclusiveMin) => new Range <uint>(0, length, bounds);
public static Range <T> New <T>(T min, T max, RangeClusivity bounds = RangeClusivity.Inclusive) where T : IComparable <T>, IEquatable <T> => new Range <T>(min, max, bounds);
public static Range <sbyte> FromLength( sbyte length, RangeClusivity bounds = RangeClusivity.InclusiveMin) => new Range <sbyte>(0, length, bounds);
public static Range <decimal> FromLength( decimal length, RangeClusivity bounds = RangeClusivity.InclusiveMin) => new Range <decimal>(0, length, bounds);
public static Range <ulong> FromLength( ulong start, ulong length, RangeClusivity bounds = RangeClusivity.InclusiveMin) => new Range <ulong>(start, start + length, bounds);
public static Range <ushort> FromLength( ushort start, ushort length, RangeClusivity bounds = RangeClusivity.InclusiveMin) => new Range <ushort>(start, (ushort)(start + length), bounds);
public static Range <sbyte> FromLength( sbyte start, sbyte length, RangeClusivity bounds = RangeClusivity.InclusiveMin) => new Range <sbyte>(start, (sbyte)(start + length), bounds);
public static Range <double> FromLength( double start, double length, RangeClusivity bounds = RangeClusivity.InclusiveMin) => new Range <double>(start, start + length, bounds);
// TODO maybe all these FromLength overloads should not accept a RangeClusivity bounds at all? public static Range <float> FromLength( float start, float length, RangeClusivity bounds = RangeClusivity.InclusiveMin) => new Range <float>(start, start + length, bounds);
/// <summary> /// Determines whether this index is within the specified range of indices. /// </summary> /// <param name="min">The minimum index.</param> /// <param name="max">The maximum index.</param> /// <param name="bounds">The inclusive/exclusive range options.</param> /// <returns>True if the value is within range, false otherwise.</returns> public bool IsIn(Index1D min, Index1D max, RangeClusivity bounds = RangeClusivity.Inclusive) => this.X.IsIn(Range.New(min.X, max.X, bounds));
/// <summary> /// Determines whether this index is within the specified range of indices. /// </summary> /// <param name="min">The minimum index.</param> /// <param name="max">The maximum index.</param> /// <param name="bounds">The inclusive/exclusive range options.</param> /// <returns>True if the value is within range, false otherwise.</returns> public bool IsIn(Index4D min, Index4D max, RangeClusivity bounds = RangeClusivity.Inclusive) => this.X.IsIn(Range.New(min.X, max.X, bounds)) && this.Y.IsIn(Range.New(min.Y, max.Y, bounds)) && this.Z.IsIn(Range.New(min.Z, max.Z, bounds)) && this.W.IsIn(Range.New(min.W, max.W, bounds));
public static Range <int> FromLength( int start, int length, RangeClusivity bounds = RangeClusivity.InclusiveMin) => new Range <int>(start, start + length, bounds);