]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/histogram/examples/guide_custom_accumulators_3.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / histogram / examples / guide_custom_accumulators_3.cpp
1 // Copyright 2019 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 //[ guide_custom_accumulators_3
8
9 #include <boost/format.hpp>
10 #include <boost/histogram.hpp>
11 #include <iostream>
12 #include <sstream>
13
14 int main() {
15 using namespace boost::histogram;
16
17 // Accumulator accepts two samples and an optional weight and computes the mean of each.
18 struct multi_mean {
19 accumulators::mean<> mx, my;
20
21 // called when no weight is passed
22 void operator()(double x, double y) {
23 mx(x);
24 my(y);
25 }
26
27 // called when a weight is passed
28 void operator()(weight_type<double> w, double x, double y) {
29 mx(w, x);
30 my(w, y);
31 }
32 };
33 // Note: The implementation can be made more efficient by sharing the sum of weights.
34
35 // Create a 1D histogram that uses the custom accumulator.
36 auto h = make_histogram_with(dense_storage<multi_mean>(), axis::integer<>(0, 2));
37 h(0, sample(1, 2)); // samples go to first cell
38 h(0, sample(3, 4)); // samples go to first cell
39 h(1, sample(5, 6), weight(2)); // samples go to second cell
40 h(1, sample(7, 8), weight(3)); // samples go to second cell
41
42 std::ostringstream os;
43 for (auto&& bin : indexed(h)) {
44 os << boost::format("index %i mean-x %.1f mean-y %.1f\n") % bin.index() %
45 bin->mx.value() % bin->my.value();
46 }
47 std::cout << os.str() << std::flush;
48 assert(os.str() == "index 0 mean-x 2.0 mean-y 3.0\n"
49 "index 1 mean-x 6.2 mean-y 7.2\n");
50 }
51
52 //]