xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/swap_ranges.h (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
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
26 pair<_ForwardIterator1, _ForwardIterator2>
27 __swap_ranges(_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2, _Sentinel2 __last2) {
28   while (__first1 != __last1 && __first2 != __last2) {
29     _IterOps<_AlgPolicy>::iter_swap(__first1, __first2);
30     ++__first1;
31     ++__first2;
32   }
33 
34   return pair<_ForwardIterator1, _ForwardIterator2>(std::move(__first1), std::move(__first2));
35 }
36 
37 // 2+1 iterators: size2 >= size1.
38 template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2>
39 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
40 pair<_ForwardIterator1, _ForwardIterator2>
41 __swap_ranges(_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2) {
42   while (__first1 != __last1) {
43     _IterOps<_AlgPolicy>::iter_swap(__first1, __first2);
44     ++__first1;
45     ++__first2;
46   }
47 
48   return pair<_ForwardIterator1, _ForwardIterator2>(std::move(__first1), std::move(__first2));
49 }
50 
51 template <class _ForwardIterator1, class _ForwardIterator2>
52 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator2
53 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) {
54   return std::__swap_ranges<_ClassicAlgPolicy>(
55       std::move(__first1), std::move(__last1), std::move(__first2)).second;
56 }
57 
58 _LIBCPP_END_NAMESPACE_STD
59 
60 #endif // _LIBCPP___ALGORITHM_SWAP_RANGES_H
61