-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndarray.h
206 lines (142 loc) · 4.72 KB
/
ndarray.h
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//==========================================================
// ndarray.h: C++ interface for easy access to numpy arrays
//
// J. De Ridder
//==========================================================
#ifndef NDARRAY_H
#define NDARRAY_H
// The C-struct to retrieve the ctypes structure.
// Note: Order of struct members must be the same as in Python.
// Member names are not recognized!
template <typename T>
struct numpyArray
{
T *data;
long *shape;
long *strides;
};
// Traits need to used because the return type of the []-operator can be
// a subarray or an element, depending whether all the axes are exhausted.
template<typename datatype, int ndim> class Ndarray; // forward declaration
template<typename datatype, int ndim>
struct getItemTraits
{
typedef Ndarray<datatype, ndim-1> returnType;
};
template<typename datatype>
struct getItemTraits<datatype, 1>
{
typedef datatype& returnType;
};
// Ndarray definition
template<typename datatype, int ndim>
class Ndarray
{
private:
datatype *data;
long *shape;
long *strides;
public:
Ndarray(datatype *data, long *shape, long *strides);
Ndarray(const Ndarray<datatype, ndim>& array);
Ndarray(const numpyArray<datatype>& array);
long getShape(const int axis);
typename getItemTraits<datatype, ndim>::returnType operator[](unsigned long i);
};
// Ndarray constructor
template<typename datatype, int ndim>
Ndarray<datatype, ndim>::Ndarray(datatype *data, long *shape, long *strides)
{
this->data = data;
this->shape = shape;
this->strides = strides;
}
// Ndarray copy constructor
template<typename datatype, int ndim>
Ndarray<datatype, ndim>::Ndarray(const Ndarray<datatype, ndim>& array)
{
this->data = array.data;
this->shape = array.shape;
this->strides = array.strides;
}
// Ndarray constructor from ctypes structure
template<typename datatype, int ndim>
Ndarray<datatype, ndim>::Ndarray(const numpyArray<datatype>& array)
{
this->data = array.data;
this->shape = array.shape;
this->strides = array.strides;
}
// Ndarray method to get length of given axis
template<typename datatype, int ndim>
long Ndarray<datatype, ndim>::getShape(const int axis)
{
return this->shape[axis];
}
// Ndarray overloaded []-operator.
// The [i][j][k] selection is recursively replaced by i*strides[0]+j*strides[1]+k*strides[2]
// at compile time, using template meta-programming. If the axes are not exhausted, return
// a subarray, else return an element.
template<typename datatype, int ndim>
typename getItemTraits<datatype, ndim>::returnType
Ndarray<datatype, ndim>::operator[](unsigned long i)
{
return Ndarray<datatype, ndim-1>(&this->data[i*this->strides[0]], &this->shape[1], &this->strides[1]);
}
// Template partial specialisation of Ndarray.
// For 1D Ndarrays, the [] operator should return an element, not a subarray, so it needs
// to be special-cased. In principle only the operator[] method should be specialised, but
// for some reason my gcc version seems to require that then the entire class with all its
// methods are specialised.
template<typename datatype>
class Ndarray<datatype, 1>
{
private:
datatype *data;
long *shape;
long *strides;
public:
Ndarray(datatype *data, long *shape, long *strides);
Ndarray(const Ndarray<datatype, 1>& array);
Ndarray(const numpyArray<datatype>& array);
long getShape(const int axis);
typename getItemTraits<datatype, 1>::returnType operator[](unsigned long i);
};
// Ndarray partial specialised constructor
template<typename datatype>
Ndarray<datatype, 1>::Ndarray(datatype *data, long *shape, long *strides)
{
this->data = data;
this->shape = shape;
this->strides = strides;
}
// Ndarray partially specialised copy constructor
template<typename datatype>
Ndarray<datatype, 1>::Ndarray(const Ndarray<datatype, 1>& array)
{
this->data = array.data;
this->shape = array.shape;
this->strides = array.strides;
}
// Ndarray partially specialised constructor from ctypes structure
template<typename datatype>
Ndarray<datatype, 1>::Ndarray(const numpyArray<datatype>& array)
{
this->data = array.data;
this->shape = array.shape;
this->strides = array.strides;
}
// Ndarray method to get length of given axis
template<typename datatype>
long Ndarray<datatype, 1>::getShape(const int axis)
{
return this->shape[axis];
}
// Partial specialised [] operator: for 1D arrays, return an element rather than a subarray
template<typename datatype>
typename getItemTraits<datatype, 1>::returnType
Ndarray<datatype, 1>::operator[](unsigned long i)
{
return this->data[i*this->strides[0]];
}
#endif