xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/transform_view.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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_TRANSFORM_VIEW_H
11 #define _LIBCPP___RANGES_TRANSFORM_VIEW_H
12 
13 #include <__compare/three_way_comparable.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/equality_comparable.h>
19 #include <__concepts/invocable.h>
20 #include <__config>
21 #include <__functional/bind_back.h>
22 #include <__functional/invoke.h>
23 #include <__functional/perfect_forward.h>
24 #include <__iterator/concepts.h>
25 #include <__iterator/iterator_traits.h>
26 #include <__memory/addressof.h>
27 #include <__ranges/access.h>
28 #include <__ranges/all.h>
29 #include <__ranges/concepts.h>
30 #include <__ranges/empty.h>
31 #include <__ranges/movable_box.h>
32 #include <__ranges/range_adaptor.h>
33 #include <__ranges/size.h>
34 #include <__ranges/view_interface.h>
35 #include <__type_traits/conditional.h>
36 #include <__type_traits/decay.h>
37 #include <__type_traits/is_nothrow_constructible.h>
38 #include <__type_traits/is_object.h>
39 #include <__type_traits/is_reference.h>
40 #include <__type_traits/maybe_const.h>
41 #include <__type_traits/remove_cvref.h>
42 #include <__utility/forward.h>
43 #include <__utility/in_place.h>
44 #include <__utility/move.h>
45 
46 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
47 #  pragma GCC system_header
48 #endif
49 
50 _LIBCPP_BEGIN_NAMESPACE_STD
51 
52 #if _LIBCPP_STD_VER >= 20
53 
54 namespace ranges {
55 
56 template <class _Fn, class _View>
57 concept __regular_invocable_with_range_ref = regular_invocable<_Fn, range_reference_t<_View>>;
58 
59 template <class _View, class _Fn>
60 concept __transform_view_constraints =
61     view<_View> && is_object_v<_Fn> && regular_invocable<_Fn&, range_reference_t<_View>> &&
62     __can_reference<invoke_result_t<_Fn&, range_reference_t<_View>>>;
63 
64 #  if _LIBCPP_STD_VER >= 23
65 template <input_range _View, move_constructible _Fn>
66 #  else
67 template <input_range _View, copy_constructible _Fn>
68 #  endif
69   requires __transform_view_constraints<_View, _Fn>
70 class _LIBCPP_ABI_2023_OVERLAPPING_SUBOBJECT_FIX_TAG transform_view
71     : public view_interface<transform_view<_View, _Fn>> {
72   template <bool>
73   class __iterator;
74   template <bool>
75   class __sentinel;
76 
77   _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Fn> __func_;
78   _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
79 
80 public:
81   _LIBCPP_HIDE_FROM_ABI transform_view()
82     requires default_initializable<_View> && default_initializable<_Fn>
83   = default;
84 
85   _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 transform_view(_View __base, _Fn __func)
86       : __func_(std::in_place, std::move(__func)), __base_(std::move(__base)) {}
87 
88   _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
89     requires copy_constructible<_View>
90   {
91     return __base_;
92   }
93   _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
94 
95   _LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> begin() { return __iterator<false>{*this, ranges::begin(__base_)}; }
96   _LIBCPP_HIDE_FROM_ABI constexpr __iterator<true> begin() const
97     requires range<const _View> && __regular_invocable_with_range_ref<const _Fn&, const _View>
98   {
99     return __iterator<true>(*this, ranges::begin(__base_));
100   }
101 
102   _LIBCPP_HIDE_FROM_ABI constexpr __sentinel<false> end() { return __sentinel<false>(ranges::end(__base_)); }
103   _LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> end()
104     requires common_range<_View>
105   {
106     return __iterator<false>(*this, ranges::end(__base_));
107   }
108   _LIBCPP_HIDE_FROM_ABI constexpr __sentinel<true> end() const
109     requires range<const _View> && __regular_invocable_with_range_ref<const _Fn&, const _View>
110   {
111     return __sentinel<true>(ranges::end(__base_));
112   }
113   _LIBCPP_HIDE_FROM_ABI constexpr __iterator<true> end() const
114     requires common_range<const _View> && __regular_invocable_with_range_ref<const _Fn&, const _View>
115   {
116     return __iterator<true>(*this, ranges::end(__base_));
117   }
118 
119   _LIBCPP_HIDE_FROM_ABI constexpr auto size()
120     requires sized_range<_View>
121   {
122     return ranges::size(__base_);
123   }
124   _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
125     requires sized_range<const _View>
126   {
127     return ranges::size(__base_);
128   }
129 };
130 
131 template <class _Range, class _Fn>
132 transform_view(_Range&&, _Fn) -> transform_view<views::all_t<_Range>, _Fn>;
133 
134 template <class _View>
135 struct __transform_view_iterator_concept {
136   using type = input_iterator_tag;
137 };
138 
139 template <random_access_range _View>
140 struct __transform_view_iterator_concept<_View> {
141   using type = random_access_iterator_tag;
142 };
143 
144 template <bidirectional_range _View>
145 struct __transform_view_iterator_concept<_View> {
146   using type = bidirectional_iterator_tag;
147 };
148 
149 template <forward_range _View>
150 struct __transform_view_iterator_concept<_View> {
151   using type = forward_iterator_tag;
152 };
153 
154 template <class, class>
155 struct __transform_view_iterator_category_base {};
156 
157 template <forward_range _View, class _Fn>
158 struct __transform_view_iterator_category_base<_View, _Fn> {
159   using _Cat = typename iterator_traits<iterator_t<_View>>::iterator_category;
160 
161   using iterator_category =
162       conditional_t< is_reference_v<invoke_result_t<_Fn&, range_reference_t<_View>>>,
163                      conditional_t< derived_from<_Cat, contiguous_iterator_tag>, random_access_iterator_tag, _Cat >,
164                      input_iterator_tag >;
165 };
166 
167 #  if _LIBCPP_STD_VER >= 23
168 template <input_range _View, move_constructible _Fn>
169 #  else
170 template <input_range _View, copy_constructible _Fn>
171 #  endif
172   requires __transform_view_constraints<_View, _Fn>
173 template <bool _Const>
174 class transform_view<_View, _Fn>::__iterator : public __transform_view_iterator_category_base<_View, _Fn> {
175 
176   using _Parent = __maybe_const<_Const, transform_view>;
177   using _Base   = __maybe_const<_Const, _View>;
178 
179   _Parent* __parent_ = nullptr;
180 
181   template <bool>
182   friend class transform_view<_View, _Fn>::__iterator;
183 
184   template <bool>
185   friend class transform_view<_View, _Fn>::__sentinel;
186 
187 public:
188   iterator_t<_Base> __current_ = iterator_t<_Base>();
189 
190   using iterator_concept = typename __transform_view_iterator_concept<_View>::type;
191   using value_type       = remove_cvref_t<invoke_result_t<_Fn&, range_reference_t<_Base>>>;
192   using difference_type  = range_difference_t<_Base>;
193 
194   _LIBCPP_HIDE_FROM_ABI __iterator()
195     requires default_initializable<iterator_t<_Base>>
196   = default;
197 
198   _LIBCPP_HIDE_FROM_ABI constexpr __iterator(_Parent& __parent, iterator_t<_Base> __current)
199       : __parent_(std::addressof(__parent)), __current_(std::move(__current)) {}
200 
201   // Note: `__i` should always be `__iterator<false>`, but directly using
202   // `__iterator<false>` is ill-formed when `_Const` is false
203   // (see http://wg21.link/class.copy.ctor#5).
204   _LIBCPP_HIDE_FROM_ABI constexpr __iterator(__iterator<!_Const> __i)
205     requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>>
206       : __parent_(__i.__parent_), __current_(std::move(__i.__current_)) {}
207 
208   _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_Base>& base() const& noexcept { return __current_; }
209 
210   _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() && { return std::move(__current_); }
211 
212   _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const
213       noexcept(noexcept(std::invoke(*__parent_->__func_, *__current_))) {
214     return std::invoke(*__parent_->__func_, *__current_);
215   }
216 
217   _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
218     ++__current_;
219     return *this;
220   }
221 
222   _LIBCPP_HIDE_FROM_ABI constexpr void operator++(int) { ++__current_; }
223 
224   _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int)
225     requires forward_range<_Base>
226   {
227     auto __tmp = *this;
228     ++*this;
229     return __tmp;
230   }
231 
232   _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--()
233     requires bidirectional_range<_Base>
234   {
235     --__current_;
236     return *this;
237   }
238 
239   _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int)
240     requires bidirectional_range<_Base>
241   {
242     auto __tmp = *this;
243     --*this;
244     return __tmp;
245   }
246 
247   _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator+=(difference_type __n)
248     requires random_access_range<_Base>
249   {
250     __current_ += __n;
251     return *this;
252   }
253 
254   _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator-=(difference_type __n)
255     requires random_access_range<_Base>
256   {
257     __current_ -= __n;
258     return *this;
259   }
260 
261   _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](difference_type __n) const
262       noexcept(noexcept(std::invoke(*__parent_->__func_, __current_[__n])))
263     requires random_access_range<_Base>
264   {
265     return std::invoke(*__parent_->__func_, __current_[__n]);
266   }
267 
268   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y)
269     requires equality_comparable<iterator_t<_Base>>
270   {
271     return __x.__current_ == __y.__current_;
272   }
273 
274   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const __iterator& __x, const __iterator& __y)
275     requires random_access_range<_Base>
276   {
277     return __x.__current_ < __y.__current_;
278   }
279 
280   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const __iterator& __x, const __iterator& __y)
281     requires random_access_range<_Base>
282   {
283     return __x.__current_ > __y.__current_;
284   }
285 
286   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y)
287     requires random_access_range<_Base>
288   {
289     return __x.__current_ <= __y.__current_;
290   }
291 
292   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const __iterator& __x, const __iterator& __y)
293     requires random_access_range<_Base>
294   {
295     return __x.__current_ >= __y.__current_;
296   }
297 
298   _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y)
299     requires random_access_range<_Base> && three_way_comparable<iterator_t<_Base>>
300   {
301     return __x.__current_ <=> __y.__current_;
302   }
303 
304   _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(__iterator __i, difference_type __n)
305     requires random_access_range<_Base>
306   {
307     return __iterator{*__i.__parent_, __i.__current_ + __n};
308   }
309 
310   _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, __iterator __i)
311     requires random_access_range<_Base>
312   {
313     return __iterator{*__i.__parent_, __i.__current_ + __n};
314   }
315 
316   _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(__iterator __i, difference_type __n)
317     requires random_access_range<_Base>
318   {
319     return __iterator{*__i.__parent_, __i.__current_ - __n};
320   }
321 
322   _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
323     requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
324   {
325     return __x.__current_ - __y.__current_;
326   }
327 
328   _LIBCPP_HIDE_FROM_ABI friend constexpr decltype(auto) iter_move(const __iterator& __i) noexcept(noexcept(*__i)) {
329     if constexpr (is_lvalue_reference_v<decltype(*__i)>)
330       return std::move(*__i);
331     else
332       return *__i;
333   }
334 };
335 
336 #  if _LIBCPP_STD_VER >= 23
337 template <input_range _View, move_constructible _Fn>
338 #  else
339 template <input_range _View, copy_constructible _Fn>
340 #  endif
341   requires __transform_view_constraints<_View, _Fn>
342 template <bool _Const>
343 class transform_view<_View, _Fn>::__sentinel {
344   using _Parent = __maybe_const<_Const, transform_view>;
345   using _Base   = __maybe_const<_Const, _View>;
346 
347   sentinel_t<_Base> __end_ = sentinel_t<_Base>();
348 
349   template <bool>
350   friend class transform_view<_View, _Fn>::__iterator;
351 
352   template <bool>
353   friend class transform_view<_View, _Fn>::__sentinel;
354 
355 public:
356   _LIBCPP_HIDE_FROM_ABI __sentinel() = default;
357 
358   _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(sentinel_t<_Base> __end) : __end_(__end) {}
359 
360   // Note: `__i` should always be `__sentinel<false>`, but directly using
361   // `__sentinel<false>` is ill-formed when `_Const` is false
362   // (see http://wg21.link/class.copy.ctor#5).
363   _LIBCPP_HIDE_FROM_ABI constexpr __sentinel(__sentinel<!_Const> __i)
364     requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>
365       : __end_(std::move(__i.__end_)) {}
366 
367   _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }
368 
369   template <bool _OtherConst>
370     requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
371   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
372     return __x.__current_ == __y.__end_;
373   }
374 
375   template <bool _OtherConst>
376     requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
377   _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
378   operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
379     return __x.__current_ - __y.__end_;
380   }
381 
382   template <bool _OtherConst>
383     requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
384   _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
385   operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) {
386     return __x.__end_ - __y.__current_;
387   }
388 };
389 
390 namespace views {
391 namespace __transform {
392 struct __fn {
393   template <class _Range, class _Fn>
394   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Fn&& __f) const
395       noexcept(noexcept(transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f))))
396           -> decltype(transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f))) {
397     return transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f));
398   }
399 
400   template <class _Fn>
401     requires constructible_from<decay_t<_Fn>, _Fn>
402   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn&& __f) const
403       noexcept(is_nothrow_constructible_v<decay_t<_Fn>, _Fn>) {
404     return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Fn>(__f)));
405   }
406 };
407 } // namespace __transform
408 
409 inline namespace __cpo {
410 inline constexpr auto transform = __transform::__fn{};
411 } // namespace __cpo
412 } // namespace views
413 
414 } // namespace ranges
415 
416 #endif // _LIBCPP_STD_VER >= 20
417 
418 _LIBCPP_END_NAMESPACE_STD
419 
420 #endif // _LIBCPP___RANGES_TRANSFORM_VIEW_H
421