Skip to content

Commit f61bc69

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

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

libcxx/include/__algorithm/ranges_shift_left.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
#include <__algorithm/shift_left.h>
1414
#include <__config>
1515
#include <__iterator/concepts.h>
16+
#include <__iterator/distance.h>
1617
#include <__iterator/incrementable_traits.h>
1718
#include <__iterator/permutable.h>
1819
#include <__ranges/access.h>
1920
#include <__ranges/concepts.h>
2021
#include <__ranges/subrange.h>
2122
#include <__utility/move.h>
22-
#include <__utility/pair.h>
2323

2424
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2525
# pragma GCC system_header
@@ -28,10 +28,10 @@
2828
_LIBCPP_PUSH_MACROS
2929
#include <__undef_macros>
3030

31-
#if _LIBCPP_STD_VER >= 23
32-
3331
_LIBCPP_BEGIN_NAMESPACE_STD
3432

33+
#if _LIBCPP_STD_VER >= 23
34+
3535
namespace ranges {
3636
namespace __shift_left {
3737

@@ -40,29 +40,34 @@ struct __fn {
4040
_LIBCPP_HIDE_FROM_ABI static constexpr subrange<_Iter>
4141
operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __n) {
4242
auto __ret = std::__shift_left<_RangeAlgPolicy>(std::move(__first), std::move(__last), std::move(__n));
43-
return {__ret.first, __ret.second};
43+
return {std::move(__ret.first), std::move(__ret.second)};
4444
}
4545

4646
template <forward_range _Range>
4747
requires permutable<iterator_t<_Range>>
4848
_LIBCPP_HIDE_FROM_ABI static constexpr borrowed_subrange_t<_Range>
4949
operator()(_Range&& __range, range_difference_t<_Range> __n) {
50+
if constexpr (sized_range<_Range>) {
51+
if (__n >= ranges::distance(__range)) {
52+
return {ranges::begin(__range), ranges::begin(__range)};
53+
}
54+
}
55+
5056
auto __ret = std::__shift_left<_RangeAlgPolicy>(ranges::begin(__range), ranges::end(__range), std::move(__n));
51-
return {__ret.first, __ret.second};
57+
return {std::move(__ret.first), std::move(__ret.second)};
5258
}
5359
};
54-
5560
} // namespace __shift_left
5661

5762
inline namespace __cpo {
5863
inline constexpr auto shift_left = __shift_left::__fn{};
5964
} // namespace __cpo
6065
} // namespace ranges
6166

62-
_LIBCPP_END_NAMESPACE_STD
63-
6467
#endif // _LIBCPP_STD_VER >= 23
6568

69+
_LIBCPP_END_NAMESPACE_STD
70+
6671
_LIBCPP_POP_MACROS
6772

6873
#endif // _LIBCPP___ALGORITHM_RANGES_SHIFT_LEFT_H

libcxx/include/__algorithm/shift_left.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter> __shift_left(
3636

3737
if (__n == 0) {
3838
_Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
39-
return {__first, __end};
39+
return {std::move(__first), std::move(__end)};
4040
}
4141

4242
_Iter __m = __first;

0 commit comments

Comments
 (0)