/// <summary>
 /// Throws an <see cref="InvalidEnumArgumentException"/> if the value is invalid for the enum type.
 /// </summary>
 /// <param name="value">The value to validate.</param>
 /// <exception cref="InvalidEnumArgumentException">If the value is invalid for the enum type.</exception>
 public void ThrowIfInvalidEnumValue(TEnum value)
 {
     if (!this.IsValidEnumValue(value))
     {
         throw InvalidEnumArgument.CreateException(nameof(value), value);
     }
 }
        public static RangeClusivity Combine(Clusivity min, Clusivity max)
        {
            switch (min)
            {
            case Clusivity.Exclusive:
                switch (max)
                {
                case Clusivity.Exclusive: return(RangeClusivity.Exclusive);

                case Clusivity.Inclusive: return(RangeClusivity.InclusiveMax);

                default: throw InvalidEnumArgument.CreateException(nameof(max), max);
                }

            case Clusivity.Inclusive:
                switch (max)
                {
                case Clusivity.Exclusive: return(RangeClusivity.InclusiveMin);

                case Clusivity.Inclusive: return(RangeClusivity.Inclusive);

                default: throw InvalidEnumArgument.CreateException(nameof(max), max);
                }

            default: throw InvalidEnumArgument.CreateException(nameof(min), min);
            }
        }
Пример #3
0
    public static void AddFlatQuad <TVertex>(
        this IMutableDivisibleMesh <TVertex> builder,
        TVertex topLeft,
        TVertex topRight,
        TVertex bottomLeft,
        TVertex bottomRight,
        QuadDiagonal diagonal)
        where TVertex : struct
    {
        Contracts.Requires.That(builder != null);

        using (var group = builder.AddTriangleGroup(new TriangleGroup(triangles: 2, vertices: 4)))
        {
            byte offTL = group.AddVertex(topLeft);
            byte offTR = group.AddVertex(topRight);
            byte offBL = group.AddVertex(bottomLeft);
            byte offBR = group.AddVertex(bottomRight);

            // vertices are added in clockwise winding order
            switch (diagonal)
            {
            case QuadDiagonal.Ascending:
                group.AddTriangleOffsets(offTL, offTR, offBL).AddTriangleOffsets(offBL, offTR, offBR);
                return;

            case QuadDiagonal.Descending:
                group.AddTriangleOffsets(offTL, offBR, offBL).AddTriangleOffsets(offTL, offTR, offBR);
                return;

            default: throw InvalidEnumArgument.CreateException(nameof(diagonal), diagonal);
            }
        }
    }
        /// <summary>
        /// Initializes a new instance of the <see cref="BoundedIndexableEnumerable{TIndex, TValue}"/> class.
        /// </summary>
        /// <param name="bounds">The bounds used to limit the enumeration.</param>
        /// <param name="enumerable">The enumerable that determines the order in which the indices are enumerated.</param>
        /// <param name="revisitPolicy">The policy for whether or not to re-yield previously enumerated indices again.</param>
        public BoundedIndexableEnumerable(
            IBoundedIndexable <TIndex, TValue> bounds,
            IEnumerable <TIndex> enumerable,
            Revisits revisitPolicy = Revisits.Yield)
        {
            Contracts.Requires.That(bounds != null);
            Contracts.Requires.That(enumerable != null);

            this.bounds     = bounds;
            this.enumerable = enumerable;

            this.maxCount = this.bounds.LowerBounds.CalculateVolume(this.bounds.UpperBounds);

            switch (revisitPolicy)
            {
            case Revisits.Yield:
                this.yieldRevisits = true;
                this.cacheRevisits = false;
                break;

            case Revisits.YieldCached:
                this.yieldRevisits = true;
                this.cacheRevisits = true;
                break;

            case Revisits.DontYield:
                this.yieldRevisits = false;
                this.cacheRevisits = false;
                break;

            default:
                throw InvalidEnumArgument.CreateException(nameof(revisitPolicy), revisitPolicy);
            }
        }
    public static Endian ToEndian(this Endianness endianness)
    {
        switch (endianness)
        {
        case Endianness.BigEndian: return(Endian.Big);

        case Endianness.LittleEndian: return(Endian.Little);

        default: throw InvalidEnumArgument.CreateException(nameof(endianness), endianness);
        }
    }
    /// <summary>
    /// Gets the axis of the specified axis direction combination.
    /// </summary>
    /// <param name="axisDirection">The axis direction.</param>
    /// <returns>The axis.</returns>
    public static Axis1D GetAxis(this AxisDirection1D axisDirection)
    {
        switch (axisDirection)
        {
        case AxisDirection1D.PositiveX: return(Axis1D.X);

        case AxisDirection1D.NegativeX: return(Axis1D.X);

        default:
            throw InvalidEnumArgument.CreateException(nameof(axisDirection), axisDirection);
        }
    }
        public static int ConvertToInt(this ComparisonResult comparison)
        {
            switch (comparison)
            {
            case ComparisonResult.Equal: return(0);

            case ComparisonResult.Less: return(-1);

            case ComparisonResult.Greater: return(1);

            default: throw InvalidEnumArgument.CreateException(nameof(comparison), comparison);
            }
        }
