project.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_ALGORITHM_PROJECT_HPP
  7. #define BOOST_HISTOGRAM_ALGORITHM_PROJECT_HPP
  8. #include <algorithm>
  9. #include <boost/histogram/detail/meta.hpp>
  10. #include <boost/histogram/histogram.hpp>
  11. #include <boost/histogram/indexed.hpp>
  12. #include <boost/histogram/unsafe_access.hpp>
  13. #include <boost/mp11/algorithm.hpp>
  14. #include <boost/mp11/list.hpp>
  15. #include <boost/mp11/set.hpp>
  16. #include <boost/throw_exception.hpp>
  17. #include <stdexcept>
  18. #include <type_traits>
  19. namespace boost {
  20. namespace histogram {
  21. namespace algorithm {
  22. /**
  23. Returns a lower-dimensional histogram, summing over removed axes.
  24. Arguments are the source histogram and compile-time numbers, the remaining indices of
  25. the axes. Returns a new histogram which only contains the subset of axes. The source
  26. histogram is summed over the removed axes.
  27. */
  28. template <class A, class S, unsigned N, typename... Ns>
  29. auto project(const histogram<A, S>& h, std::integral_constant<unsigned, N>, Ns...) {
  30. using LN = mp11::mp_list<std::integral_constant<unsigned, N>, Ns...>;
  31. static_assert(mp11::mp_is_set<LN>::value, "indices must be unique");
  32. const auto& old_axes = unsafe_access::axes(h);
  33. auto axes = detail::static_if<detail::is_tuple<A>>(
  34. [&](const auto& old_axes) {
  35. return std::make_tuple(std::get<N>(old_axes), std::get<Ns::value>(old_axes)...);
  36. },
  37. [&](const auto& old_axes) {
  38. return detail::remove_cvref_t<decltype(old_axes)>(
  39. {old_axes[N], old_axes[Ns::value]...});
  40. },
  41. old_axes);
  42. const auto& old_storage = unsafe_access::storage(h);
  43. using A2 = decltype(axes);
  44. auto result = histogram<A2, S>(std::move(axes), detail::make_default(old_storage));
  45. auto idx = detail::make_stack_buffer<int>(unsafe_access::axes(result));
  46. for (auto x : indexed(h, coverage::all)) {
  47. auto i = idx.begin();
  48. mp11::mp_for_each<LN>([&i, &x](auto J) { *i++ = x.index(J); });
  49. result.at(idx) += *x;
  50. }
  51. return result;
  52. }
  53. /**
  54. Returns a lower-dimensional histogram, summing over removed axes.
  55. This version accepts a source histogram and an iterable range containing the remaining
  56. indices.
  57. */
  58. template <class A, class S, class Iterable, class = detail::requires_iterable<Iterable>>
  59. auto project(const histogram<A, S>& h, const Iterable& c) {
  60. static_assert(detail::is_sequence_of_any_axis<A>::value,
  61. "this version of project requires histogram with non-static axes");
  62. const auto& old_axes = unsafe_access::axes(h);
  63. auto axes = detail::make_default(old_axes);
  64. axes.reserve(c.size());
  65. auto seen = detail::make_stack_buffer<bool>(old_axes, false);
  66. for (auto d : c) {
  67. if (seen[d]) BOOST_THROW_EXCEPTION(std::invalid_argument("indices must be unique"));
  68. seen[d] = true;
  69. axes.emplace_back(old_axes[d]);
  70. }
  71. const auto& old_storage = unsafe_access::storage(h);
  72. auto result = histogram<A, S>(std::move(axes), detail::make_default(old_storage));
  73. auto idx = detail::make_stack_buffer<int>(unsafe_access::axes(result));
  74. for (auto x : indexed(h, coverage::all)) {
  75. auto i = idx.begin();
  76. for (auto d : c) *i++ = x.index(d);
  77. result.at(idx) += *x;
  78. }
  79. return result;
  80. }
  81. } // namespace algorithm
  82. } // namespace histogram
  83. } // namespace boost
  84. #endif