| 37 | namespace boost { |
| 38 | namespace histogram { |
| 39 | class python_access { |
| 40 | public: |
| 41 | using mp_int = detail::mp_int; |
| 42 | using wcount = detail::wcount; |
| 43 | |
| 44 | struct dtype_visitor { |
| 45 | template <typename T, typename Buffer> |
| 46 | bp::str operator()(T*, const Buffer&, bp::list&, bp::list& strides) { |
| 47 | strides.append(sizeof(T)); |
| 48 | return bp::dtype_typestr<T>(); |
| 49 | } |
| 50 | template <typename Buffer> |
| 51 | bp::str operator()(void*, const Buffer&, bp::list&, bp::list& strides) { |
| 52 | strides.append(sizeof(uint8_t)); |
| 53 | return bp::dtype_typestr<uint8_t>(); |
| 54 | } |
| 55 | template <typename Buffer> |
| 56 | bp::str operator()(mp_int*, const Buffer&, bp::list&, bp::list& strides) { |
| 57 | strides.append(sizeof(double)); |
| 58 | return bp::dtype_typestr<double>(); |
| 59 | } |
| 60 | template <typename Buffer> |
| 61 | bp::str operator()(wcount*, const Buffer&, bp::list& shapes, bp::list& strides) { |
| 62 | strides.append(sizeof(double)); |
| 63 | strides.append(strides[-1] * 2); |
| 64 | shapes.append(2); |
| 65 | return bp::dtype_typestr<double>(); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | struct data_visitor { |
| 70 | template <typename T, typename Buffer> |
| 71 | bp::object operator()(T* tp, const Buffer&, const bp::list&, const bp::list&) const { |
| 72 | return bp::make_tuple(reinterpret_cast<uintptr_t>(tp), true); |
| 73 | } |
| 74 | template <typename Buffer> |
| 75 | bp::object operator()(void*, const Buffer&, const bp::list& shapes, |
| 76 | const bp::list&) const { |
| 77 | // cannot pass non-existent memory to numpy; make new |
| 78 | // zero-initialized uint8 array, and pass it |
| 79 | return np::zeros(bp::tuple(shapes), np::dtype::get_builtin<uint8_t>()); |
| 80 | } |
| 81 | template <typename Buffer> |
| 82 | bp::object operator()(mp_int* tp, const Buffer& b, const bp::list& shapes, |
| 83 | const bp::list& strides) const { |
| 84 | // cannot pass cpp_int to numpy; make new |
| 85 | // double array, fill it and pass it |
| 86 | auto a = np::empty(bp::tuple(shapes), np::dtype::get_builtin<double>()); |
| 87 | for (std::size_t i = 0, n = bp::len(shapes); i < n; ++i) |
| 88 | const_cast<Py_intptr_t*>(a.get_strides())[i] = bp::extract<int>(strides[i]); |
| 89 | auto* buf = (double*)a.get_data(); |
| 90 | for (std::size_t i = 0; i < b.size; ++i) { buf[i] = static_cast<double>(tp[i]); } |
| 91 | return a; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | static bp::object array_interface(const pyhistogram& self) { |
| 96 | bp::dict d; |
nothing calls this directly
no outgoing calls
no test coverage detected