File indexing completed on 2025-01-18 09:38:11
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_HISTOGRAM_DETAIL_INDEX_TRANSLATOR_HPP
0008 #define BOOST_HISTOGRAM_DETAIL_INDEX_TRANSLATOR_HPP
0009
0010 #include <algorithm>
0011 #include <boost/histogram/axis/traits.hpp>
0012 #include <boost/histogram/axis/variant.hpp>
0013 #include <boost/histogram/detail/relaxed_equal.hpp>
0014 #include <boost/histogram/detail/relaxed_tuple_size.hpp>
0015 #include <boost/histogram/fwd.hpp>
0016 #include <boost/histogram/multi_index.hpp>
0017 #include <boost/mp11/algorithm.hpp>
0018 #include <boost/mp11/integer_sequence.hpp>
0019 #include <cassert>
0020 #include <initializer_list>
0021 #include <tuple>
0022 #include <vector>
0023
0024 namespace boost {
0025 namespace histogram {
0026 namespace detail {
0027
0028 template <class A>
0029 struct index_translator {
0030 using index_type = axis::index_type;
0031 using multi_index_type = multi_index<relaxed_tuple_size_t<A>::value>;
0032 using cref = const A&;
0033
0034 cref dst, src;
0035 bool pass_through[buffer_size<A>::value];
0036
0037 index_translator(cref d, cref s) : dst{d}, src{s} { init(dst, src); }
0038
0039 template <class T>
0040 void init(const T& a, const T& b) {
0041 std::transform(a.begin(), a.end(), b.begin(), pass_through,
0042 [](const auto& a, const auto& b) {
0043 return axis::visit(
0044 [&](const auto& a) {
0045 using U = std::decay_t<decltype(a)>;
0046 return relaxed_equal{}(a, axis::get<U>(b));
0047 },
0048 a);
0049 });
0050 }
0051
0052 template <class... Ts>
0053 void init(const std::tuple<Ts...>& a, const std::tuple<Ts...>& b) {
0054 using Seq = mp11::mp_iota_c<sizeof...(Ts)>;
0055 mp11::mp_for_each<Seq>([&](auto I) {
0056 pass_through[I] = relaxed_equal{}(std::get<I>(a), std::get<I>(b));
0057 });
0058 }
0059
0060 template <class T>
0061 static index_type translate(const T& dst, const T& src, index_type i) noexcept {
0062 assert(axis::traits::is_continuous<T>::value == false);
0063 return dst.index(src.value(i));
0064 }
0065
0066 template <class... Ts, class It>
0067 void impl(const std::tuple<Ts...>& a, const std::tuple<Ts...>& b, It i,
0068 index_type* j) const noexcept {
0069 using Seq = mp11::mp_iota_c<sizeof...(Ts)>;
0070 mp11::mp_for_each<Seq>([&](auto I) {
0071 if (pass_through[I])
0072 *(j + I) = *(i + I);
0073 else
0074 *(j + I) = this->translate(std::get<I>(a), std::get<I>(b), *(i + I));
0075 });
0076 }
0077
0078 template <class T, class It>
0079 void impl(const T& a, const T& b, It i, index_type* j) const noexcept {
0080 const bool* p = pass_through;
0081 for (unsigned k = 0; k < a.size(); ++k, ++i, ++j, ++p) {
0082 if (*p)
0083 *j = *i;
0084 else {
0085 const auto& bk = b[k];
0086 axis::visit(
0087 [&](const auto& ak) {
0088 using U = std::decay_t<decltype(ak)>;
0089 *j = this->translate(ak, axis::get<U>(bk), *i);
0090 },
0091 a[k]);
0092 }
0093 }
0094 }
0095
0096 template <class Indices>
0097 auto operator()(const Indices& seq) const noexcept {
0098 auto mi = multi_index_type::create(seq.size());
0099 impl(dst, src, seq.begin(), mi.begin());
0100 return mi;
0101 }
0102 };
0103
0104 template <class Axes>
0105 auto make_index_translator(const Axes& dst, const Axes& src) noexcept {
0106 return index_translator<Axes>{dst, src};
0107 }
0108
0109 }
0110 }
0111 }
0112
0113 #endif