Skip to content

Commit e79f643

Browse files
committed
[libc++][ranges] refactor '__shift_left' to avoid redundant iteration
1 parent 020a097 commit e79f643

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

libcxx/include/__algorithm/shift_left.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,30 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2727
#if _LIBCPP_STD_VER >= 20
2828

2929
template <class _AlgPolicy, class _Iter, class _Sent>
30-
inline _LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter>
31-
__shift_left(_Iter __first, _Sent __last, typename iterator_traits<_Iter>::difference_type __n) {
32-
_Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
33-
30+
_LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter>
31+
__shift_left(_Iter __first, _Sent __last, typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __n) {
3432
if (__n == 0) {
33+
_Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
3534
return {std::move(__first), std::move(__end)};
3635
}
3736

3837
_Iter __m = __first;
39-
if constexpr (__has_random_access_iterator_category<_Iter>::value) {
40-
if (__n >= __end - __first) {
38+
if constexpr (sized_sentinel_for<_Sent, _Iter>) {
39+
auto __size = _IterOps<_AlgPolicy>::distance(__first, __last);
40+
if (__n >= __size) {
4141
return {std::move(__first), std::move(__first)};
4242
}
43-
__m += __n;
43+
_IterOps<_AlgPolicy>::advance(__m, __n);
4444
} else {
4545
for (; __n > 0; --__n) {
46-
if (__m == __end) {
46+
if (__m == __last) {
4747
return {std::move(__first), std::move(__first)};
4848
}
4949
++__m;
5050
}
5151
}
5252

53-
_Iter __result = std::__move<_AlgPolicy>(__m, __end, __first).second;
53+
_Iter __result = std::__move<_AlgPolicy>(__m, __last, __first).second;
5454
return {std::move(__first), std::move(__result)};
5555
}
5656

0 commit comments

Comments
 (0)