Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:42

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___RANGES_TRANSFORM_VIEW_H
0011 #define _LIBCPP___CXX03___RANGES_TRANSFORM_VIEW_H
0012 
0013 #include <__cxx03/__compare/three_way_comparable.h>
0014 #include <__cxx03/__concepts/constructible.h>
0015 #include <__cxx03/__concepts/convertible_to.h>
0016 #include <__cxx03/__concepts/copyable.h>
0017 #include <__cxx03/__concepts/derived_from.h>
0018 #include <__cxx03/__concepts/equality_comparable.h>
0019 #include <__cxx03/__concepts/invocable.h>
0020 #include <__cxx03/__config>
0021 #include <__cxx03/__functional/bind_back.h>
0022 #include <__cxx03/__functional/invoke.h>
0023 #include <__cxx03/__functional/perfect_forward.h>
0024 #include <__cxx03/__iterator/concepts.h>
0025 #include <__cxx03/__iterator/iterator_traits.h>
0026 #include <__cxx03/__memory/addressof.h>
0027 #include <__cxx03/__ranges/access.h>
0028 #include <__cxx03/__ranges/all.h>
0029 #include <__cxx03/__ranges/concepts.h>
0030 #include <__cxx03/__ranges/empty.h>
0031 #include <__cxx03/__ranges/movable_box.h>
0032 #include <__cxx03/__ranges/range_adaptor.h>
0033 #include <__cxx03/__ranges/size.h>
0034 #include <__cxx03/__ranges/view_interface.h>
0035 #include <__cxx03/__type_traits/conditional.h>
0036 #include <__cxx03/__type_traits/decay.h>
0037 #include <__cxx03/__type_traits/is_nothrow_constructible.h>
0038 #include <__cxx03/__type_traits/is_object.h>
0039 #include <__cxx03/__type_traits/is_reference.h>
0040 #include <__cxx03/__type_traits/maybe_const.h>
0041 #include <__cxx03/__type_traits/remove_cvref.h>
0042 #include <__cxx03/__utility/forward.h>
0043 #include <__cxx03/__utility/in_place.h>
0044 #include <__cxx03/__utility/move.h>
0045 
0046 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0047 #  pragma GCC system_header
0048 #endif
0049 
0050 _LIBCPP_PUSH_MACROS
0051 #include <__cxx03/__undef_macros>
0052 
0053 _LIBCPP_BEGIN_NAMESPACE_STD
0054 
0055 #if _LIBCPP_STD_VER >= 20
0056 
0057 namespace ranges {
0058 
0059 template <class _Fn, class _View>
0060 concept __regular_invocable_with_range_ref = regular_invocable<_Fn, range_reference_t<_View>>;
0061 
0062 template <class _View, class _Fn>
0063 concept __transform_view_constraints =
0064     view<_View> && is_object_v<_Fn> && regular_invocable<_Fn&, range_reference_t<_View>> &&
0065     __can_reference<invoke_result_t<_Fn&, range_reference_t<_View>>>;
0066 
0067 #  if _LIBCPP_STD_VER >= 23
0068 template <input_range _View, move_constructible _Fn>
0069 #  else
0070 template <input_range _View, copy_constructible _Fn>
0071 #  endif
0072   requires __transform_view_constraints<_View, _Fn>
0073 class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS transform_view : public view_interface<transform_view<_View, _Fn>> {
0074   template <bool>
0075   class __iterator;
0076   template <bool>
0077   class __sentinel;
0078 
0079   _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Fn> __func_;
0080   _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
0081 
0082 public:
0083   _LIBCPP_HIDE_FROM_ABI transform_view()
0084     requires default_initializable<_View> && default_initializable<_Fn>
0085   = default;
0086 
0087   _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 transform_view(_View __base, _Fn __func)
0088       : __func_(std::in_place, std::move(__func)), __base_(std::move(__base)) {}
0089 
0090   _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
0091     requires copy_constructible<_View>
0092   {
0093     return __base_;
0094   }
0095   _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
0096 
0097   _LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> begin() { return __iterator<false>{*this, ranges::begin(__base_)}; }
0098   _LIBCPP_HIDE_FROM_ABI constexpr __iterator<true> begin() const
0099     requires range<const _View> && __regular_invocable_with_range_ref<const _Fn&, const _View>
0100   {
0101     return __iterator<true>(*this, ranges::begin(__base_));
0102   }
0103 
0104   _LIBCPP_HIDE_FROM_ABI constexpr __sentinel<false> end() { return __sentinel<false>(ranges::end(__base_)); }
0105   _LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> end()
0106     requires common_range<_View>
0107   {
0108     return __iterator<false>(*this, ranges::end(__base_));
0109   }
0110   _LIBCPP_HIDE_FROM_ABI constexpr __sentinel<true> end() const
0111     requires range<const _View> && __regular_invocable_with_range_ref<const _Fn&, const _View>
0112   {
0113     return __sentinel<true>(ranges::end(__base_));
0114   }
0115   _LIBCPP_HIDE_FROM_ABI constexpr __iterator<true> end() const
0116     requires common_range<const _View> && __regular_invocable_with_range_ref<const _Fn&, const _View>
0117   {
0118     return __iterator<true>(*this, ranges::end(__base_));
0119   }
0120 
0121   _LIBCPP_HIDE_FROM_ABI constexpr auto size()
0122     requires sized_range<_View>
0123   {
0124     return ranges::size(__base_);
0125   }
0126   _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
0127     requires sized_range<const _View>
0128   {
0129     return ranges::size(__base_);
0130   }
0131 };
0132 
0133 template <class _Range, class _Fn>
0134 transform_view(_Range&&, _Fn) -> transform_view<views::all_t<_Range>, _Fn>;
0135 
0136 template <class _View>
0137 struct __transform_view_iterator_concept {
0138   using type = input_iterator_tag;
0139 };
0140 
0141 template <random_access_range _View>
0142 struct __transform_view_iterator_concept<_View> {
0143   using type = random_access_iterator_tag;
0144 };
0145 
0146 template <bidirectional_range _View>
0147 struct __transform_view_iterator_concept<_View> {
0148   using type = bidirectional_iterator_tag;
0149 };
0150 
0151 template <forward_range _View>
0152 struct __transform_view_iterator_concept<_View> {
0153   using type = forward_iterator_tag;
0154 };
0155 
0156 template <class, class>
0157 struct __transform_view_iterator_category_base {};
0158 
0159 template <forward_range _View, class _Fn>
0160 struct __transform_view_iterator_category_base<_View, _Fn> {
0161   using _Cat = typename iterator_traits<iterator_t<_View>>::iterator_category;
0162 
0163   using iterator_category =
0164       conditional_t< is_reference_v<invoke_result_t<_Fn&, range_reference_t<_View>>>,
0165                      conditional_t< derived_from<_Cat, contiguous_iterator_tag>, random_access_iterator_tag, _Cat >,
0166                      input_iterator_tag >;
0167 };
0168 
0169 #  if _LIBCPP_STD_VER >= 23
0170 template <input_range _View, move_constructible _Fn>
0171 #  else
0172 template <input_range _View, copy_constructible _Fn>
0173 #  endif
0174   requires __transform_view_constraints<_View, _Fn>
0175 template <bool _Const>
0176 class transform_view<_View, _Fn>::__iterator : public __transform_view_iterator_category_base<_View, _Fn> {
0177 
0178   using _Parent = __maybe_const<_Const, transform_view>;
0179   using _Base   = __maybe_const<_Const, _View>;
0180 
0181   _Parent* __parent_ = nullptr;
0182 
0183   template <bool>
0184   friend class transform_view<_View, _Fn>::__iterator;
0185 
0186   template <bool>
0187   friend class transform_view<_View, _Fn>::__sentinel;
0188 
0189 public:
0190   iterator_t<_Base> __current_ = iterator_t<_Base>();
0191 
0192   using iterator_concept = typename __transform_view_iterator_concept<_View>::type;
0193   using value_type       = remove_cvref_t<invoke_result_t<_Fn&, range_reference_t<_Base>>>;
0194   using difference_type  = range_difference_t<_Base>;
0195 
0196   _LIBCPP_HIDE_FROM_ABI __iterator()
0197     requires default_initializable<iterator_t<_Base>>
0198   = default;
0199 
0200   _LIBCPP_HIDE_FROM_ABI constexpr __iterator(_Parent& __parent, iterator_t<_Base> __current)
0201       : __parent_(std::addressof(__parent)), __current_(std::move(__current)) {}
0202 
0203   // Note: `__i` should always be `__iterator<false>`, but directly using
0204   // `__iterator<false>` is ill-formed when `_Const` is false
0205   // (see http://wg21.link/class.copy.ctor#5).
0206   _LIBCPP_HIDE_FROM_ABI constexpr __iterator(__iterator<!_Const> __i)
0207     requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>>
0208       : __parent_(__i.__parent_), __current_(std::move(__i.__current_)) {}
0209 
0210   _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_Base>& base() const& noexcept { return __current_; }
0211 
0212   _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() && { return std::move(__current_); }
0213 
0214   _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const
0215       noexcept(noexcept(std::invoke(*__parent_->__func_, *__current_))) {
0216     return std::invoke(*__parent_->__func_, *__current_);
0217   }
0218 
0219   _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
0220     ++__current_;
0221     return *this;
0222   }
0223 
0224   _LIBCPP_HIDE_FROM_ABI constexpr void operator++(int) { ++__current_; }
0225 
0226   _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int)
0227     requires forward_range<_Base>
0228   {
0229     auto __tmp = *this;
0230     ++*this;
0231     return __tmp;
0232   }
0233 
0234   _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--()
0235     requires bidirectional_range<_Base>
0236   {
0237     --__current_;
0238     return *this;
0239   }
0240 
0241   _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int)
0242     requires bidirectional_range<_Base>
0243   {
0244     auto __tmp = *this;
0245     --*this;
0246     return __tmp;
0247   }
0248 
0249   _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator+=(difference_type __n)
0250     requires random_access_range<_Base>
0251   {
0252     __current_ += __n;
0253     return *this;
0254   }
0255 
0256   _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator-=(difference_type __n)
0257     requires random_access_range<_Base>
0258   {
0259     __current_ -= __n;
0260     return *this;
0261   }
0262 
0263   _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](difference_type __n) const
0264       noexcept(noexcept(std::invoke(*__parent_->__func_, __current_[__n])))
0265     requires random_access_range<_Base>
0266   {
0267     return std::invoke(*__parent_->__func_, __current_[__n]);
0268   }
0269 
0270   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y)
0271     requires equality_comparable<iterator_t<_Base>>
0272   {
0273     return __x.__current_ == __y.__current_;
0274   }
0275 
0276   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const __iterator& __x, const __iterator& __y)
0277     requires random_access_range<_Base>
0278   {
0279     return __x.__current_ < __y.__current_;
0280   }
0281 
0282   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const __iterator& __x, const __iterator& __y)
0283     requires random_access_range<_Base>
0284   {
0285     return __x.__current_ > __y.__current_;
0286   }
0287 
0288   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y)
0289     requires random_access_range<_Base>
0290   {
0291     return __x.__current_ <= __y.__current_;
0292   }
0293 
0294   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const __iterator& __x, const __iterator& __y)
0295     requires random_access_range<_Base>
0296   {
0297     return __x.__current_ >= __y.__current_;
0298   }
0299 
0300   _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y)
0301     requires random_access_range<_Base> && three_way_comparable<iterator_t<_Base>>
0302   {
0303     return __x.__current_ <=> __y.__current_;
0304   }
0305 
0306   _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(__iterator __i, difference_type __n)
0307     requires random_access_range<_Base>
0308   {
0309     return __iterator{*__i.__parent_, __i.__current_ + __n};
0310   }
0311 
0312   _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, __iterator __i)
0313     requires random_access_range<_Base>
0314   {
0315     return __iterator{*__i.__parent_, __i.__current_ + __n};
0316   }
0317 
0318   _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(__iterator __i, difference_type __n)
0319     requires random_access_range<_Base>
0320   {
0321     return __iterator{*__i.__parent_, __i.__current_ - __n};
0322   }
0323 
0324   _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
0325     requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
0326   {
0327     return __x.__current_ - __y.__current_;
0328   }
0329 };
0330 
0331 #  if _LIBCPP_STD_VER >= 23
0332 template <input_range _View, move_constructible _Fn>
0333 #  else
0334 template <input_range _View, copy_constructible _Fn>
0335 #  endif
0336   requires __transform_view_constraints<_View, _Fn>
0337 template <bool _Const>
0338 class transform_view<_View, _Fn>::__sentinel {
0339   using _Parent = __maybe_const<_Const, transform_view>;
0340   using _Base   = __maybe_const<_Const, _View>;
0341 
0342   sentinel_t<_Base> __end_ = sentinel_t<_Base>();
0343 
0344   template <bool>
0345   friend class transform_view<_View, _Fn>::__iterator;
0346 
0347   template <bool>
0348   friend class transform_view<_View, _Fn>::__sentinel;
0349 
0350 public:
0351   _LIBCPP_HIDE_FROM_ABI __sentinel() = default;
0352 
0353   _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(sentinel_t<_Base> __end) : __end_(__end) {}
0354 
0355   // Note: `__i` should always be `__sentinel<false>`, but directly using
0356   // `__sentinel<false>` is ill-formed when `_Const` is false
0357   // (see http://wg21.link/class.copy.ctor#5).
0358   _LIBCPP_HIDE_FROM_ABI constexpr __sentinel(__sentinel<!_Const> __i)
0359     requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>
0360       : __end_(std::move(__i.__end_)) {}
0361 
0362   _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }
0363 
0364   template <bool _OtherConst>
0365     requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
0366   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
0367     return __x.__current_ == __y.__end_;
0368   }
0369 
0370   template <bool _OtherConst>
0371     requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
0372   _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
0373   operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
0374     return __x.__current_ - __y.__end_;
0375   }
0376 
0377   template <bool _OtherConst>
0378     requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
0379   _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
0380   operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) {
0381     return __x.__end_ - __y.__current_;
0382   }
0383 };
0384 
0385 namespace views {
0386 namespace __transform {
0387 struct __fn {
0388   template <class _Range, class _Fn>
0389   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Fn&& __f) const
0390       noexcept(noexcept(transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f))))
0391           -> decltype(transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f))) {
0392     return transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f));
0393   }
0394 
0395   template <class _Fn>
0396     requires constructible_from<decay_t<_Fn>, _Fn>
0397   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn&& __f) const
0398       noexcept(is_nothrow_constructible_v<decay_t<_Fn>, _Fn>) {
0399     return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Fn>(__f)));
0400   }
0401 };
0402 } // namespace __transform
0403 
0404 inline namespace __cpo {
0405 inline constexpr auto transform = __transform::__fn{};
0406 } // namespace __cpo
0407 } // namespace views
0408 
0409 } // namespace ranges
0410 
0411 #endif // _LIBCPP_STD_VER >= 20
0412 
0413 _LIBCPP_END_NAMESPACE_STD
0414 
0415 _LIBCPP_POP_MACROS
0416 
0417 #endif // _LIBCPP___CXX03___RANGES_TRANSFORM_VIEW_H