[llvm-branch-commits] [libcxx] f851db3 - [libc++] [P0879] constexpr std::reverse, partition, *_permutation.

Arthur O'Dwyer via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Jan 25 10:14:12 PST 2021


Author: Arthur O'Dwyer
Date: 2021-01-25T13:09:30-05:00
New Revision: f851db3dae5cc24ce1897918bd69fa989aa31b59

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

LOG: [libc++] [P0879] constexpr std::reverse, partition, *_permutation.

After this patch, the only parts of P0879 that remain missing will be
std::nth_element, std::sort, and the heap/partial_sort algorithms.

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

Added: 
    

Modified: 
    libcxx/include/algorithm
    libcxx/test/std/algorithms/alg.modifying.operations/alg.partitions/partition.pass.cpp
    libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse.pass.cpp
    libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/next_permutation.pass.cpp
    libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/next_permutation_comp.pass.cpp
    libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/prev_permutation.pass.cpp
    libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/prev_permutation_comp.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 77711d250188b..f7fb2013a7573 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -267,7 +267,7 @@ template <class InputIterator, class OutputIterator, class BinaryPredicate>
     unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
 
 template <class BidirectionalIterator>
-    void
+    constexpr void               // constexpr in C++20
     reverse(BidirectionalIterator first, BidirectionalIterator last);
 
 template <class BidirectionalIterator, class OutputIterator>
@@ -316,7 +316,7 @@ template <class InputIterator, class Predicate>
     is_partitioned(InputIterator first, InputIterator last, Predicate pred);
 
 template <class ForwardIterator, class Predicate>
-    ForwardIterator
+    constexpr ForwardIterator  // constexpr in C++20
     partition(ForwardIterator first, ForwardIterator last, Predicate pred);
 
 template <class InputIterator, class OutputIterator1,
@@ -626,19 +626,19 @@ template <class InputIterator1, class InputIterator2, class Compare>
                             InputIterator2 first2, InputIterator2 last2, Compare comp);
 
 template <class BidirectionalIterator>
-    bool
+    constexpr bool     // constexpr in C++20
     next_permutation(BidirectionalIterator first, BidirectionalIterator last);
 
 template <class BidirectionalIterator, class Compare>
-    bool
+    constexpr bool     // constexpr in C++20
     next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
 
 template <class BidirectionalIterator>
-    bool
+    constexpr bool     // constexpr in C++20
     prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
 
 template <class BidirectionalIterator, class Compare>
-    bool
+    constexpr bool     // constexpr in C++20
     prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
 
 }  // std
