Skip to content

Commit 219655c

Browse files
committed
[libc++][ranges] implement 'ranges::shift_left'
1 parent f61bc69 commit 219655c

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

libcxx/include/__algorithm/shift_left.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <__utility/pair.h>
1818

1919
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20-
#pragma GCC system_header
20+
# pragma GCC system_header
2121
#endif
2222

2323
_LIBCPP_PUSH_MACROS
@@ -28,11 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2828
#if _LIBCPP_STD_VER >= 20
2929

3030
template <class _AlgPolicy, class _Iter, class _Sent>
31-
_LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter> __shift_left(
32-
_Iter __first, _Sent __last,
33-
typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __n) {
34-
_LIBCPP_ASSERT_UNCATEGORIZED(__n >= 0,
35-
"__n must be greater than or equal to 0");
31+
_LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter>
32+
__shift_left(_Iter __first, _Sent __last, typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __n) {
33+
_LIBCPP_ASSERT_UNCATEGORIZED(__n >= 0, "__n must be greater than or equal to 0");
3634

3735
if (__n == 0) {
3836
_Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
@@ -61,11 +59,10 @@ _LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter> __shift_left(
6159

6260
template <class _ForwardIterator>
6361
inline _LIBCPP_HIDE_FROM_ABI constexpr _ForwardIterator
64-
shift_left(_ForwardIterator __first, _ForwardIterator __last,
62+
shift_left(_ForwardIterator __first,
63+
_ForwardIterator __last,
6564
typename iterator_traits<_ForwardIterator>::difference_type __n) {
66-
return std::__shift_left<_ClassicAlgPolicy>(std::move(__first),
67-
std::move(__last), __n)
68-
.second;
65+
return std::__shift_left<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __n).second;
6966
}
7067

7168
#endif // _LIBCPP_STD_VER >= 20

libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,31 @@
2727
#include "test_iterators.h"
2828
#include "MoveOnly.h"
2929

30-
template <class Iter, class Sent = Iter>
31-
concept HasShiftLeftIt = requires(Iter iter, Sent sent, std::size_t n) { std::ranges::shift_left(iter, sent, n); };
30+
struct InvalidDifferenceT {};
31+
32+
template <class Iter, class Sent = Iter, class N = std::iter_difference_t<Iter>>
33+
concept HasShiftLeftIt = requires(Iter iter, Sent sent, N n) { std::ranges::shift_left(iter, sent, n); };
3234

3335
static_assert(HasShiftLeftIt<int*>);
36+
static_assert(HasShiftLeftIt<int*, sentinel_wrapper<int*>>);
37+
static_assert(HasShiftLeftIt<int*, sized_sentinel<int*>>);
38+
3439
static_assert(!HasShiftLeftIt<ForwardIteratorNotDerivedFrom>);
3540
static_assert(!HasShiftLeftIt<PermutableNotForwardIterator>);
3641
static_assert(!HasShiftLeftIt<PermutableNotSwappable>);
3742

38-
template <class Range>
39-
concept HasShiftLeftR = requires(Range range, std::size_t n) { std::ranges::shift_left(range, n); };
43+
static_assert(!HasShiftLeftIt<int*, int*, InvalidDifferenceT>);
44+
45+
template <class Range, class N = std::ranges::range_difference_t<Range>>
46+
concept HasShiftLeftR = requires(Range range, N n) { std::ranges::shift_left(range, n); };
4047

4148
static_assert(HasShiftLeftR<UncheckedRange<int*>>);
4249
static_assert(!HasShiftLeftR<ForwardRangeNotDerivedFrom>);
4350
static_assert(!HasShiftLeftR<PermutableRangeNotForwardIterator>);
4451
static_assert(!HasShiftLeftR<PermutableRangeNotSwappable>);
4552

53+
static_assert(!HasShiftLeftR<UncheckedRange<int*>, InvalidDifferenceT>);
54+
4655
template <class Iter, class Sent>
4756
constexpr void test_iter_sent() {
4857
{

0 commit comments

Comments
 (0)