[pstl] Use utilities from <functional> instead of reinventing the wheel
authorLouis Dionne <ldionne@apple.com>
Thu, 4 Jul 2019 20:00:39 +0000 (20:00 +0000)
committerLouis Dionne <ldionne@apple.com>
Thu, 4 Jul 2019 20:00:39 +0000 (20:00 +0000)
llvm-svn: 365158

pstl/include/pstl/internal/algorithm_impl.h
pstl/include/pstl/internal/glue_algorithm_impl.h
pstl/include/pstl/internal/utils.h
pstl/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp
pstl/test/std/algorithms/alg.sorting/alg.set.operations/includes.pass.cpp
pstl/test/std/algorithms/alg.sorting/alg.set.operations/set.pass.cpp

index 5956e2e..ae0657d 100644 (file)
@@ -401,9 +401,7 @@ __brick_equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _
     if (__last1 - __first1 != __last2 - __first2)
         return false;
 
-    return __unseq_backend::__simd_first(__first1, __last1 - __first1, __first2,
-                                         __internal::__not_pred<_BinaryPredicate>(__p))
-               .first == __last1;
+    return __unseq_backend::__simd_first(__first1, __last1 - __first1, __first2, std::not_fn(__p)).first == __last1;
 }
 
 template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate,
@@ -453,8 +451,7 @@ bool
 __brick_equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2,
               _BinaryPredicate __p, /* is_vector = */ std::true_type) noexcept
 {
-    return __unseq_backend::__simd_first(__first1, __last1 - __first1, __first2, __not_pred<_BinaryPredicate>(__p))
-               .first == __last1;
+    return __unseq_backend::__simd_first(__first1, __last1 - __first1, __first2, std::not_fn(__p)).first == __last1;
 }
 
 template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate,
@@ -610,8 +607,7 @@ __find_subrange(_RandomAccessIterator __first, _RandomAccessIterator __last, _Ra
 
         // check that all of elements in [first+1, first+count) equal to value
         if (__first != __last && (static_cast<_Size>(__global_last - __first) >= __count) &&
-            !__internal::__brick_any_of(__first + 1, __first + __count,
-                                        __not_pred<decltype(__unary_pred)>(__unary_pred), __is_vector))
+            !__internal::__brick_any_of(__first + 1, __first + __count, std::not_fn(__unary_pred), __is_vector))
         {
             return __first;
         }
@@ -1762,7 +1758,7 @@ __pattern_is_partitioned(_ExecutionPolicy&& __exec, _ForwardIterator __first, _F
                     {
                         // find first element that don't satisfy pred
                         _ForwardIterator __x =
-                            __internal::__brick_find_if(__i + 1, __j, __not_pred<_UnaryPredicate>(__pred), __is_vector);
+                            __internal::__brick_find_if(__i + 1, __j, std::not_fn(__pred), __is_vector);
                         if (__x != __j)
                         {
                             // find first element after "x" that satisfy pred
@@ -3441,14 +3437,14 @@ __pattern_minmax_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _F
             std::forward<_ExecutionPolicy>(__exec), __first + 1, __last, std::make_pair(__first, __first),
             [=](_ForwardIterator __begin, _ForwardIterator __end, _Result __init) -> _Result {
                 const _Result __subresult = __internal::__brick_minmax_element(__begin, __end, __comp, __is_vector);
-                return std::make_pair(__internal::__cmp_iterators_by_values(__subresult.first, __init.first, __comp),
-                                      __internal::__cmp_iterators_by_values(__init.second, __subresult.second,
-                                                                            __not_pred<_Compare>(__comp)));
+                return std::make_pair(
+                    __internal::__cmp_iterators_by_values(__subresult.first, __init.first, __comp),
+                    __internal::__cmp_iterators_by_values(__init.second, __subresult.second, std::not_fn(__comp)));
             },
             [=](_Result __p1, _Result __p2) -> _Result {
                 return std::make_pair(
                     __internal::__cmp_iterators_by_values(__p1.first, __p2.first, __comp),
-                    __internal::__cmp_iterators_by_values(__p2.second, __p1.second, __not_pred<_Compare>(__comp)));
+                    __internal::__cmp_iterators_by_values(__p2.second, __p1.second, std::not_fn(__comp)));
             });
     });
 }
