From 1199ff33083076c83a45d58ba3e55da3d6d15d10 Mon Sep 17 00:00:00 2001 From: Nikolas Klauser Date: Mon, 15 Dec 2025 13:14:24 +0100 Subject: [PATCH] [libc++][NFC] Use a lambda for the segmented iterator std::copy implementation --- libcxx/include/__algorithm/copy.h | 20 ++++---------------- libcxx/include/__algorithm/find.h | 27 +++++++++------------------ libcxx/include/__algorithm/move.h | 20 ++++---------------- 3 files changed, 17 insertions(+), 50 deletions(-) diff --git a/libcxx/include/__algorithm/copy.h b/libcxx/include/__algorithm/copy.h index 4caea922dac2d..8e3eb8e0da9a7 100644 --- a/libcxx/include/__algorithm/copy.h +++ b/libcxx/include/__algorithm/copy.h @@ -69,25 +69,13 @@ struct __copy_impl { std::move(__first), std::move(__last), std::move(__result)); } - template - struct _CopySegment { - using _Traits _LIBCPP_NODEBUG = __segmented_iterator_traits<_InIter>; - - _OutIter& __result_; - - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit _CopySegment(_OutIter& __result) - : __result_(__result) {} - - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void - operator()(typename _Traits::__local_iterator __lfirst, typename _Traits::__local_iterator __llast) { - __result_ = std::__copy(__lfirst, __llast, std::move(__result_)).second; - } - }; - template , int> = 0> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> operator()(_InIter __first, _InIter __last, _OutIter __result) const { - std::__for_each_segment(__first, __last, _CopySegment<_InIter, _OutIter>(__result)); + using __local_iterator = typename __segmented_iterator_traits<_InIter>::__local_iterator; + std::__for_each_segment(__first, __last, [&__result](__local_iterator __lfirst, __local_iterator __llast) { + __result = std::__copy(std::move(__lfirst), std::move(__llast), std::move(__result)); + }); return std::make_pair(__last, std::move(__result)); } diff --git a/libcxx/include/__algorithm/find.h b/libcxx/include/__algorithm/find.h index 0ee6d9d01a98b..852bc2da3eb7b 100644 --- a/libcxx/include/__algorithm/find.h +++ b/libcxx/include/__algorithm/find.h @@ -208,32 +208,23 @@ __find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __la // segmented iterator implementation -template -struct __find_segment; - template , int> = 0> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _SegmentedIterator __find(_SegmentedIterator __first, _SegmentedIterator __last, const _Tp& __value, _Proj& __proj) { - return std::__find_segment_if(std::move(__first), std::move(__last), __find_segment<_Tp>(__value), __proj); + using __local_iterator = typename __segmented_iterator_traits<_SegmentedIterator>::__local_iterator; + return std::__find_segment_if( + std::move(__first), + std::move(__last), + [&__value](__local_iterator __lfirst, __local_iterator __llast, _Proj& __lproj) { + return std::__rewrap_iter( + __lfirst, std::__find(std::__unwrap_iter(__lfirst), std::__unwrap_iter(__llast), __value, __lproj)); + }, + __proj); } -template -struct __find_segment { - const _Tp& __value_; - - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __find_segment(const _Tp& __value) : __value_(__value) {} - - template - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _InputIterator - operator()(_InputIterator __first, _InputIterator __last, _Proj& __proj) const { - return std::__rewrap_iter( - __first, std::__find(std::__unwrap_iter(__first), std::__unwrap_iter(__last), __value_, __proj)); - } -}; - // public API template [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator diff --git a/libcxx/include/__algorithm/move.h b/libcxx/include/__algorithm/move.h index 52bd5fb5253db..ddadfa778fc24 100644 --- a/libcxx/include/__algorithm/move.h +++ b/libcxx/include/__algorithm/move.h @@ -50,25 +50,13 @@ struct __move_impl { return std::make_pair(std::move(__first), std::move(__result)); } - template - struct _MoveSegment { - using _Traits _LIBCPP_NODEBUG = __segmented_iterator_traits<_InIter>; - - _OutIter& __result_; - - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit _MoveSegment(_OutIter& __result) - : __result_(__result) {} - - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void - operator()(typename _Traits::__local_iterator __lfirst, typename _Traits::__local_iterator __llast) { - __result_ = std::__move<_AlgPolicy>(__lfirst, __llast, std::move(__result_)).second; - } - }; - template , int> = 0> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> operator()(_InIter __first, _InIter __last, _OutIter __result) const { - std::__for_each_segment(__first, __last, _MoveSegment<_InIter, _OutIter>(__result)); + using __local_iterator = typename __segmented_iterator_traits<_InIter>::__local_iterator; + std::__for_each_segment(__first, __last, [&__result](__local_iterator __lfirst, __local_iterator __llast) { + __result = std::__move<_AlgPolicy>(__lfirst, __llast, std::move(__result)).second; + }); return std::make_pair(__last, std::move(__result)); }