Skip to content

Commit e8ea829

Browse files
committed
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
1 parent d2a9592 commit e8ea829

File tree

7 files changed

+127
-18
lines changed

7 files changed

+127
-18
lines changed

libcxx/include/algorithm

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,19 @@ template <class OutputIterator, class Size, class Generator>
236236
generate_n(OutputIterator first, Size n, Generator gen);
237237
238238
template <class ForwardIterator, class T>
239-
ForwardIterator
239+
constexpr ForwardIterator // constexpr in C++20
240240
remove(ForwardIterator first, ForwardIterator last, const T& value);
241241
242242
template <class ForwardIterator, class Predicate>
243-
ForwardIterator
243+
constexpr ForwardIterator // constexpr in C++20
244244
remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
245245
246246
template <class InputIterator, class OutputIterator, class T>
247-
OutputIterator
247+
constexpr OutputIterator // constexpr in C++20
248248
remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
249249
250250
template <class InputIterator, class OutputIterator, class Predicate>
251-
OutputIterator
251+
constexpr OutputIterator // constexpr in C++20
252252
remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
253253
254254
template <class ForwardIterator>
@@ -272,7 +272,7 @@ template <class BidirectionalIterator>
272272
reverse(BidirectionalIterator first, BidirectionalIterator last);
273273
274274
template <class BidirectionalIterator, class OutputIterator>
275-
OutputIterator
275+
constexpr OutputIterator // constexpr in C++20
276276
reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
277277
278278
template <class ForwardIterator>
@@ -2098,7 +2098,7 @@ generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
20982098
// remove
20992099