@@ -2321,7 +2321,7 @@ unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __res
 // reverse
 
 template <class _BidirectionalIterator>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 void
 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
 {
@@ -2335,7 +2335,7 @@ __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirec
 }
 
 template <class _RandomAccessIterator>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 void
 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
 {
@@ -2345,7 +2345,7 @@ __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_ac
 }
 
 template <class _BidirectionalIterator>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 void
 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
 {
@@ -3393,7 +3393,7 @@ is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
 // partition
 
 template <class _Predicate, class _ForwardIterator>
-_ForwardIterator
+_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
 __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
 {
     while (true)
@@ -3416,7 +3416,7 @@ __partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred
 }
 
 template <class _Predicate, class _BidirectionalIterator>
-_BidirectionalIterator
+_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator
 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
             bidirectional_iterator_tag)
 {
@@ -3441,7 +3441,7 @@ __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Pred
 }
 
 template <class _ForwardIterator, class _Predicate>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 _ForwardIterator
 partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
 {
@@ -5760,7 +5760,7 @@ lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
 // next_permutation
 
 template <class _Compare, class _BidirectionalIterator>
-bool
+_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
 __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
 {
     _BidirectionalIterator __i = __last;
@@ -5787,7 +5787,7 @@ __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last
 }
 
 template <class _BidirectionalIterator, class _Compare>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 bool
 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
 {
@@ -5796,7 +5796,7 @@ next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last,
 }
 
 template <class _BidirectionalIterator>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 bool
 next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
 {
@@ -5807,7 +5807,7 @@ next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
 // prev_permutation
 
 template <class _Compare, class _BidirectionalIterator>
-bool
+_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
 __prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
 {
     _BidirectionalIterator __i = __last;
@@ -5834,7 +5834,7 @@ __prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last
 }
 
 template <class _BidirectionalIterator, class _Compare>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 bool
 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
 {
@@ -5843,7 +5843,7 @@ prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last,
 }
 
 template <class _BidirectionalIterator>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 bool
 prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
 {

diff  --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.partitions/partition.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.partitions/partition.pass.cpp
index c88daeeecfc61..3eaeaa432c490 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.partitions/partition.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.partitions/partition.pass.cpp
@@ -11,7 +11,7 @@
 // template<BidirectionalIterator Iter, Predicate<auto, Iter::value_type> Pred>
 //   requires ShuffleIterator<Iter>
 //         && CopyConstructible<Pred>
-//   Iter
+//   constexpr Iter  // constexpr in C++20
 //   partition(Iter first, Iter last, Pred pred);
 
 #include <algorithm>
@@ -23,11 +23,11 @@
 
 struct is_odd
 {
-    bool operator()(const int& i) const {return i & 1;}
+    TEST_CONSTEXPR bool operator()(const int& i) const {return i & 1;}
 };
 
 template <class Iter>
-void
+TEST_CONSTEXPR_CXX20 bool
 test()
 {
     // check mixed
@@ -92,6 +92,8 @@ test()
         assert(is_odd()(*i));
     for (int* i = base(r); i < ia+sa; ++i)
         assert(!is_odd()(*i));
+
+    return true;
 }
 
 int main(int, char**)
@@ -100,5 +102,11 @@ int main(int, char**)
     test<random_access_iterator<int*> >();
     test<int*>();
 
-  return 0;
+#if TEST_STD_VER >= 20
+    static_assert(test<bidirectional_iterator<int*>>());
+    static_assert(test<random_access_iterator<int*>>());
+    static_assert(test<int*>());
+#endif
+
+    return 0;
 }

diff  --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse.pass.cpp
index e7930a57acf0f..86641e6f3b329 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse.pass.cpp
@@ -10,7 +10,7 @@
 
 // template<BidirectionalIterator Iter>
 //   requires HasSwap<Iter::reference, Iter::reference>
-//   void
+//   constexpr void  // constexpr in C++20
 //   reverse(Iter first, Iter last);
 
 #include <algorithm>
@@ -20,7 +20,7 @@
 #include "test_iterators.h"
 
 template <class Iter>
-void
+TEST_CONSTEXPR_CXX20 bool
 test()
 {
     int ia[] = {0};
@@ -50,6 +50,8 @@ test()
     assert(id[1] == 2);
     assert(id[2] == 1);
     assert(id[3] == 0);
+
+    return true;
 }
 
 int main(int, char**)
@@ -58,5 +60,11 @@ int main(int, char**)
     test<random_access_iterator<int*> >();
     test<int*>();
 
-  return 0;
+#if TEST_STD_VER >= 20
+    static_assert(test<bidirectional_iterator<int*>>());
+    static_assert(test<random_access_iterator<int*>>());
+    static_assert(test<int*>());
+#endif
+
+    return 0;
 }

diff  --git a/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/next_permutation.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/next_permutation.pass.cpp
index ba0c3c6532ab9..541e7e7858191 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/next_permutation.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/next_permutation.pass.cpp
@@ -11,7 +11,7 @@
 // template<BidirectionalIterator Iter>
 //   requires ShuffleIterator<Iter>
 //         && LessThanComparable<Iter::value_type>
-//   bool
+//   constexpr bool  // constexpr in C++20
 //   next_permutation(Iter first, Iter last);
 
 #include <algorithm>
@@ -22,7 +22,7 @@
 
 #include <cstdio>
 
-int factorial(int x)
+TEST_CONSTEXPR_CXX14 int factorial(int x)
 {
     int r = 1;
     for (; x; --x)
@@ -31,7 +31,7 @@ int factorial(int x)
 }
 
 template <class Iter>
-void
+TEST_CONSTEXPR_CXX20 bool
 test()
 {
     int ia[] = {1, 2, 3, 4, 5, 6};
@@ -56,6 +56,7 @@ test()
         } while (x);
         assert(count == factorial(e));
     }
+    return true;
 }
 
 int main(int, char**)
@@ -64,5 +65,11 @@ int main(int, char**)
     test<random_access_iterator<int*> >();
     test<int*>();
 
-  return 0;
+#if TEST_STD_VER >= 20
+    static_assert(test<bidirectional_iterator<int*>>());
+    static_assert(test<random_access_iterator<int*>>());
+    static_assert(test<int*>());
+#endif
+
+    return 0;
 }

diff  --git a/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/next_permutation_comp.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/next_permutation_comp.pass.cpp
index 71e5313ebf5fc..0ce7ef41593dc 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/next_permutation_comp.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/next_permutation_comp.pass.cpp
@@ -11,7 +11,7 @@
 // template<BidirectionalIterator Iter, StrictWeakOrder<auto, Iter::value_type> Compare>
 //   requires ShuffleIterator<Iter>
 //         && CopyConstructible<Compare>
