]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/histogram/examples/guide_make_dynamic_histogram.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / histogram / examples / guide_make_dynamic_histogram.cpp
CommitLineData
92f5a8d4
TL
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//[ guide_make_dynamic_histogram
8
9#include <boost/histogram.hpp>
10#include <cassert>
11#include <sstream>
12#include <vector>
13
14const char* config = "4 1.0 2.0\n"
15 "5 3.0 4.0\n";
16
17int main() {
18 using namespace boost::histogram;
19
20 // read axis config from a config file (mocked here with std::istringstream)
21 // and create vector of regular axes, the number of axis is not known at compile-time
22 std::istringstream is(config);
23 auto v1 = std::vector<axis::regular<>>();
24 while (is.good()) {
25 unsigned bins;
26 double start, stop;
27 is >> bins >> start >> stop;
28 v1.emplace_back(bins, start, stop);
29 }
30
31 // create histogram from iterator range
32 // (copying or moving the vector also works, move is shown below)
33 auto h1 = make_histogram(v1.begin(), v1.end());
34 assert(h1.rank() == v1.size());
35
36 // with a vector of axis::variant (polymorphic axis type that can hold any one of the
37 // template arguments at a time) the types and number of axis can vary at run-time
38 auto v2 = std::vector<axis::variant<axis::regular<>, axis::integer<>>>();
39 v2.emplace_back(axis::regular<>(100, -1.0, 1.0));
40 v2.emplace_back(axis::integer<>(1, 7));
41
42 // create dynamic histogram by moving the vector
43 auto h2 = make_histogram(std::move(v2));
44 assert(h2.rank() == 2);
45}
46
47//]