]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/histogram/examples/guide_axis_basic_demo.cpp
1 // Copyright 2019 Hans Dembinski
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)
7 //[ guide_axis_basic_demo
9 #include <boost/histogram/axis.hpp>
13 using namespace boost::histogram
;
15 // make a regular axis with 10 bins over interval from 1.5 to 2.5
16 auto r
= axis::regular
<>{10, 1.5, 2.5};
17 // `<>` is needed in C++14 because the axis is templated,
18 // in C++17 you can do: auto r = axis::regular{10, 1.5, 2.5};
19 assert(r
.size() == 10);
20 // alternatively, you can define the step size with the `step` marker
21 auto r_step
= axis::regular
<>{axis::step(0.1), 1.5, 2.5};
24 // histogram uses the `index` method to convert values to indices
25 // note: intervals of builtin axis types are always semi-open [a, b)
26 assert(r
.index(1.5) == 0);
27 assert(r
.index(1.6) == 1);
28 assert(r
.index(2.4) == 9);
29 // index for a value below the start of the axis is always -1
30 assert(r
.index(1.0) == -1);
31 assert(r
.index(-std::numeric_limits
<double>::infinity()) == -1);
32 // index for a value below the above the end of the axis is always `size()`
33 assert(r
.index(3.0) == 10);
34 assert(r
.index(std::numeric_limits
<double>::infinity()) == 10);
35 // index for not-a-number is also `size()` by convention
36 assert(r
.index(std::numeric_limits
<double>::quiet_NaN()) == 10);
38 // make a variable axis with 3 bins [-1.5, 0.1), [0.1, 0.3), [0.3, 10)
39 auto v
= axis::variable
<>{-1.5, 0.1, 0.3, 10.};
40 assert(v
.index(-2.0) == -1);
41 assert(v
.index(-1.5) == 0);
42 assert(v
.index(0.1) == 1);
43 assert(v
.index(0.3) == 2);
44 assert(v
.index(10) == 3);
45 assert(v
.index(20) == 3);
47 // make an integer axis with 3 bins at -1, 0, 1
48 auto i
= axis::integer
<>{-1, 2};
49 assert(i
.index(-2) == -1);
50 assert(i
.index(-1) == 0);
51 assert(i
.index(0) == 1);
52 assert(i
.index(1) == 2);
53 assert(i
.index(2) == 3);
55 // make an integer axis called "foo"
56 auto i_with_label
= axis::integer
<>{-1, 2, "foo"};
57 // all builtin axis types allow you to pass some optional metadata as the last
58 // argument in the constructor; a string by default, but can be any copyable type
60 // two axis do not compare equal if they differ in their metadata
61 assert(i
!= i_with_label
);
63 // integer axis also work well with unscoped enums
65 auto i_for_enum
= axis::integer
<>{red
, blue
+ 1};
66 assert(i_for_enum
.index(red
) == 0);
67 assert(i_for_enum
.index(blue
) == 1);
69 // make a category axis from a scoped enum and/or if the identifiers are not consecutive
70 enum class Bar
{ red
= 12, blue
= 6 };
71 auto c
= axis::category
<Bar
>{Bar::red
, Bar::blue
};
72 assert(c
.index(Bar::red
) == 0);
73 assert(c
.index(Bar::blue
) == 1);
74 // c.index(12) is a compile-time error, since the argument must be of type `Bar`
76 // category axis can be created for any copyable and equal-comparable type
77 auto c_str
= axis::category
<std::string
>{"red", "blue"};
78 assert(c_str
.index("red") == 0);
79 assert(c_str
.index("blue") == 1);