-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.hh
140 lines (136 loc) · 3.18 KB
/
array.hh
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
#ifndef CAPY_NUMPY_HH
#define CAPY_NUMPY_HH
#include "capy.hh"
#include <numpy/arrayobject.h>
namespace Capy
{
template <typename T>
struct NumpyTypeCode
{};
template <>
struct NumpyTypeCode<bool>
{
static const int value = NPY_BOOL;
};
template <>
struct NumpyTypeCode<npy_byte>
{
static const int value = NPY_BYTE;
};
template <>
struct NumpyTypeCode<npy_ubyte>
{
static const int value = NPY_UBYTE;
};
template <>
struct NumpyTypeCode<npy_short>
{
static const int value = NPY_SHORT;
};
template <>
struct NumpyTypeCode<npy_ushort>
{
static const int value = NPY_USHORT;
};
template <>
struct NumpyTypeCode<npy_int>
{
static const int value = NPY_INT;
};
template <>
struct NumpyTypeCode<npy_uint>
{
static const int value = NPY_UINT;
};
template <>
struct NumpyTypeCode<npy_long>
{
static const int value = NPY_LONG;
};
template <>
struct NumpyTypeCode<npy_ulong>
{
static const int value = NPY_ULONG;
};
template <>
struct NumpyTypeCode<npy_longlong>
{
static const int value = NPY_LONGLONG;
};
template <>
struct NumpyTypeCode<npy_ulonglong>
{
static const int value = NPY_ULONGLONG;
};
template <>
struct NumpyTypeCode<npy_float>
{
static const int value = NPY_FLOAT;
};
template <>
struct NumpyTypeCode<npy_double>
{
static const int value = NPY_DOUBLE;
};
template <>
struct NumpyTypeCode<npy_longdouble>
{
static const int value = NPY_LONGDOUBLE;
};
class Array : public Object
{
public:
explicit Array(PyObject *self_)
: Object(self_)
{
if (!PyArray_Check(self))
throw TypeError("argument must be a numpy.ndarray instance");
}
Array(const Object &other)
: Object(other)
{
if (!PyArray_Check(self))
throw TypeError("argument must be a numpy.ndarray instance");
}
template <typename T>
Array(T *data, int nd, npy_intp *dims)
: Object(PyArray_SimpleNewFromData(
nd, dims, NumpyTypeCode<T>::value, data))
{}
template <typename T>
Array(T *data, npy_intp size)
: Object(PyArray_SimpleNewFromData(
1, &size, NumpyTypeCode<T>::value, data))
{}
template <typename T>
T *data()
{
return static_cast<T *>(PyArray_DATA(self));
}
int ndim() const
{
return PyArray_NDIM(self);
}
const npy_intp *dims() const
{
return PyArray_DIMS(self);
}
const npy_intp *strides() const
{
return PyArray_STRIDES(self);
}
int flags() const
{
return PyArray_FLAGS(self);
}
npy_intp size() const
{
return PyArray_SIZE(self);
}
int itemsize() const
{
return PyArray_ITEMSIZE(self);
}
};
}
#endif