xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/swap_ranges.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_SWAP_RANGES_H
10 #define _LIBCPP___ALGORITHM_SWAP_RANGES_H
11 
12 #include <__algorithm/iterator_operations.h>
13 #include <__config>
14 #include <__utility/move.h>
15 #include <__utility/pair.h>
16 
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #  pragma GCC system_header
19 #endif
20 
21 _LIBCPP_BEGIN_NAMESPACE_STD
22 
23 // 2+2 iterators: the shorter size will be used.
24 template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2, class _Sentinel2>
25 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator1, _ForwardIterator2>
26 __swap_ranges(_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2, _Sentinel2 __last2) {
27   while (__first1 != __last1 && __first2 != __last2) {
28     _IterOps<_AlgPolicy>::iter_swap(__first1, __first2);
29     ++__first1;
30     ++__first2;
31   }
32 
33   return pair<_ForwardIterator1, _ForwardIterator2>(std::move(__first1), std::move(__first2));
34 }
35 
36 // 2+1 iterators: size2 >= size1.
37 template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2>
38 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator1, _ForwardIterator2>
39 __swap_ranges(_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2) {
40   while (__first1 != __last1) {
41     _IterOps<_AlgPolicy>::iter_swap(__first1, __first2);
42     ++__first1;
43     ++__first2;
44   }
45 
46   return pair<_ForwardIterator1, _ForwardIterator2>(std::move(__first1), std::move(__first2));
47 }
48 
49 template <class _ForwardIterator1, class _ForwardIterator2>
50 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator2
51 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) {
52   return std::__swap_ranges<_ClassicAlgPolicy>(std::move(__first1), std::move(__last1), std::move(__first2)).second;
53 }
54 
55 _LIBCPP_END_NAMESPACE_STD
56 
57 #endif // _LIBCPP___ALGORITHM_SWAP_RANGES_H
58