linearize.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_LINEARIZE_HPP
  7. #define BOOST_HISTOGRAM_DETAIL_LINEARIZE_HPP
  8. #include <boost/histogram/axis/option.hpp>
  9. #include <boost/histogram/axis/traits.hpp>
  10. #include <boost/histogram/axis/variant.hpp>
  11. #include <boost/histogram/detail/optional_index.hpp>
  12. #include <boost/histogram/fwd.hpp>
  13. #include <boost/histogram/multi_index.hpp>
  14. #include <cassert>
  15. namespace boost {
  16. namespace histogram {
  17. namespace detail {
  18. // initial offset to out must be set
  19. template <class Index, class Opts>
  20. std::size_t linearize(Opts, Index& out, const std::size_t stride,
  21. const axis::index_type size, const axis::index_type idx) {
  22. constexpr bool u = Opts::test(axis::option::underflow);
  23. constexpr bool o = Opts::test(axis::option::overflow);
  24. // must be non-const to avoid if constexpr warning from msvc
  25. bool fast_track = std::is_same<Index, std::size_t>::value || (u && o);
  26. if (fast_track) {
  27. assert(idx >= (u ? -1 : 0));
  28. assert(idx < (o ? size + 1 : size));
  29. assert(idx >= 0 || static_cast<std::size_t>(-idx * stride) <= out);
  30. out += idx * stride;
  31. } else {
  32. assert(idx >= -1);
  33. assert(idx < size + 1);
  34. // must be non-const to avoid if constexpr warning from msvc
  35. bool is_valid = (u || idx >= 0) && (o || idx < size);
  36. if (is_valid)
  37. out += idx * stride;
  38. else
  39. out = invalid_index;
  40. }
  41. return size + u + o;
  42. }
  43. template <class Index, class Axis, class Value>
  44. std::size_t linearize(Index& out, const std::size_t stride, const Axis& ax,
  45. const Value& v) {
  46. // mask options to reduce no. of template instantiations
  47. constexpr auto opts = axis::traits::get_options<Axis>{} &
  48. (axis::option::underflow | axis::option::overflow);
  49. return linearize(opts, out, stride, ax.size(), axis::traits::index(ax, v));
  50. }
  51. // initial offset of out must be zero
  52. template <class Index, class Axis, class Value>
  53. std::size_t linearize_growth(Index& out, axis::index_type& shift,
  54. const std::size_t stride, Axis& a, const Value& v) {
  55. axis::index_type idx;
  56. std::tie(idx, shift) = axis::traits::update(a, v);
  57. constexpr bool u = axis::traits::get_options<Axis>::test(axis::option::underflow);
  58. if (u) ++idx;
  59. if (std::is_same<Index, std::size_t>::value) {
  60. assert(idx < axis::traits::extent(a));
  61. out += idx * stride;
  62. } else {
  63. if (0 <= idx && idx < axis::traits::extent(a))
  64. out += idx * stride;
  65. else
  66. out = invalid_index;
  67. }
  68. return axis::traits::extent(a);
  69. }
  70. // initial offset of out must be zero
  71. template <class A>
  72. std::size_t linearize_index(optional_index& out, const std::size_t stride, const A& ax,
  73. const axis::index_type idx) noexcept {
  74. const auto opt = axis::traits::get_options<A>();
  75. const axis::index_type begin = opt & axis::option::underflow ? -1 : 0;
  76. const axis::index_type end = opt & axis::option::overflow ? ax.size() + 1 : ax.size();
  77. const axis::index_type extent = end - begin;
  78. // i may be arbitrarily out of range
  79. if (begin <= idx && idx < end)
  80. out += (idx - begin) * stride;
  81. else
  82. out = invalid_index;
  83. return extent;
  84. }
  85. template <class A, std::size_t N>
  86. optional_index linearize_indices(const A& axes, const multi_index<N>& indices) noexcept {
  87. assert(axes_rank(axes) == detail::size(indices));
  88. optional_index idx{0}; // offset not used by linearize_index
  89. auto stride = static_cast<std::size_t>(1);
  90. using std::begin;
  91. auto i = begin(indices);
  92. for_each_axis(axes,
  93. [&](const auto& a) { stride *= linearize_index(idx, stride, a, *i++); });
  94. return idx;
  95. }
  96. template <class Index, class... Ts, class Value>
  97. std::size_t linearize(Index& o, const std::size_t s, const axis::variant<Ts...>& a,
  98. const Value& v) {
  99. return axis::visit([&o, &s, &v](const auto& a) { return linearize(o, s, a, v); }, a);
  100. }
  101. template <class Index, class... Ts, class Value>
  102. std::size_t linearize_growth(Index& o, axis::index_type& sh, const std::size_t st,
  103. axis::variant<Ts...>& a, const Value& v) {
  104. return axis::visit([&](auto& a) { return linearize_growth(o, sh, st, a, v); }, a);
  105. }
  106. } // namespace detail
  107. } // namespace histogram
  108. } // namespace boost
  109. #endif // BOOST_HISTOGRAM_DETAIL_LINEARIZE_HPP