From 06bc8ceef50983b85ef4739628cff0bced253eb6 Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Sat, 4 Aug 2018 12:43:54 +0200 Subject: [PATCH] nicer naming --- .../boost/histogram/detail/axis_visitor.hpp | 85 +++++++++---------- include/boost/histogram/dynamic_histogram.hpp | 6 +- include/boost/histogram/static_histogram.hpp | 8 +- 3 files changed, 50 insertions(+), 49 deletions(-) diff --git a/include/boost/histogram/detail/axis_visitor.hpp b/include/boost/histogram/detail/axis_visitor.hpp index ff3e8313..c81fa25d 100644 --- a/include/boost/histogram/detail/axis_visitor.hpp +++ b/include/boost/histogram/detail/axis_visitor.hpp @@ -27,12 +27,12 @@ using static_axes = std::tuple; namespace { template -struct axes_equal_tuple_vecvar { +struct axes_equal_static_dynamic_impl { bool& equal; const StaticAxes& t; const DynamicAxes& v; - axes_equal_tuple_vecvar(bool& eq, const StaticAxes& tt, - const DynamicAxes& vv) + axes_equal_static_dynamic_impl(bool& eq, const StaticAxes& tt, + const DynamicAxes& vv) : equal(eq), t(tt), v(vv) {} template void operator()(Int) const { @@ -42,11 +42,23 @@ struct axes_equal_tuple_vecvar { } }; +template +bool axes_equal_static_static_impl(mp11::mp_true, const static_axes& t, + const static_axes& u) { + return t == u; +} + +template +bool axes_equal_static_static_impl(mp11::mp_false, const static_axes&, + const static_axes&) { + return false; +} + template -struct axes_assign_tuple_vecvar { +struct axes_assign_static_dynamic_impl { StaticAxes& t; const DynamicAxes& v; - axes_assign_tuple_vecvar(StaticAxes& tt, const DynamicAxes& vv) + axes_assign_static_dynamic_impl(StaticAxes& tt, const DynamicAxes& vv) : t(tt), v(vv) {} template void operator()(Int) const { @@ -56,34 +68,21 @@ struct axes_assign_tuple_vecvar { }; template -struct axes_assign_vecvar_tuple { +struct axes_assign_dynamic_static_impl { DynamicAxes& v; const StaticAxes& t; - axes_assign_vecvar_tuple(DynamicAxes& vv, const StaticAxes& tt) + axes_assign_dynamic_static_impl(DynamicAxes& vv, const StaticAxes& tt) : v(vv), t(tt) {} template void operator()(Int) const { v[Int::value] = std::get(t); } }; - -template -bool axes_equal_impl(mp11::mp_true, const static_axes& t, - const static_axes& u) { - return t == u; -} - -template -bool axes_equal_impl(mp11::mp_false, const static_axes&, - const static_axes&) { - return false; -} - } // namespace template bool axes_equal(const static_axes& t, const static_axes& u) { - return axes_equal_impl( + return axes_equal_static_static_impl( mp11::mp_same, mp11::mp_list>(), t, u); } @@ -99,16 +98,17 @@ template bool axes_equal(const static_axes& t, const dynamic_axes& u) { if (sizeof...(Ts) != u.size()) return false; bool equal = true; - auto fn = axes_equal_tuple_vecvar, dynamic_axes>( - equal, t, u); + auto fn = + axes_equal_static_dynamic_impl, dynamic_axes>( + equal, t, u); mp11::mp_for_each>(fn); return equal; } template void axes_assign(static_axes& t, const dynamic_axes& u) { - auto fn = - axes_assign_tuple_vecvar, dynamic_axes>(t, u); + auto fn = axes_assign_static_dynamic_impl, + dynamic_axes>(t, u); mp11::mp_for_each>(fn); } @@ -120,8 +120,8 @@ bool axes_equal(const dynamic_axes& t, const static_axes& u) { template void axes_assign(dynamic_axes& t, const static_axes& u) { t.resize(sizeof...(Us)); - auto fn = - axes_assign_vecvar_tuple, static_axes>(t, u); + auto fn = axes_assign_dynamic_static_impl, + static_axes>(t, u); mp11::mp_for_each>(fn); } @@ -139,7 +139,16 @@ void axes_assign(dynamic_axes& t, const dynamic_axes& u) { for (std::size_t i = 0; i < t.size(); ++i) { t[i] = u[i]; } } -struct field_count_visitor : public static_visitor { +struct shape_collector { + std::vector::iterator iter; + shape_collector(std::vector::iterator i) : iter(i) {} + template + void operator()(const T& a) { + *iter++ = a.shape(); + } +}; + +struct field_counter { std::size_t value = 1; template void operator()(const T& t) { @@ -148,25 +157,15 @@ struct field_count_visitor : public static_visitor { }; template -struct unary_visitor : public static_visitor { - Unary& unary; - unary_visitor(Unary& u) : unary(u) {} - template - void operator()(const Axis& a) const { +struct unary_adaptor_visitor : public static_visitor { + Unary&& unary; + unary_adaptor_visitor(Unary&& u) : unary(std::forward(u)) {} + template + void operator()(const T& a) const { unary(a); } }; -struct shape_vector_visitor { - std::vector shapes; - std::vector::iterator iter; - shape_vector_visitor(unsigned n) : shapes(n) { iter = shapes.begin(); } - template - void operator()(const Axis& a) { - *iter++ = a.shape(); - } -}; - } // namespace detail } // namespace histogram } // namespace boost diff --git a/include/boost/histogram/dynamic_histogram.hpp b/include/boost/histogram/dynamic_histogram.hpp index 9dd4aacd..1cdc914c 100644 --- a/include/boost/histogram/dynamic_histogram.hpp +++ b/include/boost/histogram/dynamic_histogram.hpp @@ -230,7 +230,9 @@ public: template void for_each_axis(Unary&& unary) const { for (const auto& a : axes_) { - apply_visitor(detail::unary_visitor(unary), a); + apply_visitor( + detail::unary_adaptor_visitor(std::forward(unary)), + a); } } @@ -269,7 +271,7 @@ private: mutable detail::index_cache index_cache_; std::size_t size_from_axes() const noexcept { - detail::field_count_visitor v; + detail::field_counter v; for_each_axis(v); return v.value; } diff --git a/include/boost/histogram/static_histogram.hpp b/include/boost/histogram/static_histogram.hpp index 850e95c9..9bd9c91e 100644 --- a/include/boost/histogram/static_histogram.hpp +++ b/include/boost/histogram/static_histogram.hpp @@ -270,7 +270,7 @@ private: mutable detail::index_cache index_cache_; std::size_t size_from_axes() const noexcept { - detail::field_count_visitor v; + detail::field_counter v; for_each_axis(v); return v.value; } @@ -424,9 +424,9 @@ private: template void reduce_impl(H& h, const std::vector& b) const { - detail::shape_vector_visitor v(dim()); - for_each_axis(v); - detail::index_mapper m(v.shapes, b); + std::vector shape(dim()); + for_each_axis(detail::shape_collector(shape.begin())); + detail::index_mapper m(shape, b); do { h.storage_.add(m.second, storage_[m.first]); } while (m.next()); }