public void Case1()
        {
            var shape = new Shape(3, 3, 1, 2);
            var sh    = new ValueOffsetIncrementor(shape);

            sh.Next().Should().Be(0);
            sh.Next().Should().Be(shape.GetOffset(0, 0, 0, 1));
            sh.Next().Should().Be(shape.GetOffset(0, 1, 0, 0));
            sh.Next().Should().Be(shape.GetOffset(0, 1, 0, 1));
            sh.Next().Should().Be(shape.GetOffset(0, 2, 0, 0));
            sh.Next().Should().Be(shape.GetOffset(0, 2, 0, 1));
            sh.Next().Should().Be(shape.GetOffset(1, 0, 0, 0));
            sh.Next().Should().Be(shape.GetOffset(1, 0, 0, 1));
            sh.Next().Should().Be(shape.GetOffset(1, 1, 0, 0));
            sh.Next().Should().Be(shape.GetOffset(1, 1, 0, 1));
            sh.Next().Should().Be(shape.GetOffset(1, 2, 0, 0));
            sh.Next().Should().Be(shape.GetOffset(1, 2, 0, 1));
            sh.Next().Should().Be(shape.GetOffset(2, 0, 0, 0));
            sh.Next().Should().Be(shape.GetOffset(2, 0, 0, 1));
            sh.Next().Should().Be(shape.GetOffset(2, 1, 0, 0));
            sh.Next().Should().Be(shape.GetOffset(2, 1, 0, 1));
            sh.Next().Should().Be(shape.GetOffset(2, 2, 0, 0));
            sh.Next().Should().Be(shape.GetOffset(2, 2, 0, 1));
            sh.Next().Should().Be(-1);
        }
示例#2
0
        public void Setup()
        {
            var _  = ScalarMemoryPool.Instance;
            var __ = InfoOf <byte> .Size;

            shape = new Shape(1, 1, 100_000);
            iter  = new ValueOffsetIncrementor(shape);
        }
        public void Case6()
        {
            var shape = new Shape(1);
            var sh    = new ValueOffsetIncrementor(shape);

            sh.Next().Should().Be(0);

            sh.Next().Should().Be(-1);
        }
        public void Case8()
        {
            var shape = new Shape(100);
            var sh    = new ValueOffsetIncrementor(shape);

            sh.Next().Should().Be(0);
            sh.Next().Should().Be(shape.GetOffset(1));
            sh.Next().Should().Be(shape.GetOffset(2));
        }
        public void Case2()
        {
            var shape = new Shape(1, 1, 1, 3);
            var sh    = new ValueOffsetIncrementor(shape);

            sh.Next().Should().Be(0);
            sh.Next().Should().Be(shape.GetOffset(0, 0, 0, 1));
            sh.Next().Should().Be(shape.GetOffset(0, 0, 0, 2));

            sh.Next().Should().Be(-1);
        }
示例#6
0
        protected void setDefaults_Int64() //Int64 is the input type
        {
            if (AutoReset)
            {
                autoresetDefault_Int64();
                return;
            }

            if (typeof(TOut) == typeof(Int64))
            {
                setDefaults_NoCast();
                return;
            }

            var convert = Converts.FindConverter <Int64, TOut>();

            //non auto-resetting.
            var   localBlock = Block;
            Shape shape      = Shape;

            if (!Shape.IsContiguous || Shape.ModifiedStrides)
            {
                //Shape is sliced, not auto-resetting
                switch (Type)
                {
                case IteratorType.Scalar:
                {
                    var hasNext = new Reference <bool>(true);
                    var offset  = shape.TransformOffset(0);

                    if (offset != 0)
                    {
                        MoveNext = () =>
                        {
                            hasNext.Value = false;
                            return(convert(*((Int64 *)localBlock.Address + offset)));
                        };
                        MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    }
                    else
                    {
                        MoveNext = () =>
                        {
                            hasNext.Value = false;
                            return(convert(*((Int64 *)localBlock.Address)));
                        };
                        MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    }

                    Reset   = () => hasNext.Value = true;
                    HasNext = () => hasNext.Value;
                    break;
                }

                case IteratorType.Vector:
                {
                    MoveNext          = () => convert(*((Int64 *)localBlock.Address + shape.GetOffset(index++)));
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    Reset             = () => index = 0;
                    HasNext           = () => index < Shape.size;
                    break;
                }

                case IteratorType.Matrix:
                case IteratorType.Tensor:
                {
                    var hasNext = new Reference <bool>(true);
                    var iterator = new ValueCoordinatesIncrementor(ref shape, delegate(ref ValueCoordinatesIncrementor _) { hasNext.Value = false; });
                    Func <int[], int> getOffset = shape.GetOffset;
                    var index = iterator.Index;

                    MoveNext = () =>
                    {
                        var ret = convert(*((Int64 *)localBlock.Address + getOffset(index)));
                        iterator.Next();
                        return(ret);
                    };
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");

                    Reset = () =>
                    {
                        iterator.Reset();
                        hasNext.Value = true;
                    };

                    HasNext = () => hasNext.Value;
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            else
            {
                //Shape is not sliced, not auto-resetting
                switch (Type)
                {
                case IteratorType.Scalar:
                    var hasNext = new Reference <bool>(true);
                    MoveNext = () =>
                    {
                        hasNext.Value = false;
                        return(convert(*((Int64 *)localBlock.Address)));
                    };
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    Reset             = () => hasNext.Value = true;
                    HasNext           = () => hasNext.Value;
                    break;

                case IteratorType.Vector:
                    MoveNext          = () => convert(*((Int64 *)localBlock.Address + index++));
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    Reset             = () => index = 0;
                    HasNext           = () => index < Shape.size;
                    break;

                case IteratorType.Matrix:
                case IteratorType.Tensor:
                    var iterator = new ValueOffsetIncrementor(Shape);     //we do not copy the dimensions because there is not risk for the iterator's shape to change.
                    MoveNext          = () => convert(*((Int64 *)localBlock.Address + iterator.Next()));
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    Reset             = () => iterator.Reset();
                    HasNext           = () => iterator.HasNext;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }