1 module dopt.core.types;
2 
3 import std.algorithm;
4 
5 enum DataType
6 {
7     float32,
8     int32
9 }
10 
11 size_t sizeOf(DataType t)
12 {
13     switch(t)
14     {
15         case DataType.float32:
16             return float.sizeof;
17 
18         case DataType.int32:
19             return int.sizeof;
20 
21         default:
22             throw new Exception("Not implemented");
23     }
24 }
25 
26 struct TensorType
27 {
28     DataType elementType;
29     size_t[] shape;
30 
31     this(DataType et, size_t[] s)
32     {
33         elementType = et;
34         shape = s.dup;
35     }
36 
37     this(const TensorType t)
38     {
39         elementType = t.elementType;
40         shape = t.shape.dup;
41     }
42 
43     bool opEquals(const TensorType t) const
44     {
45         return elementType == t.elementType && shape == t.shape;
46     }
47 
48     @property size_t rank() const
49     {
50         return shape.length;
51     }
52 
53     @property size_t volume() const
54     {
55         return shape.fold!((a, b) => a * b)(cast(size_t)1);
56     }
57 }
58 
59 /**
60 Creates a wrapper around a user allocated buffer
61 */
62 struct Buffer
63 {
64     public
65     {
66         this(void[] buf)
67         {
68             mBuffer = buf;
69         }
70 
71         T[] as(T)()
72         {
73             return cast(T[])mBuffer;
74         }
75 
76         const(T)[] as(T)() const
77         {
78             return cast(const(T)[])mBuffer;
79         }
80     }
81 
82     private
83     {
84         void[] mBuffer;
85     }
86 }