[libcxx-commits] [libcxx] [libc++][ranges] implement ranges::shift_left (PR #83231)
Xiaoyang Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Feb 27 23:52:25 PST 2024
https://github.com/xiaoyang-sde created https://github.com/llvm/llvm-project/pull/83231
- Implement `ranges::shift_left` ([P2440R1](https://wg21.link/P2440R1))
>From e0800f119e62a03c9e1c2e494f98a758bf6a0312 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Tue, 27 Feb 2024 23:47:20 -0800
Subject: [PATCH] [libc++][ranges] implement ranges::shift_left
---
libcxx/include/CMakeLists.txt | 1 +
.../include/__algorithm/ranges_shift_left.h | 68 +++++++++++++++++++
libcxx/include/__algorithm/shift_left.h | 36 ++++++----
libcxx/include/algorithm | 8 +++
libcxx/include/libcxx.imp | 1 +
libcxx/include/module.modulemap.in | 1 +
libcxx/modules/std/algorithm.inc | 2 +-
...es_robust_against_proxy_iterators.pass.cpp | 3 +
.../niebloid.compile.pass.cpp | 3 +
9 files changed, 109 insertions(+), 14 deletions(-)
create mode 100644 libcxx/include/__algorithm/ranges_shift_left.h
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index cafd8c6e00d968..22929225fff728 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -183,6 +183,7 @@ set(files
__algorithm/ranges_set_intersection.h
__algorithm/ranges_set_symmetric_difference.h
__algorithm/ranges_set_union.h
+ __algorithm/ranges_shift_left.h
__algorithm/ranges_shuffle.h
__algorithm/ranges_sort.h
__algorithm/ranges_sort_heap.h
diff --git a/libcxx/include/__algorithm/ranges_shift_left.h b/libcxx/include/__algorithm/ranges_shift_left.h
new file mode 100644
index 00000000000000..bf82ce32693824
--- /dev/null
+++ b/libcxx/include/__algorithm/ranges_shift_left.h
@@ -0,0 +1,68 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/shift_left.h>
+#include <__config>
+#include <__iterator/concepts.h>
+#include <__iterator/permutable.h>
+#include <__ranges/concepts.h>
+#include <__ranges/subrange.h>
+#include <__utility/move.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 {
+ return __shift_left_impl(std::move(__first), std::move(__last), std::move(__n));
+ }
+
+ template < forward_range _Range>
+ _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range>
+ operator()(_Range&& __range, range_difference_t<_Range> __n) const {
+ 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
diff --git a/libcxx/include/__algorithm/shift_left.h b/libcxx/include/__algorithm/shift_left.h
index 06cd7c5f87644e..e6b8153ecebec8 100644
--- a/libcxx/include/__algorithm/shift_left.h
+++ b/libcxx/include/__algorithm/shift_left.h
@@ -24,30 +24,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>
+inline _LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter>
+__shift_left(_Iter __first, _Sent __last, typename iterator_traits<_Iter>::difference_type __n) {
+ _Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
+
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) {
+ 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
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 70e30bc87e8128..d9b4e87a8a2ad8 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -843,6 +843,13 @@ namespace ranges {
uniform_random_bit_generator<remove_reference_t<Gen>>
O sample(R&& r, O out, range_difference_t<R> n, Gen&& g); // since C++20
+ template<permutable I, sentinel_for<I> S>
+ constexpr subrange<I> ranges::shift_left(I first, S last, iter_difference_t<I> n); // since C++23
+
+ template<forward_range R>
+ requires permutable<iterator_t<R>>
+ constexpr borrowed_subrange_t<R> ranges::shift_left(R&& r, range_difference_t<R> n) // since C++23
+
template<random_access_iterator I, sentinel_for<I> S, class Gen>
requires permutable<I> &&
uniform_random_bit_generator<remove_reference_t<Gen>>
@@ -1960,6 +1967,7 @@ template <class BidirectionalIterator, class Compare>
#include <__algorithm/ranges_set_intersection.h>
#include <__algorithm/ranges_set_symmetric_difference.h>
#include <__algorithm/ranges_set_union.h>
+#include <__algorithm/ranges_shift_left.h>
#include <__algorithm/ranges_shuffle.h>
#include <__algorithm/ranges_sort.h>
#include <__algorithm/ranges_sort_heap.h>
diff --git a/libcxx/include/libcxx.imp b/libcxx/include/libcxx.imp
index 22fbea99b848bb..1af11eba2d260d 100644
--- a/libcxx/include/libcxx.imp
+++ b/libcxx/include/libcxx.imp
@@ -183,6 +183,7 @@
{ include: [ "<__algorithm/ranges_set_intersection.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_set_symmetric_difference.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_set_union.h>", "private", "<algorithm>", "public" ] },
+ { include: [ "<__algorithm/ranges_shift_left.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_shuffle.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_sort.h>", "private", "<algorithm>", "public" ] },
{ include: [ "<__algorithm/ranges_sort_heap.h>", "private", "<algorithm>", "public" ] },
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 219906aa9a5668..def640397570d8 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -987,6 +987,7 @@ module std_private_algorithm_ranges_set_union [system
export std_private_algorithm_in_in_out_result
export std_private_functional_ranges_operations
}
+module std_private_algorithm_ranges_shift_left [system] { header "__algorithm/ranges_shift_left.h" }
module std_private_algorithm_ranges_shuffle [system] { header "__algorithm/ranges_shuffle.h" }
module std_private_algorithm_ranges_sort [system] {
header "__algorithm/ranges_sort.h"
diff --git a/libcxx/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index e7796bfa26af81..0a4e1c1ba6cc96 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -354,7 +354,7 @@ export namespace std {
using std::shift_left;
namespace ranges {
- // using std::ranges::shift_left;
+ using std::ranges::shift_left;
}
using std::shift_right;
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
index 139f1999bc9dca..c64945e34994d3 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
@@ -154,6 +154,9 @@ constexpr void run_tests() {
test(std::ranges::remove_if, in, unary_pred);
test(std::ranges::reverse, in);
test_mid(std::ranges::rotate, in, mid);
+#if TEST_STD_VER >= 23
+ test(std::ranges::shift_left, in, count);
+#endif
if (!std::is_constant_evaluated()) // `shuffle` isn't `constexpr`.
test(std::ranges::shuffle, in, rand_gen());
if (!std::is_constant_evaluated()) {
diff --git a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
index 9506ca1c768bd7..886d112b5276d7 100644
--- a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
+++ b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
@@ -146,6 +146,9 @@ static_assert(test(std::ranges::set_difference, a, a, a));
static_assert(test(std::ranges::set_intersection, a, a, a));
static_assert(test(std::ranges::set_symmetric_difference, a, a, a));
static_assert(test(std::ranges::set_union, a, a, a));
+#if TEST_STD_VER >= 23
+static_assert(test(std::ranges::shift_left, a, 42));
+#endif
static_assert(test(std::ranges::shuffle, a, g));
static_assert(test(std::ranges::sort, a));
static_assert(test(std::ranges::sort_heap, a));
More information about the libcxx-commits
mailing list