|
6 | 6 | All rights reserved. Use of this source code is governed by a |
7 | 7 | BSD-style license that can be found in the LICENSE file. |
8 | 8 | */ |
9 | | -#pragma once |
| 9 | +#ifndef PYBIND_BACKPORT_HPP |
| 10 | +#define PYBIND_BACKPORT_HPP |
10 | 11 |
|
11 | 12 | #include <cstddef> |
12 | 13 | #include <algorithm> |
13 | 14 |
|
14 | 15 | #include "pybind11/numpy.h" |
15 | 16 |
|
16 | | -namespace pybind11 { namespace backport { |
17 | | - struct PyArray_Proxy { |
18 | | - PyObject_HEAD |
19 | | - char *data; |
20 | | - int nd; |
21 | | - ssize_t *dimensions; |
22 | | - ssize_t *strides; |
23 | | - PyObject *base; |
24 | | - PyObject *descr; |
25 | | - int flags; |
26 | | - }; |
27 | | - |
28 | | - struct PyArrayDescr_Proxy { |
29 | | - PyObject_HEAD |
30 | | - PyObject *typeobj; |
31 | | - char kind; |
32 | | - char type; |
33 | | - char byteorder; |
34 | | - char flags; |
35 | | - int type_num; |
36 | | - int elsize; |
37 | | - int alignment; |
38 | | - char *subarray; |
39 | | - PyObject *fields; |
40 | | - PyObject *names; |
41 | | - }; |
| 17 | +namespace pybind11 |
| 18 | +{ |
| 19 | + namespace backport |
| 20 | + { |
| 21 | + struct PyArray_Proxy |
| 22 | + { |
| 23 | + PyObject_HEAD |
| 24 | + char* data; |
| 25 | + int nd; |
| 26 | + ssize_t* dimensions; |
| 27 | + ssize_t* strides; |
| 28 | + PyObject* base; |
| 29 | + PyObject* descr; |
| 30 | + int flags; |
| 31 | + }; |
| 32 | + |
| 33 | + struct PyArrayDescr_Proxy |
| 34 | + { |
| 35 | + PyObject_HEAD |
| 36 | + PyObject* typeobj; |
| 37 | + char kind; |
| 38 | + char type; |
| 39 | + char byteorder; |
| 40 | + char flags; |
| 41 | + int type_num; |
| 42 | + int elsize; |
| 43 | + int alignment; |
| 44 | + char* subarray; |
| 45 | + PyObject* fields; |
| 46 | + PyObject* names; |
| 47 | + }; |
42 | 48 |
|
43 | 49 | #ifndef PyArray_GET_ |
44 | | -# define PyArray_GET_(ptr, attr) \ |
45 | | - (reinterpret_cast<::pybind11::backport::PyArray_Proxy*>(ptr)->attr) |
| 50 | +#define PyArray_GET_(ptr, attr) \ |
| 51 | + (reinterpret_cast<::pybind11::backport::PyArray_Proxy*>(ptr)->attr) |
46 | 52 | #endif |
47 | 53 | #ifndef PyArrayDescr_GET_ |
48 | | -# define PyArrayDescr_GET_(ptr, attr) \ |
49 | | - (reinterpret_cast<::pybind11::backport::PyArrayDescr_Proxy*>(ptr)->attr) |
| 54 | +#define PyArrayDescr_GET_(ptr, attr) \ |
| 55 | + (reinterpret_cast<::pybind11::backport::PyArrayDescr_Proxy*>(ptr)->attr) |
50 | 56 | #endif |
51 | 57 |
|
52 | | - class array : public pybind11::array { |
53 | | - public: |
54 | | - using size_type = std::size_t; |
| 58 | + class array : public pybind11::array |
| 59 | + { |
| 60 | + public: |
| 61 | + using size_type = std::size_t; |
| 62 | + |
| 63 | + using pybind11::array::array; |
| 64 | + |
| 65 | + array() = default; |
| 66 | + |
| 67 | + template<typename T> |
| 68 | + array(const std::vector<size_type>& shape, |
| 69 | + const std::vector<size_type>& strides, |
| 70 | + const T* ptr, handle base = {}) |
| 71 | + : array(buffer_info(const_cast<T*>(ptr), |
| 72 | + sizeof(T), |
| 73 | + format_descriptor<T>::value, |
| 74 | + shape.size(), shape, strides)) |
| 75 | + { |
| 76 | + if (base) throw std::runtime_error("array base is not supported yet"); |
| 77 | + } |
55 | 78 |
|
56 | | - using pybind11::array::array; |
| 79 | + template<typename T> |
| 80 | + array(const std::vector<size_type> &shape, |
| 81 | + const T* ptr, handle base = {}) |
| 82 | + : array(shape, default_strides(shape, sizeof(T)), ptr, base) |
| 83 | + { |
| 84 | + } |
57 | 85 |
|
58 | | - array() = default; |
59 | | - |
60 | | - template<typename T> array(const std::vector<size_type>& shape, |
61 | | - const std::vector<size_type>& strides, |
62 | | - const T* ptr, handle base = {}) |
63 | | - : array(buffer_info(const_cast<T*>(ptr), sizeof(T), format_descriptor<T>::value, |
64 | | - shape.size(), shape, strides)) { |
65 | | - if (base) throw std::runtime_error("array base is not supported yet"); |
66 | | - } |
67 | | - |
68 | | - template<typename T> array(const std::vector<size_type> &shape, |
69 | | - const T *ptr, handle base = {}) |
70 | | - : array(shape, default_strides(shape, sizeof(T)), ptr, base) { } |
71 | | - |
72 | | - template<typename T> array(size_type size, const T *ptr, handle base) |
73 | | - : array(std::vector<size_type>{size}, ptr) { } |
74 | | - |
75 | | - size_type size() const { |
76 | | - return std::accumulate(shape(), shape() + ndim(), size_type{1}, std::multiplies<size_type>()); |
77 | | - } |
78 | | - |
79 | | - size_type itemsize() const { |
80 | | - return static_cast<size_type>(PyArrayDescr_GET_(PyArray_GET_(m_ptr, descr), elsize)); |
81 | | - } |
82 | | - |
83 | | - size_type ndim() const { |
84 | | - return static_cast<size_type>(PyArray_GET_(m_ptr, nd)); |
85 | | - } |
86 | | - |
87 | | - const size_type* shape() const { |
88 | | - return reinterpret_cast<const size_type *>(PyArray_GET_(m_ptr, dimensions)); |
89 | | - } |
90 | | - |
91 | | - const size_type* strides() const { |
92 | | - return reinterpret_cast<const size_type *>(PyArray_GET_(m_ptr, strides)); |
93 | | - } |
94 | | - |
95 | | - template<typename... Ix> void* data() { |
96 | | - return static_cast<void *>(PyArray_GET_(m_ptr, data)); |
97 | | - } |
98 | | - |
99 | | - template<typename... Ix> void* mutable_data() { |
100 | | - // check_writeable(); |
101 | | - return static_cast<void *>(PyArray_GET_(m_ptr, data)); |
102 | | - } |
103 | | - |
104 | | - template<typename... Ix> size_type offset_at(Ix... index) const { |
105 | | - if (sizeof...(index) > ndim()) |
106 | | - fail_dim_check(sizeof...(index), "too many indices for an array"); |
107 | | - return get_byte_offset(index...); |
108 | | - } |
109 | | - |
110 | | - size_type offset_at() const { return 0; } |
111 | | - |
112 | | - protected: |
113 | | - void fail_dim_check(size_type dim, const std::string& msg) const { |
114 | | - throw index_error(msg + ": " + std::to_string(dim) + |
115 | | - " (ndim = " + std::to_string(ndim()) + ")"); |
116 | | - } |
117 | | - |
118 | | - template<typename... Ix> size_type get_byte_offset(Ix... index) const { |
119 | | - const size_type idx[] = { static_cast<size_type>(index)... }; |
120 | | - if (!std::equal(idx + 0, idx + sizeof...(index), shape(), std::less<size_type>{})) { |
121 | | - auto mismatch = std::mismatch(idx + 0, idx + sizeof...(index), shape(), std::less<size_type>{}); |
122 | | - throw index_error(std::string("index ") + std::to_string(*mismatch.first) + |
123 | | - " is out of bounds for axis " + std::to_string(mismatch.first - idx) + |
124 | | - " with size " + std::to_string(*mismatch.second)); |
125 | | - } |
126 | | - return std::inner_product(idx + 0, idx + sizeof...(index), strides(), size_type{0}); |
127 | | - } |
128 | | - |
129 | | - size_type get_byte_offset() const { return 0; } |
130 | | - |
131 | | - static std::vector<size_type> default_strides(const std::vector<size_type>& shape, |
132 | | - size_type itemsize) { |
133 | | - auto ndim = shape.size(); |
134 | | - std::vector<size_type> strides(ndim); |
135 | | - if (ndim) { |
136 | | - std::fill(strides.begin(), strides.end(), itemsize); |
137 | | - for (size_type i = 0; i < ndim - 1; i++) |
138 | | - for (size_type j = 0; j < ndim - 1 - i; j++) |
139 | | - strides[j] *= shape[ndim - 1 - i]; |
140 | | - } |
141 | | - return strides; |
142 | | - } |
143 | | - }; |
144 | | -}} // namespace pybind11::backport |
| 86 | + template<typename T> |
| 87 | + array(size_type size, const T *ptr, handle base) |
| 88 | + : array(std::vector<size_type>{size}, ptr) |
| 89 | + { |
| 90 | + } |
| 91 | + |
| 92 | + size_type size() const |
| 93 | + { |
| 94 | + return std::accumulate(shape(), shape() + ndim(), size_type{1}, std::multiplies<size_type>()); |
| 95 | + } |
| 96 | + |
| 97 | + size_type itemsize() const |
| 98 | + { |
| 99 | + return static_cast<size_type>(PyArrayDescr_GET_(PyArray_GET_(m_ptr, descr), elsize)); |
| 100 | + } |
| 101 | + |
| 102 | + size_type ndim() const |
| 103 | + { |
| 104 | + return static_cast<size_type>(PyArray_GET_(m_ptr, nd)); |
| 105 | + } |
| 106 | + |
| 107 | + const size_type* shape() const |
| 108 | + { |
| 109 | + return reinterpret_cast<const size_type*>(PyArray_GET_(m_ptr, dimensions)); |
| 110 | + } |
| 111 | + |
| 112 | + const size_type* strides() const |
| 113 | + { |
| 114 | + return reinterpret_cast<const size_type*>(PyArray_GET_(m_ptr, strides)); |
| 115 | + } |
| 116 | + |
| 117 | + template<typename... Ix> |
| 118 | + void* data() |
| 119 | + { |
| 120 | + return static_cast<void*>(PyArray_GET_(m_ptr, data)); |
| 121 | + } |
| 122 | + |
| 123 | + template<typename... Ix> |
| 124 | + void* mutable_data() |
| 125 | + { |
| 126 | + // check_writeable(); |
| 127 | + return static_cast<void *>(PyArray_GET_(m_ptr, data)); |
| 128 | + } |
| 129 | + |
| 130 | + template<typename... Ix> |
| 131 | + size_type offset_at(Ix... index) const |
| 132 | + { |
| 133 | + if (sizeof...(index) > ndim()) |
| 134 | + { |
| 135 | + fail_dim_check(sizeof...(index), "too many indices for an array"); |
| 136 | + } |
| 137 | + return get_byte_offset(index...); |
| 138 | + } |
| 139 | + |
| 140 | + size_type offset_at() const |
| 141 | + { |
| 142 | + return 0; |
| 143 | + } |
| 144 | + |
| 145 | + protected: |
| 146 | + |
| 147 | + void fail_dim_check(size_type dim, const std::string& msg) const |
| 148 | + { |
| 149 | + throw index_error(msg + ": " + std::to_string(dim) + |
| 150 | + " (ndim = " + std::to_string(ndim()) + ")"); |
| 151 | + } |
| 152 | + |
| 153 | + template<typename... Ix> |
| 154 | + size_type get_byte_offset(Ix... index) const |
| 155 | + { |
| 156 | + const size_type idx[] = { static_cast<size_type>(index)... }; |
| 157 | + if (!std::equal(idx + 0, idx + sizeof...(index), shape(), std::less<size_type>{})) |
| 158 | + { |
| 159 | + auto mismatch = std::mismatch(idx + 0, idx + sizeof...(index), shape(), std::less<size_type>{}); |
| 160 | + throw index_error(std::string("index ") + std::to_string(*mismatch.first) + |
| 161 | + " is out of bounds for axis " + std::to_string(mismatch.first - idx) + |
| 162 | + " with size " + std::to_string(*mismatch.second)); |
| 163 | + } |
| 164 | + return std::inner_product(idx + 0, idx + sizeof...(index), strides(), size_type{0}); |
| 165 | + } |
| 166 | + |
| 167 | + size_type get_byte_offset() const |
| 168 | + { |
| 169 | + return 0; |
| 170 | + } |
| 171 | + |
| 172 | + static std::vector<size_type> |
| 173 | + default_strides(const std::vector<size_type>& shape, size_type itemsize) |
| 174 | + { |
| 175 | + auto ndim = shape.size(); |
| 176 | + std::vector<size_type> strides(ndim); |
| 177 | + if (ndim) |
| 178 | + { |
| 179 | + std::fill(strides.begin(), strides.end(), itemsize); |
| 180 | + for (size_type i = 0; i < ndim - 1; i++) |
| 181 | + for (size_type j = 0; j < ndim - 1 - i; j++) |
| 182 | + strides[j] *= shape[ndim - 1 - i]; |
| 183 | + } |
| 184 | + return strides; |
| 185 | + } |
| 186 | + }; |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +#endif |
0 commit comments