-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprint.h
More file actions
306 lines (255 loc) · 7.89 KB
/
print.h
File metadata and controls
306 lines (255 loc) · 7.89 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
Copyright (C) 2017-2026 Topological Manifold
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "type/concept.h"
#include "type/limit.h"
#include <algorithm>
#include <bit>
#include <cmath>
#include <complex>
#include <iomanip>
#include <ios>
#include <sstream>
#include <string>
#include <string_view>
#include <type_traits>
namespace ns
{
namespace print_implementation
{
template <typename T>
char digit(const T value)
{
const int remainder = [&]
{
const int res = value % 10;
if constexpr (Signed<T>)
{
return std::abs(res);
}
return res;
}();
return remainder + '0';
}
template <typename T>
inline constexpr bool USE_SIGNED_LONG_LONG =
std::is_same_v<signed __int128, std::remove_cv_t<T>>
&& (Limits<long long>::max() < Limits<signed __int128>::max())
&& (Limits<long long>::lowest() > Limits<signed __int128>::lowest());
template <typename T>
inline constexpr bool USE_UNSIGNED_LONG_LONG =
std::is_same_v<unsigned __int128, std::remove_cv_t<T>>
&& (Limits<unsigned long long>::max() < Limits<unsigned __int128>::max());
template <typename T>
using LongLongType = std::conditional_t<
USE_SIGNED_LONG_LONG<T>,
long long,
std::conditional_t<USE_UNSIGNED_LONG_LONG<T>, unsigned long long, void>>;
template <unsigned DIGIT_GROUP_SIZE, typename T>
void make_digit(const T& value, [[maybe_unused]] const char separator, int& index, std::string& str)
{
if constexpr (DIGIT_GROUP_SIZE > 0)
{
++index;
if ((index % DIGIT_GROUP_SIZE) == 0 && index != 0)
{
str += separator;
}
}
str += digit(value);
}
template <unsigned DIGIT_GROUP_SIZE, typename T>
void make_string(T value, int index, [[maybe_unused]] const char separator, std::string& str)
{
do
{
if constexpr (USE_SIGNED_LONG_LONG<T> || USE_UNSIGNED_LONG_LONG<T>)
{
const LongLongType<T> v = value;
if (v == value)
{
make_string<DIGIT_GROUP_SIZE>(v, index, separator, str);
return;
}
}
make_digit<DIGIT_GROUP_SIZE>(value, separator, index, str);
} while ((value /= 10) != 0);
}
template <unsigned DIGIT_GROUP_SIZE, typename T>
requires Integral<T>
[[nodiscard]] std::string to_string_digit_groups(const T value, const char separator)
{
static_assert(!std::is_class_v<T>);
static_assert(Signed<T> != Unsigned<T>);
std::string res;
res.reserve(Limits<T>::digits10() * 2);
make_string<DIGIT_GROUP_SIZE, T>(value, -1, separator, res);
if (Signed<T> && value < 0)
{
res += '-';
}
std::ranges::reverse(res);
return res;
}
}
[[nodiscard]] std::string to_string(__float128 value);
template <typename T>
requires std::is_floating_point_v<T>
[[nodiscard]] std::string to_string(const std::complex<T> value)
{
std::ostringstream oss;
oss << std::setprecision(Limits<T>::max_digits10());
if (value.real() >= 0)
{
oss << " ";
}
else
{
oss << "-";
}
oss << std::abs(value.real());
if (value.imag() >= 0)
{
oss << " + ";
}
else
{
oss << " - ";
}
oss << std::abs(value.imag()) << "*I";
return oss.str();
}
template <typename T>
requires std::is_floating_point_v<T>
[[nodiscard]] std::string to_string(const T value)
{
std::ostringstream oss;
oss << std::setprecision(Limits<T>::max_digits10());
oss << value;
return oss.str();
}
template <typename T>
requires std::is_floating_point_v<T>
[[nodiscard]] std::string to_string(const T value, const unsigned digits)
{
std::ostringstream oss;
oss << std::setprecision(digits);
oss << value;
return oss.str();
}
template <typename T>
requires std::is_floating_point_v<T>
[[nodiscard]] std::string to_string_fixed(const T value, const unsigned digits)
{
std::ostringstream oss;
oss << std::setprecision(digits);
oss << std::fixed;
oss << value;
std::string res = oss.str();
while (!res.empty() && res[res.size() - 1] == '0')
{
res.resize(res.size() - 1);
}
if (!res.empty() && res[res.size() - 1] == '.')
{
res.resize(res.size() - 1);
}
if (res.empty())
{
res = oss.str();
}
return res;
}
template <typename T>
requires std::is_unsigned_v<T>
[[nodiscard]] std::string to_string_binary(T value, const std::string_view prefix = {})
{
std::string res(prefix);
if (value == 0)
{
res += '0';
return res;
}
const auto prefix_size = std::ssize(res);
res.resize(prefix_size + std::bit_width(value));
for (auto i = std::ssize(res) - 1; i >= prefix_size; --i)
{
res[i] = (value & 1u) ? '1' : '0';
value >>= 1u;
}
return res;
}
//
template <typename T>
requires Integral<T>
[[nodiscard]] std::string to_string(const T value)
{
return print_implementation::to_string_digit_groups<0, T>(value, '\x20');
}
template <typename T>
requires Integral<T>
[[nodiscard]] std::string to_string_digit_groups(const T value, const char separator = '\x20')
{
return print_implementation::to_string_digit_groups<3, T>(value, separator);
}
//
void to_string(char) = delete;
void to_string(wchar_t) = delete;
void to_string(char8_t) = delete;
void to_string(char16_t) = delete;
void to_string(char32_t) = delete;
void to_string_digit_groups(char v, char) = delete;
void to_string_digit_groups(wchar_t v, char) = delete;
void to_string_digit_groups(char8_t v, char) = delete;
void to_string_digit_groups(char16_t v, char) = delete;
void to_string_digit_groups(char32_t v, char) = delete;
template <typename T>
requires std::is_pointer_v<T>
void to_string(const T&) = delete;
template <typename T>
void to_string(std::basic_string<T>) = delete;
template <typename T>
void to_string(std::basic_string_view<T>) = delete;
//
template <typename T>
[[nodiscard]] std::string to_string(const T& data)
requires requires {
std::begin(data);
std::end(data);
}
{
static constexpr bool IS_CLASS = std::is_class_v<std::remove_cvref_t<decltype(*std::begin(data))>>;
std::string res;
auto iter = std::begin(data);
const auto end = std::end(data);
if (iter == end)
{
return res;
}
res = to_string(*iter);
while (++iter != end)
{
if constexpr (IS_CLASS)
{
res += '\n';
}
else
{
res += ", ";
}
res += to_string(*iter);
}
return res;
}
}