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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260 | #pragma once
#include <cassert><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <utility>
#include <type_traits><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <tuple><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "internal/dev_env.hpp"
#include "internal/iterator.hpp"
#include "numeric/arithmetic.hpp"
#include "view/internal/box.hpp"
#include <concepts><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ranges><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
namespace uni {
template <std::move_constructible T, std::semiregular Bound = std::unreachable_sentinel_t>
requires
std::is_object_v<T> && std::same_as<T, std::remove_cv_t<T>> &&
(
std::is_integral_v<Bound> ||
std::same_as<Bound, std::unreachable_sentinel_t>
)
struct repeat_view : public std::ranges::view_interface<repeat_view<T, Bound>> {
private:
internal::box<T> _value;
[[no_unique_address]] Bound _bound = Bound();
struct iterator;
template <typename Range>
friend constexpr auto take_of_repeat_view(Range&&, std::ranges::range_difference_t<Range>);
template <typename Range>
friend constexpr auto drop_of_repeat_view(Range &&, std::ranges::range_difference_t<Range>);
public:
repeat_view() requires std::default_initializable<T> = default;
constexpr explicit repeat_view(const T& val, Bound bound = Bound())
requires std::copy_constructible<T> : _value(val), _bound(bound) {
if constexpr(!std::same_as<Bound, std::unreachable_sentinel_t>) assert(bound >= 0);
}
constexpr explicit repeat_view(T&& val, Bound bound = Bound())
: _value(std::move(val)), _bound(bound)
{}
template <typename... Args, typename... BoundArgs>
requires std::constructible_from<T, Args...> && std::constructible_from<Bound, BoundArgs...>
constexpr explicit repeat_view(
std::piecewise_construct_t, std::tuple<Args...> args,
std::tuple<BoundArgs...> bound_args = std::tuple<>{}
)
: _value(std::make_from_tuple<T>(std::move(args))),
_bound(std::make_from_tuple<Bound>(std::move(bound_args)))
{}
constexpr iterator begin() const {
return iterator(std::addressof(*this->_value));
}
constexpr iterator end() const
requires(!std::same_as<Bound, std::unreachable_sentinel_t>)
{
return iterator(std::addressof(*this->_value), this->_bound);
}
constexpr std::unreachable_sentinel_t end() const noexcept {
return std::unreachable_sentinel;
}
constexpr auto size() const
requires(!std::same_as<Bound, std::unreachable_sentinel_t>)
{
return to_unsigned(this->_bound);
}
};
template <typename T, typename Bound>
repeat_view(T, Bound) -> repeat_view<T, Bound>;
template <std::move_constructible T, std::semiregular Bound>
requires
std::is_object_v<T> && std::same_as<T, std::remove_cv_t<T>> &&
(std::is_integral_v<Bound> || std::same_as<Bound, std::unreachable_sentinel_t>)
struct repeat_view<T, Bound>::iterator {
private:
using index_type = std::conditional_t<std::same_as<Bound, std::unreachable_sentinel_t>, std::ptrdiff_t, Bound>;
const T* _value = nullptr;
index_type _crrent = index_type();
constexpr explicit iterator(const T* val, index_type bound = index_type())
: _value(val), _crrent(bound)
{
if constexpr(!std::same_as<Bound, std::unreachable_sentinel_t>) assert(bound >= 0);
}
friend repeat_view;
public:
using iterator_concept = std::random_access_iterator_tag;
using iterator_category = std::random_access_iterator_tag;
using value_type = T;
using difference_type =
std::conditional_t<
std::is_integral_v<index_type>,
index_type,
internal::iota_diff_t<index_type>
>;
iterator() = default;
constexpr const T& operator*() const noexcept { return *this->_value; }
constexpr iterator& operator++() {
++this->_crrent;
return *this;
}
constexpr iterator operator++(int) {
auto _tmp = *this;
++*this;
return _tmp;
}
constexpr iterator& operator--() {
if constexpr(!std::same_as<Bound, std::unreachable_sentinel_t>) assert(this->_crrent > 0);
--this->_crrent;
return *this;
}
constexpr iterator operator--(int) {
auto _tmp = *this;
--*this;
return _tmp;
}
constexpr iterator& operator+=(difference_type diff) {
if constexpr(!std::same_as<Bound, std::unreachable_sentinel_t>) assert(this->_crrent + diff >= 0);
this->_crrent += diff;
return *this;
}
constexpr iterator& operator-=(difference_type diff) {
if constexpr(!std::same_as<Bound, std::unreachable_sentinel_t>) assert(this->_crrent - diff >= 0);
this->_crrent -= diff;
return *this;
}
constexpr const T& operator[](difference_type diff) const noexcept {
return *(*this + diff);
}
friend constexpr bool operator==(const iterator& lhs, const iterator& rhs) {
return lhs._crrent == rhs._crrent;
}
friend constexpr auto operator<=>(const iterator& lhs, const iterator& rhs) {
return lhs._crrent <=> rhs._crrent;
}
friend constexpr iterator operator+(iterator itr, difference_type diff) {
return itr += diff;
}
friend constexpr iterator operator+(difference_type diff, iterator itr) {
return itr + diff;
}
friend constexpr iterator operator-(iterator itr, difference_type diff) {
return itr -= diff;
}
friend constexpr difference_type operator-(const iterator& lhs, const iterator& rhs) {
return (
static_cast<difference_type>(lhs._crrent) -
static_cast<difference_type>(rhs._crrent)
);
}
};
namespace views {
namespace internal {
template<typename _Range>
inline constexpr bool is_repeat_view = false;
template <class T, class Bound>
inline constexpr bool is_repeat_view<repeat_view<T, Bound>> = true;
template <class T>
concept can_repeat_view = requires { repeat_view(std::declval<T>()); };
template <class T, class Bound>
concept can_bounded_repeat_view =
requires { repeat_view(std::declval<T>(), std::declval<Bound>()); };
} // namespace internal
struct Repeat {
template <class T>
requires internal::can_repeat_view<T>
constexpr auto operator() [[nodiscard]] (T&& val) const {
return repeat_view(std::forward<T>(val));
}
template <class T, class Bound>
requires internal::can_bounded_repeat_view<T, Bound>
constexpr auto operator()
[[nodiscard]] (T&& val, Bound bound) const {
return repeat_view(std::forward<T>(val), bound);
}
};
inline constexpr Repeat repeat;
namespace internal {
template<class Range>
constexpr auto take_of_repeat_view(Range&& range, std::ranges::range_difference_t<Range> diff) {
using T = std::remove_cvref_t<Range>;
static_assert(is_repeat_view<T>);
if constexpr(std::ranges::sized_range<T>) return views::repeat(*range._value, std::min(std::ranges::distance(range), diff));
else return views::repeat(*range._value, diff);
}
template<class Range>
constexpr auto drop_of_repeat_view(Range&& range, std::ranges::range_difference_t<Range> diff) {
using T = std::remove_cvref_t<Range>;
static_assert(is_repeat_view<T>);
if constexpr(std::ranges::sized_range<T>) {
auto size = std::ranges::distance(range);
return views::repeat(*range._value, size - std::min(size, diff));
}
else {
return range;
}
}
} // namespace internal
} // namespace views
} // namespace uni
|