More P0202 constexpr work. This commit adds fill/fill_n/generate/generate_n/unique/unique_copy. I removed a specialization of fill_n that recognized when we were dealing with raw pointers and 1 byte trivially-assignable types and did a memset, because the compiler will do that optimization for us.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@323050 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow
2018-01-20 20:14:32 +00:00
parent a75d61347e
commit 48a4655868
9 changed files with 173 additions and 44 deletions

View File

@@ -219,19 +219,19 @@ template <class InputIterator, class OutputIterator, class Predicate, class T>
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>
void constexpr void // constexpr in C++20
fill(ForwardIterator first, ForwardIterator last, const T& value); fill(ForwardIterator first, ForwardIterator last, const T& value);
template <class OutputIterator, class Size, class T> template <class OutputIterator, class Size, class T>
OutputIterator constexpr OutputIterator // constexpr in C++20
fill_n(OutputIterator first, Size n, const T& value); fill_n(OutputIterator first, Size n, const T& value);
template <class ForwardIterator, class Generator> template <class ForwardIterator, class Generator>
void constexpr void // constexpr in C++20
generate(ForwardIterator first, ForwardIterator last, Generator gen); generate(ForwardIterator first, ForwardIterator last, Generator gen);
template <class OutputIterator, class Size, class Generator> template <class OutputIterator, class Size, class Generator>
OutputIterator constexpr OutputIterator // constexpr in C++20
generate_n(OutputIterator first, Size n, Generator gen); generate_n(OutputIterator first, Size n, Generator gen);
template <class ForwardIterator, class T> template <class ForwardIterator, class T>
@@ -2025,7 +2025,7 @@ replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator _
// fill_n // fill_n
template <class _OutputIterator, class _Size, class _Tp> template <class _OutputIterator, class _Size, class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
_OutputIterator _OutputIterator
__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_) __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
{ {
@@ -2034,24 +2034,8 @@ __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
return __first; return __first;
} }
template <class _Tp, class _Size, class _Up>
inline _LIBCPP_INLINE_VISIBILITY
typename enable_if
<
is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
!is_same<_Tp, bool>::value &&
is_integral<_Up>::value && sizeof(_Up) == 1,
_Tp*
>::type
__fill_n(_Tp* __first, _Size __n,_Up __value_)
{
if (__n > 0)
_VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
return __first + __n;
}
template <class _OutputIterator, class _Size, class _Tp> template <class _OutputIterator, class _Size, class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
_OutputIterator _OutputIterator
fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_) fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
{ {
@@ -2061,7 +2045,7 @@ fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
// fill // fill
template <class _ForwardIterator, class _Tp> template <class _ForwardIterator, class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
void void
__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag) __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
{ {
@@ -2070,7 +2054,7 @@ __fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, f
} }
template <class _RandomAccessIterator, class _Tp> template <class _RandomAccessIterator, class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
void void
__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag) __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
{ {
@@ -2078,7 +2062,7 @@ __fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& _
} }
template <class _ForwardIterator, class _Tp> template <class _ForwardIterator, class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
void void
fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
{ {
@@ -2088,7 +2072,7 @@ fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
// generate // generate
template <class _ForwardIterator, class _Generator> template <class _ForwardIterator, class _Generator>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
void void
generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen) generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
{ {
@@ -2099,7 +2083,7 @@ generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
// generate_n // generate_n
template <class _OutputIterator, class _Size, class _Generator> template <class _OutputIterator, class _Size, class _Generator>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
_OutputIterator _OutputIterator
generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen) generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
{ {
@@ -2194,7 +2178,7 @@ remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __
// unique // unique
template <class _ForwardIterator, class _BinaryPredicate> template <class _ForwardIterator, class _BinaryPredicate>
_ForwardIterator _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
{ {
__first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type> __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
@@ -2213,7 +2197,7 @@ unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pre
} }
template <class _ForwardIterator> template <class _ForwardIterator>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
_ForwardIterator _ForwardIterator
unique(_ForwardIterator __first, _ForwardIterator __last) unique(_ForwardIterator __first, _ForwardIterator __last)
{ {
@@ -2224,7 +2208,7 @@ unique(_ForwardIterator __first, _ForwardIterator __last)
// unique_copy // unique_copy
template <class _BinaryPredicate, class _InputIterator, class _OutputIterator> template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
_OutputIterator _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred, __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
input_iterator_tag, output_iterator_tag) input_iterator_tag, output_iterator_tag)
{ {
@@ -2247,7 +2231,7 @@ __unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __r
} }
template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator> template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
_OutputIterator _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred, __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
forward_iterator_tag, output_iterator_tag) forward_iterator_tag, output_iterator_tag)
{ {
@@ -2270,7 +2254,7 @@ __unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator
} }
template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator> template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
_ForwardIterator _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred, __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
input_iterator_tag, forward_iterator_tag) input_iterator_tag, forward_iterator_tag)
{ {
@@ -2286,7 +2270,7 @@ __unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __
} }
template <class _InputIterator, class _OutputIterator, class _BinaryPredicate> template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
_OutputIterator _OutputIterator
unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred) unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
{ {
@@ -2297,7 +2281,7 @@ unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __res
} }
template <class _InputIterator, class _OutputIterator> template <class _InputIterator, class _OutputIterator>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
_OutputIterator _OutputIterator
unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
{ {

View File

@@ -11,14 +11,26 @@
// template<ForwardIterator Iter, class T> // template<ForwardIterator Iter, class T>
// requires OutputIterator<Iter, const T&> // requires OutputIterator<Iter, const T&>
// void // constexpr void // constexpr after C++17
// fill(Iter first, Iter last, const T& value); // fill(Iter first, Iter last, const T& 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};
std::fill(std::begin(ia), std::end(ia), 5);
return std::all_of(std::begin(ia), std::end(ia), [](int a) {return a == 5; })
;
}
#endif
template <class Iter> template <class Iter>
void void
test_char() test_char()
@@ -56,4 +68,8 @@ int main()
test_int<bidirectional_iterator<int*> >(); test_int<bidirectional_iterator<int*> >();
test_int<random_access_iterator<int*> >(); test_int<random_access_iterator<int*> >();
test_int<int*>(); test_int<int*>();
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
} }

