| 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_SHIFT_LEFT_H |
| 10 | #define _LIBCPP___ALGORITHM_SHIFT_LEFT_H |
| 11 | |
| 12 | #include <__algorithm/iterator_operations.h> |
| 13 | #include <__algorithm/move.h> |
| 14 | #include <__assert> |
| 15 | #include <__config> |
| 16 | #include <__iterator/concepts.h> |
| 17 | #include <__iterator/iterator_traits.h> |
| 18 | #include <__utility/move.h> |
| 19 | #include <__utility/pair.h> |
| 20 | |
| 21 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 22 | # pragma GCC system_header |
| 23 | #endif |
| 24 | |
| 25 | _LIBCPP_PUSH_MACROS |
| 26 | #include <__undef_macros> |
| 27 | |
| 28 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 29 | |
| 30 | #if _LIBCPP_STD_VER >= 20 |
| 31 | |
| 32 | template <class _AlgPolicy, class _Iter, class _Sent> |
| 33 | _LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter> |
| 34 | __shift_left(_Iter __first, _Sent __last, typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __n) { |
| 35 | _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n >= 0, "n must be greater than or equal to 0" ); |
| 36 | |
| 37 | if (__n == 0) { |
| 38 | _Iter __end = _IterOps<_AlgPolicy>::next(__first, __last); |
| 39 | return {std::move(__first), std::move(__end)}; |
| 40 | } |
| 41 | |
| 42 | _Iter __m = __first; |
| 43 | if constexpr (sized_sentinel_for<_Sent, _Iter>) { |
| 44 | auto __size = _IterOps<_AlgPolicy>::distance(__first, __last); |
| 45 | if (__n >= __size) { |
| 46 | return {__first, std::move(__first)}; |
| 47 | } |
| 48 | _IterOps<_AlgPolicy>::advance(__m, __n); |
| 49 | } else { |
| 50 | for (; __n > 0; --__n) { |
| 51 | if (__m == __last) { |
| 52 | return {__first, std::move(__first)}; |
| 53 | } |
| 54 | ++__m; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | _Iter __result = std::__move<_AlgPolicy>(__m, __last, __first).second; |
| 59 | return {std::move(__first), std::move(__result)}; |
| 60 | } |
| 61 | |
| 62 | template <class _ForwardIterator> |
| 63 | _LIBCPP_HIDE_FROM_ABI constexpr _ForwardIterator |
| 64 | shift_left(_ForwardIterator __first, |
| 65 | _ForwardIterator __last, |
| 66 | typename iterator_traits<_ForwardIterator>::difference_type __n) { |
| 67 | return std::__shift_left<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __n).second; |
| 68 | } |
| 69 | |
| 70 | #endif // _LIBCPP_STD_VER >= 20 |
| 71 | |
| 72 | _LIBCPP_END_NAMESPACE_STD |
| 73 | |
| 74 | _LIBCPP_POP_MACROS |
| 75 | |
| 76 | #endif // _LIBCPP___ALGORITHM_SHIFT_LEFT_H |
| 77 | |