1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric
9fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_SWAP_RANGES_H
10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_SWAP_RANGES_H
11fe6060f1SDimitry Andric
1261cfbce3SDimitry Andric #include <__algorithm/iterator_operations.h>
13fe6060f1SDimitry Andric #include <__config>
1461cfbce3SDimitry Andric #include <__utility/move.h>
1561cfbce3SDimitry Andric #include <__utility/pair.h>
16fe6060f1SDimitry Andric
17fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18fe6060f1SDimitry Andric # pragma GCC system_header
19fe6060f1SDimitry Andric #endif
20fe6060f1SDimitry Andric
21*b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
22*b3edf446SDimitry Andric #include <__undef_macros>
23*b3edf446SDimitry Andric
24fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
25fe6060f1SDimitry Andric
2661cfbce3SDimitry Andric // 2+2 iterators: the shorter size will be used.
2761cfbce3SDimitry Andric template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2, class _Sentinel2>
28cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator1, _ForwardIterator2>
__swap_ranges(_ForwardIterator1 __first1,_Sentinel1 __last1,_ForwardIterator2 __first2,_Sentinel2 __last2)2961cfbce3SDimitry Andric __swap_ranges(_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2, _Sentinel2 __last2) {
3061cfbce3SDimitry Andric while (__first1 != __last1 && __first2 != __last2) {
3161cfbce3SDimitry Andric _IterOps<_AlgPolicy>::iter_swap(__first1, __first2);
3261cfbce3SDimitry Andric ++__first1;
3361cfbce3SDimitry Andric ++__first2;
3461cfbce3SDimitry Andric }
3561cfbce3SDimitry Andric
3661cfbce3SDimitry Andric return pair<_ForwardIterator1, _ForwardIterator2>(std::move(__first1), std::move(__first2));
3761cfbce3SDimitry Andric }
3861cfbce3SDimitry Andric
3961cfbce3SDimitry Andric // 2+1 iterators: size2 >= size1.
4061cfbce3SDimitry Andric template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2>
41cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator1, _ForwardIterator2>
__swap_ranges(_ForwardIterator1 __first1,_Sentinel1 __last1,_ForwardIterator2 __first2)4261cfbce3SDimitry Andric __swap_ranges(_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2) {
4361cfbce3SDimitry Andric while (__first1 != __last1) {
4461cfbce3SDimitry Andric _IterOps<_AlgPolicy>::iter_swap(__first1, __first2);
4561cfbce3SDimitry Andric ++__first1;
4661cfbce3SDimitry Andric ++__first2;
4761cfbce3SDimitry Andric }
4861cfbce3SDimitry Andric
4961cfbce3SDimitry Andric return pair<_ForwardIterator1, _ForwardIterator2>(std::move(__first1), std::move(__first2));
5061cfbce3SDimitry Andric }
5161cfbce3SDimitry Andric
52fe6060f1SDimitry Andric template <class _ForwardIterator1, class _ForwardIterator2>
535f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator2
swap_ranges(_ForwardIterator1 __first1,_ForwardIterator1 __last1,_ForwardIterator2 __first2)54fe6060f1SDimitry Andric swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) {
55cb14a3feSDimitry Andric return std::__swap_ranges<_ClassicAlgPolicy>(std::move(__first1), std::move(__last1), std::move(__first2)).second;
56fe6060f1SDimitry Andric }
57fe6060f1SDimitry Andric
58fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
59fe6060f1SDimitry Andric
60*b3edf446SDimitry Andric _LIBCPP_POP_MACROS
61*b3edf446SDimitry Andric
62fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_SWAP_RANGES_H
63