[libcxx-commits] [libcxx] 37ba1b9 - [libc++] Implement ranges::is_partitioned

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Fri May 6 04:02:41 PDT 2022


Author: Nikolas Klauser
Date: 2022-05-06T13:02:38+02:00
New Revision: 37ba1b9d1ac729b1b05f11b684d352ec34646379

URL: https://github.com/llvm/llvm-project/commit/37ba1b9d1ac729b1b05f11b684d352ec34646379
DIFF: https://github.com/llvm/llvm-project/commit/37ba1b9d1ac729b1b05f11b684d352ec34646379.diff

LOG: [libc++] Implement ranges::is_partitioned

Reviewed By: var-const, #libc

Spies: libcxx-commits, mgorny

Differential Revision: https://reviews.llvm.org/D124440

Added: 
    libcxx/include/__algorithm/ranges_is_partitioned.h
    libcxx/test/std/algorithms/alg.sorting/alg.partitions/ranges.is_partitioned.pass.cpp

Modified: 
    libcxx/docs/Status/RangesAlgorithms.csv
    libcxx/include/CMakeLists.txt
    libcxx/include/algorithm
    libcxx/include/module.modulemap
    libcxx/test/libcxx/private_headers.verify.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv
index 0023d2b26748e..3664106093c41 100644
--- a/libcxx/docs/Status/RangesAlgorithms.csv
+++ b/libcxx/docs/Status/RangesAlgorithms.csv
@@ -26,7 +26,7 @@ Search,count_if,Nikolas Klauser, `D121523 <https://llvm.org/D121523>`_,✅
 Search,search,Not assigned,n/a,Not started
 Search,search_n,Not assigned,n/a,Not started
 Search,find_end,Not assigned,n/a,Not started
-Read-only,is_partitioned,Christopher Di Bella,`D105794 <https://llvm.org/D105794>`_,Under review
+Read-only,is_partitioned,Nikolas Klauser,`D124440 <https://llvm.org/D124440>`_,✅
 Read-only,is_sorted,Not assigned,n/a,Not started
 Read-only,is_sorted_unitl,Not assigned,n/a,Not started
 Read-only,includes,Not assigned,n/a,Not started

diff  --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 257e9d59f59d1..b79f286a71994 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -77,6 +77,7 @@ set(files
   __algorithm/ranges_find_if_not.h
   __algorithm/ranges_for_each.h
   __algorithm/ranges_for_each_n.h
+  __algorithm/ranges_is_partitioned.h
   __algorithm/ranges_max.h
   __algorithm/ranges_max_element.h
   __algorithm/ranges_min.h

diff  --git a/libcxx/include/__algorithm/ranges_is_partitioned.h b/libcxx/include/__algorithm/ranges_is_partitioned.h
new file mode 100644
index 0000000000000..3d572895ebe15
--- /dev/null
+++ b/libcxx/include/__algorithm/ranges_is_partitioned.h
@@ -0,0 +1,81 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_IS_PARTITIONED_H
+#define _LIBCPP___ALGORITHM_RANGES_IS_PARTITIONED_H
+
+#include <__config>
+#include <__functional/identity.h>
+#include <__functional/invoke.h>
+#include <__iterator/concepts.h>
+#include <__iterator/indirectly_comparable.h>
+#include <__iterator/projected.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+namespace ranges {
+namespace __is_partitioned {
+struct __fn {
+
+  template <class _Iter, class _Sent, class _Proj, class _Pred>
+  _LIBCPP_HIDE_FROM_ABI constexpr static
+  bool __is_parititioned_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
+    for (; __first != __last; ++__first) {
+      if (!std::invoke(__pred, std::invoke(__proj, *__first)))
+        break;
+    }
+
+    if (__first == __last)
+      return true;
+    ++__first;
+
+    for (; __first != __last; ++__first) {
+      if (std::invoke(__pred, std::invoke(__proj, *__first)))
+        return false;
+    }
+
+    return true;
+  }
+
+  template <input_iterator _Iter, sentinel_for<_Iter> _Sent,
+            class _Proj = identity,
+            indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+  _LIBCPP_HIDE_FROM_ABI constexpr
+  bool operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
+    return __is_parititioned_impl(std::move(__first), std::move(__last), __pred, __proj);
+  }
+
+  template <input_range _Range,
+            class _Proj = identity,
+            indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+  _LIBCPP_HIDE_FROM_ABI constexpr
+  bool operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
+    return __is_parititioned_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
+  }
+};
+} // namespace __is_partitioned
+
+inline namespace __cpo {
+  inline constexpr auto is_partitioned = __is_partitioned::__fn{};
+} // namespace __cpo
+} // namespace ranges
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
+
+#endif // _LIBCPP___ALGORITHM_RANGES_IS_PARTITIONED_H

