[libcxx-commits] [libcxx] [libc++][ranges] implement `ranges::shift_left` (PR #83231)

Xiaoyang Liu via libcxx-commits libcxx-commits at lists.llvm.org
Sun Mar 3 17:43:03 PST 2024


https://github.com/xiaoyang-sde updated https://github.com/llvm/llvm-project/pull/83231

>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 01/14] [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));

>From 68a86357dee32a6958fa1e8e89940c7ca354277b 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/14] [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 c64945e34994d3..92b224e427ab3b 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
@@ -155,7 +155,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 ade17074240be5093dd83bc53b277e505152f213 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/14] [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 0a4e1c1ba6cc96..2364899c1e0315 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -353,9 +353,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 c84792977c9ed053915e27097e0c7f9d03d99d2b 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/14] [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 bf82ce32693824..4595fc7d7031ec 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 e6b8153ecebec8..01d9f1c035f36d 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 40691ad54b3dc1d30b1edf93138c7adfaa2ab8cc 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/14] [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 01d9f1c035f36d..8df1bff87f0427 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 4de17871d76a7a390f127962ed6036ea233496d7 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/14] [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 4595fc7d7031ec..bee3e865c87ed5 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 02a20d966ee090a749d88c22f346210f26d666af 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/14] [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 bee3e865c87ed5..9a0cd3a4223c05 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 9bd0eacf41c05c531e69c8bd920c5f0f54cffdc0 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/14] [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 9a0cd3a4223c05..d9edf661d1d8ca 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 7747973ecca54399cebb000beb039bf044523a6c 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/14] [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 00000000000000..38ef57d3bca005
--- /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 aa1f598453e5a586e18c46993baa0cc5327effd9 Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Wed, 28 Feb 2024 18:50:42 -0800
Subject: [PATCH 10/14] [libc++][ranges] update the implementation status of
 'P2440R1'

---
 libcxx/docs/Status/Cxx23Papers.csv      | 2 +-
 libcxx/docs/Status/RangesAlgorithms.csv | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libcxx/docs/Status/Cxx23Papers.csv b/libcxx/docs/Status/Cxx23Papers.csv
index eb415ed8c031fa..5232d24f66ab6f 100644
--- a/libcxx/docs/Status/Cxx23Papers.csv
+++ b/libcxx/docs/Status/Cxx23Papers.csv
@@ -46,7 +46,7 @@
 "`P2255R2 <https://wg21.link/P2255R2>`__","LWG","A type trait to detect reference binding to temporary","February 2022","",""
 "`P2273R3 <https://wg21.link/P2273R3>`__","LWG","Making ``std::unique_ptr`` constexpr","February 2022","|Complete|","16.0"
 "`P2387R3 <https://wg21.link/P2387R3>`__","LWG","Pipe support for user-defined range adaptors","February 2022","","","|ranges|"
-"`P2440R1 <https://wg21.link/P2440R1>`__","LWG","``ranges::iota``, ``ranges::shift_left`` and ``ranges::shift_right``","February 2022","","","|ranges|"
+"`P2440R1 <https://wg21.link/P2440R1>`__","LWG","``ranges::iota``, ``ranges::shift_left`` and ``ranges::shift_right``","February 2022","|In progress|","","|ranges|"
 "`P2441R2 <https://wg21.link/P2441R2>`__","LWG","``views::join_with``","February 2022","|In Progress|","","|ranges|"
 "`P2442R1 <https://wg21.link/P2442R1>`__","LWG","Windowing range adaptors: ``views::chunk`` and ``views::slide``","February 2022","","","|ranges|"
 "`P2443R1 <https://wg21.link/P2443R1>`__","LWG","``views::chunk_by``","February 2022","|Complete|","18.0","|ranges|"
diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv
index f7a51f732c4b14..1d9582373ea8a0 100644
--- a/libcxx/docs/Status/RangesAlgorithms.csv
+++ b/libcxx/docs/Status/RangesAlgorithms.csv
@@ -5,7 +5,7 @@ C++23,`find_last_if <https://wg21.link/P1223R5>`_,Unassigned,No patch yet,Not st
 C++23,`find_last_if_not <https://wg21.link/P1223R5>`_,Unassigned,No patch yet,Not started
 C++23,`starts_with <https://wg21.link/P1659R3>`_,Zijun Zhao,`D150735 <https://llvm.org/D150735>`_,Complete
 C++23,`ends_with <https://wg21.link/P1659R3>`_,Zijun Zhao, `D150831 <https://llvm.org/D150831>`_,Complete
-C++23,`shift_left <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not started
+C++23,`shift_left <https://wg21.link/p2440r1>`_,Xiaoyang Liu,`83231 <https://github.com/llvm/llvm-project/pull/83231>`,✅
 C++23,`shift_right <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not started
 C++23,`iota (algorithm) <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not started
 C++23,`fold <https://wg21.link/p2322r5>`_,Unassigned,No patch yet,Not started

>From a68eb0265cfc3851221fd77ab2e71e4cb020c145 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 11/14] [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 38ef57d3bca005..2e664e47a17e2f 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 020a097ceb2079471d25ffcba7fb17a0ded98e70 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 12/14] [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 2e664e47a17e2f..485a729fea50f5 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 e79f6430c5da5f95211ffb8df257bb436f8e63fd 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 13/14] [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 8df1bff87f0427..f609fc9cdc2537 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 357f4eba5fa1157ff18c285aa108d345eef245e3 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 14/14] [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 d9edf661d1d8ca..439d4955edfa60 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)};
   }
 };
 



More information about the libcxx-commits mailing list