swap_ranges.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #ifndef _LIBCPP___ALGORITHM_SWAP_RANGES_H
  9. #define _LIBCPP___ALGORITHM_SWAP_RANGES_H
  10. #include <__algorithm/iterator_operations.h>
  11. #include <__config>
  12. #include <__utility/move.h>
  13. #include <__utility/pair.h>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_PUSH_MACROS
  18. #include <__undef_macros>
  19. _LIBCPP_BEGIN_NAMESPACE_STD
  20. // 2+2 iterators: the shorter size will be used.
  21. template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2, class _Sentinel2>
  22. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator1, _ForwardIterator2>
  23. __swap_ranges(_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2, _Sentinel2 __last2) {
  24. while (__first1 != __last1 && __first2 != __last2) {
  25. _IterOps<_AlgPolicy>::iter_swap(__first1, __first2);
  26. ++__first1;
  27. ++__first2;
  28. }
  29. return pair<_ForwardIterator1, _ForwardIterator2>(std::move(__first1), std::move(__first2));
  30. }
  31. // 2+1 iterators: size2 >= size1.
  32. template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2>
  33. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_ForwardIterator1, _ForwardIterator2>
  34. __swap_ranges(_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2) {
  35. while (__first1 != __last1) {
  36. _IterOps<_AlgPolicy>::iter_swap(__first1, __first2);
  37. ++__first1;
  38. ++__first2;
  39. }
  40. return pair<_ForwardIterator1, _ForwardIterator2>(std::move(__first1), std::move(__first2));
  41. }
  42. template <class _ForwardIterator1, class _ForwardIterator2>
  43. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator2
  44. swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) {
  45. return std::__swap_ranges<_ClassicAlgPolicy>(std::move(__first1), std::move(__last1), std::move(__first2)).second;
  46. }
  47. _LIBCPP_END_NAMESPACE_STD
  48. _LIBCPP_POP_MACROS
  49. #endif // _LIBCPP___ALGORITHM_SWAP_RANGES_H