]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/histogram/algorithm/sum.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / histogram / algorithm / sum.hpp
1 // Copyright 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_ALGORITHM_SUM_HPP
8 #define BOOST_HISTOGRAM_ALGORITHM_SUM_HPP
9
10 #include <boost/histogram/accumulators/sum.hpp>
11 #include <boost/histogram/fwd.hpp>
12 #include <boost/mp11/utility.hpp>
13 #include <numeric>
14 #include <type_traits>
15
16 namespace boost {
17 namespace histogram {
18 namespace algorithm {
19 /** Compute the sum over all histogram cells, including underflow/overflow bins.
20
21 If the value type of the histogram is an integral or floating point type,
22 boost::accumulators::sum<double> is used to compute the sum, else the original value
23 type is used. Compilation fails, if the value type does not support operator+=.
24
25 Return type is double if the value type of the histogram is integral or floating point,
26 and the original value type otherwise.
27 */
28 template <class A, class S>
29 auto sum(const histogram<A, S>& h) {
30 using T = typename histogram<A, S>::value_type;
31 using Sum = mp11::mp_if<std::is_arithmetic<T>, accumulators::sum<double>, T>;
32 Sum sum;
33 for (auto&& x : h) sum += x;
34 using R = mp11::mp_if<std::is_arithmetic<T>, double, T>;
35 return static_cast<R>(sum);
36 }
37 } // namespace algorithm
38 } // namespace histogram
39 } // namespace boost
40
41 #endif