@@ -3485,7 +3481,7 @@ __brick_mismatch(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _Forward
                  _ForwardIterator2 __last2, _Predicate __pred, /* __is_vector = */ std::true_type) noexcept
 {
     auto __n = std::min(__last1 - __first1, __last2 - __first2);
-    return __unseq_backend::__simd_first(__first1, __n, __first2, __not_pred<_Predicate>(__pred));
+    return __unseq_backend::__simd_first(__first1, __n, __first2, std::not_fn(__pred));
 }
 
 template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate, class _IsVector>
index 2c4f432..81a0d4d 100644 (file)
@@ -43,8 +43,7 @@ template <class _ExecutionPolicy, class _ForwardIterator, class _Pred>
 __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool>
 all_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Pred __pred)
 {
-    return !std::any_of(std::forward<_ExecutionPolicy>(__exec), __first, __last,
-                        __pstl::__internal::__not_pred<_Pred>(__pred));
+    return !std::any_of(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::not_fn(__pred));
 }
 
 // [alg.none_of]
@@ -96,8 +95,7 @@ template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
 __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
 find_if_not(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
 {
-    return std::find_if(std::forward<_ExecutionPolicy>(__exec), __first, __last,
-                        __pstl::__internal::__not_pred<_Predicate>(__pred));
+    return std::find_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::not_fn(__pred));
 }
 
 template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
@@ -127,7 +125,7 @@ find_end(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1
          _ForwardIterator2 __s_last)
 {
     return std::find_end(std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last,
-                         __pstl::__internal::__pstl_equal());
+                         std::equal_to<>());
 }
 
 // [alg.find_first_of]
@@ -149,7 +147,7 @@ find_first_of(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIter
               _ForwardIterator2 __s_first, _ForwardIterator2 __s_last)
 {
     return std::find_first_of(std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last,
-                              __pstl::__internal::__pstl_equal());
+                              std::equal_to<>());
 }
 
 // [alg.adjacent_find]
@@ -226,8 +224,7 @@ __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardItera
 search(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first,
        _ForwardIterator2 __s_last)
 {
-    return std::search(std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last,
-                       __pstl::__internal::__pstl_equal());
+    return std::search(std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last, std::equal_to<>());
 }
 
 template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
@@ -465,8 +462,7 @@ __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardItera
 remove_copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
                _ForwardIterator2 __result, _Predicate __pred)
 {
-    return std::copy_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
-                        __pstl::__internal::__not_pred<_Predicate>(__pred));
+    return std::copy_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, std::not_fn(__pred));
 }
 
 template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp>
@@ -514,7 +510,7 @@ template <class _ExecutionPolicy, class _ForwardIterator>
 __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
 unique(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
 {
-    return std::unique(std::forward<_ExecutionPolicy>(__exec), __first, __last, __pstl::__internal::__pstl_equal());
+    return std::unique(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::equal_to<>());
 }
 
 template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
@@ -533,7 +529,7 @@ template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterato
 __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2>
 unique_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result)
 {
-    return std::unique_copy(__exec, __first, __last, __result, __pstl::__internal::__pstl_equal());
+    return std::unique_copy(__exec, __first, __last, __result, std::equal_to<>());
 }
 
 // [alg.reverse]
@@ -710,7 +706,7 @@ mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator
          _ForwardIterator2 __last2)
 {
     return std::mismatch(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2,
-                         __pstl::__internal::__pstl_equal());
+                         std::equal_to<>());
 }
 
 template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2>
@@ -740,8 +736,7 @@ template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterato
 __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool>
 equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
 {
-    return std::equal(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2,
-                      __pstl::__internal::__pstl_equal());
+    return std::equal(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, std::equal_to<>());
 }
 
 template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
