Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:43:44

0001 // Copyright 2015-2017 Hans Dembinski
0002 //
0003 // Distributed under the Boost Software License, Version 1.0.
0004 // (See accompanying file LICENSE_1_0.txt
0005 // or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #ifndef BOOST_HISTOGRAM_ACCUMULATORS_OSTREAM_HPP
0008 #define BOOST_HISTOGRAM_ACCUMULATORS_OSTREAM_HPP
0009 
0010 #include <boost/histogram/detail/counting_streambuf.hpp>
0011 #include <boost/histogram/fwd.hpp>
0012 #include <ios>
0013 
0014 /**
0015   \file boost/histogram/accumulators/ostream.hpp
0016   Simple streaming operators for the builtin accumulator types.
0017 
0018   The text representation is not guaranteed to be stable between versions of
0019   Boost.Histogram. This header is only included by
0020   [boost/histogram/ostream.hpp](histogram/reference.html#header.boost.histogram.ostream_hpp).
0021   To use your own, include your own implementation instead of this header and do not
0022   include
0023   [boost/histogram/ostream.hpp](histogram/reference.html#header.boost.histogram.ostream_hpp).
0024  */
0025 
0026 #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
0027 
0028 namespace boost {
0029 namespace histogram {
0030 
0031 namespace detail {
0032 
0033 template <class CharT, class Traits, class T>
0034 std::basic_ostream<CharT, Traits>& handle_nonzero_width(
0035     std::basic_ostream<CharT, Traits>& os, const T& x) {
0036   const auto w = os.width();
0037   os.width(0);
0038   std::streamsize count = 0;
0039   {
0040     auto g = make_count_guard(os, count);
0041     os << x;
0042   }
0043   if (os.flags() & std::ios::left) {
0044     os << x;
0045     for (auto i = count; i < w; ++i) os << os.fill();
0046   } else {
0047     for (auto i = count; i < w; ++i) os << os.fill();
0048     os << x;
0049   }
0050   return os;
0051 }
0052 
0053 } // namespace detail
0054 
0055 namespace accumulators {
0056 
0057 template <class CharT, class Traits, class U, bool B>
0058 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
0059                                               const count<U, B>& x) {
0060   return os << x.value();
0061 }
0062 
0063 template <class CharT, class Traits, class U>
0064 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
0065                                               const sum<U>& x) {
0066   if (os.width() == 0)
0067     return os << "sum(" << x.large_part() << " + " << x.small_part() << ")";
0068   return detail::handle_nonzero_width(os, x);
0069 }
0070 
0071 template <class CharT, class Traits, class U>
0072 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
0073                                               const weighted_sum<U>& x) {
0074   if (os.width() == 0)
0075     return os << "weighted_sum(" << x.value() << ", " << x.variance() << ")";
0076   return detail::handle_nonzero_width(os, x);
0077 }
0078 
0079 template <class CharT, class Traits, class U>
0080 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
0081                                               const mean<U>& x) {
0082   if (os.width() == 0)
0083     return os << "mean(" << x.count() << ", " << x.value() << ", " << x.variance() << ")";
0084   return detail::handle_nonzero_width(os, x);
0085 }
0086 
0087 template <class CharT, class Traits, class U>
0088 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
0089                                               const weighted_mean<U>& x) {
0090   if (os.width() == 0)
0091     return os << "weighted_mean(" << x.sum_of_weights() << ", " << x.value() << ", "
0092               << x.variance() << ")";
0093   return detail::handle_nonzero_width(os, x);
0094 }
0095 
0096 template <class CharT, class Traits, class U>
0097 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
0098                                               const fraction<U>& x) {
0099   if (os.width() == 0)
0100     return os << "fraction(" << x.successes() << ", " << x.failures() << ")";
0101   return detail::handle_nonzero_width(os, x);
0102 }
0103 
0104 } // namespace accumulators
0105 } // namespace histogram
0106 } // namespace boost
0107 
0108 #endif // BOOST_HISTOGRAM_DOXYGEN_INVOKED
0109 
0110 #endif