/
input_stream.hpp
53 lines (49 loc) · 2.55 KB
/
input_stream.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
#ifndef SUISEN_INPUT_STREAM
#define SUISEN_INPUT_STREAM
#include <iostream>
#include "library/type_traits/type_traits.hpp"
namespace suisen::io {
template <typename IStream, std::enable_if_t<std::conjunction_v<std::is_base_of<std::istream, std::remove_reference_t<IStream>>, std::negation<std::is_const<std::remove_reference_t<IStream>>>>, std::nullptr_t> = nullptr>
struct InputStream {
private:
using istream_type = std::remove_reference_t<IStream>;
IStream is;
struct { InputStream* is; template <typename T> operator T() { T e; *is >> e; return e; } } _reader{ this };
public:
template <typename IStream_> InputStream(IStream_ &&is) : is(std::move(is)) {}
template <typename IStream_> InputStream(IStream_ &is) : is(is) {}
template <typename T> InputStream& operator>>(T& e) {
if constexpr (suisen::is_readable_v<T>) is >> e; else _read(e);
return *this;
}
auto read() { return _reader; }
template <typename Head, typename... Tail>
void read(Head& head, Tail &...tails) { ((*this >> head) >> ... >> tails); }
istream_type& get_stream() { return is; }
private:
static __uint128_t _stou128(const std::string& s) {
__uint128_t ret = 0;
for (char c : s) if ('0' <= c and c <= '9') ret = 10 * ret + c - '0';
return ret;
}
static __int128_t _stoi128(const std::string& s) { return (s[0] == '-' ? -1 : +1) * _stou128(s); }
void _read(__uint128_t& v) { v = _stou128(std::string(_reader)); }
void _read(__int128_t& v) { v = _stoi128(std::string(_reader)); }
template <typename T, typename U>
void _read(std::pair<T, U>& a) { *this >> a.first >> a.second; }
template <size_t N = 0, typename ...Args>
void _read(std::tuple<Args...>& a) { if constexpr (N < sizeof...(Args)) *this >> std::get<N>(a), _read<N + 1>(a); }
template <typename Iterable, std::enable_if_t<suisen::is_iterable_v<Iterable>, std::nullptr_t> = nullptr>
void _read(Iterable& a) { for (auto& e : a) *this >> e; }
};
template <typename IStream>
InputStream(IStream &&) -> InputStream<IStream>;
template <typename IStream>
InputStream(IStream &) -> InputStream<IStream&>;
InputStream cin{ std::cin };
auto read() { return cin.read(); }
template <typename Head, typename... Tail>
void read(Head& head, Tail &...tails) { cin.read(head, tails...); }
} // namespace suisen::io
namespace suisen { using io::read; } // namespace suisen
#endif // SUISEN_INPUT_STREAM