1
2
3
4
5
6
7
8 #ifndef NDARRAY_H
9 #define NDARRAY_H
10
11
12
13
14
15
16
17
18 template <typename T>
19 struct numpyArray
20 {
21 T *data;
22 long *shape;
23 long *strides;
24 };
25
26
27
28
29
30
31
32 template<typename datatype, int ndim> class Ndarray;
33
34
35 template<typename datatype, int ndim>
36 struct getItemTraits
37 {
38 typedef Ndarray<datatype, ndim-1> returnType;
39 };
40
41
42 template<typename datatype>
43 struct getItemTraits<datatype, 1>
44 {
45 typedef datatype& returnType;
46 };
47
48
49
50
51
52 template<typename datatype, int ndim>
53 class Ndarray
54 {
55 private:
56 datatype *data;
57 long *shape;
58 long *strides;
59
60 public:
61 Ndarray(datatype *data, long *shape, long *strides);
62 Ndarray(const Ndarray<datatype, ndim>& array);
63 Ndarray(const numpyArray<datatype>& array);
64 long getShape(const int axis);
65 typename getItemTraits<datatype, ndim>::returnType operator[](unsigned long i);
66 };
67
68
69
70
71 template<typename datatype, int ndim>
72 Ndarray<datatype, ndim>::Ndarray(datatype *data, long *shape, long *strides)
73 {
74 this->data = data;
75 this->shape = shape;
76 this->strides = strides;
77 }
78
79
80
81
82 template<typename datatype, int ndim>
83 Ndarray<datatype, ndim>::Ndarray(const Ndarray<datatype, ndim>& array)
84 {
85 this->data = array.data;
86 this->shape = array.shape;
87 this->strides = array.strides;
88 }
89
90
91
92
93 template<typename datatype, int ndim>
94 Ndarray<datatype, ndim>::Ndarray(const numpyArray<datatype>& array)
95 {
96 this->data = array.data;
97 this->shape = array.shape;
98 this->strides = array.strides;
99 }
100
101
102
103
104 template<typename datatype, int ndim>
105 long Ndarray<datatype, ndim>::getShape(const int axis)
106 {
107 return this->shape[axis];
108 }
109
110
111
112
113
114
115
116
117 template<typename datatype, int ndim>
118 typename getItemTraits<datatype, ndim>::returnType
119 Ndarray<datatype, ndim>::operator[](unsigned long i)
120 {
121 return Ndarray<datatype, ndim-1>(&this->data[i*this->strides[0]], &this->shape[1], &this->strides[1]);
122 }
123
124
125
126
127
128
129
130
131
132 template<typename datatype>
133 class Ndarray<datatype, 1>
134 {
135 private:
136 datatype *data;
137 long *shape;
138 long *strides;
139
140 public:
141 Ndarray(datatype *data, long *shape, long *strides);
142 Ndarray(const Ndarray<datatype, 1>& array);
143 Ndarray(const numpyArray<datatype>& array);
144 long getShape(const int axis);
145 typename getItemTraits<datatype, 1>::returnType operator[](unsigned long i);
146 };
147
148
149
150
151 template<typename datatype>
152 Ndarray<datatype, 1>::Ndarray(datatype *data, long *shape, long *strides)
153 {
154 this->data = data;
155 this->shape = shape;
156 this->strides = strides;
157 }
158
159
160
161
162
163 template<typename datatype>
164 Ndarray<datatype, 1>::Ndarray(const Ndarray<datatype, 1>& array)
165 {
166 this->data = array.data;
167 this->shape = array.shape;
168 this->strides = array.strides;
169 }
170
171
172
173
174
175 template<typename datatype>
176 Ndarray<datatype, 1>::Ndarray(const numpyArray<datatype>& array)
177 {
178 this->data = array.data;
179 this->shape = array.shape;
180 this->strides = array.strides;
181 }
182
183
184
185
186
187 template<typename datatype>
188 long Ndarray<datatype, 1>::getShape(const int axis)
189 {
190 return this->shape[axis];
191 }
192
193
194
195
196
197 template<typename datatype>
198 typename getItemTraits<datatype, 1>::returnType
199 Ndarray<datatype, 1>::operator[](unsigned long i)
200 {
201 return this->data[i*this->strides[0]];
202 }
203
204
205
206 #endif
207
, as shown below in the list of files. Do
link, since this is subject to change and can break easily.