More P0202 constexpr-ifying in <algorithm>. This commit handles replace/replace_if/replace_copy/replace_copy_if.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@322975 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -202,20 +202,20 @@ template <class InputIterator1, class InputIterator2, class OutputIterator, clas
|
|||||||
OutputIterator result, BinaryOperation binary_op);
|
OutputIterator result, BinaryOperation binary_op);
|
||||||
|
|
||||||
template <class ForwardIterator, class T>
|
template <class ForwardIterator, class T>
|
||||||
void
|
constexpr void // constexpr in C++20
|
||||||
replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
|
replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
|
||||||
|
|
||||||
template <class ForwardIterator, class Predicate, class T>
|
template <class ForwardIterator, class Predicate, class T>
|
||||||
void
|
constexpr void // constexpr in C++20
|
||||||
replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
|
replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
|
||||||
|
|
||||||
template <class InputIterator, class OutputIterator, class T>
|
template <class InputIterator, class OutputIterator, class T>
|
||||||
OutputIterator
|
constexpr OutputIterator // constexpr in C++20
|
||||||
replace_copy(InputIterator first, InputIterator last, OutputIterator result,
|
replace_copy(InputIterator first, InputIterator last, OutputIterator result,
|
||||||
const T& old_value, const T& new_value);
|
const T& old_value, const T& new_value);
|
||||||
|
|
||||||
template <class InputIterator, class OutputIterator, class Predicate, class T>
|
template <class InputIterator, class OutputIterator, class Predicate, class T>
|
||||||
OutputIterator
|
constexpr OutputIterator // constexpr in C++20
|
||||||
replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
|
replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
|
||||||
|
|
||||||
template <class ForwardIterator, class T>
|
template <class ForwardIterator, class T>
|
||||||
@@ -1461,9 +1461,9 @@ __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
|
|||||||
if (!__pred(*__first1, *__first2))
|
if (!__pred(*__first1, *__first2))
|
||||||
break;
|
break;
|
||||||
if (__first1 == __last1)
|
if (__first1 == __last1)
|
||||||
return __first2 == __last2;
|
return __first2 == __last2;
|
||||||
else if (__first2 == __last2)
|
else if (__first2 == __last2)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
|
typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
|
||||||
_D1 __l1 = _VSTD::distance(__first1, __last1);
|
_D1 __l1 = _VSTD::distance(__first1, __last1);
|
||||||
@@ -1969,7 +1969,7 @@ transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __f
|
|||||||
// replace
|
// replace
|
||||||
|
|
||||||
template <class _ForwardIterator, class _Tp>
|
template <class _ForwardIterator, class _Tp>
|
||||||
inline _LIBCPP_INLINE_VISIBILITY
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
||||||
void
|
void
|
||||||
replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
|
replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
|
||||||
{
|
{
|
||||||
@@ -1981,7 +1981,7 @@ replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_valu
|
|||||||
// replace_if
|
// replace_if
|
||||||
|
|
||||||
template <class _ForwardIterator, class _Predicate, class _Tp>
|
template <class _ForwardIterator, class _Predicate, class _Tp>
|
||||||
inline _LIBCPP_INLINE_VISIBILITY
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
||||||
void
|
void
|
||||||
replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
|
replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
|
||||||
{
|
{
|
||||||
@@ -1993,7 +1993,7 @@ replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
|
|||||||
// replace_copy
|
// replace_copy
|
||||||
|
|
||||||
template <class _InputIterator, class _OutputIterator, class _Tp>
|
template <class _InputIterator, class _OutputIterator, class _Tp>
|
||||||
inline _LIBCPP_INLINE_VISIBILITY
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
||||||
_OutputIterator
|
_OutputIterator
|
||||||
replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
|
replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
|
||||||
const _Tp& __old_value, const _Tp& __new_value)
|
const _Tp& __old_value, const _Tp& __new_value)
|
||||||
@@ -2009,7 +2009,7 @@ replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __re
|
|||||||
// replace_copy_if
|
// replace_copy_if
|
||||||
|
|
||||||
template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
|
template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
|
||||||
inline _LIBCPP_INLINE_VISIBILITY
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
||||||
_OutputIterator
|
_OutputIterator
|
||||||
replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
|
replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
|
||||||
_Predicate __pred, const _Tp& __new_value)
|
_Predicate __pred, const _Tp& __new_value)
|
||||||
|
|||||||
@@ -13,14 +13,27 @@
|
|||||||
// requires OutputIterator<Iter, Iter::reference>
|
// requires OutputIterator<Iter, Iter::reference>
|
||||||
// && OutputIterator<Iter, const T&>
|
// && OutputIterator<Iter, const T&>
|
||||||
// && HasEqualTo<Iter::value_type, T>
|
// && HasEqualTo<Iter::value_type, T>
|
||||||
// void
|
// constexpr void // constexpr after C++17
|
||||||
// replace(Iter first, Iter last, const T& old_value, const T& new_value);
|
// replace(Iter first, Iter last, const T& old_value, const T& new_value);
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "test_macros.h"
|
||||||
#include "test_iterators.h"
|
#include "test_iterators.h"
|
||||||
|
|
||||||
|
|
||||||
|
#if TEST_STD_VER > 17
|
||||||
|
TEST_CONSTEXPR bool test_constexpr() {
|
||||||
|
int ia[] = {0, 1, 2, 3, 4};
|
||||||
|
const int expected[] = {0, 1, 5, 3, 4};
|
||||||
|
|
||||||
|
std::replace(std::begin(ia), std::end(ia), 2, 5);
|
||||||
|
return std::equal(std::begin(ia), std::end(ia), std::begin(expected), std::end(expected))
|
||||||
|
;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
template <class Iter>
|
template <class Iter>
|
||||||
void
|
void
|
||||||
test()
|
test()
|
||||||
@@ -41,4 +54,8 @@ int main()
|
|||||||
test<bidirectional_iterator<int*> >();
|
test<bidirectional_iterator<int*> >();
|
||||||
test<random_access_iterator<int*> >();
|
test<random_access_iterator<int*> >();
|
||||||
test<int*>();
|
test<int*>();
|
||||||
|
|
||||||
|
#if TEST_STD_VER > 17
|
||||||
|
static_assert(test_constexpr());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,15 +13,32 @@
|
|||||||
// requires OutputIterator<OutIter, InIter::reference>
|
// requires OutputIterator<OutIter, InIter::reference>
|
||||||
// && OutputIterator<OutIter, const T&>
|
// && OutputIterator<OutIter, const T&>
|
||||||
// && HasEqualTo<InIter::value_type, T>
|
// && HasEqualTo<InIter::value_type, T>
|
||||||
// OutIter
|
// constexpr OutIter // constexpr after C++17
|
||||||
// replace_copy(InIter first, InIter last, OutIter result, const T& old_value,
|
// replace_copy(InIter first, InIter last, OutIter result, const T& old_value,
|
||||||
// const T& new_value);
|
// const T& new_value);
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "test_macros.h"
|
||||||
#include "test_iterators.h"
|
#include "test_iterators.h"
|
||||||
|
|
||||||
|
|
||||||
|
#if TEST_STD_VER > 17
|
||||||
|
TEST_CONSTEXPR bool test_constexpr() {
|
||||||
|
int ia[] = {0, 1, 2, 3, 4};
|
||||||
|
int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger
|
||||||
|
const int expected[] = {0, 1, 5, 3, 4};
|
||||||
|
|
||||||
|
auto it = std::replace_copy(std::begin(ia), std::end(ia), std::begin(ib), 2, 5);
|
||||||
|
|
||||||
|
return it == (std::begin(ib) + std::size(ia))
|
||||||
|
&& *it == 0 // don't overwrite the last value in the output array
|
||||||
|
&& std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))
|
||||||
|
;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
template <class InIter, class OutIter>
|
template <class InIter, class OutIter>
|
||||||
void
|
void
|
||||||
test()
|
test()
|
||||||
@@ -69,4 +86,8 @@ int main()
|
|||||||
test<const int*, bidirectional_iterator<int*> >();
|
test<const int*, bidirectional_iterator<int*> >();
|
||||||
test<const int*, random_access_iterator<int*> >();
|
test<const int*, random_access_iterator<int*> >();
|
||||||
test<const int*, int*>();
|
test<const int*, int*>();
|
||||||
|
|
||||||
|
#if TEST_STD_VER > 17
|
||||||
|
static_assert(test_constexpr());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,16 +14,33 @@
|
|||||||
// requires OutputIterator<OutIter, InIter::reference>
|
// requires OutputIterator<OutIter, InIter::reference>
|
||||||
// && OutputIterator<OutIter, const T&>
|
// && OutputIterator<OutIter, const T&>
|
||||||
// && CopyConstructible<Pred>
|
// && CopyConstructible<Pred>
|
||||||
// OutIter
|
// constexpr OutIter // constexpr after C++17
|
||||||
// replace_copy_if(InIter first, InIter last, OutIter result, Pred pred, const T& new_value);
|
// replace_copy_if(InIter first, InIter last, OutIter result, Pred pred, const T& new_value);
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "test_macros.h"
|
||||||
#include "test_iterators.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[] = {0, 1, 2, 3, 4};
|
||||||
|
int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger
|
||||||
|
const int expected[] = {0, 1, 5, 3, 4};
|
||||||
|
|
||||||
|
auto it = std::replace_copy_if(std::begin(ia), std::end(ia), std::begin(ib), equalToTwo, 5);
|
||||||
|
|
||||||
|
return it == (std::begin(ib) + std::size(ia))
|
||||||
|
&& *it == 0 // don't overwrite the last value in the output array
|
||||||
|
&& std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))
|
||||||
|
;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
template <class InIter, class OutIter>
|
template <class InIter, class OutIter>
|
||||||
void
|
void
|
||||||
@@ -73,4 +90,8 @@ int main()
|
|||||||
test<const int*, bidirectional_iterator<int*> >();
|
test<const int*, bidirectional_iterator<int*> >();
|
||||||
test<const int*, random_access_iterator<int*> >();
|
test<const int*, random_access_iterator<int*> >();
|
||||||
test<const int*, int*>();
|
test<const int*, int*>();
|
||||||
|
|
||||||
|
#if TEST_STD_VER > 17
|
||||||
|
static_assert(test_constexpr());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,16 +13,29 @@
|
|||||||
// requires OutputIterator<Iter, Iter::reference>
|
// requires OutputIterator<Iter, Iter::reference>
|
||||||
// && OutputIterator<Iter, const T&>
|
// && OutputIterator<Iter, const T&>
|
||||||
// && CopyConstructible<Pred>
|
// && CopyConstructible<Pred>
|
||||||
// void
|
// constexpr void // constexpr after C++17
|
||||||
// replace_if(Iter first, Iter last, Pred pred, const T& new_value);
|
// replace_if(Iter first, Iter last, Pred pred, const T& new_value);
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "test_macros.h"
|
||||||
#include "test_iterators.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[] = {0, 1, 2, 3, 4};
|
||||||
|
const int expected[] = {0, 1, 5, 3, 4};
|
||||||
|
|
||||||
|
std::replace_if(std::begin(ia), std::end(ia), equalToTwo, 5);
|
||||||
|
return std::equal(std::begin(ia), std::end(ia), std::begin(expected), std::end(expected))
|
||||||
|
;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
template <class Iter>
|
template <class Iter>
|
||||||
void
|
void
|
||||||
@@ -44,4 +57,8 @@ int main()
|
|||||||
test<bidirectional_iterator<int*> >();
|
test<bidirectional_iterator<int*> >();
|
||||||
test<random_access_iterator<int*> >();
|
test<random_access_iterator<int*> >();
|
||||||
test<int*>();
|
test<int*>();
|
||||||
|
|
||||||
|
#if TEST_STD_VER > 17
|
||||||
|
static_assert(test_constexpr());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user