-//   bool
+//   constexpr bool  // constexpr in C++20
 //   next_permutation(Iter first, Iter last, Compare comp);
 
 #include <algorithm>
@@ -23,7 +23,7 @@
 
 #include <cstdio>
 
-int factorial(int x)
+TEST_CONSTEXPR_CXX14 int factorial(int x)
 {
     int r = 1;
     for (; x; --x)
@@ -32,7 +32,7 @@ int factorial(int x)
 }
 
 template <class Iter>
-void
+TEST_CONSTEXPR_CXX20 bool
 test()
 {
     typedef std::greater<int> C;
@@ -58,6 +58,7 @@ test()
         } while (x);
         assert(count == factorial(e));
     }
+    return true;
 }
 
 int main(int, char**)
@@ -66,5 +67,11 @@ int main(int, char**)
     test<random_access_iterator<int*> >();
     test<int*>();
 
-  return 0;
+#if TEST_STD_VER >= 20
+    static_assert(test<bidirectional_iterator<int*>>());
+    static_assert(test<random_access_iterator<int*>>());
+    static_assert(test<int*>());
+#endif
+
+    return 0;
 }

diff  --git a/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/prev_permutation.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/prev_permutation.pass.cpp
index 9d1f0869f8ef7..749dfb360039b 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/prev_permutation.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/prev_permutation.pass.cpp
@@ -11,7 +11,7 @@
 // template<BidirectionalIterator Iter>
 //   requires ShuffleIterator<Iter>
 //         && LessThanComparable<Iter::value_type>
-//   bool
+//   constexpr bool  // constexpr in C++20
 //   prev_permutation(Iter first, Iter last);
 
 #include <algorithm>
@@ -22,7 +22,7 @@
 
 #include <cstdio>
 
-int factorial(int x)
+TEST_CONSTEXPR_CXX14 int factorial(int x)
 {
     int r = 1;
     for (; x; --x)
@@ -31,7 +31,7 @@ int factorial(int x)
 }
 
 template <class Iter>
-void
+TEST_CONSTEXPR_CXX20 bool
 test()
 {
     int ia[] = {6, 5, 4, 3, 2, 1};
@@ -56,6 +56,7 @@ test()
         } while (x);
         assert(count == factorial(e));
     }
+    return true;
 }
 
 int main(int, char**)
@@ -64,5 +65,11 @@ int main(int, char**)
     test<random_access_iterator<int*> >();
     test<int*>();
 
-  return 0;
+#if TEST_STD_VER >= 20
+    static_assert(test<bidirectional_iterator<int*>>());
+    static_assert(test<random_access_iterator<int*>>());
+    static_assert(test<int*>());
+#endif
+
+    return 0;
 }

diff  --git a/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/prev_permutation_comp.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/prev_permutation_comp.pass.cpp
index 59b314c510434..6d8539c7cd15d 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/prev_permutation_comp.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.permutation.generators/prev_permutation_comp.pass.cpp
@@ -11,7 +11,7 @@
 // template<BidirectionalIterator Iter, StrictWeakOrder<auto, Iter::value_type> Compare>
 //   requires ShuffleIterator<Iter>
 //         && CopyConstructible<Compare>
-//   bool
+//   constexpr bool  // constexpr in C++20
 //   prev_permutation(Iter first, Iter last, Compare comp);
 
 #include <algorithm>
@@ -23,7 +23,7 @@
 
 #include <cstdio>
 
-int factorial(int x)
+TEST_CONSTEXPR_CXX14 int factorial(int x)
 {
     int r = 1;
     for (; x; --x)
@@ -32,7 +32,7 @@ int factorial(int x)
 }
 
 template <class Iter>
-void
+TEST_CONSTEXPR_CXX20 bool
 test()
 {
     typedef std::greater<int> C;
@@ -58,6 +58,7 @@ test()
         } while (x);
         assert(count == factorial(e));
     }
+    return true;
 }
 
 int main(int, char**)
@@ -66,5 +67,11 @@ int main(int, char**)
     test<random_access_iterator<int*> >();
     test<int*>();
 
-  return 0;
+#if TEST_STD_VER >= 20
+    static_assert(test<bidirectional_iterator<int*>>());
+    static_assert(test<random_access_iterator<int*>>());
+    static_assert(test<int*>());
+#endif
+
+    return 0;
 }


        


More information about the llvm-branch-commits mailing list