forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeat.hpp
169 lines (130 loc) · 3.48 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
158
159
160
161
162
163
164
165
166
167
168
169
#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 {
private:
const TPlain* elem_;
int count_;
public:
using iterator_category = std::input_iterator_tag;
using value_type = const TPlain;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
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 {
private:
const TPlain* elem_;
public:
using iterator_category = std::input_iterator_tag;
using value_type = const TPlain;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
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