2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___RANGES_SUBRANGE_H
11 #define _LIBCPP___RANGES_SUBRANGE_H
14 #include <__concepts/constructible.h>
15 #include <__concepts/convertible_to.h>
16 #include <__concepts/copyable.h>
17 #include <__concepts/derived_from.h>
18 #include <__concepts/different_from.h>
20 #include <__cstddef/size_t.h>
21 #include <__fwd/subrange.h>
22 #include <__iterator/advance.h>
23 #include <__iterator/concepts.h>
24 #include <__iterator/incrementable_traits.h>
25 #include <__iterator/iterator_traits.h>
26 #include <__ranges/access.h>
27 #include <__ranges/concepts.h>
28 #include <__ranges/dangling.h>
29 #include <__ranges/enable_borrowed_range.h>
30 #include <__ranges/size.h>
31 #include <__ranges/view_interface.h>
32 #include <__tuple/tuple_element.h>
33 #include <__tuple/tuple_like_no_subrange.h>
34 #include <__tuple/tuple_size.h>
35 #include <__type_traits/conditional.h>
36 #include <__type_traits/decay.h>
37 #include <__type_traits/integral_constant.h>
38 #include <__type_traits/is_pointer.h>
39 #include <__type_traits/is_reference.h>
40 #include <__type_traits/make_unsigned.h>
41 #include <__type_traits/remove_const.h>
42 #include <__type_traits/remove_pointer.h>
43 #include <__utility/move.h>
45 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
46 # pragma GCC system_header
50 #include <__undef_macros>
52 _LIBCPP_BEGIN_NAMESPACE_STD
54 #if _LIBCPP_STD_VER >= 20
57 template <class _From
, class _To
>
58 concept __uses_nonqualification_pointer_conversion
=
59 is_pointer_v
<_From
> && is_pointer_v
<_To
> &&
60 !convertible_to
<remove_pointer_t
<_From
> (*)[], remove_pointer_t
<_To
> (*)[]>;
62 template <class _From
, class _To
>
63 concept __convertible_to_non_slicing
=
64 convertible_to
<_From
, _To
> && !__uses_nonqualification_pointer_conversion
<decay_t
<_From
>, decay_t
<_To
>>;
66 template <class _Pair
, class _Iter
, class _Sent
>
67 concept __pair_like_convertible_from
=
68 !range
<_Pair
> && __pair_like_no_subrange
<_Pair
> && constructible_from
<_Pair
, _Iter
, _Sent
> &&
69 __convertible_to_non_slicing
<_Iter
, tuple_element_t
<0, _Pair
>> && convertible_to
<_Sent
, tuple_element_t
<1, _Pair
>>;
71 template <input_or_output_iterator _Iter
,
72 sentinel_for
<_Iter
> _Sent
= _Iter
,
73 subrange_kind _Kind
= sized_sentinel_for
<_Sent
, _Iter
> ? subrange_kind::sized
: subrange_kind::unsized
>
74 requires(_Kind
== subrange_kind::sized
|| !sized_sentinel_for
<_Sent
, _Iter
>)
75 class _LIBCPP_TEMPLATE_VIS subrange
: public view_interface
<subrange
<_Iter
, _Sent
, _Kind
>> {
77 // Note: this is an internal implementation detail that is public only for internal usage.
78 static constexpr bool _StoreSize
= (_Kind
== subrange_kind::sized
&& !sized_sentinel_for
<_Sent
, _Iter
>);
81 static constexpr bool _MustProvideSizeAtConstruction
= !_StoreSize
; // just to improve compiler diagnostics
83 _LIBCPP_HIDE_FROM_ABI
constexpr _Empty(auto) noexcept
{}
85 using _Size
= conditional_t
<_StoreSize
, make_unsigned_t
<iter_difference_t
<_Iter
>>, _Empty
>;
86 _LIBCPP_NO_UNIQUE_ADDRESS _Iter __begin_
= _Iter();
87 _LIBCPP_NO_UNIQUE_ADDRESS _Sent __end_
= _Sent();
88 _LIBCPP_NO_UNIQUE_ADDRESS _Size __size_
= 0;
91 _LIBCPP_HIDE_FROM_ABI
subrange()
92 requires default_initializable
<_Iter
>
95 _LIBCPP_HIDE_FROM_ABI
constexpr subrange(__convertible_to_non_slicing
<_Iter
> auto __iter
, _Sent __sent
)
96 requires _MustProvideSizeAtConstruction
97 : __begin_(std::move(__iter
)), __end_(std::move(__sent
)) {}
99 _LIBCPP_HIDE_FROM_ABI
constexpr subrange(
100 __convertible_to_non_slicing
<_Iter
> auto __iter
, _Sent __sent
, make_unsigned_t
<iter_difference_t
<_Iter
>> __n
)
101 requires(_Kind
== subrange_kind::sized
)
102 : __begin_(std::move(__iter
)), __end_(std::move(__sent
)), __size_(__n
) {
103 if constexpr (sized_sentinel_for
<_Sent
, _Iter
>)
104 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS((__end_
- __begin_
) == static_cast<iter_difference_t
<_Iter
>>(__n
),
105 "std::ranges::subrange was passed an invalid size hint");
108 template <__different_from
<subrange
> _Range
>
109 requires borrowed_range
<_Range
> && __convertible_to_non_slicing
<iterator_t
<_Range
>, _Iter
> &&
110 convertible_to
<sentinel_t
<_Range
>, _Sent
>
111 _LIBCPP_HIDE_FROM_ABI
constexpr subrange(_Range
&& __range
)
112 requires(!_StoreSize
)
113 : subrange(ranges::begin(__range
), ranges::end(__range
)) {}
115 template <__different_from
<subrange
> _Range
>
116 requires borrowed_range
<_Range
> && __convertible_to_non_slicing
<iterator_t
<_Range
>, _Iter
> &&
117 convertible_to
<sentinel_t
<_Range
>, _Sent
>
118 _LIBCPP_HIDE_FROM_ABI
constexpr subrange(_Range
&& __range
)
119 requires _StoreSize
&& sized_range
<_Range
>
120 : subrange(__range
, ranges::size(__range
)) {}
122 template <borrowed_range _Range
>
123 requires __convertible_to_non_slicing
<iterator_t
<_Range
>, _Iter
> &&
124 convertible_to
<sentinel_t
<_Range
>, _Sent
>
125 _LIBCPP_HIDE_FROM_ABI
constexpr subrange(_Range
&& __range
, make_unsigned_t
<iter_difference_t
<_Iter
>> __n
)
126 requires(_Kind
== subrange_kind::sized
)
127 : subrange(ranges::begin(__range
), ranges::end(__range
), __n
) {}
129 template <__pair_like_convertible_from
<const _Iter
&, const _Sent
&> _Pair
>
130 _LIBCPP_HIDE_FROM_ABI
constexpr operator _Pair() const {
131 return _Pair(__begin_
, __end_
);
134 _LIBCPP_HIDE_FROM_ABI
constexpr _Iter
begin() const
135 requires copyable
<_Iter
>
140 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr _Iter
begin()
141 requires(!copyable
<_Iter
>)
143 return std::move(__begin_
);
146 _LIBCPP_HIDE_FROM_ABI
constexpr _Sent
end() const { return __end_
; }
148 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr bool empty() const { return __begin_
== __end_
; }
150 _LIBCPP_HIDE_FROM_ABI
constexpr make_unsigned_t
<iter_difference_t
<_Iter
>> size() const
151 requires(_Kind
== subrange_kind::sized
)
153 if constexpr (_StoreSize
)
156 return std::__to_unsigned_like(__end_
- __begin_
);
159 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr subrange
next(iter_difference_t
<_Iter
> __n
= 1) const&
160 requires forward_iterator
<_Iter
>
167 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr subrange
next(iter_difference_t
<_Iter
> __n
= 1) && {
169 return std::move(*this);
172 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr subrange
prev(iter_difference_t
<_Iter
> __n
= 1) const
173 requires bidirectional_iterator
<_Iter
>
180 _LIBCPP_HIDE_FROM_ABI
constexpr subrange
& advance(iter_difference_t
<_Iter
> __n
) {
181 if constexpr (bidirectional_iterator
<_Iter
>) {
183 ranges::advance(__begin_
, __n
);
184 if constexpr (_StoreSize
)
185 __size_
+= std::__to_unsigned_like(-__n
);
190 auto __d
= __n
- ranges::advance(__begin_
, __n
, __end_
);
191 if constexpr (_StoreSize
)
192 __size_
-= std::__to_unsigned_like(__d
);
197 template <input_or_output_iterator _Iter
, sentinel_for
<_Iter
> _Sent
>
198 subrange(_Iter
, _Sent
) -> subrange
<_Iter
, _Sent
>;
200 template <input_or_output_iterator _Iter
, sentinel_for
<_Iter
> _Sent
>
201 subrange(_Iter
, _Sent
, make_unsigned_t
<iter_difference_t
<_Iter
>>) -> subrange
<_Iter
, _Sent
, subrange_kind::sized
>;
203 template <borrowed_range _Range
>
204 subrange(_Range
&&) -> subrange
<iterator_t
<_Range
>,
206 (sized_range
<_Range
> || sized_sentinel_for
<sentinel_t
<_Range
>, iterator_t
<_Range
>>)
207 ? subrange_kind::sized
208 : subrange_kind::unsized
>;
210 template <borrowed_range _Range
>
211 subrange(_Range
&&, make_unsigned_t
<range_difference_t
<_Range
>>)
212 -> subrange
<iterator_t
<_Range
>, sentinel_t
<_Range
>, subrange_kind::sized
>;
214 template <size_t _Index
, class _Iter
, class _Sent
, subrange_kind _Kind
>
215 requires((_Index
== 0 && copyable
<_Iter
>) || _Index
== 1)
216 _LIBCPP_HIDE_FROM_ABI
constexpr auto get(const subrange
<_Iter
, _Sent
, _Kind
>& __subrange
) {
217 if constexpr (_Index
== 0)
218 return __subrange
.begin();
220 return __subrange
.end();
223 template <size_t _Index
, class _Iter
, class _Sent
, subrange_kind _Kind
>
225 _LIBCPP_HIDE_FROM_ABI
constexpr auto get(subrange
<_Iter
, _Sent
, _Kind
>&& __subrange
) {
226 if constexpr (_Index
== 0)
227 return __subrange
.begin();
229 return __subrange
.end();
232 template <class _Ip
, class _Sp
, subrange_kind _Kp
>
233 inline constexpr bool enable_borrowed_range
<subrange
<_Ip
, _Sp
, _Kp
>> = true;
236 using borrowed_subrange_t
= _If
<borrowed_range
<_Rp
>, subrange
<iterator_t
<_Rp
>>, dangling
>;
237 } // namespace ranges
239 // [range.subrange.general]
245 template <class _Ip
, class _Sp
, ranges::subrange_kind _Kp
>
246 struct tuple_size
<ranges::subrange
<_Ip
, _Sp
, _Kp
>> : integral_constant
<size_t, 2> {};
248 template <class _Ip
, class _Sp
, ranges::subrange_kind _Kp
>
249 struct tuple_element
<0, ranges::subrange
<_Ip
, _Sp
, _Kp
>> {
253 template <class _Ip
, class _Sp
, ranges::subrange_kind _Kp
>
254 struct tuple_element
<1, ranges::subrange
<_Ip
, _Sp
, _Kp
>> {
258 template <class _Ip
, class _Sp
, ranges::subrange_kind _Kp
>
259 struct tuple_element
<0, const ranges::subrange
<_Ip
, _Sp
, _Kp
>> {
263 template <class _Ip
, class _Sp
, ranges::subrange_kind _Kp
>
264 struct tuple_element
<1, const ranges::subrange
<_Ip
, _Sp
, _Kp
>> {
268 #endif // _LIBCPP_STD_VER >= 20
270 _LIBCPP_END_NAMESPACE_STD
274 #endif // _LIBCPP___RANGES_SUBRANGE_H