|
| 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_RANGES_SHIFT_RIGHT_H |
| 10 | +#define _LIBCPP___ALGORITHM_RANGES_SHIFT_RIGHT_H |
| 11 | + |
| 12 | +#include <__config> |
| 13 | +#include <__iterator/concepts.h> |
| 14 | +#include <__iterator/iterator_traits.h> |
| 15 | +#include <__ranges/access.h> |
| 16 | +#include <__ranges/concepts.h> |
| 17 | +#include <__ranges/subrange.h> |
| 18 | +#include <__utility/move.h> |
| 19 | +#include <__utility/forward.h> |
| 20 | +#include <__iterator/advance.h> |
| 21 | +#include <__iterator/distance.h> |
| 22 | +#include <algorithm> |
| 23 | + |
| 24 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 25 | +# pragma GCC system_header |
| 26 | +#endif |
| 27 | + |
| 28 | +_LIBCPP_PUSH_MACROS |
| 29 | +#include <__undef_macros> |
| 30 | + |
| 31 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 32 | + |
| 33 | +namespace ranges { |
| 34 | + |
| 35 | +namespace __shift_right { |
| 36 | + |
| 37 | +struct __fn { |
| 38 | + template <std::permutable _Iter, std::sentinel_for<_Iter> _Sent> |
| 39 | + _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter> |
| 40 | + operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __n) const { |
| 41 | + if (__n <= 0) { |
| 42 | + return {__last, __last}; |
| 43 | + } |
| 44 | + |
| 45 | + auto __dist = std::ranges::distance(__first, __last); |
| 46 | + if (__n >= __dist) { |
| 47 | + return {__last, __last}; |
| 48 | + } |
| 49 | + |
| 50 | + auto __new_first = std::ranges::next(__first, __dist - __n); |
| 51 | + std::move_backward(__first, __new_first, __last); |
| 52 | + return {__new_first, __last}; |
| 53 | + } |
| 54 | + |
| 55 | + template <std::ranges::forward_range _Range> |
| 56 | + requires std::permutable<iterator_t<_Range>> |
| 57 | + _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range> |
| 58 | + operator()(_Range&& __range, range_difference_t<_Range> __n) const { |
| 59 | + return (*this)(std::ranges::begin(__range), std::ranges::end(__range), __n); |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +} // namespace __shift_right |
| 64 | + |
| 65 | +inline namespace __cpo { |
| 66 | + inline constexpr auto shift_right = __shift_right::__fn{}; |
| 67 | +} // namespace __cpo |
| 68 | + |
| 69 | +} // namespace ranges |
| 70 | + |
| 71 | +_LIBCPP_END_NAMESPACE_STD |
| 72 | + |
| 73 | +_LIBCPP_POP_MACROS |
| 74 | + |
| 75 | +#endif // _LIBCPP___ALGORITHM_RANGES_SHIFT_RIGHT_H |
0 commit comments