diff --git a/src/NumSharp.Core/Creation/np.frombuffer.cs b/src/NumSharp.Core/Creation/np.frombuffer.cs index 20c4a6e9..66064a25 100644 --- a/src/NumSharp.Core/Creation/np.frombuffer.cs +++ b/src/NumSharp.Core/Creation/np.frombuffer.cs @@ -1,109 +1,112 @@ using System; +using System.Runtime.InteropServices; using NumSharp.Utilities; namespace NumSharp { public static partial class np { - public static NDArray frombuffer(byte[] bytes, Type dtype) + public static NDArray fromspan(Span span, Type dtype) { - //TODO! all types - if (dtype.Name == nameof(Int16)) + if (dtype == typeof(int)) { - var size = bytes.Length / InfoOf.Size; - var ints = new short[size]; - for (var index = 0; index < size; index++) - { - ints[index] = BitConverter.ToInt16(bytes, index * InfoOf.Size); - } - - return new NDArray(ints); + return new NDArray(MemoryMarshal.Cast(span).ToArray()); } - else if (dtype.Name == nameof(Int32)) + else if (dtype == typeof(uint)) { - var size = bytes.Length / InfoOf.Size; - var ints = new int[size]; - for (var index = 0; index < size; index++) - { - ints[index] = BitConverter.ToInt32(bytes, index * InfoOf.Size); - } - - return new NDArray(ints); + return new NDArray(MemoryMarshal.Cast(span).ToArray()); } - else if (dtype.Name == nameof(Int64)) + else if (dtype == typeof(short)) { - var size = bytes.Length / InfoOf.Size; - var ints = new long[size]; - for (var index = 0; index < size; index++) - { - ints[index] = BitConverter.ToInt64(bytes, index * InfoOf.Size); - } - - return new NDArray(ints); + return new NDArray(MemoryMarshal.Cast(span).ToArray()); } - else if (dtype.Name == nameof(UInt16)) + else if (dtype == typeof(ushort)) { - var size = bytes.Length / InfoOf.Size; - var ints = new ushort[size]; - for (var index = 0; index < size; index++) - { - ints[index] = BitConverter.ToUInt16(bytes, index * InfoOf.Size); - } - - return new NDArray(ints); + return new NDArray(MemoryMarshal.Cast(span).ToArray()); } - else if (dtype.Name == nameof(UInt32)) + else if (dtype == typeof(long)) { - var size = bytes.Length / InfoOf.Size; - var ints = new uint[size]; - for (var index = 0; index < size; index++) - { - ints[index] = BitConverter.ToUInt32(bytes, index * InfoOf.Size); - } - - return new NDArray(ints); + return new NDArray(MemoryMarshal.Cast(span).ToArray()); } - else if (dtype.Name == nameof(UInt64)) + else if (dtype == typeof(ulong)) { - var size = bytes.Length / InfoOf.Size; - var ints = new ulong[size]; - for (var index = 0; index < size; index++) - { - ints[index] = BitConverter.ToUInt64(bytes, index * InfoOf.Size); - } - - return new NDArray(ints); + return new NDArray(MemoryMarshal.Cast(span).ToArray()); } - else if (dtype.Name == nameof(Single)) + else if (dtype == typeof(bool)) { - var size = bytes.Length / InfoOf.Size; - var floats = new float[size]; - for (var index = 0; index < size; index++) - { - floats[index] = BitConverter.ToSingle(bytes, index * InfoOf.Size); - } - - return new NDArray(floats); + return new NDArray(MemoryMarshal.Cast(span).ToArray()); } - else if (dtype.Name == nameof(Double)) + else if (dtype == typeof(char)) { - var size = bytes.Length / InfoOf.Size; - var floats = new double[size]; - for (var index = 0; index < size; index++) - { - floats[index] = BitConverter.ToDouble(bytes, index * InfoOf.Size); - } - - return new NDArray(floats); + return new NDArray(MemoryMarshal.Cast(span).ToArray()); } - else if (dtype.Name == nameof(Byte)) + else if (dtype == typeof(byte)) { - var size = bytes.Length / InfoOf.Size; - var ints = bytes; - return new NDArray(bytes); + return new NDArray(span.ToArray()); } throw new NotImplementedException(""); + } + + + public static NDArray frombuffer(byte[] bytes, Type dtype) + { + if (dtype == typeof(int)) + { + var span = MemoryMarshal.Cast(bytes.AsSpan()); + return new NDArray(span.ToArray()); + } + else if (dtype == typeof(uint)) + { + var span = MemoryMarshal.Cast(bytes.AsSpan()); + return new NDArray(span.ToArray()); + } + else if (dtype == typeof(short)) + { + var span = MemoryMarshal.Cast(bytes.AsSpan()); + return new NDArray(span.ToArray()); + } + else if (dtype == typeof(ushort)) + { + var span = MemoryMarshal.Cast(bytes.AsSpan()); + return new NDArray(span.ToArray()); + } + else if (dtype == typeof(long)) + { + var span = MemoryMarshal.Cast(bytes.AsSpan()); + return new NDArray(span.ToArray()); + } + else if (dtype == typeof(ulong)) + { + var span = MemoryMarshal.Cast(bytes.AsSpan()); + return new NDArray(span.ToArray()); + } + else if (dtype == typeof(bool)) + { + var span = MemoryMarshal.Cast(bytes.AsSpan()); + return new NDArray(span.ToArray()); + } + else if (dtype == typeof(char)) + { + var span = MemoryMarshal.Cast(bytes.AsSpan()); + return new NDArray(span.ToArray()); + } + else if (dtype == typeof(float)) + { + var span = MemoryMarshal.Cast(bytes.AsSpan()); + return new NDArray(span.ToArray()); + } + else if (dtype == typeof(double)) + { + var span = MemoryMarshal.Cast(bytes.AsSpan()); + return new NDArray(span.ToArray()); + } + else if (dtype == typeof(byte)) + { + return new NDArray(bytes); + } + + throw new NotImplementedException($"frombuffer: dtype {dtype} not supported."); } public static NDArray frombuffer(byte[] bytes, string dtype)