Ejemplo n.º 1
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();
                }
            }
        }
Ejemplo n.º 2
0
 public void Reset()
 {
     iter.Reset();
 }