linearize.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // this faster code can be used if all axes are inclusive
  20. template <class Opts>
  21. std::size_t linearize(Opts, std::size_t& out, const std::size_t stride,
  22. const axis::index_type size, const axis::index_type idx) {
  23. constexpr bool u = Opts::test(axis::option::underflow);
  24. constexpr bool o = Opts::test(axis::option::overflow);
  25. assert(idx >= (u ? -1 : 0));
  26. assert(idx < (o ? size + 1 : size));
  27. assert(idx >= 0 || static_cast<std::size_t>(-idx * stride) <= out);
  28. out += idx * stride;
  29. return size + u + o;
  30. }
  31. // initial offset to out must be set
  32. // this slower code must be used if not all axes are inclusive
  33. template <class Opts>
  34. std::size_t linearize(Opts, optional_index& out, const std::size_t stride,
  35. const axis::index_type size, const axis::index_type idx) {
  36. constexpr bool u = Opts::test(axis::option::underflow);
  37. constexpr bool o = Opts::test(axis::option::overflow);
  38. assert(idx >= -1);
  39. assert(idx < size + 1);
  40. const bool is_valid = (u || idx >= 0) && (o || idx < size);
  41. if (is_valid)
  42. out += idx * stride;
  43. else
  44. out = invalid_index;
  45. return size + u + o;
  46. }
  47. template <class Index, class Axis, class Value>
  48. std::size_t linearize(Index& out, const std::size_t stride, const Axis& ax,
  49. const Value& v) {
  50. // mask options to reduce no. of template instantiations
  51. constexpr auto opts = axis::traits::get_options<Axis>{} &
  52. (axis::option::underflow | axis::option::overflow);
  53. return linearize(opts, out, stride, ax.size(), axis::traits::index(ax, v));
  54. }
  55. /**
  56. Must be used when axis is potentially growing. Also works for non-growing axis.
  57. Initial offset of `out` must be zero. We cannot assert on this, because we do not
  58. know if this is the first call of `linearize_growth`.
  59. */
  60. template <class Index, class Axis, class Value>
  61. std::size_t linearize_growth(Index& out, axis::index_type& shift,
  62. const std::size_t stride, Axis& a, const Value& v) {
  63. axis::index_type idx;
  64. std::tie(idx, shift) = axis::traits::update(a, v);
  65. constexpr bool u = axis::traits::get_options<Axis>::test(axis::option::underflow);
  66. if (u) ++idx;
  67. if (std::is_same<Index, std::size_t>::value) {
  68. assert(idx < axis::traits::extent(a));
  69. out += idx * stride;
  70. } else {
  71. if (0 <= idx && idx < axis::traits::extent(a))
  72. out += idx * stride;
  73. else
  74. out = invalid_index;
  75. }
  76. return axis::traits::extent(a);
  77. }
  78. // initial offset of out must be zero
  79. template <class A>
  80. std::size_t linearize_index(optional_index& out, const std::size_t stride, const A& ax,
  81. const axis::index_type idx) noexcept {
  82. const auto opt = axis::traits::get_options<A>();
  83. const axis::index_type begin = opt & axis::option::underflow ? -1 : 0;
  84. const axis::index_type end = opt & axis::option::overflow ? ax.size() + 1 : ax.size();
  85. const axis::index_type extent = end - begin;
  86. // i may be arbitrarily out of range
  87. if (begin <= idx && idx < end)
  88. out += (idx - begin) * stride;
  89. else
  90. out = invalid_index;
  91. return extent;
  92. }
  93. template <class A, std::size_t N>
  94. optional_index linearize_indices(const A& axes, const multi_index<N>& indices) noexcept {
  95. assert(axes_rank(axes) == detail::size(indices));
  96. optional_index idx{0}; // offset not used by linearize_index
  97. auto stride = static_cast<std::size_t>(1);
  98. using std::begin;
  99. auto i = begin(indices);
  100. for_each_axis(axes,
  101. [&](const auto& a) { stride *= linearize_index(idx, stride, a, *i++); });
  102. return idx;
  103. }
  104. template <class Index, class... Ts, class Value>
  105. std::size_t linearize(Index& o, const std::size_t s, const axis::variant<Ts...>& a,
  106. const Value& v) {
  107. return axis::visit([&o, &s, &v](const auto& a) { return linearize(o, s, a, v); }, a);
  108. }
  109. template <class Index, class... Ts, class Value>
  110. std::size_t linearize_growth(Index& o, axis::index_type& sh, const std::size_t st,
  111. axis::variant<Ts...>& a, const Value& v) {
  112. return axis::visit([&](auto& a) { return linearize_growth(o, sh, st, a, v); }, a);
  113. }
  114. } // namespace detail
  115. } // namespace histogram
  116. } // namespace boost
  117. #endif // BOOST_HISTOGRAM_DETAIL_LINEARIZE_HPP