multi_index.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2020 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_MULTI_INDEX_HPP
  7. #define BOOST_HISTOGRAM_MULTI_INDEX_HPP
  8. #include <algorithm>
  9. #include <boost/histogram/detail/detect.hpp>
  10. #include <boost/histogram/detail/nonmember_container_access.hpp>
  11. #include <boost/histogram/fwd.hpp>
  12. #include <boost/mp11/integer_sequence.hpp>
  13. #include <boost/throw_exception.hpp>
  14. #include <initializer_list>
  15. #include <iterator>
  16. #include <stdexcept>
  17. #include <tuple>
  18. namespace boost {
  19. namespace histogram {
  20. /** Holder for multiple axis indices.
  21. Adapts external iterable, tuple, or explicit list of indices to the same representation.
  22. */
  23. template <std::size_t Size>
  24. struct multi_index {
  25. using value_type = axis::index_type;
  26. using iterator = value_type*;
  27. using const_iterator = const value_type*;
  28. static multi_index create(std::size_t s) {
  29. if (s != size())
  30. BOOST_THROW_EXCEPTION(std::invalid_argument("size does not match static size"));
  31. return multi_index(priv_tag{});
  32. }
  33. template <class... Is>
  34. multi_index(axis::index_type i, Is... is)
  35. : multi_index(std::initializer_list<axis::index_type>{
  36. i, static_cast<axis::index_type>(is)...}) {}
  37. template <class... Is>
  38. multi_index(const std::tuple<axis::index_type, Is...>& is)
  39. : multi_index(is, mp11::make_index_sequence<(1 + sizeof...(Is))>{}) {}
  40. template <class Iterable, class = detail::requires_iterable<Iterable>>
  41. multi_index(const Iterable& is) {
  42. if (detail::size(is) != size())
  43. BOOST_THROW_EXCEPTION(std::invalid_argument("no. of axes != no. of indices"));
  44. using std::begin;
  45. using std::end;
  46. std::copy(begin(is), end(is), data_);
  47. }
  48. iterator begin() noexcept { return data_; }
  49. iterator end() noexcept { return data_ + size(); }
  50. const_iterator begin() const noexcept { return data_; }
  51. const_iterator end() const noexcept { return data_ + size(); }
  52. static constexpr std::size_t size() noexcept { return Size; }
  53. private:
  54. struct priv_tag {};
  55. multi_index(priv_tag) {}
  56. template <class T, std::size_t... Is>
  57. multi_index(const T& is, mp11::index_sequence<Is...>)
  58. : multi_index(static_cast<axis::index_type>(std::get<Is>(is))...) {}
  59. axis::index_type data_[size()];
  60. };
  61. template <>
  62. struct multi_index<static_cast<std::size_t>(-1)> {
  63. using value_type = axis::index_type;
  64. using iterator = value_type*;
  65. using const_iterator = const value_type*;
  66. static multi_index create(std::size_t s) { return multi_index(priv_tag{}, s); }
  67. template <class... Is>
  68. multi_index(axis::index_type i, Is... is)
  69. : multi_index(std::initializer_list<axis::index_type>{
  70. i, static_cast<axis::index_type>(is)...}) {}
  71. template <class... Is>
  72. multi_index(const std::tuple<axis::index_type, Is...>& is)
  73. : multi_index(is, mp11::make_index_sequence<(1 + sizeof...(Is))>{}) {}
  74. template <class Iterable, class = detail::requires_iterable<Iterable>>
  75. multi_index(const Iterable& is) : size_(detail::size(is)) {
  76. using std::begin;
  77. using std::end;
  78. std::copy(begin(is), end(is), data_);
  79. }
  80. iterator begin() noexcept { return data_; }
  81. iterator end() noexcept { return data_ + size_; }
  82. const_iterator begin() const noexcept { return data_; }
  83. const_iterator end() const noexcept { return data_ + size_; }
  84. std::size_t size() const noexcept { return size_; }
  85. private:
  86. struct priv_tag {};
  87. multi_index(priv_tag, std::size_t s) : size_(s) {}
  88. template <class T, std::size_t... Ns>
  89. multi_index(const T& is, mp11::index_sequence<Ns...>)
  90. : multi_index(static_cast<axis::index_type>(std::get<Ns>(is))...) {}
  91. std::size_t size_ = 0;
  92. static constexpr std::size_t max_size_ = BOOST_HISTOGRAM_DETAIL_AXES_LIMIT;
  93. axis::index_type data_[max_size_];
  94. };
  95. } // namespace histogram
  96. } // namespace boost
  97. #endif