linearize.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/assert.hpp>
  9. #include <boost/config.hpp>
  10. #include <boost/histogram/axis/option.hpp>
  11. #include <boost/histogram/axis/traits.hpp>
  12. #include <boost/histogram/axis/variant.hpp>
  13. #include <boost/histogram/detail/optional_index.hpp>
  14. #include <boost/histogram/fwd.hpp>
  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. #ifdef BOOST_NO_CXX17_IF_CONSTEXPR
  25. if
  26. #else
  27. if constexpr
  28. #endif
  29. (std::is_same<Index, std::size_t>::value || (u && o)) {
  30. BOOST_ASSERT(idx >= (u ? -1 : 0));
  31. BOOST_ASSERT(idx < (o ? size + 1 : size));
  32. BOOST_ASSERT(idx >= 0 || static_cast<std::size_t>(-idx * stride) <= out);
  33. out += idx * stride;
  34. } else {
  35. BOOST_ASSERT(idx >= -1);
  36. BOOST_ASSERT(idx < size + 1);
  37. if ((u || idx >= 0) && (o || idx < size))
  38. out += idx * stride;
  39. else
  40. out = invalid_index;
  41. }
  42. return size + u + o;
  43. }
  44. template <class Index, class Axis, class Value>
  45. std::size_t linearize(Index& out, const std::size_t stride, const Axis& ax,
  46. const Value& v) {
  47. // mask options to reduce no. of template instantiations
  48. constexpr auto opts = axis::traits::static_options<Axis>{} &
  49. (axis::option::underflow | axis::option::overflow);
  50. return linearize(opts, out, stride, ax.size(), axis::traits::index(ax, v));
  51. }
  52. // initial offset of out must be zero
  53. template <class Index, class Axis, class Value>
  54. std::size_t linearize_growth(Index& out, axis::index_type& shift,
  55. const std::size_t stride, Axis& a, const Value& v) {
  56. axis::index_type idx;
  57. std::tie(idx, shift) = axis::traits::update(a, v);
  58. constexpr bool u = axis::traits::static_options<Axis>::test(axis::option::underflow);
  59. if (u) ++idx;
  60. if (std::is_same<Index, std::size_t>::value) {
  61. BOOST_ASSERT(idx < axis::traits::extent(a));
  62. out += idx * stride;
  63. } else {
  64. if (0 <= idx && idx < axis::traits::extent(a))
  65. out += idx * stride;
  66. else
  67. out = invalid_index;
  68. }
  69. return axis::traits::extent(a);
  70. }
  71. // initial offset of out must be zero
  72. template <class A>
  73. std::size_t linearize_index(optional_index& out, const std::size_t stride, const A& ax,
  74. const axis::index_type idx) {
  75. // cannot use static_options here, since A may be variant
  76. const auto opt = axis::traits::options(ax);
  77. const axis::index_type begin = opt & axis::option::underflow ? -1 : 0;
  78. const axis::index_type end = opt & axis::option::overflow ? ax.size() + 1 : ax.size();
  79. const axis::index_type extent = end - begin;
  80. // i may be arbitrarily out of range
  81. if (begin <= idx && idx < end)
  82. out += (idx - begin) * stride;
  83. else
  84. out = invalid_index;
  85. return extent;
  86. }
  87. template <class Index, class... Ts, class Value>
  88. std::size_t linearize(Index& o, const std::size_t s, const axis::variant<Ts...>& a,
  89. const Value& v) {
  90. return axis::visit([&o, &s, &v](const auto& a) { return linearize(o, s, a, v); }, a);
  91. }
  92. template <class Index, class... Ts, class Value>
  93. std::size_t linearize_growth(Index& o, axis::index_type& sh, const std::size_t st,
  94. axis::variant<Ts...>& a, const Value& v) {
  95. return axis::visit([&](auto& a) { return linearize_growth(o, sh, st, a, v); }, a);
  96. }
  97. } // namespace detail
  98. } // namespace histogram
  99. } // namespace boost
  100. #endif // BOOST_HISTOGRAM_DETAIL_LINEARIZE_HPP