View File

@@ -11,15 +11,29 @@
// template<class Iter, IntegralLike Size, class T> // template<class Iter, IntegralLike Size, class T>
// requires OutputIterator<Iter, const T&> // requires OutputIterator<Iter, const T&>
// OutputIterator // constexpr OutputIterator // constexpr after C++17
// fill_n(Iter first, Size n, const T& value); // fill_n(Iter first, Size n, const T& value);
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include "test_macros.h"
#include "test_iterators.h" #include "test_iterators.h"
#include "user_defined_integral.hpp" #include "user_defined_integral.hpp"
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool test_constexpr() {
const size_t N = 5;
int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger than N
auto it = std::fill_n(std::begin(ib), N, 5);
return it == (std::begin(ib) + N)
&& std::all_of(std::begin(ib), it, [](int a) {return a == 5; })
&& *it == 0 // don't overwrite the last value in the output array
;
}
#endif
typedef UserDefinedIntegral<unsigned> UDI; typedef UserDefinedIntegral<unsigned> UDI;
template <class Iter> template <class Iter>
@@ -153,4 +167,8 @@ int main()
test5(); test5();
test6(); test6();
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
} }

View File

@@ -12,19 +12,33 @@
// template<ForwardIterator Iter, Callable Generator> // template<ForwardIterator Iter, Callable Generator>
// requires OutputIterator<Iter, Generator::result_type> // requires OutputIterator<Iter, Generator::result_type>
// && CopyConstructible<Generator> // && CopyConstructible<Generator>
// void // constexpr void // constexpr after c++17
// generate(Iter first, Iter last, Generator gen); // generate(Iter first, Iter last, Generator gen);
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include "test_macros.h"
#include "test_iterators.h" #include "test_iterators.h"
struct gen_test struct gen_test
{ {
int operator()() const {return 1;} TEST_CONSTEXPR int operator()() const {return 1;}
}; };
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool test_constexpr() {
int ia[] = {0, 1, 2, 3, 4};
std::generate(std::begin(ia), std::end(ia), gen_test());
return std::all_of(std::begin(ia), std::end(ia), [](int x) { return x == 1; })
;
}
#endif
template <class Iter> template <class Iter>
void void
test() test()
@@ -44,4 +58,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
} }

View File

@@ -12,7 +12,7 @@
// template<class Iter, IntegralLike Size, Callable Generator> // template<class Iter, IntegralLike Size, Callable Generator>
// requires OutputIterator<Iter, Generator::result_type> // requires OutputIterator<Iter, Generator::result_type>
// && CopyConstructible<Generator> // && CopyConstructible<Generator>
// void // constexpr void // constexpr after c++17
// generate_n(Iter first, Size n, Generator gen); // generate_n(Iter first, Size n, Generator gen);
#ifdef _MSC_VER #ifdef _MSC_VER
@@ -28,9 +28,25 @@
struct gen_test struct gen_test
{ {
int operator()() const {return 2;} TEST_CONSTEXPR int operator()() const {return 2;}
}; };
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool test_constexpr() {
const size_t N = 5;
int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger than N
auto it = std::generate_n(std::begin(ib), N, gen_test());
return it == (std::begin(ib) + N)
&& std::all_of(std::begin(ib), it, [](int x) { return x == 2; })
&& *it == 0 // don't overwrite the last value in the output array
;
}
#endif
template <class Iter, class Size> template <class Iter, class Size>
void void
test2() test2()
@@ -64,4 +80,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
} }

