This commit is contained in:
Hans Dembinski 2018-07-14 17:22:13 +02:00
parent aed4255fc0
commit bca4892d62
5 changed files with 40 additions and 90 deletions

View File

@ -9,7 +9,6 @@
#include <boost/histogram/axis/interval_view.hpp>
#include <boost/histogram/axis/iterator.hpp>
#include <boost/histogram/detail/axis_visitor.hpp>
#include <boost/histogram/detail/cat.hpp>
#include <boost/histogram/detail/meta.hpp>
#include <boost/histogram/histogram_fwd.hpp>
@ -103,24 +102,8 @@ struct bicmp_visitor : public static_visitor<bool> {
return false;
}
template <typename T> bool operator()(const T &t, const T &u) const {
return t == u;
}
};
template <typename T> struct cmp_visitor : public static_visitor<bool> {
const T &t;
cmp_visitor(const T &tt) : t(tt) {}
template <typename U> bool operator()(const U &u) const {
return impl(mp11::mp_same<T, U>(), u);
}
template <typename U> bool impl(mp11::mp_true, const U &u) const {
return t == u;
}
template <typename U> bool impl(mp11::mp_false, const U &) const {
return false;
template <typename T> bool operator()(const T &a, const T &b) const {
return a == b;
}
};
@ -228,7 +211,12 @@ public:
template <typename T, typename = requires_bounded_type<T>>
bool operator==(const T &t) const {
// variant::operator==(T) is implemented, but only to fail, cannot use it
return ::boost::apply_visitor(detail::cmp_visitor<T>(t), *this);
auto tp = ::boost::get<::boost::histogram::detail::rm_cv_ref<T>>(this);
return tp && *tp == t;
}
template <typename T> bool operator!=(T &&t) const {
return !operator==(std::forward<T>(t));
}
const_iterator begin() const { return const_iterator(*this, 0); }

View File

@ -10,9 +10,6 @@
#include <boost/histogram/axis/any.hpp>
#include <boost/histogram/detail/meta.hpp>
#include <boost/mp11.hpp>
#include <boost/variant/get.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/variant.hpp>
#include <tuple>
#include <type_traits>
#include <vector>
@ -22,41 +19,6 @@ namespace histogram {
namespace detail {
namespace {
template <typename Variant>
struct axes_equal_visitor : public static_visitor<bool> {
const Variant &lhs;
axes_equal_visitor(const Variant &v) : lhs(v) {}
template <typename T> bool operator()(const T &rhs) const {
return impl(mp11::mp_contains<typename Variant::types, T>(), rhs);
}
template <typename T> bool impl(mp11::mp_true, const T &rhs) const {
auto tp = boost::get<T>(&lhs);
return tp && *tp == rhs;
}
template <typename T> bool impl(mp11::mp_false, const T &) const {
return false;
}
};
template <typename V> struct axes_assign_visitor : public static_visitor<void> {
V &lhs;
axes_assign_visitor(V &v) : lhs(v) {}
template <typename U> void operator()(const U &rhs) const {
impl(mp11::mp_contains<typename V::types, U>(), rhs);
}
template <typename U> void impl(mp11::mp_true, const U &rhs) const {
lhs = rhs;
}
template <typename U> void impl(mp11::mp_false, const U &) const {
BOOST_ASSERT_MSG(false,
"cannot assign U to variant if it is not a bounded type");
}
};
template <typename Tuple, typename VecVar> struct axes_equal_tuple_vecvar {
bool &equal;
@ -89,22 +51,6 @@ template <typename VecVar, typename Tuple> struct axes_assign_vecvar_tuple {
v[Int::value] = std::get<Int::value>(t);
}
};
}
struct field_count_visitor : public static_visitor<void> {
mutable std::size_t value = 1;
template <typename T> void operator()(const T &t) const {
value *= t.shape();
}
};
template <typename Unary> struct unary_visitor : public static_visitor<void> {
Unary &unary;
unary_visitor(Unary &u) : unary(u) {}
template <typename Axis> void operator()(const Axis &a) const { unary(a); }
};
namespace {
template <typename... Ts>
bool axes_equal_impl(mp11::mp_true, const std::tuple<Ts...> &t,
@ -117,7 +63,8 @@ bool axes_equal_impl(mp11::mp_false, const std::tuple<Ts...> &,
const std::tuple<Us...> &) {
return false;
}
}
} // namespace
template <typename... Ts, typename... Us>
bool axes_equal(const std::tuple<Ts...> &t, const std::tuple<Us...> &u) {
@ -172,7 +119,7 @@ bool axes_equal(const std::vector<axis::any<Ts...>> &t,
if (t.size() != u.size())
return false;
for (std::size_t i = 0; i < t.size(); ++i) {
if (!apply_visitor(axes_equal_visitor<axis::any<Ts...>>(t[i]), u[i]))
if (t[i] != u[i])
return false;
}
return true;
@ -182,10 +129,23 @@ template <typename... Ts, typename... Us>
void axes_assign(std::vector<axis::any<Ts...>> &t,
const std::vector<axis::any<Us...>> &u) {
for (std::size_t i = 0; i < t.size(); ++i) {
apply_visitor(axes_assign_visitor<axis::any<Ts...>>(t[i]), u[i]);
t[i] = u[i];
}
}
struct field_count_visitor : public static_visitor<void> {
mutable std::size_t value = 1;
template <typename T> void operator()(const T &t) const {
value *= t.shape();
}
};
template <typename Unary> struct unary_visitor : public static_visitor<void> {
Unary &unary;
unary_visitor(Unary &u) : unary(u) {}
template <typename Axis> void operator()(const Axis &a) const { unary(a); }
};
} // namespace detail
} // namespace histogram
} // namespace boost

View File

@ -11,7 +11,6 @@
#include <boost/histogram/detail/axis_visitor.hpp>
#include <boost/histogram/detail/utility.hpp>
#include <boost/histogram/histogram_fwd.hpp>
#include <boost/variant.hpp>
#include <limits>
#include <sstream>
#include <string>

View File

@ -12,7 +12,6 @@
#include <boost/histogram/detail/utility.hpp>
#include <boost/histogram/literals.hpp>
#include <boost/mp11.hpp>
#include <boost/variant.hpp>
#include <sstream>
#include <string>
#include <tuple>
@ -74,17 +73,6 @@ int main() {
BOOST_TEST_EQ(os.str(), std::string("'\\\'abc\\\''"));
}
// // assign_axis unreachable branch
// {
// using V1 = boost::variant<float>;
// using V2 = boost::variant<int>;
// V1 v1(1.0);
// V2 v2(2);
// boost::apply_visitor(assign_axis<V1>(v1), v2);
// BOOST_TEST_EQ(v1, V1(1.0));
// BOOST_TEST_EQ(v2, V2(2));
// }
// index_mapper 1
{
std::vector<unsigned> n{{2, 2}};

View File

@ -906,6 +906,21 @@ template <typename T1, typename T2> void run_mixed_tests() {
BOOST_TEST_NE(a, b3);
}
// add
{
auto a = make_histogram<adaptive_storage>(T1{}, axis::integer<>{0, 2});
auto b = make_histogram<adaptive_storage>(T2{}, axis::integer<>{0, 2});
BOOST_TEST_EQ(a, b);
a(0); // 1 0
b(1); // 0 1
a += b; // 1 1
BOOST_TEST_EQ(a[0], 1);
BOOST_TEST_EQ(a[1], 1);
auto c = make_histogram<adaptive_storage>(T2{}, axis::integer<>{0, 3});
BOOST_TEST_THROWS(a += c, std::invalid_argument);
}
// copy_assign
{
auto a = make_histogram<adaptive_storage>(T1{}, axis::regular<>{3, 0, 3},