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