make_histogram.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_MAKE_HISTOGRAM_HPP
  7. #define BOOST_HISTOGRAM_MAKE_HISTOGRAM_HPP
  8. /**
  9. \file boost/histogram/make_histogram.hpp
  10. Collection of factory functions to conveniently create histograms.
  11. */
  12. #include <boost/histogram/accumulators/weighted_sum.hpp>
  13. #include <boost/histogram/detail/meta.hpp>
  14. #include <boost/histogram/histogram.hpp>
  15. #include <boost/histogram/storage_adaptor.hpp>
  16. #include <boost/histogram/unlimited_storage.hpp> // = default_storage
  17. #include <boost/mp11/utility.hpp>
  18. #include <tuple>
  19. #include <vector>
  20. namespace boost {
  21. namespace histogram {
  22. /**
  23. Make histogram from compile-time axis configuration and custom storage.
  24. @param storage Storage or container with standard interface (any vector, array, or map).
  25. @param axis First axis instance.
  26. @param axes Other axis instances.
  27. */
  28. template <class Storage, class Axis, class... Axes, class = detail::requires_axis<Axis>>
  29. auto make_histogram_with(Storage&& storage, Axis&& axis, Axes&&... axes) {
  30. auto a = std::make_tuple(std::forward<Axis>(axis), std::forward<Axes>(axes)...);
  31. using U = detail::remove_cvref_t<Storage>;
  32. using S = mp11::mp_if<detail::is_storage<U>, U, storage_adaptor<U>>;
  33. return histogram<decltype(a), S>(std::move(a), S(std::forward<Storage>(storage)));
  34. }
  35. /**
  36. Make histogram from compile-time axis configuration and default storage.
  37. @param axis First axis instance.
  38. @param axes Other axis instances.
  39. */
  40. template <class Axis, class... Axes, class = detail::requires_axis<Axis>>
  41. auto make_histogram(Axis&& axis, Axes&&... axes) {
  42. return make_histogram_with(default_storage(), std::forward<Axis>(axis),
  43. std::forward<Axes>(axes)...);
  44. }
  45. /**
  46. Make histogram from compile-time axis configuration and weight-counting storage.
  47. @param axis First axis instance.
  48. @param axes Other axis instances.
  49. */
  50. template <class Axis, class... Axes, class = detail::requires_axis<Axis>>
  51. auto make_weighted_histogram(Axis&& axis, Axes&&... axes) {
  52. return make_histogram_with(weight_storage(), std::forward<Axis>(axis),
  53. std::forward<Axes>(axes)...);
  54. }
  55. /**
  56. Make histogram from iterable range and custom storage.
  57. @param storage Storage or container with standard interface (any vector, array, or map).
  58. @param iterable Iterable range of axis objects.
  59. */
  60. template <class Storage, class Iterable,
  61. class = detail::requires_sequence_of_any_axis<Iterable>>
  62. auto make_histogram_with(Storage&& storage, Iterable&& iterable) {
  63. using U = detail::remove_cvref_t<Storage>;
  64. using S = mp11::mp_if<detail::is_storage<U>, U, storage_adaptor<U>>;
  65. using It = detail::remove_cvref_t<Iterable>;
  66. using A = mp11::mp_if<detail::is_indexable_container<It>, It,
  67. std::vector<mp11::mp_first<It>>>;
  68. return histogram<A, S>(std::forward<Iterable>(iterable),
  69. S(std::forward<Storage>(storage)));
  70. }
  71. /**
  72. Make histogram from iterable range and default storage.
  73. @param iterable Iterable range of axis objects.
  74. */
  75. template <class Iterable, class = detail::requires_sequence_of_any_axis<Iterable>>
  76. auto make_histogram(Iterable&& iterable) {
  77. return make_histogram_with(default_storage(), std::forward<Iterable>(iterable));
  78. }
  79. /**
  80. Make histogram from iterable range and weight-counting storage.
  81. @param iterable Iterable range of axis objects.
  82. */
  83. template <class Iterable, class = detail::requires_sequence_of_any_axis<Iterable>>
  84. auto make_weighted_histogram(Iterable&& iterable) {
  85. return make_histogram_with(weight_storage(), std::forward<Iterable>(iterable));
  86. }
  87. /**
  88. Make histogram from iterator interval and custom storage.
  89. @param storage Storage or container with standard interface (any vector, array, or map).
  90. @param begin Iterator to range of axis objects.
  91. @param end Iterator to range of axis objects.
  92. */
  93. template <class Storage, class Iterator, class = detail::requires_iterator<Iterator>>
  94. auto make_histogram_with(Storage&& storage, Iterator begin, Iterator end) {
  95. using T = detail::remove_cvref_t<decltype(*begin)>;
  96. return make_histogram_with(std::forward<Storage>(storage), std::vector<T>(begin, end));
  97. }
  98. /**
  99. Make histogram from iterator interval and default storage.
  100. @param begin Iterator to range of axis objects.
  101. @param end Iterator to range of axis objects.
  102. */
  103. template <class Iterator, class = detail::requires_iterator<Iterator>>
  104. auto make_histogram(Iterator begin, Iterator end) {
  105. return make_histogram_with(default_storage(), begin, end);
  106. }
  107. /**
  108. Make histogram from iterator interval and weight-counting storage.
  109. @param begin Iterator to range of axis objects.
  110. @param end Iterator to range of axis objects.
  111. */
  112. template <class Iterator, class = detail::requires_iterator<Iterator>>
  113. auto make_weighted_histogram(Iterator begin, Iterator end) {
  114. return make_histogram_with(weight_storage(), begin, end);
  115. }
  116. } // namespace histogram
  117. } // namespace boost
  118. #endif