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