1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___RANGES_REPEAT_VIEW_H
11#define _LIBCPP___RANGES_REPEAT_VIEW_H
12
13#include <__assert>
14#include <__concepts/constructible.h>
15#include <__concepts/same_as.h>
16#include <__concepts/semiregular.h>
17#include <__config>
18#include <__cstddef/ptrdiff_t.h>
19#include <__iterator/concepts.h>
20#include <__iterator/iterator_traits.h>
21#include <__iterator/unreachable_sentinel.h>
22#include <__memory/addressof.h>
23#include <__ranges/iota_view.h>
24#include <__ranges/movable_box.h>
25#include <__ranges/view_interface.h>
26#include <__type_traits/decay.h>
27#include <__type_traits/is_object.h>
28#include <__type_traits/make_unsigned.h>
29#include <__type_traits/remove_cv.h>
30#include <__utility/forward.h>
31#include <__utility/in_place.h>
32#include <__utility/move.h>
33#include <__utility/piecewise_construct.h>
34#include <tuple>
35
36#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
37# pragma GCC system_header
38#endif
39
40_LIBCPP_PUSH_MACROS
41#include <__undef_macros>
42
43_LIBCPP_BEGIN_NAMESPACE_STD
44
45#if _LIBCPP_STD_VER >= 23
46
47namespace ranges {
48
49template <class _Tp>
50concept __integer_like_with_usable_difference_type =
51 __signed_integer_like<_Tp> || (__integer_like<_Tp> && weakly_incrementable<_Tp>);
52
53template <class _Tp>
54struct __repeat_view_iterator_difference {
55 using type _LIBCPP_NODEBUG = _IotaDiffT<_Tp>;
56};
57
58template <__signed_integer_like _Tp>
59struct __repeat_view_iterator_difference<_Tp> {
60 using type _LIBCPP_NODEBUG = _Tp;
61};
62
63template <class _Tp>
64using __repeat_view_iterator_difference_t _LIBCPP_NODEBUG = typename __repeat_view_iterator_difference<_Tp>::type;
65
66namespace views::__drop {
67struct __fn;
68} // namespace views::__drop
69
70namespace views::__take {
71struct __fn;
72} // namespace views::__take
73
74template <move_constructible _Tp, semiregular _Bound = unreachable_sentinel_t>
75 requires(is_object_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>> &&
76 (__integer_like_with_usable_difference_type<_Bound> || same_as<_Bound, unreachable_sentinel_t>))
77class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS repeat_view : public view_interface<repeat_view<_Tp, _Bound>> {
78 friend struct views::__take::__fn;
79 friend struct views::__drop::__fn;
80 class __iterator;
81
82public:
83 _LIBCPP_HIDE_FROM_ABI repeat_view()
84 requires default_initializable<_Tp>
85 = default;
86
87 _LIBCPP_HIDE_FROM_ABI constexpr explicit repeat_view(const _Tp& __value, _Bound __bound_sentinel = _Bound())
88 requires copy_constructible<_Tp>
89 : __value_(in_place, __value), __bound_(__bound_sentinel) {
90 if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
91 _LIBCPP_ASSERT_UNCATEGORIZED(__bound_ >= 0, "The value of bound must be greater than or equal to 0");
92 }
93
94 _LIBCPP_HIDE_FROM_ABI constexpr explicit repeat_view(_Tp&& __value, _Bound __bound_sentinel = _Bound())
95 : __value_(in_place, std::move(__value)), __bound_(__bound_sentinel) {
96 if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
97 _LIBCPP_ASSERT_UNCATEGORIZED(__bound_ >= 0, "The value of bound must be greater than or equal to 0");
98 }
99
100 template <class... _TpArgs, class... _BoundArgs>
101 requires(constructible_from<_Tp, _TpArgs...> && constructible_from<_Bound, _BoundArgs...>)
102 _LIBCPP_HIDE_FROM_ABI constexpr explicit repeat_view(
103 piecewise_construct_t, tuple<_TpArgs...> __value_args, tuple<_BoundArgs...> __bound_args = tuple<>{})
104 : __value_(in_place, std::make_from_tuple<_Tp>(std::move(__value_args))),
105 __bound_(std::make_from_tuple<_Bound>(std::move(__bound_args))) {
106 if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
107 _LIBCPP_ASSERT_UNCATEGORIZED(
108 __bound_ >= 0, "The behavior is undefined if Bound is not unreachable_sentinel_t and bound is negative");
109 }
110
111 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() const {
112 return __iterator(std::addressof(*__value_));
113 }
114
115 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __iterator end() const
116 requires(!same_as<_Bound, unreachable_sentinel_t>)
117 {
118 return __iterator(std::addressof(*__value_), __bound_);
119 }
120
121 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr unreachable_sentinel_t end() const noexcept {
122 return unreachable_sentinel;
123 }
124
125 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
126 requires(!same_as<_Bound, unreachable_sentinel_t>)
127 {
128 return std::__to_unsigned_like(__bound_);
129 }
130
131private:
132 _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Tp> __value_;
133 _LIBCPP_NO_UNIQUE_ADDRESS _Bound __bound_ = _Bound();
134};
135
136template <class _Tp, class _Bound = unreachable_sentinel_t>
137repeat_view(_Tp, _Bound = _Bound()) -> repeat_view<_Tp, _Bound>;
138
139// [range.repeat.iterator]
140template <move_constructible _Tp, semiregular _Bound>
141 requires(is_object_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>> &&
142 (__integer_like_with_usable_difference_type<_Bound> || same_as<_Bound, unreachable_sentinel_t>))
143class repeat_view<_Tp, _Bound>::__iterator {
144 friend class repeat_view;
145
146 using _IndexT _LIBCPP_NODEBUG = conditional_t<same_as<_Bound, unreachable_sentinel_t>, ptrdiff_t, _Bound>;
147
148 _LIBCPP_HIDE_FROM_ABI constexpr explicit __iterator(const _Tp* __value, _IndexT __bound_sentinel = _IndexT())
149 : __value_(__value), __current_(__bound_sentinel) {}
150
151public:
152 using iterator_concept = random_access_iterator_tag;
153 using iterator_category = random_access_iterator_tag;
154 using value_type = _Tp;
155 using difference_type = __repeat_view_iterator_difference_t<_IndexT>;
156
157 _LIBCPP_HIDE_FROM_ABI __iterator() = default;
158
159 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const noexcept { return *__value_; }
160
161 _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
162 ++__current_;
163 return *this;
164 }
165
166 _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int) {
167 auto __tmp = *this;
168 ++*this;
169 return __tmp;
170 }
171
172 _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--() {
173 if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
174 _LIBCPP_ASSERT_UNCATEGORIZED(__current_ > 0, "The value of bound must be greater than or equal to 0");
175 --__current_;
176 return *this;
177 }
178
179 _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int) {
180 auto __tmp = *this;
181 --*this;
182 return __tmp;
183 }
184
185 _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator+=(difference_type __n) {
186 if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
187 _LIBCPP_ASSERT_UNCATEGORIZED(__current_ + __n >= 0, "The value of bound must be greater than or equal to 0");
188 __current_ += __n;
189 return *this;
190 }
191
192 _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator-=(difference_type __n) {
193 if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
194 _LIBCPP_ASSERT_UNCATEGORIZED(__current_ - __n >= 0, "The value of bound must be greater than or equal to 0");
195 __current_ -= __n;
196 return *this;
197 }
198
199 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator[](difference_type __n) const noexcept {
200 return *(*this + __n);
201 }
202
203 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) {
204 return __x.__current_ == __y.__current_;
205 }
206
207 _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y) {
208 return __x.__current_ <=> __y.__current_;
209 }
210
211 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(__iterator __i, difference_type __n) {
212 __i += __n;
213 return __i;
214 }
215
216 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, __iterator __i) {
217 __i += __n;
218 return __i;
219 }
220
221 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(__iterator __i, difference_type __n) {
222 __i -= __n;
223 return __i;
224 }
225
226 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type
227 operator-(const __iterator& __x, const __iterator& __y) {
228 return static_cast<difference_type>(__x.__current_) - static_cast<difference_type>(__y.__current_);
229 }
230
231private:
232 const _Tp* __value_ = nullptr;
233 _IndexT __current_ = _IndexT();
234};
235
236// clang-format off
237namespace views {
238namespace __repeat {
239struct __fn {
240 template <class _Tp>
241 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Tp&& __value)
242 noexcept(noexcept(ranges::repeat_view<decay_t<_Tp>>(std::forward<_Tp>(__value))))
243 -> decltype( ranges::repeat_view<decay_t<_Tp>>(std::forward<_Tp>(__value)))
244 { return ranges::repeat_view<decay_t<_Tp>>(std::forward<_Tp>(__value)); }
245
246 template <class _Tp, class _Bound>
247 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Tp&& __value, _Bound&& __bound_sentinel)
248 noexcept(noexcept(ranges::repeat_view(std::forward<_Tp>(__value), std::forward<_Bound>(__bound_sentinel))))
249 -> decltype( ranges::repeat_view(std::forward<_Tp>(__value), std::forward<_Bound>(__bound_sentinel)))
250 { return ranges::repeat_view(std::forward<_Tp>(__value), std::forward<_Bound>(__bound_sentinel)); }
251};
252} // namespace __repeat
253// clang-format on
254
255inline namespace __cpo {
256inline constexpr auto repeat = __repeat::__fn{};
257} // namespace __cpo
258} // namespace views
259
260template <class _Tp>
261inline constexpr bool __is_repeat_specialization = false;
262
263template <class _Tp, class _Bound>
264inline constexpr bool __is_repeat_specialization<repeat_view<_Tp, _Bound>> = true;
265
266} // namespace ranges
267
268#endif // _LIBCPP_STD_VER >= 23
269
270_LIBCPP_END_NAMESPACE_STD
271
272_LIBCPP_POP_MACROS
273
274#endif // _LIBCPP___RANGES_REPEAT_VIEW_H
275