-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathiteratorUtils.h
137 lines (106 loc) · 2.74 KB
/
iteratorUtils.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
#pragma once
struct iterator_end {};
template<typename Iterator, typename IteratorEnd>
class default_iterator {
private:
Iterator current;
Iterator end;
public:
default_iterator() = default;
default_iterator(const Iterator& start, const Iterator& end) : current(start), end(end) {
}
default_iterator& operator++() {
++current;
return *this;
}
bool operator!=(IteratorEnd) const {
return current != end;
}
decltype(*current)& operator*() const {
return *current;
}
bool operator==(Iterator iterator) const {
return current == iterator;
}
};
template<typename Iterator, typename IteratorEnd, typename Filter>
class filter_iterator {
private:
Iterator current;
Iterator end;
Filter filter;
public:
filter_iterator() = default;
filter_iterator(const Iterator& start, const Iterator& end, const Filter& filter) : current(start), end(end), filter(filter) {
while (current != end && !filter(current))
++current;
}
filter_iterator& operator++() {
do {
++current;
} while (current != end && !filter(current));
return *this;
}
bool operator!=(IteratorEnd) const {
return current != end;
}
decltype(*current)& operator*() const {
return *current;
}
bool operator==(Iterator iterator) const {
return current == iterator;
}
};
template<typename Iterator, typename IteratorEnd, typename Transform>
class transform_iterator {
private:
Iterator current;
Iterator end;
Transform transform;
public:
transform_iterator() = default;
transform_iterator(const Iterator& start, const Iterator& end, const Transform& transform) : current(start), end(end), transform(transform) {
}
transform_iterator& operator++() {
++current;
return *this;
}
bool operator!=(IteratorEnd) const {
return current != end;
}
decltype(transform(current)) operator*() const {
return transform(current);
}
bool operator==(Iterator iterator) const {
return current == iterator;
}
};
template<typename Iterator, typename IteratorEnd, typename Filter, typename Transform>
class filter_transform_iterator {
private:
Iterator current;
Iterator end;
Filter filter;
Transform transform;
public:
filter_transform_iterator() = default;
filter_transform_iterator(const Iterator& start, const Iterator& end, const Filter& filter, const Transform& transform) : current(start), end(end), filter(filter), transform(transform) {
while (current != end && !filter(current))
++current;
}
filter_transform_iterator& operator++() {
do {
++current;
} while (current != end && !filter(current));
return *this;
}
bool operator!=(IteratorEnd) const {
return current != end;
}
decltype(transform(current)) operator*() const {
return transform(current);
}
bool operator==(Iterator iterator) const {
return current == iterator;
}
};