[libcxx-commits] [libcxx] [libcxx] patch for implementing ranges::find_last (PR #67270)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Sep 29 06:22:06 PDT 2023


================
@@ -0,0 +1,364 @@
+//===----------------------------------------------------------------------===//
+//
+// 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<forward_iterator I, sentinel_for<I> S, class Proj = identity,
+//          indirect_unary_predicate<projected<I, Proj>> Pred>
+//  constexpr subrange<I> ranges::find_last_if(I first, S last, Pred pred, Proj proj = {});
+// template<forward_range R, class Proj = identity,
+//          indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
+//  constexpr borrowed_subrange_t<R>
+// ranges::find_last_if(R&& r, Pred pred, Proj proj = {});
+
+#include <algorithm>
+#include <array>
+#include <cassert>
+#include <ranges>
+
+#include "almost_satisfies_types.h"
+#include "boolean_testable.h"
+#include "test_iterators.h"
+
+struct Predicate {
+  bool operator()(int);
+};
+
+template <class It, class Sent = It>
+concept HasFindLastIfIt = requires(It it, Sent sent) {
+  std::ranges::find_last_if(it, sent, Predicate{});
+};
+static_assert(HasFindLastIfIt<int*>);
+static_assert(!HasFindLastIfIt<InputIteratorNotDerivedFrom>);
+static_assert(!HasFindLastIfIt<InputIteratorNotIndirectlyReadable>);
+static_assert(!HasFindLastIfIt<InputIteratorNotInputOrOutputIterator>);
+static_assert(!HasFindLastIfIt<cpp20_input_iterator<int*>, SentinelForNotSemiregular>);
+static_assert(!HasFindLastIfIt<cpp20_input_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
+
+static_assert(!HasFindLastIfIt<int*, int>);
+static_assert(!HasFindLastIfIt<int, int*>);
+
+template <class Pred>
+concept HasFindLastIfPred = requires(int* it, Pred pred) {
+  std::ranges::find_last_if(it, it, pred);
+};
+
+static_assert(HasFindLastIfPred<IndirectUnaryPredicate>);
+static_assert(!HasFindLastIfPred<IndirectUnaryPredicateNotCopyConstructible>);
+static_assert(!HasFindLastIfPred<IndirectUnaryPredicateNotPredicate>);
+
+template <class R>
+concept HasFindLastIfR = requires(R r) {
+  std::ranges::find_last_if(r, Predicate{});
+};
+
+static_assert(HasFindLastIfR<std::array<int, 0>>);
+static_assert(!HasFindLastIfR<int>);
+static_assert(!HasFindLastIfR<InputRangeNotDerivedFrom>);
+static_assert(!HasFindLastIfR<InputRangeNotIndirectlyReadable>);
+static_assert(!HasFindLastIfR<InputRangeNotInputOrOutputIterator>);
+static_assert(!HasFindLastIfR<InputRangeNotSentinelSemiregular>);
+static_assert(!HasFindLastIfR<InputRangeNotSentinelEqualityComparableWith>);
+
+template <class It, class Sent = It>
+constexpr void test_iterators() {
+  {// Test with an empty range
+
+    {
+      int a[] = {};
+      std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last_if(It(a), Sent(It(a)), [](int x) { return x == 4; });
+      assert(ret.empty());
+    }
+
+    {
+      int a[] = {};
+      auto range = std::ranges::subrange(It(a), Sent(It(a)));
+      std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last_if(range, [](int x) { return x == 4; });
+      assert(ret.empty());
+    }
+
+  }
+
+  {// Test with a single element range
+
+    {
+      int a[] = {4};
+      std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last_if(It(a), Sent(It(a + 1)), [](int x) { return x == 4; });
+      assert(base(ret.begin()) == a);
+      assert(*ret.begin() == 4);
+    }
+
+    {
+      int a[] = {4};
+      std::same_as<std::ranges::borrowed_subrange_t<int (&)[1]>> auto ret = std::ranges::find_last_if(a, [](int x) { return x == 4; });
+      assert(base(ret.begin()) == a);
+      assert(*ret.begin() == 4);
+    }
+
+  }
+
+  {// Test when no element satisfies the predicate
+
+    {
+      int a[] = {1, 2, 3, 4};
+      std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last_if(It(a), Sent(It(a + 4)), [](int x) { return x == 5; });
+      assert(ret.empty());
+    }
+
+    {
+      int a[] = {1, 2, 3, 4};
+      std::same_as<std::ranges::borrowed_subrange_t<int (&)[4]>> auto ret = std::ranges::find_last_if(a, [](int x) { return x == 5; });
+      assert(ret.empty());
+    }
+
+  }
+
+  {// Test when all elements satisfy the predicate
+
+    {
+      int a[] = {4, 4, 4, 4};
+      std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last_if(It(a), Sent(It(a + 4)), [](int x) { return x == 4; });
+      assert(base(ret.begin()) == a + 3);
+      assert(*ret.begin() == 4);
+    }
+
+    {
+      int a[] = {4, 4, 4, 4};
+      std::same_as<std::ranges::borrowed_subrange_t<int (&)[4]>> auto ret = std::ranges::find_last_if(a, [](int x) { return x == 4; });
+      assert(base(ret.begin()) == a + 3);
+      assert(*ret.begin() == 4);
+    }
+
+  }
+
+  {// Test when the element being searched is the first one
+
+    {
+      int a[] = {4, 1, 2, 3};
+      std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last_if(It(a), Sent(It(a + 4)), [](int x) { return x == 4; });
+      assert(base(ret.begin()) == a);
+      assert(*ret.begin() == 4);
+    }
+
+    {
+      int a[] = {4, 1, 2, 3};
+      std::same_as<std::ranges::borrowed_subrange_t<int (&)[4]>> auto ret = std::ranges::find_last_if(a, [](int x) { return x == 4; });
+      assert(base(ret.begin()) == a);
+      assert(*ret.begin() == 4);
+    }
+
+  }
+
+  {// Test when the element being searched is the last one
+
+    {
+      int a[] = {1, 2, 3, 4};
+      std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last_if(It(a), Sent(It(a + 4)), [](int x) { return x == 4; });
+      assert(base(ret.begin()) == a + 3);
+      assert(*ret.begin() == 4);
+    }
+
+    {
+      int a[] = {1, 2, 3, 4};
+      std::same_as<std::ranges::borrowed_subrange_t<int (&)[4]>> auto ret = std::ranges::find_last_if(a, [](int x) { return x == 4; });
+      assert(base(ret.begin()) == a + 3);
+      assert(*ret.begin() == 4);
+    }
+
+  }
+
+
+  {// check that past-the-end iterator is returned with no match
+
+    {
+      int a[] = {1, 1, 1};
+      std::same_as<std::ranges::subrange<int*>> auto ret = std::ranges::find_last_if(a, a + 3, [](int) { return false; });
+      assert(ret.data() == a + 3);
+    }
+
+    {
+      int a[]  = {1, 1, 1};
+      std::same_as<std::ranges::subrange<int*>> auto ret = std::ranges::find_last_if(a, [](int) { return false; });
+      assert(ret.data() == a + 3);
+    }
+
+  }
+
+}
+
+
+struct NonConstComparableValue {
+  friend constexpr bool operator==(const NonConstComparableValue&, const NonConstComparableValue&) { return false; }
+  friend constexpr bool operator==(NonConstComparableValue&, NonConstComparableValue&) { return false; }
+  friend constexpr bool operator==(const NonConstComparableValue&, NonConstComparableValue&) { return false; }
+  friend constexpr bool operator==(NonConstComparableValue&, const NonConstComparableValue&) { return true; }
+};
+
+constexpr bool test() {
+  test_iterators<const int*>();
+  test_iterators<int*>();
+  test_iterators<bidirectional_iterator<int*>>();
+  test_iterators<forward_iterator<int*>>();
+  test_iterators<random_access_iterator<int*>>();
+  test_iterators<contiguous_iterator<int*>>();
----------------
huixie90 wrote:

Please have tests when sentinel is a different type than the iterator.
This applies for other tests

https://github.com/llvm/llvm-project/pull/67270


More information about the libcxx-commits mailing list