[lldb] Fix warning: 'sprintf' is deprecated in RNBSocketTest
[llvm-project.git] / libcxx / include / __ranges / transform_view.h
blob0ce1bfe883500f44df8205ec2c7e17df03944a33
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___RANGES_TRANSFORM_VIEW_H
11 #define _LIBCPP___RANGES_TRANSFORM_VIEW_H
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/invoke.h>
38 #include <__type_traits/is_nothrow_constructible.h>
39 #include <__type_traits/is_object.h>
40 #include <__type_traits/is_reference.h>
41 #include <__type_traits/maybe_const.h>
42 #include <__type_traits/remove_cvref.h>
43 #include <__utility/forward.h>
44 #include <__utility/in_place.h>
45 #include <__utility/move.h>
47 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
48 # pragma GCC system_header
49 #endif
51 _LIBCPP_PUSH_MACROS
52 #include <__undef_macros>
54 _LIBCPP_BEGIN_NAMESPACE_STD
56 #if _LIBCPP_STD_VER >= 20
58 namespace ranges {
60 template <class _Fn, class _View>
61 concept __regular_invocable_with_range_ref = regular_invocable<_Fn, range_reference_t<_View>>;
63 template <class _View, class _Fn>
64 concept __transform_view_constraints =
65 view<_View> && is_object_v<_Fn> && regular_invocable<_Fn&, range_reference_t<_View>> &&
66 __can_reference<invoke_result_t<_Fn&, range_reference_t<_View>>>;
68 # if _LIBCPP_STD_VER >= 23
69 template <input_range _View, move_constructible _Fn>
70 # else
71 template <input_range _View, copy_constructible _Fn>
72 # endif
73 requires __transform_view_constraints<_View, _Fn>
74 class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS transform_view : public view_interface<transform_view<_View, _Fn>> {
75 template <bool>
76 class __iterator;
77 template <bool>
78 class __sentinel;
80 _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Fn> __func_;
81 _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
83 public:
84 _LIBCPP_HIDE_FROM_ABI transform_view()
85 requires default_initializable<_View> && default_initializable<_Fn>
86 = default;
88 _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 transform_view(_View __base, _Fn __func)
89 : __func_(std::in_place, std::move(__func)), __base_(std::move(__base)) {}
91 _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
92 requires copy_constructible<_View>
94 return __base_;
96 _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
98 _LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> begin() { return __iterator<false>{*this, ranges::begin(__base_)}; }
99 _LIBCPP_HIDE_FROM_ABI constexpr __iterator<true> begin() const
100 requires range<const _View> && __regular_invocable_with_range_ref<const _Fn&, const _View>
102 return __iterator<true>(*this, ranges::begin(__base_));
105 _LIBCPP_HIDE_FROM_ABI constexpr __sentinel<false> end() { return __sentinel<false>(ranges::end(__base_)); }
106 _LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> end()
107 requires common_range<_View>
109 return __iterator<false>(*this, ranges::end(__base_));
111 _LIBCPP_HIDE_FROM_ABI constexpr __sentinel<true> end() const
112 requires range<const _View> && __regular_invocable_with_range_ref<const _Fn&, const _View>
114 return __sentinel<true>(ranges::end(__base_));
116 _LIBCPP_HIDE_FROM_ABI constexpr __iterator<true> end() const
117 requires common_range<const _View> && __regular_invocable_with_range_ref<const _Fn&, const _View>
119 return __iterator<true>(*this, ranges::end(__base_));
122 _LIBCPP_HIDE_FROM_ABI constexpr auto size()
123 requires sized_range<_View>
125 return ranges::size(__base_);
127 _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
128 requires sized_range<const _View>
130 return ranges::size(__base_);
134 template <class _Range, class _Fn>
135 transform_view(_Range&&, _Fn) -> transform_view<views::all_t<_Range>, _Fn>;
137 template <class _View>
138 struct __transform_view_iterator_concept {
139 using type = input_iterator_tag;
142 template <random_access_range _View>
143 struct __transform_view_iterator_concept<_View> {
144 using type = random_access_iterator_tag;
147 template <bidirectional_range _View>
148 struct __transform_view_iterator_concept<_View> {
149 using type = bidirectional_iterator_tag;
152 template <forward_range _View>
153 struct __transform_view_iterator_concept<_View> {
154 using type = forward_iterator_tag;
157 template <class, class>
158 struct __transform_view_iterator_category_base {};
160 template <forward_range _View, class _Fn>
161 struct __transform_view_iterator_category_base<_View, _Fn> {
162 using _Cat = typename iterator_traits<iterator_t<_View>>::iterator_category;
164 using iterator_category =
165 conditional_t< is_reference_v<invoke_result_t<_Fn&, range_reference_t<_View>>>,
166 conditional_t< derived_from<_Cat, contiguous_iterator_tag>, random_access_iterator_tag, _Cat >,
167 input_iterator_tag >;
170 # if _LIBCPP_STD_VER >= 23
171 template <input_range _View, move_constructible _Fn>
172 # else
173 template <input_range _View, copy_constructible _Fn>
174 # endif
175 requires __transform_view_constraints<_View, _Fn>
176 template <bool _Const>
177 class transform_view<_View, _Fn>::__iterator
178 : public __transform_view_iterator_category_base<_View, __maybe_const<_Const, _Fn>> {
180 using _Parent = __maybe_const<_Const, transform_view>;
181 using _Base = __maybe_const<_Const, _View>;
183 _Parent* __parent_ = nullptr;
185 template <bool>
186 friend class transform_view<_View, _Fn>::__iterator;
188 template <bool>
189 friend class transform_view<_View, _Fn>::__sentinel;
191 public:
192 iterator_t<_Base> __current_ = iterator_t<_Base>();
194 using iterator_concept = typename __transform_view_iterator_concept<_View>::type;
195 using value_type = remove_cvref_t<invoke_result_t<__maybe_const<_Const, _Fn>&, range_reference_t<_Base>>>;
196 using difference_type = range_difference_t<_Base>;
198 _LIBCPP_HIDE_FROM_ABI __iterator()
199 requires default_initializable<iterator_t<_Base>>
200 = default;
202 _LIBCPP_HIDE_FROM_ABI constexpr __iterator(_Parent& __parent, iterator_t<_Base> __current)
203 : __parent_(std::addressof(__parent)), __current_(std::move(__current)) {}
205 // Note: `__i` should always be `__iterator<false>`, but directly using
206 // `__iterator<false>` is ill-formed when `_Const` is false
207 // (see http://wg21.link/class.copy.ctor#5).
208 _LIBCPP_HIDE_FROM_ABI constexpr __iterator(__iterator<!_Const> __i)
209 requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>>
210 : __parent_(__i.__parent_), __current_(std::move(__i.__current_)) {}
212 _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_Base>& base() const& noexcept { return __current_; }
214 _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() && { return std::move(__current_); }
216 _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const
217 noexcept(noexcept(std::invoke(*__parent_->__func_, *__current_))) {
218 return std::invoke(*__parent_->__func_, *__current_);
221 _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
222 ++__current_;
223 return *this;
226 _LIBCPP_HIDE_FROM_ABI constexpr void operator++(int) { ++__current_; }
228 _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int)
229 requires forward_range<_Base>
231 auto __tmp = *this;
232 ++*this;
233 return __tmp;
236 _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--()
237 requires bidirectional_range<_Base>
239 --__current_;
240 return *this;
243 _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int)
244 requires bidirectional_range<_Base>
246 auto __tmp = *this;
247 --*this;
248 return __tmp;
251 _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator+=(difference_type __n)
252 requires random_access_range<_Base>
254 __current_ += __n;
255 return *this;
258 _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator-=(difference_type __n)
259 requires random_access_range<_Base>
261 __current_ -= __n;
262 return *this;
265 _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](difference_type __n) const
266 noexcept(noexcept(std::invoke(*__parent_->__func_, __current_[__n])))
267 requires random_access_range<_Base>
269 return std::invoke(*__parent_->__func_, __current_[__n]);
272 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y)
273 requires equality_comparable<iterator_t<_Base>>
275 return __x.__current_ == __y.__current_;
278 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const __iterator& __x, const __iterator& __y)
279 requires random_access_range<_Base>
281 return __x.__current_ < __y.__current_;
284 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const __iterator& __x, const __iterator& __y)
285 requires random_access_range<_Base>
287 return __x.__current_ > __y.__current_;
290 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y)
291 requires random_access_range<_Base>
293 return __x.__current_ <= __y.__current_;
296 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const __iterator& __x, const __iterator& __y)
297 requires random_access_range<_Base>
299 return __x.__current_ >= __y.__current_;
302 _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y)
303 requires random_access_range<_Base> && three_way_comparable<iterator_t<_Base>>
305 return __x.__current_ <=> __y.__current_;
308 _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(__iterator __i, difference_type __n)
309 requires random_access_range<_Base>
311 return __iterator{*__i.__parent_, __i.__current_ + __n};
314 _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, __iterator __i)
315 requires random_access_range<_Base>
317 return __iterator{*__i.__parent_, __i.__current_ + __n};
320 _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(__iterator __i, difference_type __n)
321 requires random_access_range<_Base>
323 return __iterator{*__i.__parent_, __i.__current_ - __n};
326 _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
327 requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
329 return __x.__current_ - __y.__current_;
333 # if _LIBCPP_STD_VER >= 23
334 template <input_range _View, move_constructible _Fn>
335 # else
336 template <input_range _View, copy_constructible _Fn>
337 # endif
338 requires __transform_view_constraints<_View, _Fn>
339 template <bool _Const>
340 class transform_view<_View, _Fn>::__sentinel {
341 using _Parent = __maybe_const<_Const, transform_view>;
342 using _Base = __maybe_const<_Const, _View>;
344 sentinel_t<_Base> __end_ = sentinel_t<_Base>();
346 template <bool>
347 friend class transform_view<_View, _Fn>::__iterator;
349 template <bool>
350 friend class transform_view<_View, _Fn>::__sentinel;
352 public:
353 _LIBCPP_HIDE_FROM_ABI __sentinel() = default;
355 _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(sentinel_t<_Base> __end) : __end_(__end) {}
357 // Note: `__i` should always be `__sentinel<false>`, but directly using
358 // `__sentinel<false>` is ill-formed when `_Const` is false
359 // (see http://wg21.link/class.copy.ctor#5).
360 _LIBCPP_HIDE_FROM_ABI constexpr __sentinel(__sentinel<!_Const> __i)
361 requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>
362 : __end_(std::move(__i.__end_)) {}
364 _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }
366 template <bool _OtherConst>
367 requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
368 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
369 return __x.__current_ == __y.__end_;
372 template <bool _OtherConst>
373 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
374 _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
375 operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
376 return __x.__current_ - __y.__end_;
379 template <bool _OtherConst>
380 requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
381 _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
382 operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) {
383 return __x.__end_ - __y.__current_;
387 namespace views {
388 namespace __transform {
389 struct __fn {
390 template <class _Range, class _Fn>
391 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Fn&& __f) const
392 noexcept(noexcept(transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f))))
393 -> decltype(transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f))) {
394 return transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f));
397 template <class _Fn>
398 requires constructible_from<decay_t<_Fn>, _Fn>
399 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn&& __f) const
400 noexcept(is_nothrow_constructible_v<decay_t<_Fn>, _Fn>) {
401 return __pipeable(std::__bind_back(*this, std::forward<_Fn>(__f)));
404 } // namespace __transform
406 inline namespace __cpo {
407 inline constexpr auto transform = __transform::__fn{};
408 } // namespace __cpo
409 } // namespace views
411 } // namespace ranges
413 #endif // _LIBCPP_STD_VER >= 20
415 _LIBCPP_END_NAMESPACE_STD
417 _LIBCPP_POP_MACROS
419 #endif // _LIBCPP___RANGES_TRANSFORM_VIEW_H