private static char OrderStyleToChar(BufferOrderStyle order, bool eitherOneValid) { char style = 'C'; if (order == BufferOrderStyle.C) { style = 'C'; } else if (order == BufferOrderStyle.Fortran) { style = 'F'; } else if (order == BufferOrderStyle.EitherOne) { if (eitherOneValid) { style = 'A'; } else { throw new ArgumentException("BufferOrderStyle can not be EitherOne and has to be C or Fortran"); } } return(style); }
/// <summary> /// Returns true if the memory defined by the view is C-style (order is 'C') or Fortran-style (order is 'F') contiguous or either one (order is 'A'). Returns false otherwise. /// </summary> /// <param name="order">C-style (order is 'C') or Fortran-style (order is 'F') contiguous or either one (order is 'A')</param> public bool IsContiguous(BufferOrderStyle order) { if (disposedValue) { throw new ObjectDisposedException(nameof(PyBuffer)); } return(Convert.ToBoolean(Runtime.PyBuffer_IsContiguous(ref _view, OrderStyleToChar(order, true)))); }
/// <summary> /// Copy len bytes from view to its contiguous representation in buf. order can be 'C' or 'F' or 'A' (for C-style or Fortran-style ordering or either one). 0 is returned on success, -1 on error. /// </summary> /// <param name="order">order can be 'C' or 'F' or 'A' (for C-style or Fortran-style ordering or either one).</param> /// <param name="buf">Buffer to copy to</param> public void ToContiguous(IntPtr buf, BufferOrderStyle order) { if (disposedValue) { throw new ObjectDisposedException(nameof(PyBuffer)); } if (Runtime.PyBuffer_ToContiguous(buf, ref _view, _view.len, OrderStyleToChar(order, true)) < 0) { throw PythonException.ThrowLastAsClrException(); } }
/// <summary> /// Copy contiguous len bytes from buf to view. fort can be 'C' or 'F' (for C-style or Fortran-style ordering). /// </summary> public void FromContiguous(IntPtr buf, long len, BufferOrderStyle fort) { if (disposedValue) { throw new ObjectDisposedException(nameof(PyBuffer)); } if (Runtime.PyVersion < new Version(3, 7)) { throw new NotSupportedException("FromContiguous requires at least Python 3.7"); } if (Runtime.PyBuffer_FromContiguous(ref _view, buf, (IntPtr)len, OrderStyleToChar(fort, false)) < 0) { throw PythonException.ThrowLastAsClrException(); } }
/// <summary> /// Copy len bytes from view to its contiguous representation in buf. order can be 'C' or 'F' or 'A' (for C-style or Fortran-style ordering or either one). 0 is returned on success, -1 on error. /// </summary> /// <param name="order">order can be 'C' or 'F' or 'A' (for C-style or Fortran-style ordering or either one).</param> /// <param name="buf">Buffer to copy to</param> public void ToContiguous(IntPtr buf, BufferOrderStyle order) { if (disposedValue) { throw new ObjectDisposedException(nameof(PyBuffer)); } if (Runtime.PyVersion < new Version(3, 6)) { throw new NotSupportedException("ToContiguous requires at least Python 3.6"); } if (Runtime.PyBuffer_ToContiguous(buf, ref _view, _view.len, OrderStyleToChar(order, true)) < 0) { throw new PythonException(); } }
/// <summary> /// Fill the strides array with byte-strides of a contiguous (C-style if order is 'C' or Fortran-style if order is 'F') array of the given shape with the given number of bytes per element. /// </summary> public static void FillContiguousStrides(int ndims, IntPtr shape, IntPtr strides, int itemsize, BufferOrderStyle order) { Runtime.PyBuffer_FillContiguousStrides(ndims, shape, strides, itemsize, OrderStyleToChar(order, false)); }