at.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2015-2018 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_AT_HPP
  7. #define BOOST_HISTOGRAM_DETAIL_AT_HPP
  8. #include <boost/histogram/axis/option.hpp>
  9. #include <boost/histogram/axis/traits.hpp>
  10. #include <boost/histogram/detail/axes.hpp>
  11. #include <boost/histogram/detail/linearize.hpp>
  12. #include <boost/histogram/fwd.hpp>
  13. #include <boost/mp11/algorithm.hpp>
  14. #include <tuple>
  15. namespace boost {
  16. namespace histogram {
  17. namespace detail {
  18. template <class A, class... Us>
  19. optional_index at(const A& axes, const std::tuple<Us...>& args) noexcept {
  20. optional_index idx{0}; // offset not used by linearize_index
  21. mp11::mp_for_each<mp11::mp_iota_c<sizeof...(Us)>>(
  22. [&, stride = static_cast<std::size_t>(1)](auto i) mutable {
  23. stride *= linearize_index(idx, stride, axis_get<i>(axes),
  24. static_cast<axis::index_type>(std::get<i>(args)));
  25. });
  26. return idx;
  27. }
  28. template <class A, class U>
  29. optional_index at(const A& axes, const U& args) noexcept {
  30. optional_index idx{0};
  31. using std::begin;
  32. for_each_axis(axes, [&, it = begin(args),
  33. stride = static_cast<std::size_t>(1)](const auto& a) mutable {
  34. stride *= linearize_index(idx, stride, a, static_cast<axis::index_type>(*it++));
  35. });
  36. return idx;
  37. }
  38. } // namespace detail
  39. } // namespace histogram
  40. } // namespace boost
  41. #endif