[llvm] [libc++] Implement ranges::contains (PR #65148)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 12 17:12:20 PDT 2023


github-actions[bot] wrote:


<!--LLVM CODE FORMAT COMMENT: {clang-format}-->

:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 2cea1babefbb726b00573c4cb5c89dc47664dc17 de8820d9428b86b2edc51f55f8158339116e1bc1 -- libcxx/include/__algorithm/ranges_contains.h libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libcxx/include/__algorithm/ranges_contains.h b/libcxx/include/__algorithm/ranges_contains.h
index 08d530547788..f0047960a6f7 100644
--- a/libcxx/include/__algorithm/ranges_contains.h
+++ b/libcxx/include/__algorithm/ranges_contains.h
@@ -43,8 +43,8 @@ struct __fn {
     requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Type*>
   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool
   operator()(_Range&& __range, const _Type& __value, _Proj __proj = {}) const {
-    return ranges::find(ranges::begin(__range), ranges::end(__range), __value,
-    std::ref(__proj)) != ranges::end(__range);
+    return ranges::find(ranges::begin(__range), ranges::end(__range), __value, std::ref(__proj)) !=
+           ranges::end(__range);
   }
 };
 } // namespace __contains
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
index 66b2732590b5..98666dadaf96 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
@@ -63,17 +63,15 @@ static std::vector<int> comparable_data;
 template <class Iter, class Sent = Iter>
 constexpr void test_iterators() {
   using ValueT = std::iter_value_t<Iter>;
-  {  // simple tests
+  { // simple tests
     ValueT a[] = {1, 2, 3, 4, 5, 6};
     auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a + 6)));
     {
-      [[maybe_unused]] std::same_as<bool> decltype(auto) ret =
-        std::ranges::contains(whole.begin(), whole.end(), 3);
+      [[maybe_unused]] std::same_as<bool> decltype(auto) ret = std::ranges::contains(whole.begin(), whole.end(), 3);
       assert(ret);
     }
     {
-      [[maybe_unused]] std::same_as<bool> decltype(auto) ret =
-        std::ranges::contains(whole, 3);
+      [[maybe_unused]] std::same_as<bool> decltype(auto) ret = std::ranges::contains(whole, 3);
       assert(ret);
     }
   }
@@ -151,20 +149,20 @@ constexpr void test_iterators() {
     }
     {
       auto range = std::ranges::subrange(a, a + 5);
-      bool ret = std::ranges::contains(range, -13, [&](int i) { return i * -1; });
+      bool ret   = std::ranges::contains(range, -13, [&](int i) { return i * -1; });
       assert(ret);
     }
   }
 
   { // check the nodiscard extension
-    // use #pragma around to suppress error: ignoring return value of function
-    // declared with 'nodiscard' attribute [-Werror,-Wunused-result]
-    #pragma clang diagnostic push
-    #pragma clang diagnostic ignored "-Wunused-result"
+// use #pragma around to suppress error: ignoring return value of function
+// declared with 'nodiscard' attribute [-Werror,-Wunused-result]
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wunused-result"
     ValueT a[] = {1, 9, 0, 13, 25};
     auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a + 5)));
     std::ranges::contains(whole, 12);
-    #pragma clang diagnostic pop
+#pragma clang diagnostic pop
   }
 
   if (!std::is_constant_evaluated())
@@ -192,39 +190,41 @@ public:
           return size;
         }()) {}
 
-  bool operator==(const Comparable& other) const {
-    return comparable_data[other.index_] == comparable_data[index_];
-  }
+  bool operator==(const Comparable& other) const { return comparable_data[other.index_] == comparable_data[index_]; }
 
   friend bool operator==(const Comparable& lhs, long long rhs) { return comparable_data[lhs.index_] == rhs; }
 };
 
 constexpr bool test() {
-  types::for_each(types::type_list<char, short, int, long, long long,
-                  TriviallyComparable<char>, TriviallyComparable<wchar_t>>{},
-                  []<class T> {
-                    types::for_each(types::cpp20_input_iterator_list<T*>{},
-                      []<class Iter> {
-                      if constexpr (std::forward_iterator<Iter>)
-                        test_iterators<Iter>();
-                      test_iterators<Iter, sentinel_wrapper<Iter>>();
-                      test_iterators<Iter, sized_sentinel<Iter>>();
-                    });
-                  });
+  types::for_each(
+      types::type_list<char, short, int, long, long long, TriviallyComparable<char>, TriviallyComparable<wchar_t>>{},
+      []<class T> {
+        types::for_each(types::cpp20_input_iterator_list<T*>{}, []<class Iter> {
+          if constexpr (std::forward_iterator<Iter>)
+            test_iterators<Iter>();
+          test_iterators<Iter, sentinel_wrapper<Iter>>();
+          test_iterators<Iter, sized_sentinel<Iter>>();
+        });
+      });
 
   { // count invocations of the projection
-    int a[] = {1, 9, 0, 13, 25};
+    int a[]              = {1, 9, 0, 13, 25};
     int projection_count = 0;
     {
-      bool ret = std::ranges::contains(a, a + 5, 0,
-                                [&](int i) { ++projection_count; return i; });
+      bool ret = std::ranges::contains(a, a + 5, 0, [&](int i) {
+        ++projection_count;
+        return i;
+      });
       assert(ret);
       assert(projection_count == 3);
     }
     {
       projection_count = 0;
-      auto range = std::ranges::subrange(a, a + 5);
-      bool ret = std::ranges::contains(range, 0, [&](int i) { ++projection_count; return i; });
+      auto range       = std::ranges::subrange(a, a + 5);
+      bool ret         = std::ranges::contains(range, 0, [&](int i) {
+        ++projection_count;
+        return i;
+      });
       assert(ret);
       assert(projection_count == 3);
     }
@@ -237,16 +237,14 @@ int main(int, char**) {
   test();
   static_assert(test());
 
-  types::for_each(types::type_list<Comparable<char>, Comparable<wchar_t>>{},
-                []<class T> {
-                  types::for_each(types::cpp20_input_iterator_list<T*>{},
-                    []<class Iter> {
-                    if constexpr (std::forward_iterator<Iter>)
-                      test_iterators<Iter>();
-                    test_iterators<Iter, sentinel_wrapper<Iter>>();
-                    test_iterators<Iter, sized_sentinel<Iter>>();
-                  });
-                });
+  types::for_each(types::type_list<Comparable<char>, Comparable<wchar_t>>{}, []<class T> {
+    types::for_each(types::cpp20_input_iterator_list<T*>{}, []<class Iter> {
+      if constexpr (std::forward_iterator<Iter>)
+        test_iterators<Iter>();
+      test_iterators<Iter, sentinel_wrapper<Iter>>();
+      test_iterators<Iter, sized_sentinel<Iter>>();
+    });
+  });
 
   return 0;
 }

``````````

</details>


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


More information about the llvm-commits mailing list