diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/boost/libs/histogram/examples/guide_fill_histogram.cpp | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/histogram/examples/guide_fill_histogram.cpp')
-rw-r--r-- | src/boost/libs/histogram/examples/guide_fill_histogram.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/boost/libs/histogram/examples/guide_fill_histogram.cpp b/src/boost/libs/histogram/examples/guide_fill_histogram.cpp new file mode 100644 index 000000000..172be66e0 --- /dev/null +++ b/src/boost/libs/histogram/examples/guide_fill_histogram.cpp @@ -0,0 +1,61 @@ +// Copyright 2015-2018 Hans Dembinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt +// or copy at http://www.boost.org/LICENSE_1_0.txt) + +//[ guide_fill_histogram + +#include <boost/histogram.hpp> +#include <cassert> +#include <functional> +#include <numeric> +#include <utility> +#include <vector> + +int main() { + using namespace boost::histogram; + + auto h = make_histogram(axis::integer<>(0, 3), axis::regular<>(2, 0.0, 1.0)); + + // fill histogram, number of arguments must be equal to number of axes, + // types must be convertible to axis value type (here integer and double) + h(0, 0.2); // increase a cell value by one + h(2, 0.5); // increase another cell value by one + + // fills from a tuple are also supported; passing a tuple of wrong size + // causes an error at compile-time or an assertion at runtime in debug mode + auto xy = std::make_tuple(1, 0.3); + h(xy); + + // chunk-wise filling is also supported and more efficient, make some data... + std::vector<double> xy2[2] = {{0, 2, 5}, {0.8, 0.4, 0.7}}; + + // ... and call fill method + h.fill(xy2); + + // once histogram is filled, access individual cells using operator[] or at(...) + // - operator[] can only accept a single argument in the current version of C++, + // it is convenient when you have a 1D histogram + // - at(...) can accept several values, so use this by default + // - underflow bins are at index -1, overflow bins at index `size()` + // - passing an invalid index triggers a std::out_of_range exception + assert(h.at(0, 0) == 1); + assert(h.at(0, 1) == 1); + assert(h.at(1, 0) == 1); + assert(h.at(1, 1) == 0); + assert(h.at(2, 0) == 1); + assert(h.at(2, 1) == 1); + assert(h.at(-1, -1) == 0); // underflow for axis 0 and 1 + assert(h.at(-1, 0) == 0); // underflow for axis 0, normal bin for axis 1 + assert(h.at(-1, 2) == 0); // underflow for axis 0, overflow for axis 1 + assert(h.at(3, 1) == 1); // overflow for axis 0, normal bin for axis 1 + + // iteration over values works, but see next example for a better way + // - iteration using begin() and end() includes under- and overflow bins + // - iteration order is an implementation detail and should not be relied upon + const double sum = std::accumulate(h.begin(), h.end(), 0.0); + assert(sum == 6); +} + +//] |