Beispiel #1
0
        internal MemoryView(IBufferProtocol @object, int start, int?end, int step, string format, PythonTuple shape)
        {
            _buffer = @object;
            _format = format;
            _shape  = shape;
            _start  = start;
            _end    = end;
            _step   = step;

            if (!TypecodeOps.TryGetTypecodeWidth(format, out _itemsize))
            {
                _itemsize = (int)_buffer.ItemSize;
            }

            _matchesBuffer = _format == _buffer.Format && _start % itemsize == 0;
        }
Beispiel #2
0
        public MemoryView cast(object format, [NotNull] object shape)
        {
            if (!(format is string formatAsString))
            {
                throw PythonOps.TypeError("memoryview: format argument must be a string");
            }

            if (_step != 1)
            {
                throw PythonOps.TypeError("memoryview: casts are restricted to C-contiguous views");
            }

            if ((shape != null || ndim != 0) && this.shape.Contains(0))
            {
                throw PythonOps.TypeError("memoryview: cannot cast view with zeros in shape or strides");
            }

            PythonTuple shapeAsTuple = null;

            if (shape != null)
            {
                if (!(shape is PythonList) && !(shape is PythonTuple))
                {
                    throw PythonOps.TypeError("shape must be a list or a tuple");
                }

                shapeAsTuple = PythonOps.MakeTupleFromSequence(shape);
                int newNDim = shapeAsTuple.Count;

                if (newNDim > MaximumDimensions)
                {
                    throw PythonOps.TypeError("memoryview: number of dimensions must not exceed {0}", MaximumDimensions);
                }

                if (ndim != 1 && newNDim != 1)
                {
                    throw PythonOps.TypeError("memoryview: cast must be 1D -> ND or ND -> 1D");
                }
            }

            int newItemsize;

            if (!TypecodeOps.TryGetTypecodeWidth(formatAsString, out newItemsize))
            {
                throw PythonOps.ValueError(
                          "memoryview: destination format must be a native single character format prefixed with an optional '@'");
            }

            bool thisIsBytes  = this.format == "B" || this.format == "b" || this.format == "c";
            bool otherIsBytes = formatAsString == "B" || formatAsString == "b" || formatAsString == "c";

            if (!thisIsBytes && !otherIsBytes)
            {
                throw PythonOps.TypeError("memoryview: cannot cast between two non-byte formats");
            }

            int length = numberOfElements();

            if (length % newItemsize != 0)
            {
                throw PythonOps.TypeError("memoryview: length is not a multiple of itemsize");
            }

            int newLength = length * _itemsize / newItemsize;

            if (shapeAsTuple != null)
            {
                int lengthGivenShape = 1;
                for (int i = 0; i < shapeAsTuple.Count; i++)
                {
                    lengthGivenShape *= Converter.ConvertToInt32(shapeAsTuple[i]);
                }

                if (lengthGivenShape != newLength)
                {
                    throw PythonOps.TypeError("memoryview: product(shape) * itemsize != buffer size");
                }
            }

            return(new MemoryView(_buffer, _start, _end, _step, formatAsString, shapeAsTuple ?? PythonOps.MakeTuple(newLength)));
        }
Beispiel #3
0
        public MemoryView cast([NotNull] string format, [NotNull, AllowNull] object shape)
        {
            if (!_isCContig)
            {
                throw PythonOps.TypeError("memoryview: casts are restricted to C-contiguous views");
            }

            if (_shape.Contains(0) || _strides.Contains(0))
            {
                throw PythonOps.TypeError("memoryview: cannot cast view with zeros in shape or strides");
            }

            PythonTuple?shapeAsTuple = null;

            if (shape != null)
            {
                if (!(shape is PythonList) && !(shape is PythonTuple))
                {
                    throw PythonOps.TypeError("shape must be a list or a tuple");
                }

                shapeAsTuple = PythonTuple.Make(shape);
                int newNDim = shapeAsTuple.Count;

                if (newNDim > MaximumDimensions)
                {
                    throw PythonOps.TypeError("memoryview: number of dimensions must not exceed {0}", MaximumDimensions);
                }

                if (_numDims != 1 && newNDim != 1)
                {
                    throw PythonOps.TypeError("memoryview: cast must be 1D -> ND or ND -> 1D");
                }
            }

            int newItemsize;

            if (!TypecodeOps.TryGetTypecodeWidth(format, out newItemsize))
            {
                throw PythonOps.ValueError(
                          "memoryview: destination format must be a native single character format prefixed with an optional '@'");
            }

            if (!TypecodeOps.IsByteFormat(this._format) && !TypecodeOps.IsByteFormat(format))
            {
                throw PythonOps.TypeError("memoryview: cannot cast between two non-byte formats");
            }

            if ((_numItems * _itemSize) % newItemsize != 0)
            {
                throw PythonOps.TypeError("memoryview: length is not a multiple of itemsize");
            }

            int newLength = _numItems * _itemSize / newItemsize;

            int[] newShape;
            if (shapeAsTuple != null)
            {
                newShape = new int[shapeAsTuple.Count];
                int lengthGivenShape = 1;
                for (int i = 0; i < shapeAsTuple.Count; i++)
                {
                    newShape[i]       = Converter.ConvertToInt32(shapeAsTuple[i]);
                    lengthGivenShape *= newShape[i];
                }

                if (lengthGivenShape != newLength)
                {
                    throw PythonOps.TypeError("memoryview: product(shape) * itemsize != buffer size");
                }
            }
            else
            {
                newShape = new int[] { newLength };
            }

            return(new MemoryView(this, format, newItemsize, newShape));
        }