-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange.cpp
More file actions
144 lines (107 loc) · 2.78 KB
/
range.cpp
File metadata and controls
144 lines (107 loc) · 2.78 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
#include "print_iterable.hpp"
class Range
{
int start, end_;
public:
Range(int start, int end) : start(start), end_(end) {}
void iterate(std::function<void(int)> yield_fn = [](int i) {std::cout << i << '\n';})
{
for (int i = start; i < end_; i++)
yield_fn(i);
}
// C++
class CppIterator
{
int val;
public:
CppIterator(int val) : val(val) {}
bool operator!=(CppIterator it) const {return val != it.val;}
auto &operator*() {return val;}
void operator++() {++val;}
};
CppIterator begin() const {return CppIterator(start);}
CppIterator end () const {return CppIterator(end_);}
// D
class DRange
{
int cur, end;
public:
DRange(int start, int end) : cur(start), end(end) {}
bool empty() {return cur >= end;}
auto front() {return cur;}
void popFront() {++cur;}
};
DRange range() const {return DRange(start, end_);}
// Python
class PythonIterator
{
int cur, end;
public:
PythonIterator(int start, int end) : cur(start), end(end) {}
int __next__()
{
if (cur >= end) throw StopIteration();
return cur++;
}
};
auto __iter__() const {return PythonIterator(start, end_);}
// Rust
class RustIterator
{
int cur, end;
public:
RustIterator(int start, int end) : cur(start), end(end) {}
std::optional<int> next()
{
if (cur >= end) return std::nullopt;
return cur++;
}
};
auto iter() const {return RustIterator(start, end_);}
// Java
class JavaIterator
{
int cur, end;
public:
JavaIterator(int start, int end) : cur(start), end(end) {}
bool hasNext() {return cur < end;}
int next()
{
if (!hasNext()) throw NoSuchElementException();
return cur++;
}
};
auto iterator() const {return JavaIterator(start, end_);}
// С#
class CsharpIterator
{
int cur, end;
public:
CsharpIterator(int start, int end) : cur(start - 1), end(end) {}
bool MoveNext()
{
return ++cur < end;
}
int Current() {return cur;}
};
auto GetEnumerator() const {return CsharpIterator(start, end_);}
// 11l
class Iterator11l
{
int cur, end;
public:
Iterator11l(int start, int end) : cur(start), end(end) {}
int current() {return cur;}
bool advance() {return ++cur < end;}
};
std::optional<Iterator11l> iter11l() const
{
if (start >= end_)
return std::nullopt;
return Iterator11l(start, end_);
}
};
int main()
{
print_iterable(Range(1, 10));
}