-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio.hpp
179 lines (146 loc) · 2.99 KB
/
io.hpp
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
// This file is covered by the LICENSE file in the root of this project.
#pragma once
#include <cassert>
#include <cstddef>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <sstream>
#include <string>
#include <tuple>
#include <type_traits>
std::istream* istream;
bool ignore_line()
{
return !!istream->ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
char peek()
{
const auto ch = istream->peek();
assert(istream->good());
return static_cast<char>(ch);
}
bool read()
{
return true;
}
template<typename T>
bool read(T& arg)
{
return !!((*istream) >> arg);
}
template<typename T, typename... Ts>
bool read(T& arg, Ts&... args)
{
return read(arg) && read(args...);
}
template<typename T1, typename T2>
bool read(std::pair<T1, T2>& pair)
{
return read(pair.first, pair.second);
}
bool read_ln_non_empty(std::string& str)
{
return !!std::getline(*istream, str) && !str.empty();
}
bool read_ln(std::string& str)
{
return !!std::getline(*istream, str);
}
template<typename... Ts>
bool read_ln(Ts&... args)
{
return read(args...) && ignore_line();
}
template<class T = void, class V, class Fn>
bool read_vec(std::size_t count, V& vec, Fn fn)
{
using S = typename std::conditional<std::is_void<T>::value, typename V::value_type, T>::type;
vec.reserve(vec.size() + count);
for (std::size_t i = 0; i < count; ++i)
{
S y;
if (!read(y))
return false;
vec.push_back(fn(y));
}
return true;
}
template<class V>
bool read_vec(std::size_t count, V& vec)
{
using T = typename V::value_type;
return read_vec<T>(count, vec, [](T x) { return x; });
}
template<typename T = void, class V, class Fn>
bool read_size_vec(V& vec, Fn fn)
{
std::size_t size;
read(size);
if (size == 0)
return false;
vec.clear();
return read_vec<T>(size, vec, fn);
}
template<class V>
bool read_size_vec(V& vec)
{
std::size_t size;
vec.clear();
if (!read(size) || size == 0)
return false;
return read_vec(size, vec);
}
template<class V>
bool read_vec_ln(V& vec)
{
using T = typename V::value_type;
std::string str;
if (!read_ln(str))
return false;
std::istringstream ss(str);
T x;
while (ss >> x)
vec.push_back(x);
return !vec.empty();
}
void write()
{}
template<typename T>
void write(const T& arg)
{
std::cout << arg;
}
template<typename T, typename... Ts>
void write(const T& arg, const Ts&... args)
{
write(arg);
write(args...);
}
template<typename... Ts>
void write_ln(const Ts&... args)
{
write(args..., '\n');
}
template<class It, class Fn, typename Join>
void write_range(It first, It last, Fn fn, Join join)
{
if (first == last)
return;
write(fn(*first++));
while (first != last)
write(join, fn(*first++));
}
template<class It, typename Join>
void write_range(It first, It last, Join join)
{
using T = typename std::iterator_traits<It>::value_type;
write_range(
first, last, [](const T& x) { return x; }, join);
}
template<class Vec, class... Args>
void write_vec(const Vec& vec, Args... args)
{
write_range(vec.begin(), vec.end(), args...);
}