1 // Copyright 2019 Hans Dembinski
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 #ifndef BOOST_HISTOGRAM_DETAIL_INDEX_TRANSLATOR_HPP
8 #define BOOST_HISTOGRAM_DETAIL_INDEX_TRANSLATOR_HPP
9
10 #include <algorithm>
11 #include <boost/histogram/axis/traits.hpp>
12 #include <boost/histogram/axis/variant.hpp>
13 #include <boost/histogram/detail/relaxed_equal.hpp>
14 #include <boost/histogram/detail/relaxed_tuple_size.hpp>
15 #include <boost/histogram/fwd.hpp>
16 #include <boost/histogram/multi_index.hpp>
17 #include <boost/mp11/algorithm.hpp>
18 #include <boost/mp11/integer_sequence.hpp>
19 #include <cassert>
20 #include <initializer_list>
21 #include <tuple>
22 #include <vector>
23
24 namespace boost {
25 namespace histogram {
26 namespace detail {
27
28 template <class A>
29 struct index_translator {
30 using index_type = axis::index_type;
31 using multi_index_type = multi_index<relaxed_tuple_size_t<A>::value>;
32 using cref = const A&;
33
34 cref dst, src;
35 bool pass_through[buffer_size<A>::value];
36
index_translatorboost::histogram::detail::index_translator37 index_translator(cref d, cref s) : dst{d}, src{s} { init(dst, src); }
38
39 template <class T>
initboost::histogram::detail::index_translator40 void init(const T& a, const T& b) {
41 std::transform(a.begin(), a.end(), b.begin(), pass_through,
42 [](const auto& a, const auto& b) {
43 return axis::visit(
44 [&](const auto& a) {
45 using U = std::decay_t<decltype(a)>;
46 return relaxed_equal{}(a, axis::get<U>(b));
47 },
48 a);
49 });
50 }
51
52 template <class... Ts>
initboost::histogram::detail::index_translator53 void init(const std::tuple<Ts...>& a, const std::tuple<Ts...>& b) {
54 using Seq = mp11::mp_iota_c<sizeof...(Ts)>;
55 mp11::mp_for_each<Seq>([&](auto I) {
56 pass_through[I] = relaxed_equal{}(std::get<I>(a), std::get<I>(b));
57 });
58 }
59
60 template <class T>
translateboost::histogram::detail::index_translator61 static index_type translate(const T& dst, const T& src, index_type i) noexcept {
62 assert(axis::traits::is_continuous<T>::value == false); // LCOV_EXCL_LINE: unreachable
63 return dst.index(src.value(i));
64 }
65
66 template <class... Ts, class It>
implboost::histogram::detail::index_translator67 void impl(const std::tuple<Ts...>& a, const std::tuple<Ts...>& b, It i,
68 index_type* j) const noexcept {
69 using Seq = mp11::mp_iota_c<sizeof...(Ts)>;
70 mp11::mp_for_each<Seq>([&](auto I) {
71 if (pass_through[I])
72 *(j + I) = *(i + I);
73 else
74 *(j + I) = this->translate(std::get<I>(a), std::get<I>(b), *(i + I));
75 });
76 }
77
78 template <class T, class It>
implboost::histogram::detail::index_translator79 void impl(const T& a, const T& b, It i, index_type* j) const noexcept {
80 const bool* p = pass_through;
81 for (unsigned k = 0; k < a.size(); ++k, ++i, ++j, ++p) {
82 if (*p)
83 *j = *i;
84 else {
85 const auto& bk = b[k];
86 axis::visit(
87 [&](const auto& ak) {
88 using U = std::decay_t<decltype(ak)>;
89 *j = this->translate(ak, axis::get<U>(bk), *i);
90 },
91 a[k]);
92 }
93 }
94 }
95
96 template <class Indices>
operator ()boost::histogram::detail::index_translator97 auto operator()(const Indices& seq) const noexcept {
98 auto mi = multi_index_type::create(seq.size());
99 impl(dst, src, seq.begin(), mi.begin());
100 return mi;
101 }
102 };
103
104 template <class Axes>
make_index_translator(const Axes & dst,const Axes & src)105 auto make_index_translator(const Axes& dst, const Axes& src) noexcept {
106 return index_translator<Axes>{dst, src};
107 }
108
109 } // namespace detail
110 } // namespace histogram
111 } // namespace boost
112
113 #endif
114