-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathArray.h
More file actions
160 lines (141 loc) · 5.49 KB
/
Array.h
File metadata and controls
160 lines (141 loc) · 5.49 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
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
// -*- c++ -*-
/*
* Copyright (c) 2010-2012, Jim Bosch
* All rights reserved.
*
* ndarray is distributed under a simple BSD-like license;
* see the LICENSE file that should be present in the root
* of the source distribution, or alternately available at:
* https://github.com/ndarray/ndarray
*/
#ifndef NDARRAY_BP_Array_h_INCLUDED
#define NDARRAY_BP_Array_h_INCLUDED
#include "boost/numpy.hpp"
#include "ndarray.h"
#include "ndarray/bp_fwd.h"
#include <vector>
namespace ndarray {
namespace detail {
inline void destroyManagerCObject(void * p) {
#if PY_MAJOR_VERSION == 2
Manager::Ptr * b = reinterpret_cast<Manager::Ptr*>(p);
#else
Manager::Ptr * b = reinterpret_cast<Manager::Ptr*>(PyCapsule_GetPointer(reinterpret_cast<PyObject*>(p), 0));
#endif
delete b;
}
inline boost::python::object makePyObject(Manager::Ptr const & x) {
boost::intrusive_ptr< ExternalManager<boost::python::object> > y
= boost::dynamic_pointer_cast< ExternalManager<boost::python::object> >(x);
if (y) {
return y->getOwner();
}
#if PY_MAJOR_VERSION == 2
boost::python::handle<> h(::PyCObject_FromVoidPtr(new Manager::Ptr(x), &destroyManagerCObject));
#else
boost::python::handle<> h(::PyCapsule_New(new Manager::Ptr(x), 0, (PyCapsule_Destructor)&destroyManagerCObject));
#endif
return boost::python::object(h);
}
} // namespace detail
template <typename T, int N, int C>
class ToBoostPython< Array<T,N,C> > {
public:
typedef boost::numpy::ndarray result_type;
static boost::numpy::ndarray apply(Array<T,N,C> const & array) {
boost::numpy::dtype dtype
= boost::numpy::dtype::get_builtin<typename boost::remove_const<T>::type>();
boost::python::object owner = detail::makePyObject(array.getManager());
Py_ssize_t itemsize = dtype.get_itemsize();
ndarray::Vector<Size,N> shape_elements = array.getShape();
ndarray::Vector<Offset,N> strides_elements = array.getStrides();
std::vector<Py_intptr_t> shape_bytes(N);
std::vector<Py_intptr_t> strides_bytes(N);
for (int n=0; n<N; ++n) {
shape_bytes[n] = shape_elements[n];
strides_bytes[n] = strides_elements[n] * itemsize;
}
return boost::numpy::from_data(array.getData(), dtype, shape_bytes, strides_bytes, owner);
}
};
template <typename T, int N, int C>
class FromBoostPython< Array<T,N,C> > {
public:
explicit FromBoostPython(boost::python::object const & input_) : input(input_) {}
bool convertible() {
if (input.is_none()) return true;
try {
boost::numpy::ndarray array = boost::python::extract<boost::numpy::ndarray>(input);
boost::numpy::dtype dtype
= boost::numpy::dtype::get_builtin<typename boost::remove_const<T>::type>();
boost::numpy::ndarray::bitflag flags = array.get_flags();
if (dtype != array.get_dtype()) return false;
if (N != array.get_nd()) return false;
if (!boost::is_const<T>::value && !(flags & boost::numpy::ndarray::WRITEABLE)) return false;
if (C > 0) {
Offset requiredStride = sizeof(T);
for (int i = 0; i < C; ++i) {
if ((array.shape(N-i-1) > 1) && (array.strides(N-i-1) != requiredStride)) {
return false;
}
requiredStride *= array.shape(N-i-1);
}
} else if (C < 0) {
Offset requiredStride = sizeof(T);
for (int i = 0; i < -C; ++i) {
if ((array.shape(i) > 1) && (array.strides(i) != requiredStride)) {
return false;
}
requiredStride *= array.shape(i);
}
}
} catch (boost::python::error_already_set) {
boost::python::handle_exception();
PyErr_Clear();
return false;
}
return true;
}
Array<T,N,C> operator()() {
if (input.is_none()) return Array<T,N,C>();
boost::numpy::ndarray array = boost::python::extract<boost::numpy::ndarray>(input);
boost::numpy::dtype dtype = array.get_dtype();
Py_ssize_t itemsize = dtype.get_itemsize();
for (int i = 0; i < N; ++i) {
if ((array.shape(i) > 1) && (array.strides(i) % itemsize != 0)) {
PyErr_SetString(
PyExc_TypeError,
"Cannot convert array to C++: strides must be an integer multiple of the element size"
);
boost::python::throw_error_already_set();
}
}
boost::python::object obj_owner = array.get_base();
if (obj_owner.is_none()) {
obj_owner = array;
}
Vector<Size,N> shape;
Vector<Offset,N> strides;
for (int i=0; i<N; ++i) {
shape[i] = array.shape(i);
if (shape[i] > 1) {
strides[i] = array.strides(i) / itemsize;
} else {
strides[i] = 1;
}
}
Array<T,N,C> r = ndarray::external(
reinterpret_cast<T*>(array.get_data()), shape, strides, obj_owner
);
return r;
}
boost::python::object input;
};
} // namespace ndarray
namespace boost { namespace numpy {
template <typename T, int N, int C>
numpy::ndarray array(::ndarray::Array<T,N,C> const & arg) {
return ::ndarray::ToBoostPython< ::ndarray::Array<T,N,C> >::apply(arg);
}
}} // namespace boost::numpy
#endif // !NDARRAY_BP_Array_h_INCLUDED