• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_REVERSE_VIEW_H
11 #define _LIBCPP___RANGES_REVERSE_VIEW_H
12 
13 #include <__concepts/constructible.h>
14 #include <__config>
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>
31 
32 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
33 #  pragma GCC system_header
34 #endif
35 
36 _LIBCPP_BEGIN_NAMESPACE_STD
37 
38 #if _LIBCPP_STD_VER >= 20
39 
40 namespace ranges {
41   template<view _View>
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();
50 
51   public:
52     _LIBCPP_HIDE_FROM_ABI
53     reverse_view() requires default_initializable<_View> = default;
54 
55     _LIBCPP_HIDE_FROM_ABI
reverse_view(_View __view)56     constexpr explicit reverse_view(_View __view) : __base_(std::move(__view)) {}
57 
58     _LIBCPP_HIDE_FROM_ABI
base()59     constexpr _View base() const& requires copy_constructible<_View> { return __base_; }
60 
61     _LIBCPP_HIDE_FROM_ABI
base()62     constexpr _View base() && { return std::move(__base_); }
63 
64     _LIBCPP_HIDE_FROM_ABI
begin()65     constexpr reverse_iterator<iterator_t<_View>> begin() {
66       if constexpr (_UseCache)
67         if (__cached_begin_.__has_value())
68           return *__cached_begin_;
69 
70       auto __tmp = std::make_reverse_iterator(ranges::next(ranges::begin(__base_), ranges::end(__base_)));
71       if constexpr (_UseCache)
72         __cached_begin_.__emplace(__tmp);
73       return __tmp;
74     }
75 
76     _LIBCPP_HIDE_FROM_ABI
begin()77     constexpr reverse_iterator<iterator_t<_View>> begin() requires common_range<_View> {
78       return std::make_reverse_iterator(ranges::end(__base_));
79     }
80 
81     _LIBCPP_HIDE_FROM_ABI
begin()82     constexpr auto begin() const requires common_range<const _View> {
83       return std::make_reverse_iterator(ranges::end(__base_));
84     }
85 
86     _LIBCPP_HIDE_FROM_ABI
end()87     constexpr reverse_iterator<iterator_t<_View>> end() {
88       return std::make_reverse_iterator(ranges::begin(__base_));
89     }
90 
91     _LIBCPP_HIDE_FROM_ABI
end()92     constexpr auto end() const requires common_range<const _View> {
93       return std::make_reverse_iterator(ranges::begin(__base_));
94     }
95 
96     _LIBCPP_HIDE_FROM_ABI
size()97     constexpr auto size() requires sized_range<_View> {
98       return ranges::size(__base_);
99     }
100 
101     _LIBCPP_HIDE_FROM_ABI
size()102     constexpr auto size() const requires sized_range<const _View> {
103       return ranges::size(__base_);
104     }
105   };
106 
107   template<class _Range>
108   reverse_view(_Range&&) -> reverse_view<views::all_t<_Range>>;
109 
110   template<class _Tp>
111   inline constexpr bool enable_borrowed_range<reverse_view<_Tp>> = enable_borrowed_range<_Tp>;
112 
113   namespace views {
114   namespace __reverse {
115     template<class _Tp>
116     inline constexpr bool __is_reverse_view = false;
117 
118     template<class _Tp>
119     inline constexpr bool __is_reverse_view<reverse_view<_Tp>> = true;
120 
121     template<class _Tp>
122     inline constexpr bool __is_sized_reverse_subrange = false;
123 
124     template<class _Iter>
125     inline constexpr bool __is_sized_reverse_subrange<subrange<reverse_iterator<_Iter>, reverse_iterator<_Iter>, subrange_kind::sized>> = true;
126 
127     template<class _Tp>
128     inline constexpr bool __is_unsized_reverse_subrange = false;
129 
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;
132 
133     template<class _Tp>
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
136     };
137 
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>;
141     };
142 
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(); }
151 
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()); }
160 
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()); }
169 
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)}; }
179     };
180   } // namespace __reverse
181 
182   inline namespace __cpo {
183     inline constexpr auto reverse = __reverse::__fn{};
184   } // namespace __cpo
185   } // namespace views
186 } // namespace ranges
187 
188 #endif // _LIBCPP_STD_VER >= 20
189 
190 _LIBCPP_END_NAMESPACE_STD
191 
192 #endif // _LIBCPP___RANGES_REVERSE_VIEW_H
193