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 <__utility/forward.h>
28 #include <__utility/move.h>
29 #include <type_traits>
30 
31 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32 #  pragma GCC system_header
33 #endif
34 
35 _LIBCPP_BEGIN_NAMESPACE_STD
36 
37 #if _LIBCPP_STD_VER > 17
38 
39 namespace ranges {
40   template<view _View>
41     requires bidirectional_range<_View>
42   class reverse_view : public view_interface<reverse_view<_View>> {
43     // We cache begin() whenever ranges::next is not guaranteed O(1) to provide an
44     // amortized O(1) begin() method.
45     static constexpr bool _UseCache = !random_access_range<_View> && !common_range<_View>;
46     using _Cache = _If<_UseCache, __non_propagating_cache<reverse_iterator<iterator_t<_View>>>, __empty_cache>;
47     _LIBCPP_NO_UNIQUE_ADDRESS _Cache __cached_begin_ = _Cache();
48     _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
49 
50   public:
51     _LIBCPP_HIDE_FROM_ABI
52     reverse_view() requires default_initializable<_View> = default;
53 
54     _LIBCPP_HIDE_FROM_ABI
reverse_view(_View __view)55     constexpr explicit reverse_view(_View __view) : __base_(std::move(__view)) {}
56 
57     _LIBCPP_HIDE_FROM_ABI
base()58     constexpr _View base() const& requires copy_constructible<_View> { return __base_; }
59 
60     _LIBCPP_HIDE_FROM_ABI
base()61     constexpr _View base() && { return std::move(__base_); }
62 
63     _LIBCPP_HIDE_FROM_ABI
begin()64     constexpr reverse_iterator<iterator_t<_View>> begin() {
65       if constexpr (_UseCache)
66         if (__cached_begin_.__has_value())
67           return *__cached_begin_;
68 
69       auto __tmp = std::make_reverse_iterator(ranges::next(ranges::begin(__base_), ranges::end(__base_)));
70       if constexpr (_UseCache)
71         __cached_begin_.__emplace(__tmp);
72       return __tmp;
73     }
74 
75     _LIBCPP_HIDE_FROM_ABI
begin()76     constexpr reverse_iterator<iterator_t<_View>> begin() requires common_range<_View> {
77       return std::make_reverse_iterator(ranges::end(__base_));
78     }
79 
80     _LIBCPP_HIDE_FROM_ABI
begin()81     constexpr auto begin() const requires common_range<const _View> {
82       return std::make_reverse_iterator(ranges::end(__base_));
83     }
84 
85     _LIBCPP_HIDE_FROM_ABI
end()86     constexpr reverse_iterator<iterator_t<_View>> end() {
87       return std::make_reverse_iterator(ranges::begin(__base_));
88     }
89 
90     _LIBCPP_HIDE_FROM_ABI
end()91     constexpr auto end() const requires common_range<const _View> {
92       return std::make_reverse_iterator(ranges::begin(__base_));
93     }
94 
95     _LIBCPP_HIDE_FROM_ABI
size()96     constexpr auto size() requires sized_range<_View> {
97       return ranges::size(__base_);
98     }
99 
100     _LIBCPP_HIDE_FROM_ABI
size()101     constexpr auto size() const requires sized_range<const _View> {
102       return ranges::size(__base_);
103     }
104   };
105 
106   template<class _Range>
107   reverse_view(_Range&&) -> reverse_view<views::all_t<_Range>>;
108 
109   template<class _Tp>
110   inline constexpr bool enable_borrowed_range<reverse_view<_Tp>> = enable_borrowed_range<_Tp>;
111 
112   namespace views {
113   namespace __reverse {
114     template<class _Tp>
115     inline constexpr bool __is_reverse_view = false;
116 
117     template<class _Tp>
118     inline constexpr bool __is_reverse_view<reverse_view<_Tp>> = true;
119 
120     template<class _Tp>
121     inline constexpr bool __is_sized_reverse_subrange = false;
122 
123     template<class _Iter>
124     inline constexpr bool __is_sized_reverse_subrange<subrange<reverse_iterator<_Iter>, reverse_iterator<_Iter>, subrange_kind::sized>> = true;
125 
126     template<class _Tp>
127     inline constexpr bool __is_unsized_reverse_subrange = false;
128 
129     template<class _Iter, subrange_kind _Kind>
130     inline constexpr bool __is_unsized_reverse_subrange<subrange<reverse_iterator<_Iter>, reverse_iterator<_Iter>, _Kind>> = _Kind == subrange_kind::unsized;
131 
132     template<class _Tp>
133     struct __unwrapped_reverse_subrange {
134       using type = void; // avoid SFINAE-ing out the overload below -- let the concept requirements do it for better diagnostics
135     };
136 
137     template<class _Iter, subrange_kind _Kind>
138     struct __unwrapped_reverse_subrange<subrange<reverse_iterator<_Iter>, reverse_iterator<_Iter>, _Kind>> {
139       using type = subrange<_Iter, _Iter, _Kind>;
140     };
141 
142     struct __fn : __range_adaptor_closure<__fn> {
143       template<class _Range>
144         requires __is_reverse_view<remove_cvref_t<_Range>>
145       [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
146       constexpr auto operator()(_Range&& __range) const
147         noexcept(noexcept(std::forward<_Range>(__range).base()))
148         -> decltype(      std::forward<_Range>(__range).base())
149         { return          std::forward<_Range>(__range).base(); }
150 
151       template<class _Range,
152                class _UnwrappedSubrange = typename __unwrapped_reverse_subrange<remove_cvref_t<_Range>>::type>
153         requires __is_sized_reverse_subrange<remove_cvref_t<_Range>>
154       [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
155       constexpr auto operator()(_Range&& __range) const
156         noexcept(noexcept(_UnwrappedSubrange(__range.end().base(), __range.begin().base(), __range.size())))
157         -> decltype(      _UnwrappedSubrange(__range.end().base(), __range.begin().base(), __range.size()))
158         { return          _UnwrappedSubrange(__range.end().base(), __range.begin().base(), __range.size()); }
159 
160       template<class _Range,
161                class _UnwrappedSubrange = typename __unwrapped_reverse_subrange<remove_cvref_t<_Range>>::type>
162         requires __is_unsized_reverse_subrange<remove_cvref_t<_Range>>
163       [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
164       constexpr auto operator()(_Range&& __range) const
165         noexcept(noexcept(_UnwrappedSubrange(__range.end().base(), __range.begin().base())))
166         -> decltype(      _UnwrappedSubrange(__range.end().base(), __range.begin().base()))
167         { return          _UnwrappedSubrange(__range.end().base(), __range.begin().base()); }
168 
169       template<class _Range>
170         requires (!__is_reverse_view<remove_cvref_t<_Range>> &&
171                   !__is_sized_reverse_subrange<remove_cvref_t<_Range>> &&
172                   !__is_unsized_reverse_subrange<remove_cvref_t<_Range>>)
173       [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
174       constexpr auto operator()(_Range&& __range) const
175         noexcept(noexcept(reverse_view{std::forward<_Range>(__range)}))
176         -> decltype(      reverse_view{std::forward<_Range>(__range)})
177         { return          reverse_view{std::forward<_Range>(__range)}; }
178     };
179   } // namespace __reverse
180 
181   inline namespace __cpo {
182     inline constexpr auto reverse = __reverse::__fn{};
183   } // namespace __cpo
184   } // namespace views
185 } // namespace ranges
186 
187 #endif // _LIBCPP_STD_VER > 17
188 
189 _LIBCPP_END_NAMESPACE_STD
190 
191 #endif // _LIBCPP___RANGES_REVERSE_VIEW_H
192