@@ -761,8 +756,7 @@ __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool>
 equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
       _ForwardIterator2 __last2)
 {
-    return equal(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2,
-                 __pstl::__internal::__pstl_equal());
+    return equal(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, std::equal_to<>());
 }
 
 // [alg.move]
@@ -825,7 +819,7 @@ partial_sort_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardI
                   _RandomAccessIterator __d_first, _RandomAccessIterator __d_last)
 {
     return std::partial_sort_copy(std::forward<_ExecutionPolicy>(__exec), __first, __last, __d_first, __d_last,
-                                  __pstl::__internal::__pstl_less());
+                                  std::less<>());
 }
 
 // [is.sorted]
@@ -891,7 +885,7 @@ merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 _
       _ForwardIterator2 __last2, _ForwardIterator __d_first)
 {
     return std::merge(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __d_first,
-                      __pstl::__internal::__pstl_less());
+                      std::less<>());
 }
 
 template <class _ExecutionPolicy, class _BidirectionalIterator, class _Compare>
@@ -934,8 +928,7 @@ __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool>
 includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
          _ForwardIterator2 __last2)
 {
-    return std::includes(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2,
-                         __pstl::__internal::__pstl_less());
+    return std::includes(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, std::less<>());
 }
 
 // [set.union]
@@ -961,7 +954,7 @@ set_union(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterato
           _ForwardIterator2 __last2, _ForwardIterator __result)
 {
     return std::set_union(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result,
-                          __pstl::__internal::__pstl_less());
+                          std::less<>());
 }
 
 // [set.intersection]
@@ -987,7 +980,7 @@ set_intersection(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _Forward
                  _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result)
 {
     return std::set_intersection(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result,
-                                 __pstl::__internal::__pstl_less());
+                                 std::less<>());
 }
 
 // [set.difference]
@@ -1013,7 +1006,7 @@ set_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIt
                _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result)
 {
     return std::set_difference(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result,
-                               __pstl::__internal::__pstl_less());
+                               std::less<>());
 }
 
 // [set.symmetric.difference]
@@ -1040,7 +1033,7 @@ set_symmetric_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1,
                          _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result)
 {
     return std::set_symmetric_difference(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2,
-                                         __result, __pstl::__internal::__pstl_less());
+                                         __result, std::less<>());
 }
 
 // [is.heap]
@@ -1178,7 +1171,7 @@ lexicographical_compare(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _
                         _ForwardIterator2 __first2, _ForwardIterator2 __last2)
 {
     return std::lexicographical_compare(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2,
-                                        __pstl::__internal::__pstl_less());
+                                        std::less<>());
 }
 
 } // namespace std
index 16e6f4c..0f5b834 100644 (file)
@@ -85,23 +85,6 @@ struct __no_op
     }
 };
 
-//! Logical negation of a predicate
-template <typename _Pred>
-class __not_pred
-{
-    _Pred _M_pred;
-
-  public:
-    explicit __not_pred(_Pred __pred) : _M_pred(__pred) {}
-
-    template <typename... _Args>
-    bool
-    operator()(_Args&&... __args)
-    {
-        return !_M_pred(std::forward<_Args>(__args)...);
-    }
-};
-
 template <typename _Pred>
 class __reorder_pred
 {
@@ -118,36 +101,6 @@ class __reorder_pred
     }
 };
 
-//! "==" comparison.
-/** Not called "equal" to avoid (possibly unfounded) concerns about accidental invocation via
-    argument-dependent name lookup by code expecting to find the usual std::equal. */
-class __pstl_equal
-{
-  public:
-    explicit __pstl_equal() {}
-
-    template <typename _Xp, typename _Yp>
-    bool
-    operator()(_Xp&& __x, _Yp&& __y) const
-    {
-        return std::forward<_Xp>(__x) == std::forward<_Yp>(__y);
-    }
-};
-
-//! "<" comparison.
-class __pstl_less
-{
-  public:
-    explicit __pstl_less() {}
-
-    template <typename _Xp, typename _Yp>
-    bool
-    operator()(_Xp&& __x, _Yp&& __y) const
-    {
-        return std::forward<_Xp>(__x) < std::forward<_Yp>(__y);
-    }
-};
-
 //! Like a polymorphic lambda for pred(...,value)
 template <typename _Tp, typename _Predicate>
 class __equal_value_by_pred