Пример #8
0
        public static IEnumerable <Index2D> OscillateRange(
            Index2D startIndex, Index2D dimensions, OscillationOrder2D order = OscillationOrder2D.XY)
        {
            Contracts.Requires.That(dimensions.IsAllPositiveOrZero());

            switch (order)
            {
            case OscillationOrder2D.XY: return(OscillateRangeXY(startIndex, dimensions));

            case OscillationOrder2D.YX: return(OscillateRangeYX(startIndex, dimensions));

            default: throw InvalidEnumArgument.CreateException(nameof(order), order);
            }
        }
Пример #9
0
        /// <inheritdoc />
        public T this[Endian endianness]
        {
            get
            {
                switch (endianness)
                {
                case Endian.Big: return(this.BigEndian);

                case Endian.Little: return(this.LittleEndian);

                default: throw InvalidEnumArgument.CreateException(nameof(endianness), endianness);
                }
            }
        }
    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);
        }
    }
Пример #11
0
        /// <inheritdoc />
        public override string ToString()
        {
            switch (this.Bounds)
            {
            case RangeClusivity.Inclusive: return($"[{this.Min}, {this.Max}]");

            case RangeClusivity.Exclusive: return($"({this.Min}, {this.Max})");

            case RangeClusivity.InclusiveMin: return($"[{this.Min}, {this.Max})");

            case RangeClusivity.InclusiveMax: return($"({this.Min}, {this.Max}]");

            default: throw InvalidEnumArgument.CreateException(nameof(this.Bounds), this.Bounds);
            }
        }
Пример #12
0
    /// <summary>
    /// Gets the number of bytes needed to represent a primitive type.
    /// </summary>
    /// <param name="primitiveType">Type of the primitive.</param>
    /// <returns>The number of bytes needed to represent the primitive type.</returns>
    public static int NumberOfBytes(this PrimitiveType primitiveType)
    {
        switch (primitiveType)
        {
        case PrimitiveType.Byte:
            return(PrimitiveTypeSizes.ByteNumberOfBytes);

        case PrimitiveType.SByte:
            return(PrimitiveTypeSizes.SByteNumberOfBytes);

        case PrimitiveType.Short:
            return(PrimitiveTypeSizes.ShortNumberOfBytes);

        case PrimitiveType.UShort:
            return(PrimitiveTypeSizes.UShortNumberOfBytes);

        case PrimitiveType.Int:
            return(PrimitiveTypeSizes.IntNumberOfBytes);

        case PrimitiveType.UInt:
            return(PrimitiveTypeSizes.UIntNumberOfBytes);

        case PrimitiveType.Long:
            return(PrimitiveTypeSizes.LongNumberOfBytes);

        case PrimitiveType.ULong:
            return(PrimitiveTypeSizes.ULongNumberOfBytes);

        case PrimitiveType.Float:
            return(PrimitiveTypeSizes.FloatNumberOfBytes);

        case PrimitiveType.Double:
            return(PrimitiveTypeSizes.DoubleNumberOfBytes);

        case PrimitiveType.Decimal:
            return(PrimitiveTypeSizes.DecimalNumberOfBytes);

        case PrimitiveType.Char:
            return(PrimitiveTypeSizes.CharNumberOfBytes);

        case PrimitiveType.Bool:
            return(PrimitiveTypeSizes.BoolNumberOfBytes);

        default:
            throw InvalidEnumArgument.CreateException(nameof(primitiveType), primitiveType);
        }
    }
