xref: /llvm-project/libcxx/include/__algorithm/swap_ranges.h (revision 7b4622514d232ce5f7110dd8b20d90e81127c467)
16adbc83eSChristopher Di Bella //===----------------------------------------------------------------------===//
26adbc83eSChristopher Di Bella //
36adbc83eSChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
46adbc83eSChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information.
56adbc83eSChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66adbc83eSChristopher Di Bella //
76adbc83eSChristopher Di Bella //===----------------------------------------------------------------------===//
86adbc83eSChristopher Di Bella 
96adbc83eSChristopher Di Bella #ifndef _LIBCPP___ALGORITHM_SWAP_RANGES_H
106adbc83eSChristopher Di Bella #define _LIBCPP___ALGORITHM_SWAP_RANGES_H
116adbc83eSChristopher Di Bella 
1236c746caSKonstantin Varlamov #include <__algorithm/iterator_operations.h>
136adbc83eSChristopher Di Bella #include <__config>
1436c746caSKonstantin Varlamov #include <__utility/move.h>
1536c746caSKonstantin Varlamov #include <__utility/pair.h>
166adbc83eSChristopher Di Bella 
176adbc83eSChristopher Di Bella #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
186adbc83eSChristopher Di Bella #  pragma GCC system_header
196adbc83eSChristopher Di Bella #endif
206adbc83eSChristopher Di Bella 
21*7b462251SLouis Dionne _LIBCPP_PUSH_MACROS
22*7b462251SLouis Dionne #include <__undef_macros>
23*7b462251SLouis Dionne 
246adbc83eSChristopher Di Bella _LIBCPP_BEGIN_NAMESPACE_STD
256adbc83eSChristopher Di Bella 
2636c746caSKonstantin Varlamov // 2+2 iterators: the shorter size will be used.
2736c746caSKonstantin Varlamov template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2, class _Sentinel2>
289783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator1, _ForwardIterator2>
__swap_ranges(_ForwardIterator1 __first1,_Sentinel1 __last1,_ForwardIterator2 __first2,_Sentinel2 __last2)2936c746caSKonstantin Varlamov __swap_ranges(_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2, _Sentinel2 __last2) {
3036c746caSKonstantin Varlamov   while (__first1 != __last1 && __first2 != __last2) {
3136c746caSKonstantin Varlamov     _IterOps<_AlgPolicy>::iter_swap(__first1, __first2);
3236c746caSKonstantin Varlamov     ++__first1;
3336c746caSKonstantin Varlamov     ++__first2;
3436c746caSKonstantin Varlamov   }
3536c746caSKonstantin Varlamov 
3636c746caSKonstantin Varlamov   return pair<_ForwardIterator1, _ForwardIterator2>(std::move(__first1), std::move(__first2));
3736c746caSKonstantin Varlamov }
3836c746caSKonstantin Varlamov 
3936c746caSKonstantin Varlamov // 2+1 iterators: size2 >= size1.
4036c746caSKonstantin Varlamov template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2>
419783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator1, _ForwardIterator2>
__swap_ranges(_ForwardIterator1 __first1,_Sentinel1 __last1,_ForwardIterator2 __first2)4236c746caSKonstantin Varlamov __swap_ranges(_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2) {
4336c746caSKonstantin Varlamov   while (__first1 != __last1) {
4436c746caSKonstantin Varlamov     _IterOps<_AlgPolicy>::iter_swap(__first1, __first2);
4536c746caSKonstantin Varlamov     ++__first1;
4636c746caSKonstantin Varlamov     ++__first2;
4736c746caSKonstantin Varlamov   }
4836c746caSKonstantin Varlamov 
4936c746caSKonstantin Varlamov   return pair<_ForwardIterator1, _ForwardIterator2>(std::move(__first1), std::move(__first2));
5036c746caSKonstantin Varlamov }
5136c746caSKonstantin Varlamov 
526adbc83eSChristopher Di Bella template <class _ForwardIterator1, class _ForwardIterator2>
534c198542SLouis Dionne inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator2
swap_ranges(_ForwardIterator1 __first1,_ForwardIterator1 __last1,_ForwardIterator2 __first2)546adbc83eSChristopher Di Bella swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) {
559783f28cSLouis Dionne   return std::__swap_ranges<_ClassicAlgPolicy>(std::move(__first1), std::move(__last1), std::move(__first2)).second;
566adbc83eSChristopher Di Bella }
576adbc83eSChristopher Di Bella 
586adbc83eSChristopher Di Bella _LIBCPP_END_NAMESPACE_STD
596adbc83eSChristopher Di Bella 
60*7b462251SLouis Dionne _LIBCPP_POP_MACROS
61*7b462251SLouis Dionne 
626adbc83eSChristopher Di Bella #endif // _LIBCPP___ALGORITHM_SWAP_RANGES_H
63