SIRIUS 7.5.0
Electronic structure library and applications
ostream_tools.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2023 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file ostream_tools.hpp
21 *
22 * \brief Output stream tools.
23 */
24
25#ifndef __OSTREAM_TOOLS_HPP__
26#define __OSTREAM_TOOLS_HPP__
27
28#include <ostream>
29#include <iomanip>
30#include <vector>
31#include <cmath>
32
33namespace sirius {
34
35class null_stream_t : public std::ostream
36{
37 public:
38 null_stream_t() : std::ostream(nullptr)
39 {
40 }
41 null_stream_t(null_stream_t&&) : std::ostream(nullptr)
42 {
43 };
44};
45
46null_stream_t& null_stream();
47
48inline std::string boolstr(bool b__)
49{
50 if (b__) {
51 return "true";
52 } else {
53 return "false";
54 }
55}
56
57/// Horisontal bar.
58class hbar {
59 private:
60 int w_;
61 char c_;
62 public:
63 hbar(int w__, char c__)
64 : w_(w__)
65 , c_(c__)
66 {
67 }
68 int w() const
69 {
70 return w_;
71 }
72 char c() const
73 {
74 return c_;
75 }
76};
77
78/// Inject horisontal bar to ostream.
79inline std::ostream&
80operator<<(std::ostream& out, hbar&& b)
81{
82 char prev = out.fill();
83 out << std::setfill(b.c()) << std::setw(b.w()) << b.c() << std::setfill(prev);
84 return out;
85}
86
87/// Floating-point formatting (precision and width).
88class ffmt {
89 private:
90 int w_;
91 int p_;
92 public:
93 ffmt(int w__, int p__)
94 : w_(w__)
95 , p_(p__)
96 {
97 }
98 int w() const
99 {
100 return w_;
101 }
102 int p() const
103 {
104 return p_;
105 }
106};
107
108/// Inject floating point format to ostream.
109inline std::ostream&
110operator<<(std::ostream& out, ffmt&& f)
111{
112 out.precision(f.p());
113 out.width(f.w());
114 out.setf(std::ios_base::fixed, std::ios_base::floatfield);
115 return out;
116}
117
118/// Print std::vector to ostream.
119template <typename T>
120inline std::ostream&
121operator<<(std::ostream& out, std::vector<T>& v)
122{
123 if (v.size() == 0) {
124 out << "{}";
125 } else {
126 out << "{";
127 for (size_t i = 0; i < v.size() - 1; i++) {
128 out << v[i] << ", ";
129 }
130 out << v.back() << "}";
131 }
132 return out;
133}
134
135/// Convert double to a string with a given precision.
136inline std::string double_to_string(double val, int precision = -1)
137{
138 char buf[100];
139
140 double abs_val = std::abs(val);
141
142 if (precision == -1) {
143 if (abs_val > 1.0) {
144 precision = 6;
145 } else if (abs_val > 1e-14) {
146 precision = int(-std::log(abs_val) / std::log(10.0)) + 7;
147 } else {
148 return std::string("0.0");
149 }
150 }
151
152 std::stringstream fmt;
153 fmt << "%." << precision << "f";
154
155 int len = snprintf(buf, 100, fmt.str().c_str(), val);
156 for (int i = len - 1; i >= 1; i--) {
157 if (buf[i] == '0' && buf[i - 1] == '0') {
158 buf[i] = 0;
159 } else {
160 break;
161 }
162 }
163 return std::string(buf);
164}
165
166template <typename T, typename OUT>
167inline void print_checksum(std::string label__, T value__, OUT&& out__)
168{
169 out__ << "checksum(" << label__ << ") : " << ffmt(16, 8) << value__ << std::endl;
170}
171
172template <typename OUT>
173inline void print_hash(std::string label__, unsigned long long int hash__, OUT&& out__)
174{
175 out__ << "hashsum(" << label__ << ") : " << std::hex << hash__ << std::endl;
176}
177
178} // namespace
179
180#endif
Floating-point formatting (precision and width).
Horisontal bar.
Namespace of the SIRIUS library.
Definition: sirius.f90:5
std::ostream & operator<<(std::ostream &out, hbar &&b)
Inject horisontal bar to ostream.
std::string double_to_string(double val, int precision=-1)
Convert double to a string with a given precision.