[libcxx-commits] [libcxx] [libc++][ranges] implement `ranges::shift_left` (PR #83231)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Jan 24 07:13:43 PST 2026
https://github.com/huixie90 updated https://github.com/llvm/llvm-project/pull/83231
>From bd692a49cfe4d07a354b6ab8d99ccc44c3ead0a8 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 01/33] [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/module.modulemap.in | 3 +
libcxx/modules/std/algorithm.inc | 2 +-
...es_robust_against_proxy_iterators.pass.cpp | 3 +
.../niebloid.compile.pass.cpp | 3 +
8 files changed, 110 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 2ccc766f8109b..2ae4810a70b11 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -157,6 +157,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 0000000000000..bf82ce3269382
--- /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 06cd7c5f87644..e6b8153ecebec 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 64e9fa6306145..baa682e95b185 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -883,6 +883,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>>
@@ -2035,6 +2042,7 @@ template <class BidirectionalIterator, class Compare>
# include <__algorithm/ranges_ends_with.h>
# include <__algorithm/ranges_find_last.h>
# include <__algorithm/ranges_fold.h>
+# include <__algorithm/ranges_shift_left.h>
# include <__algorithm/ranges_starts_with.h>
# endif // _LIBCPP_STD_VER >= 23
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index cbe1e221844f8..177fe475fb229 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -760,6 +760,9 @@ module std [system] {
export std.functional.ranges_operations
export std.algorithm.in_in_out_result
}
+ module ranges_shift_left {
+ header "__algorithm/ranges_shift_left.h"
+ }
module ranges_shuffle {
header "__algorithm/ranges_shuffle.h"
}
diff --git a/libcxx/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index 95c05f01e5562..018292b38d06e 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -357,7 +357,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 9d4b0d608518a..8bd76cfc9982d 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
@@ -165,6 +165,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 dc9134f3deb3c..c799af04fc688 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
@@ -155,6 +155,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));
>From 83d9ca9ce7f7aa48d2648a51b28486a9138f9782 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Wed, 28 Feb 2024 00:30:35 -0800
Subject: [PATCH 02/33] [libc++][ranges] format code with 'clang-format'
---
.../algorithms/ranges_robust_against_proxy_iterators.pass.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 8bd76cfc9982d..4d4651e64fbf4 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
@@ -166,7 +166,7 @@ constexpr void run_tests() {
test(std::ranges::reverse, in);
test_mid(std::ranges::rotate, in, mid);
#if TEST_STD_VER >= 23
- test(std::ranges::shift_left, in, count);
+ test(std::ranges::shift_left, in, count);
#endif
if (!std::is_constant_evaluated()) // `shuffle` isn't `constexpr`.
test(std::ranges::shuffle, in, rand_gen());
>From 9abd3c052118ca206e47f652c91943627da95968 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Wed, 28 Feb 2024 11:28:07 -0800
Subject: [PATCH 03/33] [libc++][ranges] check '_LIBCPP_STD_VER' in
'algorithm.inc'
---
libcxx/modules/std/algorithm.inc | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libcxx/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index 018292b38d06e..540bcc3879678 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -356,9 +356,11 @@ export namespace std {
// [alg.shift], shift
using std::shift_left;
+#if _LIBCPP_STD_VER >= 23
namespace ranges {
using std::ranges::shift_left;
}
+#endif // _LIBCPP_STD_VER >= 23
using std::shift_right;
>From 95bb06980909ea336d92779d0d5c254b39d90c94 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Wed, 28 Feb 2024 11:54:13 -0800
Subject: [PATCH 04/33] [libc++][ranges] add missing headers to 'shift_left.h'
and 'ranges_shift_left.h'
---
libcxx/include/__algorithm/ranges_shift_left.h | 1 +
libcxx/include/__algorithm/shift_left.h | 1 +
2 files changed, 2 insertions(+)
diff --git a/libcxx/include/__algorithm/ranges_shift_left.h b/libcxx/include/__algorithm/ranges_shift_left.h
index bf82ce3269382..4595fc7d7031e 100644
--- a/libcxx/include/__algorithm/ranges_shift_left.h
+++ b/libcxx/include/__algorithm/ranges_shift_left.h
@@ -16,6 +16,7 @@
#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
diff --git a/libcxx/include/__algorithm/shift_left.h b/libcxx/include/__algorithm/shift_left.h
index e6b8153ecebec..01d9f1c035f36 100644
--- a/libcxx/include/__algorithm/shift_left.h
+++ b/libcxx/include/__algorithm/shift_left.h
@@ -9,6 +9,7 @@
#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>
>From a0506d9de25e6e392dbb64f00b573b1c176594dc Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Wed, 28 Feb 2024 12:33:50 -0800
Subject: [PATCH 05/33] [libc++][ranges] add missing headers to 'shift_left.h'
---
libcxx/include/__algorithm/shift_left.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/libcxx/include/__algorithm/shift_left.h b/libcxx/include/__algorithm/shift_left.h
index 01d9f1c035f36..8df1bff87f042 100644
--- a/libcxx/include/__algorithm/shift_left.h
+++ b/libcxx/include/__algorithm/shift_left.h
@@ -13,6 +13,7 @@
#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
>From 7ebc412ef0cd6ef3bdb8bce57674fb233e269f22 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Wed, 28 Feb 2024 12:34:21 -0800
Subject: [PATCH 06/33] [libc++][ranges] add missing constraints to
'ranges_shift_left.h'
---
libcxx/include/__algorithm/ranges_shift_left.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__algorithm/ranges_shift_left.h b/libcxx/include/__algorithm/ranges_shift_left.h
index 4595fc7d7031e..bee3e865c87ed 100644
--- a/libcxx/include/__algorithm/ranges_shift_left.h
+++ b/libcxx/include/__algorithm/ranges_shift_left.h
@@ -46,7 +46,8 @@ struct __fn {
return __shift_left_impl(std::move(__first), std::move(__last), std::move(__n));
}
- template < forward_range _Range>
+ 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 {
return __shift_left_impl(ranges::begin(__range), ranges::end(__range), std::move(__n));
@@ -62,7 +63,7 @@ inline constexpr auto shift_left = __shift_left::__fn{};
_LIBCPP_END_NAMESPACE_STD
-#endif // _LIBCPP_STD_VER == 23
+#endif // _LIBCPP_STD_VER >= 23
_LIBCPP_POP_MACROS
>From 3e522678dc2dd5886563bcc71cd4542e931523ce Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Wed, 28 Feb 2024 12:38:39 -0800
Subject: [PATCH 07/33] [libc++][ranges] format code with 'clang-format'
---
libcxx/include/__algorithm/ranges_shift_left.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__algorithm/ranges_shift_left.h b/libcxx/include/__algorithm/ranges_shift_left.h
index bee3e865c87ed..9a0cd3a4223c0 100644
--- a/libcxx/include/__algorithm/ranges_shift_left.h
+++ b/libcxx/include/__algorithm/ranges_shift_left.h
@@ -47,7 +47,7 @@ struct __fn {
}
template <forward_range _Range>
- requires permutable<iterator_t<_Range>>
+ requires permutable<iterator_t<_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));
>From 0f196464445aa1a034750bd19a6609dbc9c7ed25 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Wed, 28 Feb 2024 16:03:17 -0800
Subject: [PATCH 08/33] [libc++][ranges] add missing headers to
'ranges_shift_left.h'
---
libcxx/include/__algorithm/ranges_shift_left.h | 3 +++
1 file changed, 3 insertions(+)
diff --git a/libcxx/include/__algorithm/ranges_shift_left.h b/libcxx/include/__algorithm/ranges_shift_left.h
index 9a0cd3a4223c0..d9edf661d1d8c 100644
--- a/libcxx/include/__algorithm/ranges_shift_left.h
+++ b/libcxx/include/__algorithm/ranges_shift_left.h
@@ -9,10 +9,13 @@
#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>
>From 4d5b86e25cec209a1c997a3a62bcc84804062fb5 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Wed, 28 Feb 2024 18:44:12 -0800
Subject: [PATCH 09/33] [libc++][ranges] implement 'ranges.shift_left.pass.cpp'
to test 'ranges::shift_left.h'
---
.../alg.shift/ranges.shift_left.pass.cpp | 194 ++++++++++++++++++
1 file changed, 194 insertions(+)
create mode 100644 libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
new file mode 100644
index 0000000000000..38ef57d3bca00
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
@@ -0,0 +1,194 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// template<permutable I, sentinel_for<I> S>
+// constexpr subrange<I> ranges::shift_left(I first, S last, iter_difference_t<I> n);
+
+// template<forward_range R>
+// requires permutable<iterator_t<R>>
+// constexpr borrowed_subrange_t<R> ranges::shift_left(R&& r, range_difference_t<R> n)
+
+#include <algorithm>
+#include <array>
+#include <ranges>
+#include <iterator>
+
+#include "almost_satisfies_types.h"
+#include "test_iterators.h"
+#include "MoveOnly.h"
+
+template <class Iter, class Sent = Iter, class Count = std::size_t>
+concept HasShiftLeftIt = requires(Iter iter, Sent sent, Count n) { std::ranges::shift_left(iter, sent, n); };
+
+static_assert(HasShiftLeftIt<int*>);
+static_assert(!HasShiftLeftIt<ForwardIteratorNotDerivedFrom>);
+static_assert(!HasShiftLeftIt<PermutableNotForwardIterator>);
+static_assert(!HasShiftLeftIt<PermutableNotSwappable>);
+
+template <class Range, class Count = std::size_t>
+concept HasShiftLeftR = requires(Range range, Count n) { std::ranges::shift_left(range, n); };
+
+static_assert(HasShiftLeftR<UncheckedRange<int*>>);
+static_assert(!HasShiftLeftR<ForwardRangeNotDerivedFrom>);
+static_assert(!HasShiftLeftR<PermutableRangeNotForwardIterator>);
+static_assert(!HasShiftLeftR<PermutableRangeNotSwappable>);
+
+template <class Iter, class Sent>
+constexpr void test_iter_sent() {
+ {
+ const std::array<int, 15> original = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9};
+ std::array<int, 15> scratch;
+
+ // (iterator, sentinel) overload
+ for (size_t n = 0; n <= 15; ++n) {
+ for (size_t k = 0; k <= n + 2; ++k) {
+ auto begin = Iter(scratch.data());
+ auto end = Sent(Iter(scratch.data() + n));
+ std::ranges::copy(original.begin(), original.begin() + n, begin);
+ auto result = std::ranges::shift_left(begin, end, k);
+
+ assert(result.begin() == begin);
+ if (k < n) {
+ assert(result.end() == Iter(scratch.data() + n - k));
+ assert(std::ranges::equal(original.begin() + k, original.begin() + n, result.begin(), result.end()));
+ } else {
+ assert(result.end() == begin);
+ assert(std::ranges::equal(original.begin(), original.begin() + n, begin, end));
+ }
+ }
+ }
+
+ // (range) overload
+ for (size_t n = 0; n <= 15; ++n) {
+ for (size_t k = 0; k <= n + 2; ++k) {
+ auto begin = Iter(scratch.data());
+ auto end = Sent(Iter(scratch.data() + n));
+ std::ranges::copy(original.begin(), original.begin() + n, begin);
+ auto range = std::ranges::subrange(begin, end);
+ auto result = std::ranges::shift_left(range, k);
+
+ assert(result.begin() == begin);
+ if (k < n) {
+ assert(result.end() == Iter(scratch.data() + n - k));
+ assert(std::ranges::equal(original.begin() + k, original.begin() + n, begin, result.end()));
+ } else {
+ assert(result.end() == begin);
+ assert(std::ranges::equal(original.begin(), original.begin() + n, begin, end));
+ }
+ }
+ }
+ }
+
+ // n == 0
+ {
+ std::array<int, 3> input = {0, 1, 2};
+ const std::array<int, 3> expected = {0, 1, 2};
+
+ { // (iterator, sentinel) overload
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ auto result = std::ranges::shift_left(begin, end, 0);
+ assert(std::ranges::equal(expected, result));
+ assert(result.begin() == begin);
+ assert(result.end() == end);
+ }
+
+ { // (range) overload
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ auto range = std::ranges::subrange(begin, end);
+ auto result = std::ranges::shift_left(range, 0);
+ assert(std::ranges::equal(expected, result));
+ assert(result.begin() == begin);
+ assert(result.end() == end);
+ }
+ }
+
+ // n == len
+ {
+ std::array<int, 3> input = {0, 1, 2};
+ const std::array<int, 3> expected = {0, 1, 2};
+
+ { // (iterator, sentinel) overload
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ auto result = std::ranges::shift_left(begin, end, input.size());
+ assert(std::ranges::equal(expected, input));
+ assert(result.begin() == begin);
+ assert(result.end() == begin);
+ }
+
+ { // (range) overload
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ auto range = std::ranges::subrange(begin, end);
+ auto result = std::ranges::shift_left(range, input.size());
+ assert(std::ranges::equal(expected, input));
+ assert(result.begin() == begin);
+ assert(result.end() == begin);
+ }
+ }
+
+ // n > len
+ {
+ std::array<int, 3> input = {0, 1, 2};
+ const std::array<int, 3> expected = {0, 1, 2};
+
+ { // (iterator, sentinel) overload
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ auto result = std::ranges::shift_left(begin, end, input.size() + 1);
+ assert(std::ranges::equal(expected, input));
+ assert(result.begin() == begin);
+ assert(result.end() == begin);
+ }
+
+ { // (range) overload
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ auto range = std::ranges::subrange(begin, end);
+ auto result = std::ranges::shift_left(range, input.size() + 1);
+ assert(std::ranges::equal(expected, input));
+ assert(result.begin() == begin);
+ assert(result.end() == begin);
+ }
+ }
+}
+
+template <class Iter>
+constexpr void test_iter() {
+ test_iter_sent<Iter, Iter>();
+ test_iter_sent<Iter, sentinel_wrapper<Iter>>();
+ test_iter_sent<Iter, sized_sentinel<Iter>>();
+}
+
+constexpr bool test() {
+ test_iter<forward_iterator<int*>>();
+ test_iter<bidirectional_iterator<int*>>();
+ test_iter<random_access_iterator<int*>>();
+ test_iter<contiguous_iterator<int*>>();
+ test_iter<int*>();
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
>From 5ac12e0cb3eec5871bd7028091caa0ddbdb573ff Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Wed, 28 Feb 2024 19:37:13 -0800
Subject: [PATCH 10/33] [libc++][ranges] attempt to reduce the steps required
for 'constexpr' evaluation in 'ranges.shift_left.pass.cpp'
---
.../alg.shift/ranges.shift_left.pass.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
index 38ef57d3bca00..2e664e47a17e2 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
@@ -45,11 +45,11 @@ static_assert(!HasShiftLeftR<PermutableRangeNotSwappable>);
template <class Iter, class Sent>
constexpr void test_iter_sent() {
{
- const std::array<int, 15> original = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9};
- std::array<int, 15> scratch;
+ const std::array<int, 8> original = {3, 1, 4, 1, 5, 9, 2, 6};
+ std::array<int, 8> scratch;
// (iterator, sentinel) overload
- for (size_t n = 0; n <= 15; ++n) {
+ for (size_t n = 0; n <= original.size(); ++n) {
for (size_t k = 0; k <= n + 2; ++k) {
auto begin = Iter(scratch.data());
auto end = Sent(Iter(scratch.data() + n));
>From 25a64f69d9b52381d511ce5712abb5e94c78203f Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Wed, 28 Feb 2024 20:58:56 -0800
Subject: [PATCH 11/33] [libc++][ranges] fix an out-of-bounds access in
'ranges.shift_left.pass.cpp'
---
.../alg.shift/ranges.shift_left.pass.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
index 2e664e47a17e2..485a729fea50f 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
@@ -68,7 +68,7 @@ constexpr void test_iter_sent() {
}
// (range) overload
- for (size_t n = 0; n <= 15; ++n) {
+ for (size_t n = 0; n <= original.size(); ++n) {
for (size_t k = 0; k <= n + 2; ++k) {
auto begin = Iter(scratch.data());
auto end = Sent(Iter(scratch.data() + n));
>From 913b52fee2201baf33700f08e1078ca8e49b8c87 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Sun, 3 Mar 2024 17:29:39 -0800
Subject: [PATCH 12/33] [libc++][ranges] refactor '__shift_left' to avoid
redundant iteration
---
libcxx/include/__algorithm/shift_left.h | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/libcxx/include/__algorithm/shift_left.h b/libcxx/include/__algorithm/shift_left.h
index 8df1bff87f042..f609fc9cdc253 100644
--- a/libcxx/include/__algorithm/shift_left.h
+++ b/libcxx/include/__algorithm/shift_left.h
@@ -27,30 +27,30 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 20
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);
-
+_LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter>
+__shift_left(_Iter __first, _Sent __last, typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __n) {
if (__n == 0) {
+ _Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
return {std::move(__first), std::move(__end)};
}
_Iter __m = __first;
- if constexpr (__has_random_access_iterator_category<_Iter>::value) {
- if (__n >= __end - __first) {
+ if constexpr (sized_sentinel_for<_Sent, _Iter>) {
+ auto __size = _IterOps<_AlgPolicy>::distance(__first, __last);
+ if (__n >= __size) {
return {std::move(__first), std::move(__first)};
}
- __m += __n;
+ _IterOps<_AlgPolicy>::advance(__m, __n);
} else {
for (; __n > 0; --__n) {
- if (__m == __end) {
+ if (__m == __last) {
return {std::move(__first), std::move(__first)};
}
++__m;
}
}
- _Iter __result = std::__move<_AlgPolicy>(__m, __end, __first).second;
+ _Iter __result = std::__move<_AlgPolicy>(__m, __last, __first).second;
return {std::move(__first), std::move(__result)};
}
>From aaf062666e0ec9bcf6a79339cd75532b50d7138b Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Sun, 3 Mar 2024 17:32:42 -0800
Subject: [PATCH 13/33] [libc++][ranges] refactor 'ranges::shift_left'
---
.../include/__algorithm/ranges_shift_left.h | 19 +++++++------------
1 file changed, 7 insertions(+), 12 deletions(-)
diff --git a/libcxx/include/__algorithm/ranges_shift_left.h b/libcxx/include/__algorithm/ranges_shift_left.h
index d9edf661d1d8c..439d4955edfa6 100644
--- a/libcxx/include/__algorithm/ranges_shift_left.h
+++ b/libcxx/include/__algorithm/ranges_shift_left.h
@@ -36,24 +36,19 @@ 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) {
+ template <permutable _Iter, sentinel_for<_Iter> _Sent>
+ _LIBCPP_HIDE_FROM_ABI static constexpr subrange<_Iter>
+ operator()(_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>
requires permutable<iterator_t<_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));
+ _LIBCPP_HIDE_FROM_ABI static constexpr borrowed_subrange_t<_Range>
+ operator()(_Range&& __range, range_difference_t<_Range> __n) {
+ auto __ret = std::__shift_left<_RangeAlgPolicy>(ranges::begin(__range), ranges::end(__range), std::move(__n));
+ return {std::move(__ret.first), std::move(__ret.second)};
}
};
>From 256e74b94cd063749b2c57c4be5e3f121f57f86f Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Mon, 4 Mar 2024 22:09:03 -0800
Subject: [PATCH 14/33] [libc++][ranges] check the precondition in
'__shift_left'
---
libcxx/include/__algorithm/shift_left.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libcxx/include/__algorithm/shift_left.h b/libcxx/include/__algorithm/shift_left.h
index f609fc9cdc253..3e020052d0373 100644
--- a/libcxx/include/__algorithm/shift_left.h
+++ b/libcxx/include/__algorithm/shift_left.h
@@ -29,6 +29,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _AlgPolicy, class _Iter, class _Sent>
_LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter>
__shift_left(_Iter __first, _Sent __last, typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __n) {
+ _LIBCPP_ASSERT_UNCATEGORIZED(__n >= 0, "__n must be greater than or equal to 0");
+
if (__n == 0) {
_Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
return {std::move(__first), std::move(__end)};
>From a4ff077c0e73a6ab237de2ed8fb82135f205ccd4 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Mon, 4 Mar 2024 22:27:04 -0800
Subject: [PATCH 15/33] [libc++][ranges] refactor 'ranges.shift_left.pass.cpp'
---
.../alg.shift/ranges.shift_left.pass.cpp | 27 +++++++------------
1 file changed, 10 insertions(+), 17 deletions(-)
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
index 485a729fea50f..70a909736b841 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
@@ -19,6 +19,7 @@
#include <algorithm>
#include <array>
+#include <cassert>
#include <ranges>
#include <iterator>
@@ -26,16 +27,16 @@
#include "test_iterators.h"
#include "MoveOnly.h"
-template <class Iter, class Sent = Iter, class Count = std::size_t>
-concept HasShiftLeftIt = requires(Iter iter, Sent sent, Count n) { std::ranges::shift_left(iter, sent, n); };
+template <class Iter, class Sent = Iter>
+concept HasShiftLeftIt = requires(Iter iter, Sent sent, std::size_t n) { std::ranges::shift_left(iter, sent, n); };
static_assert(HasShiftLeftIt<int*>);
static_assert(!HasShiftLeftIt<ForwardIteratorNotDerivedFrom>);
static_assert(!HasShiftLeftIt<PermutableNotForwardIterator>);
static_assert(!HasShiftLeftIt<PermutableNotSwappable>);
-template <class Range, class Count = std::size_t>
-concept HasShiftLeftR = requires(Range range, Count n) { std::ranges::shift_left(range, n); };
+template <class Range>
+concept HasShiftLeftR = requires(Range range, std::size_t n) { std::ranges::shift_left(range, n); };
static_assert(HasShiftLeftR<UncheckedRange<int*>>);
static_assert(!HasShiftLeftR<ForwardRangeNotDerivedFrom>);
@@ -170,25 +171,17 @@ constexpr void test_iter_sent() {
}
}
-template <class Iter>
-constexpr void test_iter() {
- test_iter_sent<Iter, Iter>();
- test_iter_sent<Iter, sentinel_wrapper<Iter>>();
- test_iter_sent<Iter, sized_sentinel<Iter>>();
-}
-
constexpr bool test() {
- test_iter<forward_iterator<int*>>();
- test_iter<bidirectional_iterator<int*>>();
- test_iter<random_access_iterator<int*>>();
- test_iter<contiguous_iterator<int*>>();
- test_iter<int*>();
+ types::for_each(types::forward_iterator_list<int*>{}, []<class Iter> {
+ test_iter_sent<Iter, Iter>();
+ test_iter_sent<Iter, sentinel_wrapper<Iter>>();
+ test_iter_sent<Iter, sized_sentinel<Iter>>();
+ });
return true;
}
int main(int, char**) {
test();
static_assert(test());
-
return 0;
}
>From 7c851cfe65e499662a68a627132b39a980b4b47a Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Tue, 5 Mar 2024 18:46:22 -0800
Subject: [PATCH 16/33] [libc++][ranges] add missing headers to 'shift_left.h'
---
libcxx/include/__algorithm/shift_left.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/libcxx/include/__algorithm/shift_left.h b/libcxx/include/__algorithm/shift_left.h
index 3e020052d0373..cfab41e56c2c8 100644
--- a/libcxx/include/__algorithm/shift_left.h
+++ b/libcxx/include/__algorithm/shift_left.h
@@ -11,6 +11,7 @@
#include <__algorithm/iterator_operations.h>
#include <__algorithm/move.h>
+#include <__assert>
#include <__config>
#include <__iterator/iterator_traits.h>
#include <__utility/pair.h>
>From 76f2a4da73e40114d6818c8ba47595c472d2d6a7 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Tue, 5 Mar 2024 19:20:46 -0800
Subject: [PATCH 17/33] [libc++][ranges] add benchmark for 'ranges::shift_left'
---
.../algorithms/ranges_shift_left.bench.cpp | 67 +++++++++++++++++++
1 file changed, 67 insertions(+)
create mode 100644 libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp
diff --git a/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp b/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp
new file mode 100644
index 0000000000000..5998ed6a71471
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp
@@ -0,0 +1,67 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include <algorithm>
+#include <benchmark/benchmark.h>
+#include <iterator>
+#include <vector>
+
+#include "test_iterators.h"
+
+static void bm_shift_left_random_access_range_with_sized_sentinel(benchmark::State& state) {
+ std::vector<int> a(state.range(), 1);
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(a);
+
+ auto begin = random_access_iterator(a.data());
+ auto end = random_access_iterator(a.data() + a.size());
+
+ static_assert(std::sized_sentinel_for<decltype(end), decltype(begin)>);
+ static_assert(std::random_access_iterator<decltype(begin)>);
+
+ benchmark::DoNotOptimize(std::ranges::shift_left(begin, end, a.size() / 2));
+ }
+}
+BENCHMARK(bm_shift_left_random_access_range_with_sized_sentinel)->RangeMultiplier(16)->Range(16, 16 << 20);
+
+static void bm_shift_left_forward_range_with_sized_sentinel(benchmark::State& state) {
+ std::vector<int> a(state.range(), 1);
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(a);
+
+ auto begin = forward_iterator(a.data());
+ auto end = sized_sentinel(forward_iterator(a.data() + a.size()));
+
+ static_assert(std::sized_sentinel_for<decltype(end), decltype(begin)>);
+ static_assert(!std::random_access_iterator<decltype(begin)>);
+
+ benchmark::DoNotOptimize(std::ranges::shift_left(begin, end, a.size() / 2));
+ }
+}
+BENCHMARK(bm_shift_left_forward_range_with_sized_sentinel)->RangeMultiplier(16)->Range(16, 16 << 20);
+
+static void bm_shift_left_forward_range(benchmark::State& state) {
+ std::vector<int> a(state.range(), 1);
+
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(a);
+
+ auto begin = forward_iterator(a.data());
+ auto end = forward_iterator(a.data() + a.size());
+
+ static_assert(!std::sized_sentinel_for<decltype(end), decltype(begin)>);
+ static_assert(!std::random_access_iterator<decltype(begin)>);
+
+ benchmark::DoNotOptimize(std::ranges::shift_left(begin, end, a.size() / 2));
+ }
+}
+BENCHMARK(bm_shift_left_forward_range)->RangeMultiplier(16)->Range(16, 16 << 20);
+
+BENCHMARK_MAIN();
>From 3a3908f7b7b0b5f291ecb8affe82a3b3c8f28c04 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Tue, 5 Mar 2024 19:26:04 -0800
Subject: [PATCH 18/33] [libc++][ranges] add benchmark for 'ranges::shift_left'
---
.../test/benchmarks/algorithms/ranges_shift_left.bench.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp b/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp
index 5998ed6a71471..8f20f87f55866 100644
--- a/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp
+++ b/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp
@@ -22,8 +22,8 @@ static void bm_shift_left_random_access_range_with_sized_sentinel(benchmark::Sta
auto begin = random_access_iterator(a.data());
auto end = random_access_iterator(a.data() + a.size());
- static_assert(std::sized_sentinel_for<decltype(end), decltype(begin)>);
static_assert(std::random_access_iterator<decltype(begin)>);
+ static_assert(std::sized_sentinel_for<decltype(end), decltype(begin)>);
benchmark::DoNotOptimize(std::ranges::shift_left(begin, end, a.size() / 2));
}
@@ -39,8 +39,8 @@ static void bm_shift_left_forward_range_with_sized_sentinel(benchmark::State& st
auto begin = forward_iterator(a.data());
auto end = sized_sentinel(forward_iterator(a.data() + a.size()));
- static_assert(std::sized_sentinel_for<decltype(end), decltype(begin)>);
static_assert(!std::random_access_iterator<decltype(begin)>);
+ static_assert(std::sized_sentinel_for<decltype(end), decltype(begin)>);
benchmark::DoNotOptimize(std::ranges::shift_left(begin, end, a.size() / 2));
}
@@ -56,8 +56,8 @@ static void bm_shift_left_forward_range(benchmark::State& state) {
auto begin = forward_iterator(a.data());
auto end = forward_iterator(a.data() + a.size());
- static_assert(!std::sized_sentinel_for<decltype(end), decltype(begin)>);
static_assert(!std::random_access_iterator<decltype(begin)>);
+ static_assert(!std::sized_sentinel_for<decltype(end), decltype(begin)>);
benchmark::DoNotOptimize(std::ranges::shift_left(begin, end, a.size() / 2));
}
>From 2425e335c42732f57394f4e3c655069ca2326903 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Tue, 24 Dec 2024 13:02:07 -0500
Subject: [PATCH 19/33] [libc++][ranges] implement 'ranges::shift_left'
---
libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp b/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp
index 8f20f87f55866..9ba253c3dc15d 100644
--- a/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp
+++ b/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp
@@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
#include <algorithm>
#include <benchmark/benchmark.h>
#include <iterator>
>From a4d89ec6149aea89ae26c7c709c4fd830396abf7 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Tue, 24 Dec 2024 13:11:17 -0500
Subject: [PATCH 20/33] [libc++][ranges] implement 'ranges::shift_left'
---
libcxx/include/__algorithm/ranges_shift_left.h | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/libcxx/include/__algorithm/ranges_shift_left.h b/libcxx/include/__algorithm/ranges_shift_left.h
index 439d4955edfa6..7d494820cc412 100644
--- a/libcxx/include/__algorithm/ranges_shift_left.h
+++ b/libcxx/include/__algorithm/ranges_shift_left.h
@@ -13,13 +13,13 @@
#include <__algorithm/shift_left.h>
#include <__config>
#include <__iterator/concepts.h>
+#include <__iterator/distance.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
@@ -28,10 +28,10 @@
_LIBCPP_PUSH_MACROS
#include <__undef_macros>
-#if _LIBCPP_STD_VER >= 23
-
_LIBCPP_BEGIN_NAMESPACE_STD
+#if _LIBCPP_STD_VER >= 23
+
namespace ranges {
namespace __shift_left {
@@ -47,11 +47,16 @@ struct __fn {
requires permutable<iterator_t<_Range>>
_LIBCPP_HIDE_FROM_ABI static constexpr borrowed_subrange_t<_Range>
operator()(_Range&& __range, range_difference_t<_Range> __n) {
+ if constexpr (sized_range<_Range>) {
+ if (__n >= ranges::distance(__range)) {
+ return {ranges::begin(__range), ranges::begin(__range)};
+ }
+ }
+
auto __ret = std::__shift_left<_RangeAlgPolicy>(ranges::begin(__range), ranges::end(__range), std::move(__n));
return {std::move(__ret.first), std::move(__ret.second)};
}
};
-
} // namespace __shift_left
inline namespace __cpo {
@@ -59,10 +64,10 @@ inline constexpr auto shift_left = __shift_left::__fn{};
} // namespace __cpo
} // namespace ranges
-_LIBCPP_END_NAMESPACE_STD
-
#endif // _LIBCPP_STD_VER >= 23
+_LIBCPP_END_NAMESPACE_STD
+
_LIBCPP_POP_MACROS
#endif // _LIBCPP___ALGORITHM_RANGES_SHIFT_LEFT_H
>From cc72045fc6a21c9bcabbe71b7a59e2c8801744e6 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Tue, 24 Dec 2024 13:24:52 -0500
Subject: [PATCH 21/33] [libc++][ranges] implement 'ranges::shift_left'
---
.../alg.shift/ranges.shift_left.pass.cpp | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
index 70a909736b841..4dfacc6f11dc3 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
@@ -27,22 +27,31 @@
#include "test_iterators.h"
#include "MoveOnly.h"
-template <class Iter, class Sent = Iter>
-concept HasShiftLeftIt = requires(Iter iter, Sent sent, std::size_t n) { std::ranges::shift_left(iter, sent, n); };
+struct InvalidDifferenceT {};
+
+template <class Iter, class Sent = Iter, class N = std::iter_difference_t<Iter>>
+concept HasShiftLeftIt = requires(Iter iter, Sent sent, N n) { std::ranges::shift_left(iter, sent, n); };
static_assert(HasShiftLeftIt<int*>);
+static_assert(HasShiftLeftIt<int*, sentinel_wrapper<int*>>);
+static_assert(HasShiftLeftIt<int*, sized_sentinel<int*>>);
+
static_assert(!HasShiftLeftIt<ForwardIteratorNotDerivedFrom>);
static_assert(!HasShiftLeftIt<PermutableNotForwardIterator>);
static_assert(!HasShiftLeftIt<PermutableNotSwappable>);
-template <class Range>
-concept HasShiftLeftR = requires(Range range, std::size_t n) { std::ranges::shift_left(range, n); };
+static_assert(!HasShiftLeftIt<int*, int*, InvalidDifferenceT>);
+
+template <class Range, class N = std::ranges::range_difference_t<Range>>
+concept HasShiftLeftR = requires(Range range, N n) { std::ranges::shift_left(range, n); };
static_assert(HasShiftLeftR<UncheckedRange<int*>>);
static_assert(!HasShiftLeftR<ForwardRangeNotDerivedFrom>);
static_assert(!HasShiftLeftR<PermutableRangeNotForwardIterator>);
static_assert(!HasShiftLeftR<PermutableRangeNotSwappable>);
+static_assert(!HasShiftLeftR<UncheckedRange<int*>, InvalidDifferenceT>);
+
template <class Iter, class Sent>
constexpr void test_iter_sent() {
{
>From 063924a7f854cfbcfb0279129aed7b69d7ef1ffa Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Tue, 24 Dec 2024 13:31:11 -0500
Subject: [PATCH 22/33] [libc++][ranges] implement 'ranges::shift_left'
---
.../alg.shift/ranges.shift_left.pass.cpp | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
index 4dfacc6f11dc3..1bd8f37551b55 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
@@ -35,23 +35,22 @@ concept HasShiftLeftIt = requires(Iter iter, Sent sent, N n) { std::ranges::shif
static_assert(HasShiftLeftIt<int*>);
static_assert(HasShiftLeftIt<int*, sentinel_wrapper<int*>>);
static_assert(HasShiftLeftIt<int*, sized_sentinel<int*>>);
+static_assert(!HasShiftLeftIt<int*, int*, InvalidDifferenceT>);
static_assert(!HasShiftLeftIt<ForwardIteratorNotDerivedFrom>);
static_assert(!HasShiftLeftIt<PermutableNotForwardIterator>);
static_assert(!HasShiftLeftIt<PermutableNotSwappable>);
-static_assert(!HasShiftLeftIt<int*, int*, InvalidDifferenceT>);
-
template <class Range, class N = std::ranges::range_difference_t<Range>>
concept HasShiftLeftR = requires(Range range, N n) { std::ranges::shift_left(range, n); };
static_assert(HasShiftLeftR<UncheckedRange<int*>>);
+static_assert(!HasShiftLeftR<UncheckedRange<int*>, InvalidDifferenceT>);
+
static_assert(!HasShiftLeftR<ForwardRangeNotDerivedFrom>);
static_assert(!HasShiftLeftR<PermutableRangeNotForwardIterator>);
static_assert(!HasShiftLeftR<PermutableRangeNotSwappable>);
-static_assert(!HasShiftLeftR<UncheckedRange<int*>, InvalidDifferenceT>);
-
template <class Iter, class Sent>
constexpr void test_iter_sent() {
{
>From 18a406e2849fe0810c32437b2e732df53fed1ae1 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Tue, 24 Dec 2024 14:11:56 -0500
Subject: [PATCH 23/33] [libc++][ranges] implement 'ranges::shift_left'
---
libcxx/include/__algorithm/shift_left.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libcxx/include/__algorithm/shift_left.h b/libcxx/include/__algorithm/shift_left.h
index cfab41e56c2c8..5aafc53db75b4 100644
--- a/libcxx/include/__algorithm/shift_left.h
+++ b/libcxx/include/__algorithm/shift_left.h
@@ -13,7 +13,9 @@
#include <__algorithm/move.h>
#include <__assert>
#include <__config>
+#include <__iterator/concepts.h>
#include <__iterator/iterator_traits.h>
+#include <__utility/move.h>
#include <__utility/pair.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
>From 2871657ac2373820d79a21ff28624b03a62a9e0d Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Tue, 24 Dec 2024 20:12:18 -0500
Subject: [PATCH 24/33] [libc++][ranges] implement 'ranges::shift_left'
---
.../ranges_robust_against_proxy_iterators.pass.cpp | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
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 4d4651e64fbf4..a442743333ea8 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
@@ -165,9 +165,6 @@ 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()) {
@@ -194,7 +191,9 @@ constexpr void run_tests() {
test(std::ranges::sort_heap, in);
test(std::ranges::prev_permutation, in);
test(std::ranges::next_permutation, in);
-
+#if TEST_STD_VER >= 23
+ test(std::ranges::shift_left, in, count);
+#endif
// The algorithms that work on uninitialized memory have constraints that prevent proxy iterators from being used with
// them.
}
>From 74ab3bf7f51745eb129d93f8ac2de201768c8dab Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 18 Jan 2026 10:50:47 +0000
Subject: [PATCH 25/33] address some comments
---
libcxx/include/algorithm | 2 +-
.../alg.shift/ranges.shift_left.pass.cpp | 96 +++++++++++++------
.../ranges_robust_against_dangling.pass.cpp | 1 +
3 files changed, 67 insertions(+), 32 deletions(-)
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index baa682e95b185..5dd7b9dc990e1 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -885,7 +885,7 @@ namespace ranges {
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
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
index 1bd8f37551b55..0c7599a6e03a3 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
@@ -22,6 +22,7 @@
#include <cassert>
#include <ranges>
#include <iterator>
+#include <vector>
#include "almost_satisfies_types.h"
#include "test_iterators.h"
@@ -36,6 +37,7 @@ static_assert(HasShiftLeftIt<int*>);
static_assert(HasShiftLeftIt<int*, sentinel_wrapper<int*>>);
static_assert(HasShiftLeftIt<int*, sized_sentinel<int*>>);
static_assert(!HasShiftLeftIt<int*, int*, InvalidDifferenceT>);
+static_assert(!HasShiftLeftIt<int*, int, int>);
static_assert(!HasShiftLeftIt<ForwardIteratorNotDerivedFrom>);
static_assert(!HasShiftLeftIt<PermutableNotForwardIterator>);
@@ -55,15 +57,15 @@ template <class Iter, class Sent>
constexpr void test_iter_sent() {
{
const std::array<int, 8> original = {3, 1, 4, 1, 5, 9, 2, 6};
- std::array<int, 8> scratch;
// (iterator, sentinel) overload
for (size_t n = 0; n <= original.size(); ++n) {
for (size_t k = 0; k <= n + 2; ++k) {
+ std::array<int, 8> scratch;
auto begin = Iter(scratch.data());
auto end = Sent(Iter(scratch.data() + n));
std::ranges::copy(original.begin(), original.begin() + n, begin);
- auto result = std::ranges::shift_left(begin, end, k);
+ std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::shift_left(begin, end, k);
assert(result.begin() == begin);
if (k < n) {
@@ -79,11 +81,12 @@ constexpr void test_iter_sent() {
// (range) overload
for (size_t n = 0; n <= original.size(); ++n) {
for (size_t k = 0; k <= n + 2; ++k) {
+ std::array<int, 8> scratch;
auto begin = Iter(scratch.data());
auto end = Sent(Iter(scratch.data() + n));
std::ranges::copy(original.begin(), original.begin() + n, begin);
- auto range = std::ranges::subrange(begin, end);
- auto result = std::ranges::shift_left(range, k);
+ auto range = std::ranges::subrange(begin, end);
+ std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::shift_left(range, k);
assert(result.begin() == begin);
if (k < n) {
@@ -103,21 +106,21 @@ constexpr void test_iter_sent() {
const std::array<int, 3> expected = {0, 1, 2};
{ // (iterator, sentinel) overload
- auto in = input;
- auto begin = Iter(in.data());
- auto end = Sent(Iter(in.data() + in.size()));
- auto result = std::ranges::shift_left(begin, end, 0);
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::shift_left(begin, end, 0);
assert(std::ranges::equal(expected, result));
assert(result.begin() == begin);
assert(result.end() == end);
}
{ // (range) overload
- auto in = input;
- auto begin = Iter(in.data());
- auto end = Sent(Iter(in.data() + in.size()));
- auto range = std::ranges::subrange(begin, end);
- auto result = std::ranges::shift_left(range, 0);
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ auto range = std::ranges::subrange(begin, end);
+ std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::shift_left(range, 0);
assert(std::ranges::equal(expected, result));
assert(result.begin() == begin);
assert(result.end() == end);
@@ -130,21 +133,22 @@ constexpr void test_iter_sent() {
const std::array<int, 3> expected = {0, 1, 2};
{ // (iterator, sentinel) overload
- auto in = input;
- auto begin = Iter(in.data());
- auto end = Sent(Iter(in.data() + in.size()));
- auto result = std::ranges::shift_left(begin, end, input.size());
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ std::same_as<std::ranges::subrange<Iter>> decltype(auto) result =
+ std::ranges::shift_left(begin, end, input.size());
assert(std::ranges::equal(expected, input));
assert(result.begin() == begin);
assert(result.end() == begin);
}
{ // (range) overload
- auto in = input;
- auto begin = Iter(in.data());
- auto end = Sent(Iter(in.data() + in.size()));
- auto range = std::ranges::subrange(begin, end);
- auto result = std::ranges::shift_left(range, input.size());
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ auto range = std::ranges::subrange(begin, end);
+ std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::shift_left(range, input.size());
assert(std::ranges::equal(expected, input));
assert(result.begin() == begin);
assert(result.end() == begin);
@@ -157,21 +161,51 @@ constexpr void test_iter_sent() {
const std::array<int, 3> expected = {0, 1, 2};
{ // (iterator, sentinel) overload
- auto in = input;
- auto begin = Iter(in.data());
- auto end = Sent(Iter(in.data() + in.size()));
- auto result = std::ranges::shift_left(begin, end, input.size() + 1);
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ std::same_as<std::ranges::subrange<Iter>> decltype(auto) result =
+ std::ranges::shift_left(begin, end, input.size() + 1);
assert(std::ranges::equal(expected, input));
assert(result.begin() == begin);
assert(result.end() == begin);
}
{ // (range) overload
- auto in = input;
- auto begin = Iter(in.data());
- auto end = Sent(Iter(in.data() + in.size()));
- auto range = std::ranges::subrange(begin, end);
- auto result = std::ranges::shift_left(range, input.size() + 1);
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ auto range = std::ranges::subrange(begin, end);
+ std::same_as<std::ranges::subrange<Iter>> decltype(auto) result =
+ std::ranges::shift_left(range, input.size() + 1);
+ assert(std::ranges::equal(expected, input));
+ assert(result.begin() == begin);
+ assert(result.end() == begin);
+ }
+ }
+
+ // empty range
+ {
+ std::vector<int> input = {};
+ const std::vector<int> expected = {};
+ { // (iterator, sentinel) overload
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ std::same_as<std::ranges::subrange<Iter>> decltype(auto) result =
+ std::ranges::shift_left(begin, end, input.size() + 1);
+ assert(std::ranges::equal(expected, input));
+ assert(result.begin() == begin);
+ assert(result.end() == begin);
+ }
+
+ { // (range) overload
+ auto in = input;
+ auto begin = Iter(in.data());
+ auto end = Sent(Iter(in.data() + in.size()));
+ auto range = std::ranges::subrange(begin, end);
+ std::same_as<std::ranges::subrange<Iter>> decltype(auto) result =
+ std::ranges::shift_left(range, input.size() + 1);
assert(std::ranges::equal(expected, input));
assert(result.begin() == begin);
assert(result.end() == begin);
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
index e0a6aaaf03aa0..17037acfe7184 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
@@ -224,6 +224,7 @@ constexpr bool test_all() {
#if TEST_STD_VER >= 23
dangling_1st<out_value_result<dangling, decltype(x)>>(std::ranges::iota, in, x);
+ dangling_1st(std::ranges::shift_left, in, x);
#endif
return true;
>From fe641fc89ea2831e6bc1c7315eb6a4b2d3ca90f0 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 18 Jan 2026 10:50:58 +0000
Subject: [PATCH 26/33] fix double move
---
libcxx/include/__algorithm/shift_left.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__algorithm/shift_left.h b/libcxx/include/__algorithm/shift_left.h
index 5aafc53db75b4..47eb12e64aa3f 100644
--- a/libcxx/include/__algorithm/shift_left.h
+++ b/libcxx/include/__algorithm/shift_left.h
@@ -43,13 +43,13 @@ __shift_left(_Iter __first, _Sent __last, typename _IterOps<_AlgPolicy>::templat
if constexpr (sized_sentinel_for<_Sent, _Iter>) {
auto __size = _IterOps<_AlgPolicy>::distance(__first, __last);
if (__n >= __size) {
- return {std::move(__first), std::move(__first)};
+ return {__first, std::move(__first)};
}
_IterOps<_AlgPolicy>::advance(__m, __n);
} else {
for (; __n > 0; --__n) {
if (__m == __last) {
- return {std::move(__first), std::move(__first)};
+ return {__first, std::move(__first)};
}
++__m;
}
>From e10532c16e4f4f97b96b9e60814a353bcf6c3e71 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 18 Jan 2026 10:54:30 +0000
Subject: [PATCH 27/33] release notes
---
libcxx/docs/ReleaseNotes/23.rst | 2 ++
libcxx/docs/Status/Cxx23Papers.csv | 2 +-
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/libcxx/docs/ReleaseNotes/23.rst b/libcxx/docs/ReleaseNotes/23.rst
index 73f5984768592..73a85343df8b3 100644
--- a/libcxx/docs/ReleaseNotes/23.rst
+++ b/libcxx/docs/ReleaseNotes/23.rst
@@ -38,6 +38,8 @@ What's New in Libc++ 23.0.0?
Implemented Papers
------------------
+- P2440R1: ``ranges::iota``, ``ranges::shift_left`` and ``ranges::shift_right`` (`Github <https://github.com/llvm/llvm-project/issues/105184>`__) (``ranges::shift_left`` is implemented in this release)
+
Improvements and New Features
-----------------------------
diff --git a/libcxx/docs/Status/Cxx23Papers.csv b/libcxx/docs/Status/Cxx23Papers.csv
index e4c6cb0f51065..b0a87319a0e95 100644
--- a/libcxx/docs/Status/Cxx23Papers.csv
+++ b/libcxx/docs/Status/Cxx23Papers.csv
@@ -46,7 +46,7 @@
"`P2255R2 <https://wg21.link/P2255R2>`__","A type trait to detect reference binding to temporary","2022-02 (Virtual)","|Partial|","","`#105180 <https://github.com/llvm/llvm-project/issues/105180>`__","Implemented the type traits only."
"`P2273R3 <https://wg21.link/P2273R3>`__","Making ``std::unique_ptr`` constexpr","2022-02 (Virtual)","|Complete|","16","`#105182 <https://github.com/llvm/llvm-project/issues/105182>`__",""
"`P2387R3 <https://wg21.link/P2387R3>`__","Pipe support for user-defined range adaptors","2022-02 (Virtual)","|Complete|","19","`#105183 <https://github.com/llvm/llvm-project/issues/105183>`__",""
-"`P2440R1 <https://wg21.link/P2440R1>`__","``ranges::iota``, ``ranges::shift_left`` and ``ranges::shift_right``","2022-02 (Virtual)","|Partial|","","`#105184 <https://github.com/llvm/llvm-project/issues/105184>`__","Only ``ranges::iota`` is implemented."
+"`P2440R1 <https://wg21.link/P2440R1>`__","``ranges::iota``, ``ranges::shift_left`` and ``ranges::shift_right``","2022-02 (Virtual)","|Partial|","","`#105184 <https://github.com/llvm/llvm-project/issues/105184>`__","``ranges::iota`` and ``ranges::shift_left`` are implemented."
"`P2441R2 <https://wg21.link/P2441R2>`__","``views::join_with``","2022-02 (Virtual)","|Complete|","21","`#105185 <https://github.com/llvm/llvm-project/issues/105185>`__",""
"`P2442R1 <https://wg21.link/P2442R1>`__","Windowing range adaptors: ``views::chunk`` and ``views::slide``","2022-02 (Virtual)","","","`#105187 <https://github.com/llvm/llvm-project/issues/105187>`__",""
"`P2443R1 <https://wg21.link/P2443R1>`__","``views::chunk_by``","2022-02 (Virtual)","|Complete|","18","`#105188 <https://github.com/llvm/llvm-project/issues/105188>`__",""
>From 567d4eb9cfcc6207e98fe45dcc24caec49a40ab6 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 18 Jan 2026 12:29:27 +0000
Subject: [PATCH 28/33] constexpr steps test
---
.../alg.shift/ranges.shift_left.pass.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
index 0c7599a6e03a3..2902d817cd285 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
@@ -9,6 +9,7 @@
// <algorithm>
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=9000000
// template<permutable I, sentinel_for<I> S>
// constexpr subrange<I> ranges::shift_left(I first, S last, iter_difference_t<I> n);
>From caceb16d542849c7cd3da158ade73bea2edde46a Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 18 Jan 2026 14:37:36 +0000
Subject: [PATCH 29/33] more tests
---
.../alg.shift/ranges.shift_left.pass.cpp | 141 ++++++++++++++++++
1 file changed, 141 insertions(+)
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
index 2902d817cd285..9639fc529c2a0 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
@@ -54,6 +54,95 @@ static_assert(!HasShiftLeftR<ForwardRangeNotDerivedFrom>);
static_assert(!HasShiftLeftR<PermutableRangeNotForwardIterator>);
static_assert(!HasShiftLeftR<PermutableRangeNotSwappable>);
+// An iterator whose iterator_traits::difference_type is not the same as
+// std::iter_difference_t<It>.
+struct DiffTypeIter {
+ int* it_;
+
+ using difference_type = InvalidDifferenceT;
+ using value_type = int;
+ using reference = int&;
+ using pointer = int*;
+ using iterator_category = std::random_access_iterator_tag;
+
+ constexpr DiffTypeIter() : it_() {}
+ constexpr explicit DiffTypeIter(int* it) : it_(it) {}
+
+ constexpr reference operator*() const { return *it_; }
+ constexpr reference operator[](int n) const { return it_[n]; }
+
+ constexpr DiffTypeIter& operator++() {
+ ++it_;
+ return *this;
+ }
+ constexpr DiffTypeIter& operator--() {
+ --it_;
+ return *this;
+ }
+ constexpr DiffTypeIter operator++(int) { return DiffTypeIter(it_++); }
+ constexpr DiffTypeIter operator--(int) { return DiffTypeIter(it_--); }
+
+ constexpr DiffTypeIter& operator+=(int n) {
+ it_ += n;
+ return *this;
+ }
+ constexpr DiffTypeIter& operator-=(int n) {
+ it_ -= n;
+ return *this;
+ }
+ friend constexpr DiffTypeIter operator+(DiffTypeIter x, int n) {
+ x += n;
+ return x;
+ }
+ friend constexpr DiffTypeIter operator+(int n, DiffTypeIter x) {
+ x += n;
+ return x;
+ }
+ friend constexpr DiffTypeIter operator-(DiffTypeIter x, int n) {
+ x -= n;
+ return x;
+ }
+ friend constexpr int operator-(DiffTypeIter x, DiffTypeIter y) { return x.it_ - y.it_; }
+
+ friend constexpr bool operator==(const DiffTypeIter& x, const DiffTypeIter& y) { return x.it_ == y.it_; }
+ friend constexpr bool operator!=(const DiffTypeIter& x, const DiffTypeIter& y) { return x.it_ != y.it_; }
+ friend constexpr bool operator<(const DiffTypeIter& x, const DiffTypeIter& y) { return x.it_ < y.it_; }
+ friend constexpr bool operator<=(const DiffTypeIter& x, const DiffTypeIter& y) { return x.it_ <= y.it_; }
+ friend constexpr bool operator>(const DiffTypeIter& x, const DiffTypeIter& y) { return x.it_ > y.it_; }
+ friend constexpr bool operator>=(const DiffTypeIter& x, const DiffTypeIter& y) { return x.it_ >= y.it_; }
+};
+
+template <>
+struct std::incrementable_traits<DiffTypeIter> {
+ using difference_type = std::ptrdiff_t;
+};
+
+struct TrackCopyMove {
+ mutable int copy_count = 0;
+ int move_count = 0;
+
+ constexpr TrackCopyMove() = default;
+ constexpr TrackCopyMove(const TrackCopyMove& other) : copy_count(other.copy_count), move_count(other.move_count) {
+ ++copy_count;
+ ++other.copy_count;
+ }
+
+ constexpr TrackCopyMove(TrackCopyMove&& other) noexcept : copy_count(other.copy_count), move_count(other.move_count) {
+ ++move_count;
+ ++other.move_count;
+ }
+ constexpr TrackCopyMove& operator=(const TrackCopyMove& other) {
+ ++copy_count;
+ ++other.copy_count;
+ return *this;
+ }
+ constexpr TrackCopyMove& operator=(TrackCopyMove&& other) noexcept {
+ ++move_count;
+ ++other.move_count;
+ return *this;
+ }
+};
+
template <class Iter, class Sent>
constexpr void test_iter_sent() {
{
@@ -220,6 +309,58 @@ constexpr bool test() {
test_iter_sent<Iter, sentinel_wrapper<Iter>>();
test_iter_sent<Iter, sized_sentinel<Iter>>();
});
+ test_iter_sent<DiffTypeIter, DiffTypeIter>();
+
+ // Complexity: At most (last - first) - n assignments
+ {
+ constexpr int length = 100;
+ constexpr int n = length / 2;
+ auto make_vec = []() {
+ std::vector<TrackCopyMove> vec;
+ vec.reserve(length);
+ for (int i = 0; i < length; ++i) {
+ vec.emplace_back();
+ }
+ return vec;
+ };
+
+ { // (iterator, sentinel) overload
+ auto input = make_vec();
+ auto result = std::ranges::shift_left(input.begin(), input.end(), n);
+ assert(result.begin() == input.begin());
+ assert(result.end() == input.begin() + (length - n));
+
+ auto total_copies = 0;
+ auto total_moves = 0;
+ for (auto it = result.begin(); it != result.end(); ++it) {
+ const auto& item = *it;
+ total_copies += item.copy_count;
+ total_moves += item.move_count;
+ }
+
+ assert(total_copies == 0);
+ assert(total_moves <= length - n);
+ }
+
+ { // (range) overload
+ auto input = make_vec();
+ auto result = std::ranges::shift_left(input, n);
+ assert(result.begin() == input.begin());
+ assert(result.end() == input.begin() + (length - n));
+
+ auto total_copies = 0;
+ auto total_moves = 0;
+ for (auto it = result.begin(); it != result.end(); ++it) {
+ const auto& item = *it;
+ total_copies += item.copy_count;
+ total_moves += item.move_count;
+ }
+
+ assert(total_copies == 0);
+ assert(total_moves <= length - n);
+ }
+ }
+
return true;
}
>From b177b84ff1dd238bbd8ccccff915fc2369f23ac4 Mon Sep 17 00:00:00 2001
From: Hui <hui.xie1990 at gmail.com>
Date: Sat, 24 Jan 2026 14:55:40 +0000
Subject: [PATCH 30/33] Update libcxx/docs/ReleaseNotes/23.rst
Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>
---
libcxx/docs/ReleaseNotes/23.rst | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/libcxx/docs/ReleaseNotes/23.rst b/libcxx/docs/ReleaseNotes/23.rst
index 73a85343df8b3..ce6811ce911a3 100644
--- a/libcxx/docs/ReleaseNotes/23.rst
+++ b/libcxx/docs/ReleaseNotes/23.rst
@@ -38,8 +38,7 @@ What's New in Libc++ 23.0.0?
Implemented Papers
------------------
-- P2440R1: ``ranges::iota``, ``ranges::shift_left`` and ``ranges::shift_right`` (`Github <https://github.com/llvm/llvm-project/issues/105184>`__) (``ranges::shift_left`` is implemented in this release)
-
+- P2440R1 (partial): ``ranges::iota`` and ``ranges::shift_left`` are supported
Improvements and New Features
-----------------------------
>From 8f614ffc8fc242a946abb1be1aa151c944b4bc95 Mon Sep 17 00:00:00 2001
From: Hui <hui.xie1990 at gmail.com>
Date: Sat, 24 Jan 2026 14:56:01 +0000
Subject: [PATCH 31/33] Update libcxx/include/__algorithm/shift_left.h
Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>
---
libcxx/include/__algorithm/shift_left.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__algorithm/shift_left.h b/libcxx/include/__algorithm/shift_left.h
index 47eb12e64aa3f..d2335394c08fc 100644
--- a/libcxx/include/__algorithm/shift_left.h
+++ b/libcxx/include/__algorithm/shift_left.h
@@ -32,7 +32,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _AlgPolicy, class _Iter, class _Sent>
_LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter>
__shift_left(_Iter __first, _Sent __last, typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __n) {
- _LIBCPP_ASSERT_UNCATEGORIZED(__n >= 0, "__n must be greater than or equal to 0");
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n >= 0, "__n must be greater than or equal to 0");
if (__n == 0) {
_Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
>From e21972c559140f8b3dd4219657a0e0cae4e5e517 Mon Sep 17 00:00:00 2001
From: Hui <hui.xie1990 at gmail.com>
Date: Sat, 24 Jan 2026 14:56:11 +0000
Subject: [PATCH 32/33] Update libcxx/include/__algorithm/shift_left.h
Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>
---
libcxx/include/__algorithm/shift_left.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__algorithm/shift_left.h b/libcxx/include/__algorithm/shift_left.h
index d2335394c08fc..bd3f554bef2c1 100644
--- a/libcxx/include/__algorithm/shift_left.h
+++ b/libcxx/include/__algorithm/shift_left.h
@@ -60,7 +60,7 @@ __shift_left(_Iter __first, _Sent __last, typename _IterOps<_AlgPolicy>::templat
}
template <class _ForwardIterator>
-inline _LIBCPP_HIDE_FROM_ABI constexpr _ForwardIterator
+_LIBCPP_HIDE_FROM_ABI constexpr _ForwardIterator
shift_left(_ForwardIterator __first,
_ForwardIterator __last,
typename iterator_traits<_ForwardIterator>::difference_type __n) {
>From e66d5ed632ba371201eefa1a6c693e249342e4d6 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sat, 24 Jan 2026 15:13:12 +0000
Subject: [PATCH 33/33] review
---
libcxx/include/__algorithm/shift_left.h | 2 +-
.../algorithms/ranges_shift_left.bench.cpp | 69 -------------------
.../alg.shift/assert.shift_left.pass.cpp | 28 ++++++++
3 files changed, 29 insertions(+), 70 deletions(-)
delete mode 100644 libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp
create mode 100644 libcxx/test/libcxx/algorithms/alg.modifying.operations/alg.shift/assert.shift_left.pass.cpp
diff --git a/libcxx/include/__algorithm/shift_left.h b/libcxx/include/__algorithm/shift_left.h
index bd3f554bef2c1..42d6c2321ff7d 100644
--- a/libcxx/include/__algorithm/shift_left.h
+++ b/libcxx/include/__algorithm/shift_left.h
@@ -32,7 +32,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _AlgPolicy, class _Iter, class _Sent>
_LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter>
__shift_left(_Iter __first, _Sent __last, typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __n) {
- _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n >= 0, "__n must be greater than or equal to 0");
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n >= 0, "n must be greater than or equal to 0");
if (__n == 0) {
_Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
diff --git a/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp b/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp
deleted file mode 100644
index 9ba253c3dc15d..0000000000000
--- a/libcxx/test/benchmarks/algorithms/ranges_shift_left.bench.cpp
+++ /dev/null
@@ -1,69 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-#include <algorithm>
-#include <benchmark/benchmark.h>
-#include <iterator>
-#include <vector>
-
-#include "test_iterators.h"
-
-static void bm_shift_left_random_access_range_with_sized_sentinel(benchmark::State& state) {
- std::vector<int> a(state.range(), 1);
-
- for (auto _ : state) {
- benchmark::DoNotOptimize(a);
-
- auto begin = random_access_iterator(a.data());
- auto end = random_access_iterator(a.data() + a.size());
-
- static_assert(std::random_access_iterator<decltype(begin)>);
- static_assert(std::sized_sentinel_for<decltype(end), decltype(begin)>);
-
- benchmark::DoNotOptimize(std::ranges::shift_left(begin, end, a.size() / 2));
- }
-}
-BENCHMARK(bm_shift_left_random_access_range_with_sized_sentinel)->RangeMultiplier(16)->Range(16, 16 << 20);
-
-static void bm_shift_left_forward_range_with_sized_sentinel(benchmark::State& state) {
- std::vector<int> a(state.range(), 1);
-
- for (auto _ : state) {
- benchmark::DoNotOptimize(a);
-
- auto begin = forward_iterator(a.data());
- auto end = sized_sentinel(forward_iterator(a.data() + a.size()));
-
- static_assert(!std::random_access_iterator<decltype(begin)>);
- static_assert(std::sized_sentinel_for<decltype(end), decltype(begin)>);
-
- benchmark::DoNotOptimize(std::ranges::shift_left(begin, end, a.size() / 2));
- }
-}
-BENCHMARK(bm_shift_left_forward_range_with_sized_sentinel)->RangeMultiplier(16)->Range(16, 16 << 20);
-
-static void bm_shift_left_forward_range(benchmark::State& state) {
- std::vector<int> a(state.range(), 1);
-
- for (auto _ : state) {
- benchmark::DoNotOptimize(a);
-
- auto begin = forward_iterator(a.data());
- auto end = forward_iterator(a.data() + a.size());
-
- static_assert(!std::random_access_iterator<decltype(begin)>);
- static_assert(!std::sized_sentinel_for<decltype(end), decltype(begin)>);
-
- benchmark::DoNotOptimize(std::ranges::shift_left(begin, end, a.size() / 2));
- }
-}
-BENCHMARK(bm_shift_left_forward_range)->RangeMultiplier(16)->Range(16, 16 << 20);
-
-BENCHMARK_MAIN();
diff --git a/libcxx/test/libcxx/algorithms/alg.modifying.operations/alg.shift/assert.shift_left.pass.cpp b/libcxx/test/libcxx/algorithms/alg.modifying.operations/alg.shift/assert.shift_left.pass.cpp
new file mode 100644
index 0000000000000..0d29ebc1b3390
--- /dev/null
+++ b/libcxx/test/libcxx/algorithms/alg.modifying.operations/alg.shift/assert.shift_left.pass.cpp
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// UNSUPPORTED: libcpp-hardening-mode=none
+// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
+
+#include <algorithm>
+#include <array>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ std::array<int, 5> arr = {1, 2, 3, 4, 5};
+ TEST_LIBCPP_ASSERT_FAILURE(std::ranges::shift_left(arr, -2), "n must be greater than or equal to 0");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ std::ranges::shift_left(arr.begin(), arr.end(), -2), "n must be greater than or equal to 0");
+
+ return 0;
+}
More information about the libcxx-commits
mailing list