]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/histogram/examples/guide_custom_storage.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / histogram / examples / guide_custom_storage.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 //[ guide_custom_storage
8
9 #include <algorithm> // std::for_each
10 #include <array>
11 #include <boost/histogram.hpp>
12 #include <boost/histogram/algorithm/sum.hpp>
13 #include <functional> // std::ref
14 #include <unordered_map>
15 #include <vector>
16
17 int main() {
18 using namespace boost::histogram;
19 const auto axis = axis::regular<>(10, 0.0, 1.0);
20
21 auto data = {0.1, 0.3, 0.2, 0.7};
22
23 // Create static histogram with vector<int> as counter storage, you can use
24 // other arithmetic types as counters, e.g. double.
25 auto h1 = make_histogram_with(std::vector<int>(), axis);
26 std::for_each(data.begin(), data.end(), std::ref(h1));
27 assert(algorithm::sum(h1) == 4);
28
29 // Create static histogram with array<int, N> as counter storage which is
30 // allocated completely on the stack (this is very fast). N may be larger than
31 // the actual number of bins used; an exception is raised if N is too small to
32 // hold all bins.
33 auto h2 = make_histogram_with(std::array<int, 12>(), axis);
34 std::for_each(data.begin(), data.end(), std::ref(h2));
35 assert(algorithm::sum(h2) == 4);
36
37 // Create static histogram with unordered_map as counter storage; this
38 // generates a sparse histogram where only memory is allocated for bins that
39 // are non-zero. This sounds like a good idea for high-dimensional histograms,
40 // but maps come with a memory and run-time overhead. The default_storage
41 // usually performs better in high dimensions.
42 auto h3 = make_histogram_with(std::unordered_map<std::size_t, int>(), axis);
43 std::for_each(data.begin(), data.end(), std::ref(h3));
44 assert(algorithm::sum(h3) == 4);
45 }
46
47 //]