-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_lines.cpp
More file actions
161 lines (123 loc) · 3.4 KB
/
read_lines.cpp
File metadata and controls
161 lines (123 loc) · 3.4 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
#include "print_iterable.hpp"
#include <fstream>
#include <string>
class Lines
{
const char *fname;
public:
Lines(const char *fname) : fname(fname) {}
void iterate(std::function<void(const std::string&)> yield_fn = [](auto &&line) {std::cout << line << '\n';})
{
std::ifstream f(fname);
std::string line;
while (std::getline(f, line))
yield_fn(line);
}
// C++
class CppSentinel
{
};
class CppIterator
{
std::ifstream f;
std::string line;
bool has_next;
public:
CppIterator(const char *fname) : f(fname) {operator++();}
bool operator!=(CppSentinel) const {return has_next;}
auto &operator*() {return line;}
void operator++() {has_next = (bool)std::getline(f, line);}
};
CppIterator begin() const {return CppIterator(fname);}
CppSentinel end () const {return CppSentinel();}
// D
class DRange
{
std::ifstream f;
std::string line;
bool has_next;
public:
DRange(const char *fname) : f(fname) {popFront();}
bool empty() {return !has_next;}
auto &front() {return line;}
void popFront() {has_next = (bool)std::getline(f, line);}
};
DRange range() const {return DRange(fname);}
// Python
class PythonIterator
{
std::ifstream f;
public:
PythonIterator(const char *fname) : f(fname) {}
auto __next__()
{
std::string line;
if (!std::getline(f, line)) throw StopIteration();
return line;
}
};
auto __iter__() const {return PythonIterator(fname);}
// Rust
class RustIterator
{
std::ifstream f;
public:
RustIterator(const char *fname) : f(fname) {}
std::optional<std::string> next()
{
std::string line;
if (!std::getline(f, line)) return std::nullopt;
return line;
}
};
auto iter() const {return RustIterator(fname);}
// Java
class JavaIterator
{
std::ifstream f;
std::string next_line;
bool has_next;
public:
JavaIterator(const char *fname) : f(fname) {has_next = (bool)std::getline(f, next_line);}
bool hasNext() {return has_next;}
std::string next()
{
if (!hasNext()) throw NoSuchElementException();
std::string current_line = std::move(next_line);
has_next = (bool)std::getline(f, next_line);
return current_line;
}
};
auto iterator() const {return JavaIterator(fname);}
// С#
class CsharpIterator
{
std::ifstream f;
std::string line;
public:
CsharpIterator(const char *fname) : f(fname) {}
bool MoveNext() {return (bool)std::getline(f, line);}
auto &Current() {return line;}
};
auto GetEnumerator() const {return CsharpIterator(fname);}
// 11l
class Iterator11l
{
std::ifstream f;
std::string line;
public:
Iterator11l(const char *fname) : f(fname) {}
std::string ¤t() {return line;}
bool advance() {return (bool)std::getline(f, line);}
};
std::optional<Iterator11l> iter11l() const
{
Iterator11l r(fname);
if (r.advance()) return r;
return std::nullopt;
}
};
int main()
{
print_iterable(Lines("lines.txt"));
}