index b1f6cd9..a391ba8 100644 (file)
 // Tests for stable_partition and partition_copy
 #include "support/pstl_test_config.h"
 
-#include <execution>
 #include <algorithm>
 #include <cstdlib>
+#include <execution>
+#include <functional>
 #include <iterator>
 
 #include "support/utils.h"
@@ -26,16 +27,15 @@ struct test_partition_copy
     template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2,
               typename UnaryOp>
     void
-    operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator true_first,
-               OutputIterator, OutputIterator2 false_first, OutputIterator2, UnaryOp unary_op)
+    operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator true_first, OutputIterator,
+               OutputIterator2 false_first, OutputIterator2, UnaryOp unary_op)
     {
 
         auto actual_ret = std::partition_copy(exec, first, last, true_first, false_first, unary_op);
 
         EXPECT_TRUE(std::distance(true_first, actual_ret.first) == std::count_if(first, last, unary_op),
                     "partition_copy has wrong effect from true sequence");
-        EXPECT_TRUE(std::distance(false_first, actual_ret.second) ==
-                        std::count_if(first, last, __pstl::__internal::__not_pred<UnaryOp>(unary_op)),
+        EXPECT_TRUE(std::distance(false_first, actual_ret.second) == std::count_if(first, last, std::not_fn(unary_op)),
                     "partition_copy has wrong effect from false sequence");
     }
 
index 8851cec..2f4a72f 100644 (file)
 
 #include "support/pstl_test_config.h"
 
+#include <algorithm>
 #include <cmath>
 #include <execution>
-#include <algorithm>
+#include <functional>
 
 #include "support/utils.h"
 
@@ -57,8 +58,7 @@ struct test_one_policy
 
     template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare>
     typename std::enable_if<TestUtils::isReverse<InputIterator1>::value, void>::type
-    operator()(Policy&&, InputIterator1, InputIterator1, InputIterator2, InputIterator2,
-               Compare)
+    operator()(Policy&&, InputIterator1, InputIterator1, InputIterator2, InputIterator2, Compare)
     {
     }
 };
@@ -98,7 +98,7 @@ int32_t
 main()
 {
 
-    test_includes<float64_t, float64_t>(__pstl::__internal::__pstl_less());
+    test_includes<float64_t, float64_t>(std::less<>());
     test_includes<Num<int64_t>, Num<int32_t>>([](const Num<int64_t>& x, const Num<int32_t>& y) { return x < y; });
     std::cout << done() << std::endl;
 
index b54a6b6..35ab452 100644 (file)
 
 #include "support/pstl_test_config.h"
 
-#include <cmath>
+#include <algorithm>
 #include <chrono>
+#include <cmath>
 #include <execution>
-#include <algorithm>
+#include <functional>
 
 #include "support/utils.h"
 
@@ -97,8 +98,7 @@ struct test_one_policy
 
     template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare>
     typename std::enable_if<TestUtils::isReverse<InputIterator1>::value, void>::type
-    operator()(Policy&&, InputIterator1, InputIterator1, InputIterator2, InputIterator2,
-               Compare)
+    operator()(Policy&&, InputIterator1, InputIterator1, InputIterator2, InputIterator2, Compare)
     {
     }
 };
@@ -151,7 +151,7 @@ int32_t
 main()
 {
 
-    test_set<float64_t, float64_t>(__pstl::__internal::__pstl_less());
+    test_set<float64_t, float64_t>(std::less<>());
     test_set<Num<int64_t>, Num<int32_t>>([](const Num<int64_t>& x, const Num<int32_t>& y) { return x < y; });
 
     test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());