Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:03

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