forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp.frombuffer.cs
More file actions
126 lines (113 loc) · 4.25 KB
/
np.frombuffer.cs
File metadata and controls
126 lines (113 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using NumSharp.Utilities;
namespace NumSharp
{
public static partial class np
{
public static NDArray frombuffer(byte[] bytes, Type dtype)
{
//TODO! all types
if (dtype.Name == nameof(Int16))
{
var size = bytes.Length / InfoOf<short>.Size;
var ints = new short[size];
for (var index = 0; index < size; index++)
{
ints[index] = BitConverter.ToInt16(bytes, index * InfoOf<short>.Size);
}
return new NDArray(ints);
}
else if (dtype.Name == nameof(Int32))
{
var size = bytes.Length / InfoOf<int>.Size;
var ints = new int[size];
for (var index = 0; index < size; index++)
{
ints[index] = BitConverter.ToInt32(bytes, index * InfoOf<int>.Size);
}
return new NDArray(ints);
}
else if (dtype.Name == nameof(Int64))
{
var size = bytes.Length / InfoOf<long>.Size;
var ints = new long[size];
for (var index = 0; index < size; index++)
{
ints[index] = BitConverter.ToInt64(bytes, index * InfoOf<long>.Size);
}
return new NDArray(ints);
}
else if (dtype.Name == nameof(UInt16))
{
var size = bytes.Length / InfoOf<ushort>.Size;
var ints = new ushort[size];
for (var index = 0; index < size; index++)
{
ints[index] = BitConverter.ToUInt16(bytes, index * InfoOf<ushort>.Size);
}
return new NDArray(ints);
}
else if (dtype.Name == nameof(UInt32))
{
var size = bytes.Length / InfoOf<uint>.Size;
var ints = new uint[size];
for (var index = 0; index < size; index++)
{
ints[index] = BitConverter.ToUInt32(bytes, index * InfoOf<uint>.Size);
}
return new NDArray(ints);
}
else if (dtype.Name == nameof(UInt64))
{
var size = bytes.Length / InfoOf<ulong>.Size;
var ints = new ulong[size];
for (var index = 0; index < size; index++)
{
ints[index] = BitConverter.ToUInt64(bytes, index * InfoOf<ulong>.Size);
}
return new NDArray(ints);
}
else if (dtype.Name == nameof(Single))
{
var size = bytes.Length / InfoOf<float>.Size;
var floats = new float[size];
for (var index = 0; index < size; index++)
{
floats[index] = BitConverter.ToSingle(bytes, index * InfoOf<float>.Size);
}
return new NDArray(floats);
}
else if (dtype.Name == nameof(Double))
{
var size = bytes.Length / InfoOf<double>.Size;
var floats = new double[size];
for (var index = 0; index < size; index++)
{
floats[index] = BitConverter.ToDouble(bytes, index * InfoOf<double>.Size);
}
return new NDArray(floats);
}
else if (dtype.Name == nameof(Byte))
{
var size = bytes.Length / InfoOf<byte>.Size;
var ints = bytes;
return new NDArray(bytes);
}
throw new NotImplementedException("");
}
public static NDArray frombuffer(byte[] bytes, string dtype)
{
if (dtype == ">u4")
{
var size = bytes.Length / InfoOf<uint>.Size;
var ints = new uint[size];
for (var index = 0; index < size; index++)
{
ints[index] = (uint)(bytes[0] * 256 + bytes[1] + bytes[2] * 256 + bytes[3]);
}
return new NDArray(ints);
}
throw new NotImplementedException("");
}
}
}