diff  --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 13b306429eaec..1a75dc0dacb22 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -256,6 +256,15 @@ namespace ranges {
            indirectly_unary_invocable<projected<I, Proj>> Fun>
     constexpr ranges::for_each_n_result<I, Fun>
       ranges::for_each_n(I first, iter_
diff erence_t<I> n, Fun f, Proj proj = {});           // since C++20
+
+  template<input_iterator I, sentinel_for<I> S, class Proj = identity,
+           indirect_unary_predicate<projected<I, Proj>> Pred>
+    constexpr bool ranges::is_partitioned(I first, S last, Pred pred, Proj proj = {});      // since C++20
+
+  template<input_range R, class Proj = identity,
+           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
+    constexpr bool ranges::is_partitioned(R&& r, Pred pred, Proj proj = {});                // since C++20
+
 }
 
     constexpr bool     // constexpr in C++20
@@ -983,6 +992,7 @@ template <class BidirectionalIterator, class Compare>
 #include <__algorithm/ranges_find_if_not.h>
 #include <__algorithm/ranges_for_each.h>
 #include <__algorithm/ranges_for_each_n.h>
+#include <__algorithm/ranges_is_partitioned.h>
 #include <__algorithm/ranges_max.h>
 #include <__algorithm/ranges_max_element.h>
 #include <__algorithm/ranges_min.h>