View File

@@ -12,7 +12,7 @@
// template<ForwardIterator Iter> // template<ForwardIterator Iter>
// requires OutputIterator<Iter, Iter::reference> // requires OutputIterator<Iter, Iter::reference>
// && EqualityComparable<Iter::value_type> // && EqualityComparable<Iter::value_type>
// Iter // constexpr Iter // constexpr after C++17
// unique(Iter first, Iter last); // unique(Iter first, Iter last);
#include <algorithm> #include <algorithm>
@@ -22,6 +22,19 @@
#include "test_macros.h" #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, 1, 3, 4};
const int expected[] = {0, 1, 3, 4};
const size_t N = 4;
auto it = std::unique(std::begin(ia), std::end(ia));
return it == (std::begin(ia) + N)
&& std::equal(std::begin(ia), it, std::begin(expected), std::end(expected))
;
}
#endif
template <class Iter> template <class Iter>
void void
test() test()
@@ -182,4 +195,8 @@ int main()
test1<random_access_iterator<Ptr*> >(); test1<random_access_iterator<Ptr*> >();
test1<Ptr*>(); test1<Ptr*>();
#endif #endif
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
} }

View File

@@ -14,14 +14,29 @@
// && EqualityComparable<InIter::value_type> // && EqualityComparable<InIter::value_type>
// && HasAssign<InIter::value_type, InIter::reference> // && HasAssign<InIter::value_type, InIter::reference>
// && Constructible<InIter::value_type, InIter::reference> // && Constructible<InIter::value_type, InIter::reference>
// OutIter // constexpr OutIter // constexpr after C++17
// unique_copy(InIter first, InIter last, OutIter result); // unique_copy(InIter first, InIter last, OutIter result);
#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, 2, 4};
int ib[] = {0, 0, 0, 0, 0};
const int expected[] = {0, 1, 2, 4};
auto it = std::unique_copy(std::begin(ia), std::end(ia), std::begin(ib));
return it == (std::begin(ib) + std::size(expected))
&& *it == 0 // don't overwrite final value in output
&& 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()
@@ -122,4 +137,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
} }

View File

@@ -15,14 +15,30 @@
// && HasAssign<InIter::value_type, InIter::reference> // && HasAssign<InIter::value_type, InIter::reference>
// && Constructible<InIter::value_type, InIter::reference> // && Constructible<InIter::value_type, InIter::reference>
// && CopyConstructible<Pred> // && CopyConstructible<Pred>
// OutIter // constexpr OutIter // constexpr after C++17
// unique_copy(InIter first, InIter last, OutIter result, Pred pred); // unique_copy(InIter first, InIter last, OutIter result, Pred pred);
#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, 2, 4};
int ib[] = {0, 0, 0, 0, 0};
const int expected[] = {0, 1, 2, 4};
auto it = std::unique_copy(std::begin(ia), std::end(ia), std::begin(ib),
[](int a, int b) {return a == b; });
return it == (std::begin(ib) + std::size(expected))
&& *it == 0 // don't overwrite final value in output
&& std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))
;
}
#endif
struct count_equal struct count_equal
{ {
static unsigned count; static unsigned count;
@@ -149,4 +165,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
} }

View File

@@ -12,7 +12,7 @@
// template<ForwardIterator Iter, EquivalenceRelation<auto, Iter::value_type> Pred> // template<ForwardIterator Iter, EquivalenceRelation<auto, Iter::value_type> Pred>
// requires OutputIterator<Iter, RvalueOf<Iter::reference>::type> // requires OutputIterator<Iter, RvalueOf<Iter::reference>::type>
// && CopyConstructible<Pred> // && CopyConstructible<Pred>
// Iter // constexpr Iter // constexpr after C++17
// unique(Iter first, Iter last, Pred pred); // unique(Iter first, Iter last, Pred pred);
#include <algorithm> #include <algorithm>
@@ -22,6 +22,19 @@
#include "test_macros.h" #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, 1, 3, 4};
const int expected[] = {0, 1, 3, 4};
const size_t N = 4;
auto it = std::unique(std::begin(ia), std::end(ia), [](int a, int b) {return a == b; });
return it == (std::begin(ia) + N)
&& std::equal(std::begin(ia), it, std::begin(expected), std::end(expected))
;
}
#endif
struct count_equal struct count_equal
{ {
static unsigned count; static unsigned count;
@@ -224,4 +237,8 @@ int main()
test1<random_access_iterator<Ptr*> >(); test1<random_access_iterator<Ptr*> >();
test1<Ptr*>(); test1<Ptr*>();
#endif #endif
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
} }