make_histogram.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/detect.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,
  29. class = detail::requires_storage_or_adaptible<Storage>,
  30. class = detail::requires_axis<Axis>>
  31. auto make_histogram_with(Storage&& storage, Axis&& axis, Axes&&... axes) {
  32. auto a = std::make_tuple(std::forward<Axis>(axis), std::forward<Axes>(axes)...);
  33. using U = std::decay_t<Storage>;
  34. using S = mp11::mp_if<detail::is_storage<U>, U, storage_adaptor<U>>;
  35. return histogram<decltype(a), S>(std::move(a), S(std::forward<Storage>(storage)));
  36. }
  37. /**
  38. Make histogram from compile-time axis configuration and default storage.
  39. @param axis First axis instance.
  40. @param axes Other axis instances.
  41. */
  42. template <class Axis, class... Axes, class = detail::requires_axis<Axis>>
  43. auto make_histogram(Axis&& axis, Axes&&... axes) {
  44. return make_histogram_with(default_storage(), std::forward<Axis>(axis),
  45. std::forward<Axes>(axes)...);
  46. }
  47. /**
  48. Make histogram from compile-time axis configuration and weight-counting storage.
  49. @param axis First axis instance.
  50. @param axes Other axis instances.
  51. */
  52. template <class Axis, class... Axes, class = detail::requires_axis<Axis>>
  53. auto make_weighted_histogram(Axis&& axis, Axes&&... axes) {
  54. return make_histogram_with(weight_storage(), std::forward<Axis>(axis),
  55. std::forward<Axes>(axes)...);
  56. }
  57. /**
  58. Make histogram from iterable range and custom storage.
  59. @param storage Storage or container with standard interface (any vector, array, or map).
  60. @param iterable Iterable range of axis objects.
  61. */
  62. template <class Storage, class Iterable,
  63. class = detail::requires_storage_or_adaptible<Storage>,
  64. class = detail::requires_sequence_of_any_axis<Iterable>>
  65. auto make_histogram_with(Storage&& storage, Iterable&& iterable) {
  66. using U = std::decay_t<Storage>;
  67. using S = mp11::mp_if<detail::is_storage<U>, U, storage_adaptor<U>>;
  68. using It = std::decay_t<Iterable>;
  69. using A = mp11::mp_if<detail::is_indexable_container<It>, It,
  70. std::vector<mp11::mp_first<It>>>;
  71. return histogram<A, S>(std::forward<Iterable>(iterable),
  72. S(std::forward<Storage>(storage)));
  73. }
  74. /**
  75. Make histogram from iterable range and default storage.
  76. @param iterable Iterable range of axis objects.
  77. */
  78. template <class Iterable, class = detail::requires_sequence_of_any_axis<Iterable>>
  79. auto make_histogram(Iterable&& iterable) {
  80. return make_histogram_with(default_storage(), std::forward<Iterable>(iterable));
  81. }
  82. /**
  83. Make histogram from iterable range and weight-counting storage.
  84. @param iterable Iterable range of axis objects.
  85. */
  86. template <class Iterable, class = detail::requires_sequence_of_any_axis<Iterable>>
  87. auto make_weighted_histogram(Iterable&& iterable) {
  88. return make_histogram_with(weight_storage(), std::forward<Iterable>(iterable));
  89. }
  90. /**
  91. Make histogram from iterator interval and custom storage.
  92. @param storage Storage or container with standard interface (any vector, array, or map).
  93. @param begin Iterator to range of axis objects.
  94. @param end Iterator to range of axis objects.
  95. */
  96. template <class Storage, class Iterator,
  97. class = detail::requires_storage_or_adaptible<Storage>,
  98. class = detail::requires_iterator<Iterator>>
  99. auto make_histogram_with(Storage&& storage, Iterator begin, Iterator end) {
  100. using T = std::decay_t<decltype(*begin)>;
  101. return make_histogram_with(std::forward<Storage>(storage), std::vector<T>(begin, end));
  102. }
  103. /**
  104. Make histogram from iterator interval and default storage.
  105. @param begin Iterator to range of axis objects.
  106. @param end Iterator to range of axis objects.
  107. */
  108. template <class Iterator, class = detail::requires_iterator<Iterator>>
  109. auto make_histogram(Iterator begin, Iterator end) {
  110. return make_histogram_with(default_storage(), begin, end);
  111. }
  112. /**
  113. Make histogram from iterator interval and weight-counting storage.
  114. @param begin Iterator to range of axis objects.
  115. @param end Iterator to range of axis objects.
  116. */
  117. template <class Iterator, class = detail::requires_iterator<Iterator>>
  118. auto make_weighted_histogram(Iterator begin, Iterator end) {
  119. return make_histogram_with(weight_storage(), begin, end);
  120. }
  121. } // namespace histogram
  122. } // namespace boost
  123. #endif