File: ostream.hpp

package info (click to toggle)
python-boost-histogram 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,232 kB
  • sloc: python: 7,745; cpp: 3,243; makefile: 22; sh: 1
file content (87 lines) | stat: -rw-r--r-- 2,992 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2015-2020 Henry Schreiner and Hans Dembinski
//
// Distributed under the 3-Clause BSD License.  See accompanying
// file LICENSE or https://github.com/scikit-hep/boost-histogram for details.
//
// Based on boost/histogram/axis/ostream.hpp
// String representations here evaluate correctly in Python.

#pragma once

#include <bh_python/accumulators/mean.hpp>
#include <bh_python/accumulators/weighted_mean.hpp>
#include <bh_python/accumulators/weighted_sum.hpp>

#include <boost/histogram/accumulators/sum.hpp>
#include <boost/histogram/detail/counting_streambuf.hpp>
#include <boost/histogram/fwd.hpp>
#include <iosfwd>

/**
  \file boost/histogram/accumulators/ostream.hpp
  Simple streaming operators for the builtin accumulator types.
  Mostly similar to boost/histogram/accumulators/ostream.hpp
 */

template <class CharT, class Traits, class T>
std::basic_ostream<CharT, Traits>&
handle_nonzero_width(std::basic_ostream<CharT, Traits>& os, const T& x) {
    const auto w = os.width();
    os.width(0);
    std::streamsize count = 0;
    {
        auto g = ::boost::histogram::detail::make_count_guard(os, count);
        os << x;
    }
    if(os.flags() & std::ios::left) {
        os << x;
        for(auto i = count; i < w; ++i)
            os << os.fill();
    } else {
        for(auto i = count; i < w; ++i)
            os << os.fill();
        os << x;
    }
    return os;
}

// Note that the names are *not* included here, so they can be added in pybind11.

namespace accumulators {

template <class CharT, class Traits, class W>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
                                              const weighted_sum<W>& x) {
    if(os.width() == 0)
        return os << "value=" << x.value << ", variance=" << x.variance;
    return handle_nonzero_width(os, x);
}

template <class CharT, class Traits, class W>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
                                              const mean<W>& x) {
    if(os.width() == 0)
        return os << "count=" << x.count << ", value=" << x.value
                  << ", variance=" << x.variance();
    return handle_nonzero_width(os, x);
}

template <class CharT, class Traits, class W>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
                                              const weighted_mean<W>& x) {
    if(os.width() == 0)
        return os << "sum_of_weights=" << x.sum_of_weights
                  << ", sum_of_weights_squared=" << x.sum_of_weights_squared
                  << ", value=" << x.value << ", variance=" << x.variance();
    return handle_nonzero_width(os, x);
}

template <class CharT, class Traits, class T>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os,
           const ::boost::histogram::accumulators::count<T, true>& x) {
    os << x.load();
    return os;
}

} // namespace accumulators