make_histogram.hpp 5.1 KB

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