forked from alabuzhev/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repeat.hpp
157 lines (120 loc) · 3.18 KB
/
repeat.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
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
#ifndef ITER_REPEAT_HPP_
#define ITER_REPEAT_HPP_
#include <iterator>
#include <type_traits>
#include <utility>
namespace iter {
namespace impl {
template <typename T>
class RepeaterWithCount;
}
template <typename T>
constexpr impl::RepeaterWithCount<T> repeat(T&&, int);
}
template <typename T>
class iter::impl::RepeaterWithCount {
// see stackoverflow.com/questions/32174186/ about why this isn't
// declaring just a specialization as friend
template <typename U>
friend constexpr RepeaterWithCount<U> iter::repeat(U&&, int);
private:
T elem;
int count;
constexpr RepeaterWithCount(T e, int c)
: elem(std::forward<T>(e)), count{c} {}
using TPlain = typename std::remove_reference<T>::type;
public:
RepeaterWithCount(RepeaterWithCount&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag, const TPlain> {
private:
const TPlain* elem;
int count;
public:
constexpr Iterator(const TPlain* e, int c) : elem{e}, count{c} {}
Iterator& operator++() {
--this->count;
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
constexpr bool operator!=(const Iterator& other) const {
return !(*this == other);
}
constexpr bool operator==(const Iterator& other) const {
return this->count == other.count;
}
constexpr const TPlain& operator*() const {
return *this->elem;
}
constexpr const TPlain* operator->() const {
return this->elem;
}
};
constexpr Iterator begin() const {
return {&this->elem, this->count};
}
constexpr Iterator end() const {
return {&this->elem, 0};
}
};
template <typename T>
constexpr iter::impl::RepeaterWithCount<T> iter::repeat(T&& e, int count) {
return {std::forward<T>(e), count < 0 ? 0 : count};
}
namespace iter {
namespace impl {
template <typename T>
class Repeater;
}
template <typename T>
constexpr impl::Repeater<T> repeat(T&&);
}
template <typename T>
class iter::impl::Repeater {
template <typename U>
friend constexpr Repeater<U> iter::repeat(U&&);
private:
using TPlain = typename std::remove_reference<T>::type;
T elem;
constexpr Repeater(T e) : elem(std::forward<T>(e)) {}
public:
Repeater(Repeater&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag, const TPlain> {
private:
const TPlain* elem;
public:
constexpr Iterator(const TPlain* e) : elem{e} {}
constexpr const Iterator& operator++() const {
return *this;
}
constexpr Iterator operator++(int) const {
return *this;
}
constexpr bool operator!=(const Iterator&) const {
return true;
}
constexpr bool operator==(const Iterator&) const {
return false;
}
constexpr const TPlain& operator*() const {
return *this->elem;
}
constexpr const TPlain* operator->() const {
return this->elem;
}
};
constexpr Iterator begin() const {
return {&this->elem};
}
constexpr Iterator end() const {
return {nullptr};
}
};
template <typename T>
constexpr iter::impl::Repeater<T> iter::repeat(T&& e) {
return {std::forward<T>(e)};
}
#endif