forked from wesley-a-leung/Problem-Setting-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput.h
199 lines (171 loc) · 6.12 KB
/
Input.h
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
#include <cassert>
#include <cstdlib>
#include <functional>
#include <fstream>
#include <limits>
#include <memory>
#include <regex>
#include <stdexcept>
#include <string>
#include <utility>
namespace ExitCode {
const int AC = 0, WA = 1, PE = 2, IE = 3, PARTIAL = 7;
}
namespace Input {
struct PresentationError : public std::exception {};
struct IntegerParsingError : public PresentationError {};
struct FloatingPointParsingError : public PresentationError {};
struct WhitespaceError : public PresentationError {};
struct RequirementFailure : public std::exception {};
struct IntegerRangeError : public RequirementFailure {};
struct FloatingPointRangeError : public RequirementFailure {};
struct RegexError : public RequirementFailure {};
struct ValidatorErrorHandler {
public:
template <class E> void handle(const E &e) {
throw e;
}
};
struct CheckerErrorHandler {
public:
void handle(const PresentationError &) {
exit(ExitCode::PE);
}
void handle(const RequirementFailure &) {
exit(ExitCode::WA);
}
};
const bool IDENTICAL = true, STANDARD = false;
template <class ErrorHandler, const bool IDENTICAL_WHITESPACE> struct Reader : public ErrorHandler {
private:
std::unique_ptr<std::ifstream> streamPtr;
std::istream &stream;
bool hasLast;
bool skipToNextLine;
char last;
bool isWhitespace(char c) {
if (IDENTICAL_WHITESPACE) return c == ' ' || c == '\n';
return isspace(c);
}
bool isNewLine(char c) {
if (IDENTICAL_WHITESPACE) return c == '\n';
return c == '\n' || c == '\r';
}
bool isEOF(char c) {
return c == std::char_traits<char>::eof();
}
char peekChar() {
if (!hasLast) last = stream.get();
hasLast = true;
return last;
}
char getChar() {
char ret = peekChar();
hasLast = false;
return ret;
}
void skipWhitespace() {
while (isWhitespace(peekChar()) && (skipToNextLine || !isNewLine(peekChar()))) getChar();
skipToNextLine = false;
}
public:
template <class E> void require(bool expr, const E &e) {
if (!expr) ErrorHandler::handle(e);
}
template <class ...Args> Reader(std::istream &stream, Args &&...args)
: ErrorHandler(std::forward<Args>(args)...), stream(stream), hasLast(false), skipToNextLine(true) {}
template <class ...Args> Reader(const std::string &fileName, Args &&...args)
: ErrorHandler(std::forward<Args>(args)...), streamPtr(std::make_unique<std::ifstream>(fileName)),
stream(*streamPtr), hasLast(false), skipToNextLine(true) {}
long long readInt(long long minValid = std::numeric_limits<long long>::min(),
long long maxValid = std::numeric_limits<long long>::max()) {
if (!IDENTICAL_WHITESPACE) skipWhitespace();
std::string token = "";
while (isdigit(peekChar()) || peekChar() == '-') token.push_back(getChar());
long long ret = 0;
try {
ret = std::stoll(token);
} catch (std::invalid_argument &) {
ErrorHandler::handle(IntegerParsingError());
} catch (std::out_of_range &) {
ErrorHandler::handle(IntegerParsingError());
}
require(minValid <= ret && ret <= maxValid, IntegerRangeError());
return ret;
}
long double readFloat(long double minValid = std::numeric_limits<long double>::lowest(),
long double maxValid = std::numeric_limits<long double>::max()) {
if (!IDENTICAL_WHITESPACE) skipWhitespace();
std::string token = "";
while (isdigit(peekChar()) || peekChar() == '-' || peekChar() == '.') token.push_back(getChar());
long double ret = 0;
try {
ret = std::stold(token);
} catch (std::invalid_argument &) {
ErrorHandler::handle(FloatingPointParsingError());
} catch (std::out_of_range &) {
ErrorHandler::handle(FloatingPointParsingError());
}
require(minValid <= ret && ret <= maxValid, FloatingPointRangeError());
return ret;
}
std::string readString(std::regex rgx) {
if (!IDENTICAL_WHITESPACE) skipWhitespace();
std::string ret = "";
while (!isWhitespace(peekChar()) && !isEOF(peekChar())) ret.push_back(getChar());
require(std::regex_match(ret, rgx), RegexError());
return ret;
}
std::string readString() {
if (!IDENTICAL_WHITESPACE) skipWhitespace();
std::string ret = "";
while (!isWhitespace(peekChar()) && !isEOF(peekChar())) ret.push_back(getChar());
return ret;
}
char readChar(std::regex rgx) {
if (!IDENTICAL_WHITESPACE) skipWhitespace();
char ret = getChar();
require(std::regex_match(std::string(1, ret), rgx), RegexError());
return ret;
}
char readChar() {
if (!IDENTICAL_WHITESPACE) skipWhitespace();
return getChar();
}
std::string readLine(std::regex rgx) {
if (!IDENTICAL_WHITESPACE) skipWhitespace();
std::string ret = "";
while (!isNewLine(peekChar()) && !isEOF(peekChar())) ret.push_back(getChar());
require(std::regex_match(ret, rgx), RegexError());
return ret;
}
std::string readLine() {
if (!IDENTICAL_WHITESPACE) skipWhitespace();
std::string ret = "";
while (!isNewLine(peekChar()) && !isEOF(peekChar())) ret.push_back(getChar());
return ret;
}
void readSpace() {
require(isWhitespace(peekChar()) && !isNewLine(peekChar()), WhitespaceError());
getChar();
}
void readNewLine() {
if (!IDENTICAL_WHITESPACE) skipWhitespace();
skipToNextLine = true;
require((!IDENTICAL_WHITESPACE && isEOF(peekChar())) || isNewLine(getChar()), WhitespaceError());
}
bool atEOF() {
skipToNextLine = true;
if (!IDENTICAL_WHITESPACE) skipWhitespace();
return isEOF(peekChar());
}
void readEOF() {
skipToNextLine = true;
if (!IDENTICAL_WHITESPACE) skipWhitespace();
require(isEOF(getChar()), WhitespaceError());
}
};
using Validator = Reader<ValidatorErrorHandler, IDENTICAL>;
using StandardReader = Reader<CheckerErrorHandler, STANDARD>;
using IdenticalReader = Reader<CheckerErrorHandler, IDENTICAL>;
}