index_translator.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #ifndef BOOST_HISTOGRAM_DETAIL_INDEX_TRANSLATOR_HPP
  7. #define BOOST_HISTOGRAM_DETAIL_INDEX_TRANSLATOR_HPP
  8. #include <algorithm>
  9. #include <boost/histogram/axis/traits.hpp>
  10. #include <boost/histogram/axis/variant.hpp>
  11. #include <boost/histogram/detail/relaxed_equal.hpp>
  12. #include <boost/histogram/detail/relaxed_tuple_size.hpp>
  13. #include <boost/histogram/fwd.hpp>
  14. #include <boost/histogram/multi_index.hpp>
  15. #include <boost/mp11/algorithm.hpp>
  16. #include <boost/mp11/integer_sequence.hpp>
  17. #include <cassert>
  18. #include <initializer_list>
  19. #include <tuple>
  20. #include <vector>
  21. namespace boost {
  22. namespace histogram {
  23. namespace detail {
  24. template <class A>
  25. struct index_translator {
  26. using index_type = axis::index_type;
  27. using multi_index_type = multi_index<relaxed_tuple_size_t<A>::value>;
  28. using cref = const A&;
  29. cref dst, src;
  30. bool pass_through[buffer_size<A>::value];
  31. index_translator(cref d, cref s) : dst{d}, src{s} { init(dst, src); }
  32. template <class T>
  33. void init(const T& a, const T& b) {
  34. std::transform(a.begin(), a.end(), b.begin(), pass_through,
  35. [](const auto& a, const auto& b) {
  36. return axis::visit(
  37. [&](const auto& a) {
  38. using U = std::decay_t<decltype(a)>;
  39. return relaxed_equal{}(a, axis::get<U>(b));
  40. },
  41. a);
  42. });
  43. }
  44. template <class... Ts>
  45. void init(const std::tuple<Ts...>& a, const std::tuple<Ts...>& b) {
  46. using Seq = mp11::mp_iota_c<sizeof...(Ts)>;
  47. mp11::mp_for_each<Seq>([&](auto I) {
  48. pass_through[I] = relaxed_equal{}(std::get<I>(a), std::get<I>(b));
  49. });
  50. }
  51. template <class T>
  52. static index_type translate(const T& dst, const T& src, index_type i) noexcept {
  53. assert(axis::traits::is_continuous<T>::value == false); // LCOV_EXCL_LINE: unreachable
  54. return dst.index(src.value(i));
  55. }
  56. template <class... Ts, class It>
  57. void impl(const std::tuple<Ts...>& a, const std::tuple<Ts...>& b, It i,
  58. index_type* j) const noexcept {
  59. using Seq = mp11::mp_iota_c<sizeof...(Ts)>;
  60. mp11::mp_for_each<Seq>([&](auto I) {
  61. if (pass_through[I])
  62. *(j + I) = *(i + I);
  63. else
  64. *(j + I) = this->translate(std::get<I>(a), std::get<I>(b), *(i + I));
  65. });
  66. }
  67. template <class T, class It>
  68. void impl(const T& a, const T& b, It i, index_type* j) const noexcept {
  69. const bool* p = pass_through;
  70. for (unsigned k = 0; k < a.size(); ++k, ++i, ++j, ++p) {
  71. if (*p)
  72. *j = *i;
  73. else {
  74. const auto& bk = b[k];
  75. axis::visit(
  76. [&](const auto& ak) {
  77. using U = std::decay_t<decltype(ak)>;
  78. *j = this->translate(ak, axis::get<U>(bk), *i);
  79. },
  80. a[k]);
  81. }
  82. }
  83. }
  84. template <class Indices>
  85. auto operator()(const Indices& seq) const noexcept {
  86. auto mi = multi_index_type::create(seq.size());
  87. impl(dst, src, seq.begin(), mi.begin());
  88. return mi;
  89. }
  90. };
  91. template <class Axes>
  92. auto make_index_translator(const Axes& dst, const Axes& src) noexcept {
  93. return index_translator<Axes>{dst, src};
  94. }
  95. } // namespace detail
  96. } // namespace histogram
  97. } // namespace boost
  98. #endif