Path: blob/main/contrib/llvm-project/libcxx/include/__algorithm/copy.h
35232 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___ALGORITHM_COPY_H9#define _LIBCPP___ALGORITHM_COPY_H1011#include <__algorithm/copy_move_common.h>12#include <__algorithm/for_each_segment.h>13#include <__algorithm/iterator_operations.h>14#include <__algorithm/min.h>15#include <__config>16#include <__iterator/segmented_iterator.h>17#include <__type_traits/common_type.h>18#include <__utility/move.h>19#include <__utility/pair.h>2021#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)22# pragma GCC system_header23#endif2425_LIBCPP_PUSH_MACROS26#include <__undef_macros>2728_LIBCPP_BEGIN_NAMESPACE_STD2930template <class, class _InIter, class _Sent, class _OutIter>31inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> __copy(_InIter, _Sent, _OutIter);3233template <class _AlgPolicy>34struct __copy_impl {35template <class _InIter, class _Sent, class _OutIter>36_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>37operator()(_InIter __first, _Sent __last, _OutIter __result) const {38while (__first != __last) {39*__result = *__first;40++__first;41++__result;42}4344return std::make_pair(std::move(__first), std::move(__result));45}4647template <class _InIter, class _OutIter>48struct _CopySegment {49using _Traits = __segmented_iterator_traits<_InIter>;5051_OutIter& __result_;5253_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit _CopySegment(_OutIter& __result)54: __result_(__result) {}5556_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void57operator()(typename _Traits::__local_iterator __lfirst, typename _Traits::__local_iterator __llast) {58__result_ = std::__copy<_AlgPolicy>(__lfirst, __llast, std::move(__result_)).second;59}60};6162template <class _InIter, class _OutIter, __enable_if_t<__is_segmented_iterator<_InIter>::value, int> = 0>63_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>64operator()(_InIter __first, _InIter __last, _OutIter __result) const {65std::__for_each_segment(__first, __last, _CopySegment<_InIter, _OutIter>(__result));66return std::make_pair(__last, std::move(__result));67}6869template <class _InIter,70class _OutIter,71__enable_if_t<__has_random_access_iterator_category<_InIter>::value &&72!__is_segmented_iterator<_InIter>::value && __is_segmented_iterator<_OutIter>::value,73int> = 0>74_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>75operator()(_InIter __first, _InIter __last, _OutIter __result) const {76using _Traits = __segmented_iterator_traits<_OutIter>;77using _DiffT = typename common_type<__iter_diff_t<_InIter>, __iter_diff_t<_OutIter> >::type;7879if (__first == __last)80return std::make_pair(std::move(__first), std::move(__result));8182auto __local_first = _Traits::__local(__result);83auto __segment_iterator = _Traits::__segment(__result);84while (true) {85auto __local_last = _Traits::__end(__segment_iterator);86auto __size = std::min<_DiffT>(__local_last - __local_first, __last - __first);87auto __iters = std::__copy<_AlgPolicy>(__first, __first + __size, __local_first);88__first = std::move(__iters.first);8990if (__first == __last)91return std::make_pair(std::move(__first), _Traits::__compose(__segment_iterator, std::move(__iters.second)));9293__local_first = _Traits::__begin(++__segment_iterator);94}95}9697// At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.98template <class _In, class _Out, __enable_if_t<__can_lower_copy_assignment_to_memmove<_In, _Out>::value, int> = 0>99_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>100operator()(_In* __first, _In* __last, _Out* __result) const {101return std::__copy_trivial_impl(__first, __last, __result);102}103};104105template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter>106pair<_InIter, _OutIter> inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14107__copy(_InIter __first, _Sent __last, _OutIter __result) {108return std::__copy_move_unwrap_iters<__copy_impl<_AlgPolicy> >(109std::move(__first), std::move(__last), std::move(__result));110}111112template <class _InputIterator, class _OutputIterator>113inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator114copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {115return std::__copy<_ClassicAlgPolicy>(__first, __last, __result).second;116}117118_LIBCPP_END_NAMESPACE_STD119120_LIBCPP_POP_MACROS121122#endif // _LIBCPP___ALGORITHM_COPY_H123124125