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_REVERSE_VIEW_H
11 #define _LIBCPP___RANGES_REVERSE_VIEW_H
13 #include <__concepts/constructible.h>
15 #include <__iterator/concepts.h>
16 #include <__iterator/next.h>
17 #include <__iterator/reverse_iterator.h>
18 #include <__ranges/access.h>
19 #include <__ranges/all.h>
20 #include <__ranges/concepts.h>
21 #include <__ranges/enable_borrowed_range.h>
22 #include <__ranges/non_propagating_cache.h>
23 #include <__ranges/range_adaptor.h>
24 #include <__ranges/size.h>
25 #include <__ranges/subrange.h>
26 #include <__ranges/view_interface.h>
27 #include <__type_traits/conditional.h>
28 #include <__type_traits/remove_cvref.h>
29 #include <__utility/forward.h>
30 #include <__utility/move.h>
32 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
33 # pragma GCC system_header
36 _LIBCPP_BEGIN_NAMESPACE_STD
38 #if _LIBCPP_STD_VER >= 20
42 requires bidirectional_range
<_View
>
43 class reverse_view
: public view_interface
<reverse_view
<_View
>> {
44 // We cache begin() whenever ranges::next is not guaranteed O(1) to provide an
45 // amortized O(1) begin() method.
46 static constexpr bool _UseCache
= !random_access_range
<_View
> && !common_range
<_View
>;
47 using _Cache
= _If
<_UseCache
, __non_propagating_cache
<reverse_iterator
<iterator_t
<_View
>>>, __empty_cache
>;
48 _LIBCPP_NO_UNIQUE_ADDRESS _Cache __cached_begin_
= _Cache();
49 _LIBCPP_NO_UNIQUE_ADDRESS _View __base_
= _View();
53 reverse_view() requires default_initializable
<_View
> = default;
56 constexpr explicit reverse_view(_View __view
) : __base_(std::move(__view
)) {}
59 constexpr _View
base() const& requires copy_constructible
<_View
> { return __base_
; }
62 constexpr _View
base() && { return std::move(__base_
); }
65 constexpr reverse_iterator
<iterator_t
<_View
>> begin() {
66 if constexpr (_UseCache
)
67 if (__cached_begin_
.__has_value())
68 return *__cached_begin_
;
70 auto __tmp
= std::make_reverse_iterator(ranges::next(ranges::begin(__base_
), ranges::end(__base_
)));
71 if constexpr (_UseCache
)
72 __cached_begin_
.__emplace(__tmp
);
77 constexpr reverse_iterator
<iterator_t
<_View
>> begin() requires common_range
<_View
> {
78 return std::make_reverse_iterator(ranges::end(__base_
));
82 constexpr auto begin() const requires common_range
<const _View
> {
83 return std::make_reverse_iterator(ranges::end(__base_
));
87 constexpr reverse_iterator
<iterator_t
<_View
>> end() {
88 return std::make_reverse_iterator(ranges::begin(__base_
));
92 constexpr auto end() const requires common_range
<const _View
> {
93 return std::make_reverse_iterator(ranges::begin(__base_
));
97 constexpr auto size() requires sized_range
<_View
> {
98 return ranges::size(__base_
);
101 _LIBCPP_HIDE_FROM_ABI
102 constexpr auto size() const requires sized_range
<const _View
> {
103 return ranges::size(__base_
);
107 template<class _Range
>
108 reverse_view(_Range
&&) -> reverse_view
<views::all_t
<_Range
>>;
111 inline constexpr bool enable_borrowed_range
<reverse_view
<_Tp
>> = enable_borrowed_range
<_Tp
>;
114 namespace __reverse
{
116 inline constexpr bool __is_reverse_view
= false;
119 inline constexpr bool __is_reverse_view
<reverse_view
<_Tp
>> = true;
122 inline constexpr bool __is_sized_reverse_subrange
= false;
124 template<class _Iter
>
125 inline constexpr bool __is_sized_reverse_subrange
<subrange
<reverse_iterator
<_Iter
>, reverse_iterator
<_Iter
>, subrange_kind::sized
>> = true;
128 inline constexpr bool __is_unsized_reverse_subrange
= false;
130 template<class _Iter
, subrange_kind _Kind
>
131 inline constexpr bool __is_unsized_reverse_subrange
<subrange
<reverse_iterator
<_Iter
>, reverse_iterator
<_Iter
>, _Kind
>> = _Kind
== subrange_kind::unsized
;
134 struct __unwrapped_reverse_subrange
{
135 using type
= void; // avoid SFINAE-ing out the overload below -- let the concept requirements do it for better diagnostics
138 template<class _Iter
, subrange_kind _Kind
>
139 struct __unwrapped_reverse_subrange
<subrange
<reverse_iterator
<_Iter
>, reverse_iterator
<_Iter
>, _Kind
>> {
140 using type
= subrange
<_Iter
, _Iter
, _Kind
>;
143 struct __fn
: __range_adaptor_closure
<__fn
> {
144 template<class _Range
>
145 requires __is_reverse_view
<remove_cvref_t
<_Range
>>
146 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
147 constexpr auto operator()(_Range
&& __range
) const
148 noexcept(noexcept(std::forward
<_Range
>(__range
).base()))
149 -> decltype( std::forward
<_Range
>(__range
).base())
150 { return std::forward
<_Range
>(__range
).base(); }
152 template<class _Range
,
153 class _UnwrappedSubrange
= typename __unwrapped_reverse_subrange
<remove_cvref_t
<_Range
>>::type
>
154 requires __is_sized_reverse_subrange
<remove_cvref_t
<_Range
>>
155 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
156 constexpr auto operator()(_Range
&& __range
) const
157 noexcept(noexcept(_UnwrappedSubrange(__range
.end().base(), __range
.begin().base(), __range
.size())))
158 -> decltype( _UnwrappedSubrange(__range
.end().base(), __range
.begin().base(), __range
.size()))
159 { return _UnwrappedSubrange(__range
.end().base(), __range
.begin().base(), __range
.size()); }
161 template<class _Range
,
162 class _UnwrappedSubrange
= typename __unwrapped_reverse_subrange
<remove_cvref_t
<_Range
>>::type
>
163 requires __is_unsized_reverse_subrange
<remove_cvref_t
<_Range
>>
164 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
165 constexpr auto operator()(_Range
&& __range
) const
166 noexcept(noexcept(_UnwrappedSubrange(__range
.end().base(), __range
.begin().base())))
167 -> decltype( _UnwrappedSubrange(__range
.end().base(), __range
.begin().base()))
168 { return _UnwrappedSubrange(__range
.end().base(), __range
.begin().base()); }
170 template<class _Range
>
171 requires (!__is_reverse_view
<remove_cvref_t
<_Range
>> &&
172 !__is_sized_reverse_subrange
<remove_cvref_t
<_Range
>> &&
173 !__is_unsized_reverse_subrange
<remove_cvref_t
<_Range
>>)
174 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
175 constexpr auto operator()(_Range
&& __range
) const
176 noexcept(noexcept(reverse_view
{std::forward
<_Range
>(__range
)}))
177 -> decltype( reverse_view
{std::forward
<_Range
>(__range
)})
178 { return reverse_view
{std::forward
<_Range
>(__range
)}; }
180 } // namespace __reverse
182 inline namespace __cpo
{
183 inline constexpr auto reverse
= __reverse::__fn
{};
186 } // namespace ranges
188 #endif // _LIBCPP_STD_VER >= 20
190 _LIBCPP_END_NAMESPACE_STD
192 #endif // _LIBCPP___RANGES_REVERSE_VIEW_H