[PATCH] D42518: Fix the BinaryPredicate form of std::is_permutation to not rely on operator==

Peter Collingbourne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 26 13:26:09 PST 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL323563: Fix the BinaryPredicate form of std::is_permutation to not rely on operator== (authored by pcc, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D42518?vs=131495&id=131646#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D42518

Files:
  libcxx/trunk/include/algorithm
  libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.is_permutation/is_permutation_pred.pass.cpp


Index: libcxx/trunk/include/algorithm
===================================================================
--- libcxx/trunk/include/algorithm
+++ libcxx/trunk/include/algorithm
@@ -1418,7 +1418,11 @@
     for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
     {
     //  Have we already counted the number of *__i in [f1, l1)?
-        if (find(__first1, __i, *__i) == __i) {
+        _ForwardIterator1 __match = __first1;
+        for (; __match != __i; ++__match)
+            if (__pred(*__match, *__i))
+                break;
+        if (__match == __i) {
             // Count number of *__i in [f2, l2)
             _D1 __c2 = 0;
             for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
@@ -1479,7 +1483,11 @@
     for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
     {
     //  Have we already counted the number of *__i in [f1, l1)?
-        if (find(__first1, __i, *__i) == __i) {
+        _ForwardIterator1 __match = __first1;
+        for (; __match != __i; ++__match)
+            if (__pred(*__match, *__i))
+                break;
+        if (__match == __i) {
             // Count number of *__i in [f2, l2)
             _D1 __c2 = 0;
             for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
Index: libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.is_permutation/is_permutation_pred.pass.cpp
===================================================================
--- libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.is_permutation/is_permutation_pred.pass.cpp
+++ libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.is_permutation/is_permutation_pred.pass.cpp
@@ -738,6 +738,30 @@
                                    std::equal_to<const int>()) == false);
 #endif
     }
+    {
+      struct S {
+          S(int i) : i_(i) {}
+          bool operator==(const S& other) = delete;
+          int i_;
+      };
+      struct eq {
+          bool operator()(const S& a, const S&b) { return a.i_ == b.i_; }
+      };
+      const S a[] = {S(0), S(1)};
+      const S b[] = {S(1), S(0)};
+      const unsigned sa = sizeof(a)/sizeof(a[0]);
+      assert(std::is_permutation(forward_iterator<const S*>(a),
+                                 forward_iterator<const S*>(a + sa),
+                                 forward_iterator<const S*>(b),
+                                 eq()));
+#if TEST_STD_VER >= 14
+      assert(std::is_permutation(forward_iterator<const S*>(a),
+                                 forward_iterator<const S*>(a + sa),
+                                 forward_iterator<const S*>(b),
+                                 forward_iterator<const S*>(b + sa),
+                                 eq()));
+#endif
+    }
 
 #if TEST_STD_VER > 17
     static_assert(test_constexpr());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42518.131646.patch
Type: text/x-patch
Size: 2779 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180126/72e88faf/attachment.bin>


More information about the llvm-commits mailing list