From e8ea8296fccc6d9345817ece7d2f4ea3b35a55e1 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Mon, 22 Jan 2018 21:43:04 +0000 Subject: [PATCH] Another batch of P0202 constepr algirithms. remove/remove_if/remove_copy/remove_copy_if/reverse_copy, and tests (commented out) for rotate_copy, because that depends on std::copy llvm-svn: 323152 --- libcxx/include/algorithm | 20 +++++++++---------- .../alg.remove/remove.pass.cpp | 18 ++++++++++++++++- .../alg.remove/remove_copy.pass.cpp | 21 +++++++++++++++++++- .../alg.remove/remove_copy_if.pass.cpp | 23 ++++++++++++++++++++-- .../alg.remove/remove_if.pass.cpp | 20 +++++++++++++++++-- .../alg.reverse/reverse_copy.pass.cpp | 20 ++++++++++++++++++- .../alg.rotate/rotate_copy.pass.cpp | 23 +++++++++++++++++++++- 7 files changed, 127 insertions(+), 18 deletions(-) diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index c00845f..3dda30d 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -236,19 +236,19 @@ template generate_n(OutputIterator first, Size n, Generator gen); template - ForwardIterator + constexpr ForwardIterator // constexpr in C++20 remove(ForwardIterator first, ForwardIterator last, const T& value); template - ForwardIterator + constexpr ForwardIterator // constexpr in C++20 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred); template - OutputIterator + constexpr OutputIterator // constexpr in C++20 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value); template - OutputIterator + constexpr OutputIterator // constexpr in C++20 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred); template @@ -272,7 +272,7 @@ template reverse(BidirectionalIterator first, BidirectionalIterator last); template - OutputIterator + constexpr OutputIterator // constexpr in C++20 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result); template @@ -2098,7 +2098,7 @@ generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen) // remove template -_ForwardIterator +_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) { __first = _VSTD::find(__first, __last, __value_); @@ -2120,7 +2120,7 @@ remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) // remove_if template -_ForwardIterator +_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) { __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type> @@ -2143,7 +2143,7 @@ remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) // remove_copy template -inline _LIBCPP_INLINE_VISIBILITY +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_) { @@ -2161,7 +2161,7 @@ remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __res // remove_copy_if template -inline _LIBCPP_INLINE_VISIBILITY +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred) { @@ -2327,7 +2327,7 @@ reverse(_BidirectionalIterator __first, _BidirectionalIterator __last) // reverse_copy template -inline _LIBCPP_INLINE_VISIBILITY +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result) { diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove.pass.cpp index a77a9ed..557984f3 100644 --- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove.pass.cpp +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove.pass.cpp @@ -12,7 +12,7 @@ // template // requires OutputIterator::type> // && HasEqualTo -// Iter +// constexpr Iter // constexpr after C++17 // remove(Iter first, Iter last, const T& value); #include @@ -22,6 +22,18 @@ #include "test_macros.h" #include "test_iterators.h" +#if TEST_STD_VER > 17 +TEST_CONSTEXPR bool test_constexpr() { + int ia[] = {1, 3, 5, 2, 5, 6}; + + auto it = std::remove(std::begin(ia), std::end(ia), 5); + + return (std::begin(ia) + std::size(ia) - 2) == it // we removed two elements + && std::none_of(std::begin(ia), it, [](int a) {return a == 5; }) + ; + } +#endif + template void test() @@ -75,4 +87,8 @@ int main() test1*> >(); test1*>(); #endif // TEST_STD_VER >= 11 + +#if TEST_STD_VER > 17 + static_assert(test_constexpr()); +#endif } diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_copy.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_copy.pass.cpp index bf5f79c..ae7b341 100644 --- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_copy.pass.cpp +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_copy.pass.cpp @@ -11,14 +11,29 @@ // template OutIter, class T> // requires HasEqualTo -// OutIter +// constexpr OutIter // constexpr after C++17 // remove_copy(InIter first, InIter last, OutIter result, const T& value); #include #include +#include "test_macros.h" #include "test_iterators.h" +#if TEST_STD_VER > 17 +TEST_CONSTEXPR bool test_constexpr() { + int ia[] = {1, 3, 5, 2, 5, 6}; + int ib[std::size(ia)] = {0}; + + auto it = std::remove_copy(std::begin(ia), std::end(ia), std::begin(ib), 5); + + return std::distance(std::begin(ib), it) == (std::size(ia) - 2) // we removed two elements + && std::none_of(std::begin(ib), it, [](int a) {return a == 5;}) + && std::all_of (it, std::end(ib), [](int a) {return a == 0;}) + ; + } +#endif + template void test() @@ -67,4 +82,8 @@ int main() test >(); test >(); test(); + +#if TEST_STD_VER > 17 + static_assert(test_constexpr()); +#endif } diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_copy_if.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_copy_if.pass.cpp index 8532998..111c59e 100644 --- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_copy_if.pass.cpp +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_copy_if.pass.cpp @@ -12,16 +12,31 @@ // template OutIter, // Predicate Pred> // requires CopyConstructible -// OutIter +// constexpr OutIter // constexpr after C++17 // remove_copy_if(InIter first, InIter last, OutIter result, Pred pred); #include #include #include +#include "test_macros.h" #include "test_iterators.h" -bool equalToTwo(int v) { return v == 2; } +TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; } + +#if TEST_STD_VER > 17 +TEST_CONSTEXPR bool test_constexpr() { + int ia[] = {1, 3, 5, 2, 5, 6}; + int ib[std::size(ia)] = {0}; + + auto it = std::remove_copy_if(std::begin(ia), std::end(ia), std::begin(ib), equalToTwo); + + return std::distance(std::begin(ib), it) == (std::size(ia) - 1) // we removed one element + && std::none_of(std::begin(ib), it, equalToTwo) + && std::all_of (it, std::end(ib), [](int a) {return a == 0;}) + ; + } +#endif template void @@ -72,4 +87,8 @@ int main() test >(); test >(); test(); + +#if TEST_STD_VER > 17 + static_assert(test_constexpr()); +#endif } diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_if.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_if.pass.cpp index c40f3e7..d8123ee 100644 --- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_if.pass.cpp +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_if.pass.cpp @@ -12,7 +12,7 @@ // template Pred> // requires OutputIterator::type> // && CopyConstructible -// Iter +// constexpr Iter // constexpr after C++17 // remove_if(Iter first, Iter last, Pred pred); #include @@ -24,7 +24,19 @@ #include "test_iterators.h" #include "counting_predicates.hpp" -bool equal2 ( int i ) { return i == 2; } +TEST_CONSTEXPR bool equal2 ( int i ) { return i == 2; } + +#if TEST_STD_VER > 17 +TEST_CONSTEXPR bool test_constexpr() { + int ia[] = {1, 3, 5, 2, 5, 6}; + + auto it = std::remove_if(std::begin(ia), std::end(ia), equal2); + + return (std::begin(ia) + std::size(ia) - 1) == it // we removed one element + && std::none_of(std::begin(ia), it, equal2) + ; + } +#endif template void @@ -90,4 +102,8 @@ int main() test1*> >(); test1*>(); #endif // TEST_STD_VER >= 11 + +#if TEST_STD_VER > 17 + static_assert(test_constexpr()); +#endif } diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse_copy.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse_copy.pass.cpp index 70840d1..ec9aa9e 100644 --- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse_copy.pass.cpp +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse_copy.pass.cpp @@ -10,14 +10,28 @@ // // template OutIter> -// OutIter +// constexpr OutIter // constexpr after C++17 // reverse_copy(InIter first, InIter last, OutIter result); #include #include +#include "test_macros.h" #include "test_iterators.h" +#if TEST_STD_VER > 17 +TEST_CONSTEXPR bool test_constexpr() { + int ia[] = {1, 3, 5, 2, 5, 6}; + int ib[std::size(ia)] = {0}; + + auto it = std::reverse_copy(std::begin(ia), std::end(ia), std::begin(ib)); + + return std::distance(std::begin(ib), it) == std::size(ia) + && std::equal (std::begin(ia), std::end(ia), std::rbegin(ib)) + ; + } +#endif + template void test() @@ -78,4 +92,8 @@ int main() test >(); test >(); test(); + +#if TEST_STD_VER > 17 + static_assert(test_constexpr()); +#endif } diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate_copy.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate_copy.pass.cpp index f2ad535a..2294c79 100644 --- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate_copy.pass.cpp +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate_copy.pass.cpp @@ -10,14 +10,31 @@ // // template OutIter> -// OutIter +// constexpr OutIter // constexpr after C++17 // rotate_copy(InIter first, InIter middle, InIter last, OutIter result); #include #include +#include "test_macros.h" #include "test_iterators.h" +// #if TEST_STD_VER > 17 +// TEST_CONSTEXPR bool test_constexpr() { +// int ia[] = {1, 3, 5, 2, 5, 6}; +// int ib[std::size(ia)] = {0}; +// +// const size_t N = 2; +// const auto middle = std::begin(ia) + N; +// auto it = std::rotate_copy(std::begin(ia), middle, std::end(ia), std::begin(ib)); +// +// return std::distance(std::begin(ib), it) == std::size(ia) +// && std::equal (std::begin(ia), middle, std::begin(ib) + std::size(ia) - N) +// && std::equal (middle, std::end(ia), std::begin(ib)) +// ; +// } +// #endif + template void test() @@ -131,4 +148,8 @@ int main() test >(); test >(); test(); + +// #if TEST_STD_VER > 17 +// static_assert(test_constexpr()); +// #endif } -- 2.7.4