mirror of
https://github.com/boostorg/histogram.git
synced 2026-01-29 19:42:12 +00:00
simplify
This commit is contained in:
@@ -40,13 +40,6 @@ class access;
|
||||
}
|
||||
} // namespace boost
|
||||
|
||||
// forward declaration for python
|
||||
namespace boost {
|
||||
namespace python {
|
||||
class access;
|
||||
}
|
||||
} // namespace boost
|
||||
|
||||
namespace boost {
|
||||
namespace histogram {
|
||||
|
||||
@@ -467,7 +460,7 @@ private:
|
||||
friend class histogram;
|
||||
template <typename H>
|
||||
friend class iterator_over;
|
||||
friend class ::boost::python::access;
|
||||
friend class python_access;
|
||||
friend class ::boost::serialization::access;
|
||||
template <typename Archive>
|
||||
void serialize(Archive&, unsigned);
|
||||
|
||||
@@ -524,7 +524,7 @@ private:
|
||||
Alloc alloc_;
|
||||
buffer_type buffer_;
|
||||
|
||||
friend class ::boost::python::access;
|
||||
friend class python_access;
|
||||
friend class ::boost::serialization::access;
|
||||
template <class Archive>
|
||||
void serialize(Archive&, unsigned);
|
||||
|
||||
@@ -32,61 +32,59 @@ namespace bp = boost::python;
|
||||
|
||||
using pyhistogram = bh::dynamic_histogram<>;
|
||||
|
||||
namespace boost {
|
||||
namespace python {
|
||||
|
||||
#ifdef HAVE_NUMPY
|
||||
class access {
|
||||
namespace boost {
|
||||
namespace histogram {
|
||||
class python_access {
|
||||
public:
|
||||
using mp_int = bh::detail::mp_int;
|
||||
using wcount = bh::detail::wcount;
|
||||
using mp_int = detail::mp_int;
|
||||
using wcount = detail::wcount;
|
||||
|
||||
struct dtype_visitor {
|
||||
list &shapes, &strides;
|
||||
dtype_visitor(list& sh, list& st) : shapes(sh), strides(st) {}
|
||||
template <typename T, typename Buffer>
|
||||
str operator()(T*, const Buffer&) {
|
||||
bp::str operator()(T*, const Buffer&, bp::list&, bp::list& strides) {
|
||||
strides.append(sizeof(T));
|
||||
return dtype_typestr<T>();
|
||||
return bp::dtype_typestr<T>();
|
||||
}
|
||||
template <typename Buffer>
|
||||
str operator()(void*, const Buffer&) {
|
||||
bp::str operator()(void*, const Buffer&, bp::list&, bp::list& strides) {
|
||||
strides.append(sizeof(uint8_t));
|
||||
return dtype_typestr<uint8_t>();
|
||||
return bp::dtype_typestr<uint8_t>();
|
||||
}
|
||||
template <typename Buffer>
|
||||
str operator()(mp_int*, const Buffer&) {
|
||||
bp::str operator()(mp_int*, const Buffer&, bp::list&, bp::list& strides) {
|
||||
strides.append(sizeof(double));
|
||||
return dtype_typestr<double>();
|
||||
return bp::dtype_typestr<double>();
|
||||
}
|
||||
template <typename Buffer>
|
||||
str operator()(wcount*, const Buffer&) {
|
||||
bp::str operator()(wcount*, const Buffer&, bp::list& shapes,
|
||||
bp::list& strides) {
|
||||
strides.append(sizeof(double));
|
||||
strides.append(strides[-1] * 2);
|
||||
shapes.append(2);
|
||||
return dtype_typestr<double>();
|
||||
return bp::dtype_typestr<double>();
|
||||
}
|
||||
};
|
||||
|
||||
struct data_visitor {
|
||||
const list& shapes;
|
||||
const list& strides;
|
||||
data_visitor(const list& sh, const list& st) : shapes(sh), strides(st) {}
|
||||
template <typename T, typename Buffer>
|
||||
object operator()(T* tp, const Buffer&) const {
|
||||
return make_tuple(reinterpret_cast<uintptr_t>(tp), true);
|
||||
bp::object operator()(T* tp, const Buffer&, const bp::list&,
|
||||
const bp::list&) const {
|
||||
return bp::make_tuple(reinterpret_cast<uintptr_t>(tp), true);
|
||||
}
|
||||
template <typename Buffer>
|
||||
object operator()(void*, const Buffer&) const {
|
||||
bp::object operator()(void*, const Buffer&, const bp::list& shapes,
|
||||
const bp::list&) const {
|
||||
// cannot pass non-existent memory to numpy; make new
|
||||
// zero-initialized uint8 array, and pass it
|
||||
return np::zeros(tuple(shapes), np::dtype::get_builtin<uint8_t>());
|
||||
return np::zeros(bp::tuple(shapes), np::dtype::get_builtin<uint8_t>());
|
||||
}
|
||||
template <typename Buffer>
|
||||
object operator()(mp_int* tp, const Buffer& b) const {
|
||||
bp::object operator()(mp_int* tp, const Buffer& b, const bp::list& shapes,
|
||||
const bp::list& strides) const {
|
||||
// cannot pass cpp_int to numpy; make new
|
||||
// double array, fill it and pass it
|
||||
auto a = np::empty(tuple(shapes), np::dtype::get_builtin<double>());
|
||||
auto a = np::empty(bp::tuple(shapes), np::dtype::get_builtin<double>());
|
||||
for (std::size_t i = 0, n = bp::len(shapes); i < n; ++i)
|
||||
const_cast<Py_intptr_t*>(a.get_strides())[i] =
|
||||
bp::extract<int>(strides[i]);
|
||||
@@ -98,27 +96,26 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
static object array_interface(const pyhistogram& self) {
|
||||
dict d;
|
||||
list shapes;
|
||||
list strides;
|
||||
static bp::object array_interface(const pyhistogram& self) {
|
||||
bp::dict d;
|
||||
bp::list shapes;
|
||||
bp::list strides;
|
||||
auto& b = self.storage_.buffer_;
|
||||
d["typestr"] = bh::detail::apply(dtype_visitor(shapes, strides), b);
|
||||
for (auto i = 0u; i < self.dim(); ++i) {
|
||||
if (i) strides.append(strides[-1] * shapes[-1]);
|
||||
d["typestr"] = bh::detail::apply(dtype_visitor(), b, shapes, strides);
|
||||
for (unsigned i = 0; i < self.dim(); ++i) {
|
||||
if (i > 0) strides.append(strides[-1] * shapes[-1]);
|
||||
shapes.append(self.axis(i).shape());
|
||||
}
|
||||
if (self.dim() == 0) shapes.append(0);
|
||||
d["shape"] = tuple(shapes);
|
||||
d["strides"] = tuple(strides);
|
||||
d["data"] = bh::detail::apply(data_visitor(shapes, strides), b);
|
||||
d["shape"] = bp::tuple(shapes);
|
||||
d["strides"] = bp::tuple(strides);
|
||||
d["data"] = bh::detail::apply(data_visitor(), b, shapes, strides);
|
||||
return d;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
} // namespace python
|
||||
} // namespace histogram
|
||||
} // namespace boost
|
||||
#endif
|
||||
|
||||
struct axis_visitor : public boost::static_visitor<bp::object> {
|
||||
template <typename T>
|
||||
@@ -383,7 +380,8 @@ void register_histogram() {
|
||||
.def(bp::init<const pyhistogram&>())
|
||||
// .def(bp::init<pyhistogram &&>())
|
||||
#ifdef HAVE_NUMPY
|
||||
.add_property("__array_interface__", &bp::access::array_interface)
|
||||
.add_property("__array_interface__",
|
||||
&bh::python_access::array_interface)
|
||||
#endif
|
||||
.add_property("dim", &pyhistogram::dim)
|
||||
.def("axis", histogram_axis, bp::arg("i") = 0,
|
||||
|
||||
Reference in New Issue
Block a user