21002100
template <class _ForwardIterator, class _Tp>
2101-
_ForwardIterator
2101+
_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
21022102
remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
21032103
{
21042104
__first = _VSTD::find(__first, __last, __value_);
@@ -2120,7 +2120,7 @@ remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
21202120
// remove_if
21212121

21222122
template <class _ForwardIterator, class _Predicate>
2123-
_ForwardIterator
2123+
_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
21242124
remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
21252125
{
21262126
__first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
@@ -2143,7 +2143,7 @@ remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
21432143
// remove_copy
21442144

21452145
template <class _InputIterator, class _OutputIterator, class _Tp>
2146-
inline _LIBCPP_INLINE_VISIBILITY
2146+
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
21472147
_OutputIterator
21482148
remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
21492149
{
@@ -2161,7 +2161,7 @@ remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __res
21612161
// remove_copy_if
21622162

21632163
template <class _InputIterator, class _OutputIterator, class _Predicate>
2164-
inline _LIBCPP_INLINE_VISIBILITY
2164+
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
21652165
_OutputIterator
21662166
remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
21672167
{
@@ -2327,7 +2327,7 @@ reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
23272327
// reverse_copy
23282328

23292329
template <class _BidirectionalIterator, class _OutputIterator>
2330-
inline _LIBCPP_INLINE_VISIBILITY
2330+
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
23312331
_OutputIterator
23322332
reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
23332333
{

libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove.pass.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// template<ForwardIterator Iter, class T>
1313
// requires OutputIterator<Iter, RvalueOf<Iter::reference>::type>
1414
// && HasEqualTo<Iter::value_type, T>
15-
// Iter
15+
// constexpr Iter // constexpr after C++17
1616
// remove(Iter first, Iter last, const T& value);
1717

1818
#include <algorithm>
@@ -22,6 +22,18 @@
2222
#include "test_macros.h"
2323
#include "test_iterators.h"
2424

25+
#if TEST_STD_VER > 17
26+
TEST_CONSTEXPR bool test_constexpr() {
27+
int ia[] = {1, 3, 5, 2, 5, 6};
28+
29+
auto it = std::remove(std::begin(ia), std::end(ia), 5);
30+
31+
return (std::begin(ia) + std::size(ia) - 2) == it // we removed two elements
32+
&& std::none_of(std::begin(ia), it, [](int a) {return a == 5; })
33+
;
34+
}
35+
#endif
36+
2537
template <class Iter>
2638
void
2739
test()
@@ -75,4 +87,8 @@ int main()
7587
test1<random_access_iterator<std::unique_ptr<int>*> >();
7688
test1<std::unique_ptr<int>*>();
7789
#endif // TEST_STD_VER >= 11
90+
91+
#if TEST_STD_VER > 17
92+
static_assert(test_constexpr());
93+
#endif
7894
}

libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_copy.pass.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,29 @@
1111

1212
// template<InputIterator InIter, OutputIterator<auto, InIter::reference> OutIter, class T>
1313
// requires HasEqualTo<InIter::value_type, T>
14-
// OutIter
14+
// constexpr OutIter // constexpr after C++17
1515
// remove_copy(InIter first, InIter last, OutIter result, const T& value);
1616

1717
#include <algorithm>
1818
#include <cassert>
1919

20+
#include "test_macros.h"
2021
#include "test_iterators.h"
2122

23+
#if TEST_STD_VER > 17
24+
TEST_CONSTEXPR bool test_constexpr() {
25+
int ia[] = {1, 3, 5, 2, 5, 6};
26+
int ib[std::size(ia)] = {0};
27+
28+
auto it = std::remove_copy(std::begin(ia), std::end(ia), std::begin(ib), 5);
29+
30+
return std::distance(std::begin(ib), it) == (std::size(ia) - 2) // we removed two elements
31+
&& std::none_of(std::begin(ib), it, [](int a) {return a == 5;})
32+
&& std::all_of (it, std::end(ib), [](int a) {return a == 0;})
33+
;
34+
}
35+
#endif
36+
2237
template <class InIter, class OutIter>
2338
void
2439
test()
@@ -67,4 +82,8 @@ int main()
6782
test<const int*, bidirectional_iterator<int*> >();
6883
test<const int*, random_access_iterator<int*> >();
6984
test<const int*, int*>();
85+
86+
#if TEST_STD_VER > 17
87+
static_assert(test_constexpr());
88+
#endif
7089
}

libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_copy_if.pass.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,31 @@
1212
// template<InputIterator InIter, OutputIterator<auto, InIter::reference> OutIter,
1313
// Predicate<auto, InIter::value_type> Pred>
1414
// requires CopyConstructible<Pred>
15-
// OutIter
15+
// constexpr OutIter // constexpr after C++17
1616
// remove_copy_if(InIter first, InIter last, OutIter result, Pred pred);
1717

1818
#include <algorithm>
1919
#include <functional>
2020
#include <cassert>
2121

22+
#include "test_macros.h"
2223
#include "test_iterators.h"
2324

24-
bool equalToTwo(int v) { return v == 2; }
25+
TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; }
26+
27+
#if TEST_STD_VER > 17
28+
TEST_CONSTEXPR bool test_constexpr() {
29+
int ia[] = {1, 3, 5, 2, 5, 6};
30+
int ib[std::size(ia)] = {0};
31+
32+
auto it = std::remove_copy_if(std::begin(ia), std::end(ia), std::begin(ib), equalToTwo);
33+
34+
return std::distance(std::begin(ib), it) == (std::size(ia) - 1) // we removed one element
35+
&& std::none_of(std::begin(ib), it, equalToTwo)
36+
&& std::all_of (it, std::end(ib), [](int a) {return a == 0;})
37+
;
38+
}
39+
#endif
2540

2641
template <class InIter, class OutIter>
2742
void
@@ -72,4 +87,8 @@ int main()
7287
test<const int*, bidirectional_iterator<int*> >();
7388
test<const int*, random_access_iterator<int*> >();
7489
test<const int*, int*>();
90+
91+
#if TEST_STD_VER > 17
92+
static_assert(test_constexpr());
93+
#endif
7594
}

libcxx/test/std/algorithms/alg.modifying.operations/alg.remove/remove_if.pass.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// template<ForwardIterator Iter, Predicate<auto, Iter::value_type> Pred>
1313
// requires OutputIterator<Iter, RvalueOf<Iter::reference>::type>
1414
// && CopyConstructible<Pred>
15-
// Iter
15+
// constexpr Iter // constexpr after C++17
1616
// remove_if(Iter first, Iter last, Pred pred);
1717

1818
#include <algorithm>
@@ -24,7 +24,19 @@
2424
#include "test_iterators.h"
2525
#include "counting_predicates.hpp"
2626

27-
bool equal2 ( int i ) { return i == 2; }
27+
TEST_CONSTEXPR bool equal2 ( int i ) { return i == 2; }
28+
29+
#if TEST_STD_VER > 17
30+
TEST_CONSTEXPR bool test_constexpr() {
31+
int ia[] = {1, 3, 5, 2, 5, 6};
32+
33+
auto it = std::remove_if(std::begin(ia), std::end(ia), equal2);
34+
35+
return (std::begin(ia) + std::size(ia) - 1) == it // we removed one element
36+
&& std::none_of(std::begin(ia), it, equal2)
37+
;
38+
}
39+
#endif
2840

2941
template <class Iter>
3042
void
@@ -90,4 +102,8 @@ int main()
90102
test1<random_access_iterator<std::unique_ptr<int>*> >();
91103
test1<std::unique_ptr<int>*>();
92104
#endif // TEST_STD_VER >= 11
105+
106+
#if TEST_STD_VER > 17
107+
static_assert(test_constexpr());
108+
#endif
93109
}

libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse_copy.pass.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,28 @@
1010
// <algorithm>
1111

1212
// template<BidirectionalIterator InIter, OutputIterator<auto, InIter::reference> OutIter>
13-
// OutIter
13+
// constexpr OutIter // constexpr after C++17
1414
// reverse_copy(InIter first, InIter last, OutIter result);
1515

1616
#include <algorithm>
1717
#include <cassert>
1818

19+
#include "test_macros.h"
1920
#include "test_iterators.h"
2021

22+
#if TEST_STD_VER > 17
23+
TEST_CONSTEXPR bool test_constexpr() {
24+
int ia[] = {1, 3, 5, 2, 5, 6};
25+
int ib[std::size(ia)] = {0};
26+
27+
auto it = std::reverse_copy(std::begin(ia), std::end(ia), std::begin(ib));
28+
29+
return std::distance(std::begin(ib), it) == std::size(ia)
30+
&& std::equal (std::begin(ia), std::end(ia), std::rbegin(ib))
31+
;
32+
}
33+
#endif
34+
2135
template <class InIter, class OutIter>
2236
void
2337
test()
@@ -78,4 +92,8 @@ int main()
7892
test<const int*, bidirectional_iterator<int*> >();
7993
test<const int*, random_access_iterator<int*> >();
8094
test<const int*, int*>();
95+
96+
#if TEST_STD_VER > 17
97+
static_assert(test_constexpr());
98+
#endif
8199
}

libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate_copy.pass.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,31 @@
1010
// <algorithm>
1111

1212
// template<ForwardIterator InIter, OutputIterator<auto, InIter::reference> OutIter>
13-
// OutIter
13+
// constexpr OutIter // constexpr after C++17
1414
// rotate_copy(InIter first, InIter middle, InIter last, OutIter result);
1515

1616
#include <algorithm>
1717
#include <cassert>
1818

19+
#include "test_macros.h"
1920
#include "test_iterators.h"
2021

22+
// #if TEST_STD_VER > 17
23+
// TEST_CONSTEXPR bool test_constexpr() {
24+
// int ia[] = {1, 3, 5, 2, 5, 6};
25+
// int ib[std::size(ia)] = {0};
26+
//
27+
// const size_t N = 2;
28+
// const auto middle = std::begin(ia) + N;
29+
// auto it = std::rotate_copy(std::begin(ia), middle, std::end(ia), std::begin(ib));
30+
//
31+
// return std::distance(std::begin(ib), it) == std::size(ia)
32+
// && std::equal (std::begin(ia), middle, std::begin(ib) + std::size(ia) - N)
33+
// && std::equal (middle, std::end(ia), std::begin(ib))
34+
// ;
35+
// }
36+
// #endif
37+
2138
template <class InIter, class OutIter>
2239
void
2340
test()
@@ -131,4 +148,8 @@ int main()
131148
test<const int*, bidirectional_iterator<int*> >();
132149
test<const int*, random_access_iterator<int*> >();
133150
test<const int*, int*>();
151+
152+
// #if TEST_STD_VER > 17
153+
// static_assert(test_constexpr());
154+
// #endif
134155
}

0 commit comments

Comments
 (0)