]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/histogram/examples/getting_started_listing_01.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / histogram / examples / getting_started_listing_01.cpp
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 // clang-format off
8
9 //[ getting_started_listing_01
10
11 #include <algorithm> // std::for_each
12 #include <boost/format.hpp> // only needed for printing
13 #include <boost/histogram.hpp> // make_histogram, regular, weight, indexed
14 #include <cassert> // assert (used to test this example for correctness)
15 #include <functional> // std::ref
16 #include <iostream> // std::cout, std::flush
17 #include <sstream> // std::ostringstream
18
19 int main() {
20 using namespace boost::histogram; // strip the boost::histogram prefix
21
22 /*
23 Create a 1d-histogram with a regular axis that has 6 equidistant bins on
24 the real line from -1.0 to 2.0, and label it as "x". A family of overloaded
25 factory functions called `make_histogram` makes creating histograms easy.
26
27 A regular axis is a sequence of semi-open bins. Extra under- and overflow
28 bins extend the axis by default (this can be turned off).
29
30 index : -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6
31 bin edges: -inf -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 inf
32 */
33 auto h = make_histogram(axis::regular<>(6, -1.0, 2.0, "x"));
34
35 /*
36 Let's fill a histogram with data, typically this happens in a loop.
37
38 STL algorithms are supported. std::for_each is very convenient to fill a
39 histogram from an iterator range. Use std::ref in the call, if you don't
40 want std::for_each to make a copy of your histogram.
41 */
42 auto data = {-0.5, 1.1, 0.3, 1.7};
43 std::for_each(data.begin(), data.end(), std::ref(h));
44 // let's fill some more values manually
45 h(-1.5); // is placed in underflow bin -1
46 h(-1.0); // is placed in bin 0, bin interval is semi-open
47 h(2.0); // is placed in overflow bin 6, bin interval is semi-open
48 h(20.0); // is placed in overflow bin 6
49
50 /*
51 This does a weighted fill using the `weight` function as an additional
52 argument. It may appear at the beginning or end of the argument list. C++
53 doesn't have keyword arguments like Python, this is the next-best thing.
54 */
55 h(0.1, weight(1.0));
56
57 /*
58 Iterate over bins with the `indexed` range generator, which provides a
59 special accessor object, that can be used to obtain the current bin index,
60 and the current bin value by dereferncing (it acts like a pointer to the
61 value). Using `indexed` is convenient and gives you better performance than
62 looping over the histogram cells with hand-written for loops. By default,
63 under- and overflow bins are skipped. Passing `coverage::all` as the
64 optional second argument iterates over all bins.
65
66 - Access the value with the dereference operator.
67 - Access the current index with `index(d)` method of the accessor.
68 - Access the corresponding bin interval view with `bin(d)`.
69
70 The return type of `bin(d)` depends on the axis type (see the axis reference
71 for details). It usually is a class that represents a semi-open interval.
72 Edges can be accessed with methods `lower()` and `upper()`.
73 */
74
75 std::ostringstream os;
76 for (auto&& x : indexed(h, coverage::all)) {
77 os << boost::format("bin %2i [%4.1f, %4.1f): %i\n")
78 % x.index() % x.bin().lower() % x.bin().upper() % *x;
79 }
80
81 std::cout << os.str() << std::flush;
82
83 assert(os.str() == "bin -1 [-inf, -1.0): 1\n"
84 "bin 0 [-1.0, -0.5): 1\n"
85 "bin 1 [-0.5, -0.0): 1\n"
86 "bin 2 [-0.0, 0.5): 2\n"
87 "bin 3 [ 0.5, 1.0): 0\n"
88 "bin 4 [ 1.0, 1.5): 1\n"
89 "bin 5 [ 1.5, 2.0): 1\n"
90 "bin 6 [ 2.0, inf): 2\n");
91 }
92
93 //]