From d1a3541bdbe74e2cd3cc181925f7292a720c39db Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Wed, 1 Aug 2018 00:07:51 +0200 Subject: [PATCH] simplify --- include/boost/histogram/dynamic_histogram.hpp | 9 +-- .../histogram/storage/adaptive_storage.hpp | 2 +- src/python/histogram.cpp | 76 +++++++++---------- 3 files changed, 39 insertions(+), 48 deletions(-) diff --git a/include/boost/histogram/dynamic_histogram.hpp b/include/boost/histogram/dynamic_histogram.hpp index 10111a9c..a6938213 100644 --- a/include/boost/histogram/dynamic_histogram.hpp +++ b/include/boost/histogram/dynamic_histogram.hpp @@ -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 friend class iterator_over; - friend class ::boost::python::access; + friend class python_access; friend class ::boost::serialization::access; template void serialize(Archive&, unsigned); diff --git a/include/boost/histogram/storage/adaptive_storage.hpp b/include/boost/histogram/storage/adaptive_storage.hpp index 82b24629..ffd3ed39 100644 --- a/include/boost/histogram/storage/adaptive_storage.hpp +++ b/include/boost/histogram/storage/adaptive_storage.hpp @@ -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 void serialize(Archive&, unsigned); diff --git a/src/python/histogram.cpp b/src/python/histogram.cpp index 83f1bc26..1f71eaf0 100644 --- a/src/python/histogram.cpp +++ b/src/python/histogram.cpp @@ -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 - str operator()(T*, const Buffer&) { + bp::str operator()(T*, const Buffer&, bp::list&, bp::list& strides) { strides.append(sizeof(T)); - return dtype_typestr(); + return bp::dtype_typestr(); } template - str operator()(void*, const Buffer&) { + bp::str operator()(void*, const Buffer&, bp::list&, bp::list& strides) { strides.append(sizeof(uint8_t)); - return dtype_typestr(); + return bp::dtype_typestr(); } template - 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(); + return bp::dtype_typestr(); } template - 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(); + return bp::dtype_typestr(); } }; struct data_visitor { - const list& shapes; - const list& strides; - data_visitor(const list& sh, const list& st) : shapes(sh), strides(st) {} template - object operator()(T* tp, const Buffer&) const { - return make_tuple(reinterpret_cast(tp), true); + bp::object operator()(T* tp, const Buffer&, const bp::list&, + const bp::list&) const { + return bp::make_tuple(reinterpret_cast(tp), true); } template - 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()); + return np::zeros(bp::tuple(shapes), np::dtype::get_builtin()); } template - 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()); + auto a = np::empty(bp::tuple(shapes), np::dtype::get_builtin()); for (std::size_t i = 0, n = bp::len(shapes); i < n; ++i) const_cast(a.get_strides())[i] = bp::extract(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 { template @@ -383,7 +380,8 @@ void register_histogram() { .def(bp::init()) // .def(bp::init()) #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,