-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc++][ranges] implement ranges::shift_left
#83231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 12 commits
e0800f1
68a8635
ade1707
c847929
40691ad
4de1787
02a20d9
9bd0eac
7747973
aa1f598
a68eb02
020a097
e79f643
357f4eb
c2fc0dc
e2aa538
424c31b
7cde586
ead91b5
e1360e0
956b915
be2162d
f61bc69
219655c
cdf0c94
ac2e129
20b1cce
dfeaf68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP___ALGORITHM_RANGES_SHIFT_LEFT_H | ||
#define _LIBCPP___ALGORITHM_RANGES_SHIFT_LEFT_H | ||
|
||
#include <__algorithm/iterator_operations.h> | ||
#include <__algorithm/shift_left.h> | ||
#include <__config> | ||
#include <__iterator/concepts.h> | ||
#include <__iterator/incrementable_traits.h> | ||
#include <__iterator/permutable.h> | ||
#include <__ranges/access.h> | ||
#include <__ranges/concepts.h> | ||
#include <__ranges/subrange.h> | ||
#include <__utility/move.h> | ||
#include <__utility/pair.h> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
_LIBCPP_PUSH_MACROS | ||
#include <__undef_macros> | ||
|
||
#if _LIBCPP_STD_VER >= 23 | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
namespace ranges { | ||
namespace __shift_left { | ||
|
||
struct __fn { | ||
template <class _Iter, class _Sent> | ||
_LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter> static __shift_left_impl( | ||
_Iter __first, _Sent __last, iter_difference_t<_Iter> __n) { | ||
auto __ret = std::__shift_left<_RangeAlgPolicy>(std::move(__first), std::move(__last), std::move(__n)); | ||
return {std::move(__ret.first), std::move(__ret.second)}; | ||
} | ||
|
||
template <permutable _Iter, sentinel_for<_Iter> _Sent> | ||
_LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter> | ||
operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __n) const { | ||
xiaoyang-sde marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return __shift_left_impl(std::move(__first), std::move(__last), std::move(__n)); | ||
} | ||
|
||
template <forward_range _Range> | ||
requires permutable<iterator_t<_Range>> | ||
_LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range> | ||
operator()(_Range&& __range, range_difference_t<_Range> __n) const { | ||
xiaoyang-sde marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return __shift_left_impl(ranges::begin(__range), ranges::end(__range), std::move(__n)); | ||
} | ||
}; | ||
|
||
} // namespace __shift_left | ||
|
||
inline namespace __cpo { | ||
inline constexpr auto shift_left = __shift_left::__fn{}; | ||
} // namespace __cpo | ||
} // namespace ranges | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCPP_STD_VER >= 23 | ||
|
||
_LIBCPP_POP_MACROS | ||
|
||
#endif // _LIBCPP___ALGORITHM_RANGES_SHIFT_LEFT_H |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,9 +9,11 @@ | |||||
#ifndef _LIBCPP___ALGORITHM_SHIFT_LEFT_H | ||||||
#define _LIBCPP___ALGORITHM_SHIFT_LEFT_H | ||||||
|
||||||
#include <__algorithm/iterator_operations.h> | ||||||
#include <__algorithm/move.h> | ||||||
#include <__config> | ||||||
#include <__iterator/iterator_traits.h> | ||||||
#include <__utility/pair.h> | ||||||
|
||||||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||||||
# pragma GCC system_header | ||||||
|
@@ -24,30 +26,40 @@ _LIBCPP_BEGIN_NAMESPACE_STD | |||||
|
||||||
#if _LIBCPP_STD_VER >= 20 | ||||||
|
||||||
template <class _ForwardIterator> | ||||||
inline _LIBCPP_HIDE_FROM_ABI constexpr _ForwardIterator | ||||||
shift_left(_ForwardIterator __first, | ||||||
_ForwardIterator __last, | ||||||
typename iterator_traits<_ForwardIterator>::difference_type __n) { | ||||||
template <class _AlgPolicy, class _Iter, class _Sent> | ||||||
xiaoyang-sde marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
inline _LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter> | ||||||
xiaoyang-sde marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
__shift_left(_Iter __first, _Sent __last, typename iterator_traits<_Iter>::difference_type __n) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please add a test! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Could you please provide more details about the tests I should consider adding? |
||||||
_Iter __end = _IterOps<_AlgPolicy>::next(__first, __last); | ||||||
xiaoyang-sde marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
if (__n == 0) { | ||||||
return __last; | ||||||
return {std::move(__first), std::move(__end)}; | ||||||
} | ||||||
|
||||||
_ForwardIterator __m = __first; | ||||||
if constexpr (__has_random_access_iterator_category<_ForwardIterator>::value) { | ||||||
if (__n >= __last - __first) { | ||||||
return __first; | ||||||
_Iter __m = __first; | ||||||
if constexpr (__has_random_access_iterator_category<_Iter>::value) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! I refactored this function. |
||||||
if (__n >= __end - __first) { | ||||||
return {std::move(__first), std::move(__first)}; | ||||||
} | ||||||
__m += __n; | ||||||
} else { | ||||||
for (; __n > 0; --__n) { | ||||||
if (__m == __last) { | ||||||
return __first; | ||||||
if (__m == __end) { | ||||||
return {std::move(__first), std::move(__first)}; | ||||||
} | ||||||
++__m; | ||||||
} | ||||||
} | ||||||
return std::move(__m, __last, __first); | ||||||
|
||||||
_Iter __result = std::__move<_AlgPolicy>(__m, __end, __first).second; | ||||||
return {std::move(__first), std::move(__result)}; | ||||||
} | ||||||
|
||||||
template <class _ForwardIterator> | ||||||
inline _LIBCPP_HIDE_FROM_ABI constexpr _ForwardIterator | ||||||
shift_left(_ForwardIterator __first, | ||||||
_ForwardIterator __last, | ||||||
typename iterator_traits<_ForwardIterator>::difference_type __n) { | ||||||
return std::__shift_left<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __n).second; | ||||||
} | ||||||
|
||||||
#endif // _LIBCPP_STD_VER >= 20 | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.