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

Xiaoyang Liu via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 28 12:38:54 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 1/7] [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 2/7] [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 3/7] [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 4/7] [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 5/7] [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 6/7] [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 7/7] [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));



More information about the libcxx-commits mailing list