diff  --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index c027bdcb522e3..c291901c6bba0 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -305,6 +305,7 @@ module std [system] {
       module ranges_find_if_not       { private header "__algorithm/ranges_find_if_not.h" }
       module ranges_for_each          { private header "__algorithm/ranges_for_each.h" }
       module ranges_for_each_n        { private header "__algorithm/ranges_for_each_n.h" }
+      module ranges_is_partitioned    { private header "__algorithm/ranges_is_partitioned.h" }
       module ranges_max               { private header "__algorithm/ranges_max.h" }
       module ranges_max_element       { private header "__algorithm/ranges_max_element.h" }
       module ranges_min               { private header "__algorithm/ranges_min.h" }

diff  --git a/libcxx/test/libcxx/private_headers.verify.cpp b/libcxx/test/libcxx/private_headers.verify.cpp
index f8b7ea5239acb..c0228a94590f6 100644
--- a/libcxx/test/libcxx/private_headers.verify.cpp
+++ b/libcxx/test/libcxx/private_headers.verify.cpp
@@ -114,6 +114,7 @@ END-SCRIPT
 #include <__algorithm/ranges_find_if_not.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_find_if_not.h'}}
 #include <__algorithm/ranges_for_each.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_for_each.h'}}
 #include <__algorithm/ranges_for_each_n.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_for_each_n.h'}}
+#include <__algorithm/ranges_is_partitioned.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_is_partitioned.h'}}
 #include <__algorithm/ranges_max.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_max.h'}}
 #include <__algorithm/ranges_max_element.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_max_element.h'}}
 #include <__algorithm/ranges_min.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_min.h'}}

diff  --git a/libcxx/test/std/algorithms/alg.sorting/alg.partitions/ranges.is_partitioned.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.partitions/ranges.is_partitioned.pass.cpp
new file mode 100644
index 0000000000000..264a707dce619
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.partitions/ranges.is_partitioned.pass.cpp
@@ -0,0 +1,229 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-has-no-incomplete-ranges
+
+// <algorithm>
+
+// template<input_iterator I, sentinel_for<I> S, class Proj = identity,
+//          indirect_unary_predicate<projected<I, Proj>> Pred>
+//   constexpr bool ranges::is_partitioned(I first, S last, Pred pred, Proj proj = {});
+// template<input_range R, class Proj = identity,
+//          indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
+//   constexpr bool ranges::is_partitioned(R&& r, Pred pred, Proj proj = {});
+
+
+#include <algorithm>
+#include <cassert>
+
+#include "almost_satisfies_types.h"
+#include "test_iterators.h"
+
+struct Functor {
+  bool operator()(auto&&);
+};
+
+template <class Iter, class Sent = sentinel_wrapper<Iter>>
+concept HasIsPartitionedIt = requires(Iter iter, Sent sent) {
+  std::ranges::is_partitioned(iter, sent, Functor{});
+};
+
+static_assert(HasIsPartitionedIt<int*>);
+static_assert(!HasIsPartitionedIt<InputIteratorNotDerivedFrom>);
+static_assert(!HasIsPartitionedIt<InputIteratorNotIndirectlyReadable>);
+static_assert(!HasIsPartitionedIt<InputIteratorNotInputOrOutputIterator>);
+static_assert(!HasIsPartitionedIt<int*, SentinelForNotSemiregular>);
+static_assert(!HasIsPartitionedIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
+
+template <class Pred>
+concept HasIsPartitionedItPred = requires(int* first, int* last, Pred pred) {
+  std::ranges::is_partitioned(first, last, pred);
+};
+
+static_assert(HasIsPartitionedItPred<Functor>);
+static_assert(!HasIsPartitionedItPred<IndirectUnaryPredicateNotCopyConstructible>);
+static_assert(!HasIsPartitionedItPred<IndirectUnaryPredicateNotPredicate>);
+
+template <class Range>
+concept HasIsPartitionedR = requires (Range range) {
+    std::ranges::is_partitioned(range, Functor{});
+};
+
+static_assert(HasIsPartitionedR<UncheckedRange<int*>>);
+static_assert(!HasIsPartitionedR<InputRangeNotDerivedFrom>);
+static_assert(!HasIsPartitionedR<InputRangeNotIndirectlyReadable>);
+static_assert(!HasIsPartitionedR<InputRangeNotInputOrOutputIterator>);
+static_assert(!HasIsPartitionedR<InputRangeNotSentinelSemiregular>);
+static_assert(!HasIsPartitionedR<InputRangeNotSentinelEqualityComparableWith>);
+
+template <class Pred>
+concept HasIsPartitionedRPred = requires(Pred pred) {
+  std::ranges::is_partitioned(UncheckedRange<int*>{}, pred);
+};
+
+static_assert(HasIsPartitionedRPred<Functor>);
+static_assert(!HasIsPartitionedRPred<IndirectUnaryPredicateNotCopyConstructible>);
+static_assert(!HasIsPartitionedRPred<IndirectUnaryPredicateNotPredicate>);
+
+template <class Iter, class Sent = Iter>
+constexpr void test_iterators() {
+  { // simple test
+    {
+      int a[] = {1, 2, 3, 4, 5};
+      std::same_as<bool> decltype(auto) ret =
+          std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 5)), [](int i) { return i < 3; });
+      assert(ret);
+    }
+    {
+      int a[] = {1, 2, 3, 4, 5};
+      auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 5)));
+      std::same_as<bool> decltype(auto) ret = std::ranges::is_partitioned(range, [](int i) { return i < 3; });
+      assert(ret);
+    }
+  }
+
+  { // check that it's partitoned if the predicate is true for all elements
+    {
+      int a[] = {1, 2, 3, 4};
+      auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 4)), [](int) { return true; });
+      assert(ret);
+    }
+    {
+      int a[] = {1, 2, 3, 4};
+      auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 4)));
+      auto ret = std::ranges::is_partitioned(range, [](int) { return true; });
+      assert(ret);
+    }
+  }
+
+  { // check that it's partitioned if the predicate is false for all elements
+    {
+      int a[] = {1, 2, 3, 4};
+      auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 4)), [](int) { return false; });
+      assert(ret);
+    }
+    {
+      int a[] = {1, 2, 3, 4};
+      auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 4)));
+      auto ret = std::ranges::is_partitioned(range, [](int) { return false; });
+      assert(ret);
+    }
+  }
+
+  { // check that false is returned if the range isn't partitioned
+    {
+      int a[] = {1, 3, 2, 4};
+      auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 4)), [](int i) { return i < 3; });
+      assert(!ret);
+    }
+    {
+      int a[] = {1, 3, 2, 4};
+      auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 4)));
+      auto ret = std::ranges::is_partitioned(range, [](int i) { return i < 3; });
+      assert(!ret);
+    }
+  }
+
+  { // check that an empty range is partitioned
+    {
+      int a[] = {};
+      auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a)), [](int i) { return i < 3; });
+      assert(ret);
+    }
+    {
+      int a[] = {};
+      auto range = std::ranges::subrange(Iter(a), Sent(Iter(a)));
+      auto ret = std::ranges::is_partitioned(range, [](int i) { return i < 3; });
+      assert(ret);
+    }
+  }
+
+  { // check that a single element is partitioned
+    {
+      int a[] = {1};
+      auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 1)), [](int i) { return i < 3; });
+      assert(ret);
+    }
+    {
+      int a[] = {1};
+      auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 1)));
+      auto ret = std::ranges::is_partitioned(range, [](int i) { return i < 3; });
+      assert(ret);
+    }
+  }
+
+  { // check that it is partitioned when the first element is the partition point
+    {
+      int a[] = {0, 1, 1};
+      auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 3)), [](int i) { return i < 1; });
+      assert(ret);
+    }
+    {
+      int a[] = {0, 1, 1};
+      auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 3)));
+      auto ret = std::ranges::is_partitioned(range, [](int i) { return i < 1; });
+      assert(ret);
+    }
+  }
+
+  { // check that it is partitioned when the last element is the partition point
+    {
+      int a[] = {0, 0, 1};
+      auto ret = std::ranges::is_partitioned(Iter(a), Sent(Iter(a + 3)), [](int i) { return i < 1; });
+      assert(ret);
+    }
+    {
+      int a[] = {0, 0, 1};
+      auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 3)));
+      auto ret = std::ranges::is_partitioned(range, [](int i) { return i < 1; });
+      assert(ret);
+    }
+  }
+}
+
+constexpr bool test() {
+  test_iterators<cpp17_input_iterator<int*>, sentinel_wrapper<cpp17_input_iterator<int*>>>();
+  test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
+  test_iterators<forward_iterator<int*>>();
+  test_iterators<bidirectional_iterator<int*>>();
+  test_iterators<random_access_iterator<int*>>();
+  test_iterators<contiguous_iterator<int*>>();
+  test_iterators<int*>();
+  test_iterators<const int*>();
+
+  { // check that std:invoke is used
+    struct S {
+      int check;
+      int other;
+
+      constexpr S& identity() {
+        return *this;
+      }
+    };
+    {
+      S a[] = {{1, 2}, {3, 4}, {5, 6}};
+      auto ret = std::ranges::is_partitioned(a, a + 3, &S::check, &S::identity);
+      assert(ret);
+    }
+    {
+      S a[] = {{1, 2}, {3, 4}, {5, 6}};
+      auto ret = std::ranges::is_partitioned(a, &S::check, &S::identity);
+      assert(ret);
+    }
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}


        


More information about the libcxx-commits mailing list