Пример #13
0
    /// <summary>
    /// Gets the <see cref="Type"/> the primitive type enumeration represents.
    /// </summary>
    /// <param name="primitiveType">Type of the primitive.</param>
    /// <returns>The type.</returns>
    public static Type ToType(this PrimitiveType primitiveType)
    {
        switch (primitiveType)
        {
        case PrimitiveType.Byte:
            return(typeof(byte));

        case PrimitiveType.SByte:
            return(typeof(sbyte));

        case PrimitiveType.Short:
            return(typeof(short));

        case PrimitiveType.UShort:
            return(typeof(ushort));

        case PrimitiveType.Int:
            return(typeof(int));

        case PrimitiveType.UInt:
            return(typeof(uint));

        case PrimitiveType.Long:
            return(typeof(long));

        case PrimitiveType.ULong:
            return(typeof(ulong));

        case PrimitiveType.Float:
            return(typeof(float));

        case PrimitiveType.Double:
            return(typeof(double));

        case PrimitiveType.Decimal:
            return(typeof(decimal));

        case PrimitiveType.Char:
            return(typeof(char));

        case PrimitiveType.Bool:
            return(typeof(bool));

        default:
            throw InvalidEnumArgument.CreateException(nameof(primitiveType), primitiveType);
        }
    }
Пример #14
0
    public static int GetLength(this Range <int> range)
    {
        int length = range.Max - range.Min;

        switch (range.Bounds)
        {
        case RangeClusivity.Inclusive: return(length == int.MaxValue ? int.MaxValue : length + 1);

        case RangeClusivity.Exclusive: return(length == 0 ? 0 : length - 1);

        case RangeClusivity.InclusiveMin: return(length);

        case RangeClusivity.InclusiveMax: return(length);

        default: throw InvalidEnumArgument.CreateException(nameof(range.Bounds), range.Bounds);
        }
    }
Пример #15
0
        public static ITargetBlock <T> Create <T>(
            Func <IReadOnlyList <T>, Task> action, int maxBatchSize, Batching batching, DataflowBlockOptions options)
        {
            Contracts.Requires.That(action != null);
            Contracts.Requires.That(options != null);

            switch (batching)
            {
            case Batching.Dynamic:
                options = options.CreateCopy();
                options.BoundedCapacity = maxBatchSize;
                return(new DynamicBatchActionBlock <T>(action, options));

            case Batching.Static:
                return(new StaticBatchActionBlock <T>(action, maxBatchSize, options));

            default: throw InvalidEnumArgument.CreateException(nameof(batching), batching);
            }
        }
Пример #16
0
        public bool Contains(T value)
        {
            Contracts.Requires.That(value != null);

            switch (this.Bounds)
            {
            case RangeClusivity.Exclusive:
                return(value.IsGreaterThan(this.Min) && value.IsLessThan(this.Max));

            case RangeClusivity.InclusiveMin:
                return(value.IsGreaterThanOrEqual(this.Min) && value.IsLessThan(this.Max));

            case RangeClusivity.InclusiveMax:
                return(value.IsGreaterThan(this.Min) && value.IsLessThanOrEqual(this.Max));

            case RangeClusivity.Inclusive:
                return(value.IsGreaterThanOrEqual(this.Min) && value.IsLessThanOrEqual(this.Max));

            default:
                throw InvalidEnumArgument.CreateException(nameof(this.Bounds), this.Bounds);
            }
        }
Пример #17
0
    /// <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);
        }
    }
Пример #18
0
    /// <summary>
    /// Gets the maximum value for the primitive type.
    /// </summary>
    /// <param name="primitiveType">The primitive type.</param>
    /// <returns>The maximum value of the primitive type.</returns>
    /// <remarks>
    /// The return type of decimal is used because it is the only type larger enough to encompass the values of all the primitive
    /// types accurately, with the exception of float and double. The range of values represented by a float or double can not be
    /// accurately represented as a decimal, nor can a float or double accurately represent the range of values of the other
    /// integral types. Therefor, this method is incompatible with floats and doubles.
    /// </remarks>
    public static decimal MaxValue(this PrimitiveType primitiveType)
    {
        Contracts.Requires.That(primitiveType.IsIntegral() || primitiveType == PrimitiveType.Decimal);

        switch (primitiveType)
        {
        case PrimitiveType.Byte:
            return(byte.MaxValue);

        case PrimitiveType.SByte:
            return(sbyte.MaxValue);

        case PrimitiveType.Short:
            return(short.MaxValue);

        case PrimitiveType.UShort:
            return(ushort.MaxValue);

        case PrimitiveType.Int:
            return(int.MaxValue);

        case PrimitiveType.UInt:
            return(uint.MaxValue);

        case PrimitiveType.Long:
            return(long.MaxValue);

        case PrimitiveType.ULong:
            return(ulong.MaxValue);

        case PrimitiveType.Decimal:
            return(decimal.MaxValue);

        default:
            throw InvalidEnumArgument.CreateException(nameof(primitiveType), primitiveType);
        }
    }
Пример #19
0
    public static IEnumerable <int> EnumerateTriangles <TVertex>(
        this IDivisibleMesh <TVertex> mesh, VertexWindingOrder winding)
        where TVertex : struct
    {
        Contracts.Requires.That(mesh != null);
        Contracts.Requires.That(winding.IsValidEnumValue());

        var offsets      = mesh.Offsets.GetEnumerator();
        int globalOffset = 0;

        switch (winding)
        {
        case VertexWindingOrder.Clockwise:
            foreach (var group in mesh.Groups)
            {
                for (int count = 0; count < group.Offsets; count++)
                {
                    var success = offsets.MoveNext();
                    Contracts.Assert.That(success);
                    yield return(globalOffset + offsets.Current);
                }

                globalOffset += group.Vertices;
            }

            yield break;

        case VertexWindingOrder.Counterclockwise:
            foreach (var group in mesh.Groups)
            {
                // IDivisibleMesh<TVertex> is always stored in clockwise winding order
                // so the last 2 offsets of each triangle need to be swapped to become counterclockwise
                for (int count = 0; count < group.Triangles; count++)
                {
                    // return the first offset as normal
                    var success = offsets.MoveNext();
                    Contracts.Assert.That(success);
                    yield return(globalOffset + offsets.Current);

                    // temporarily store the second offset
                    success = offsets.MoveNext();
                    Contracts.Assert.That(success);
                    var tempOffset = offsets.Current;

                    // return the third offset as though it were the second offset
                    success = offsets.MoveNext();
                    Contracts.Assert.That(success);
                    yield return(globalOffset + offsets.Current);

                    // return the second offset (stored) as though it were the third offset
                    yield return(globalOffset + tempOffset);
                }

                globalOffset += group.Vertices;
            }

            yield break;

        default: throw InvalidEnumArgument.CreateException(nameof(winding), winding);
        }
    }
        public void GenerateValues(
            IVoxelProjection <TVoxel, TSurfaceData> projection,
            out TValue topLeftValue,
            out TValue topRightValue,
            out TValue bottomLeftValue,
            out TValue bottomRightValue)
        {
            switch (projection.AxisDirection)
            {
            case AxisDirection3D.PositiveX:
                this.positiveXAxisGenerator.GenerateValues(
                    projection,
                    out topLeftValue,
                    out topRightValue,
                    out bottomLeftValue,
                    out bottomRightValue);
                break;

            case AxisDirection3D.NegativeX:
                this.negativeXAxisGenerator.GenerateValues(
                    projection,
                    out topLeftValue,
                    out topRightValue,
                    out bottomLeftValue,
                    out bottomRightValue);
                break;

            case AxisDirection3D.PositiveY:
                this.positiveYAxisGenerator.GenerateValues(
                    projection,
                    out topLeftValue,
                    out topRightValue,
                    out bottomLeftValue,
                    out bottomRightValue);
                break;

            case AxisDirection3D.NegativeY:
                this.negativeYAxisGenerator.GenerateValues(
                    projection,
                    out topLeftValue,
                    out topRightValue,
                    out bottomLeftValue,
                    out bottomRightValue);
                break;

            case AxisDirection3D.PositiveZ:
                this.positiveZAxisGenerator.GenerateValues(
                    projection,
                    out topLeftValue,
                    out topRightValue,
                    out bottomLeftValue,
                    out bottomRightValue);
                break;

            case AxisDirection3D.NegativeZ:
                this.negativeZAxisGenerator.GenerateValues(
                    projection,
                    out topLeftValue,
                    out topRightValue,
                    out bottomLeftValue,
                    out bottomRightValue);
                break;

            default:
                throw InvalidEnumArgument.CreateException(nameof(projection.AxisDirection), projection.AxisDirection);
            }
        }