Commit ed920373 by Jonathan Wakely Committed by Jonathan Wakely

Implement new serial algorithms from Parallelism TS (P0024R2)

These new (non-parallel) algorithms were added to C++17 along with the
parallel algorithms, but were missing from libstdc++.

	* include/bits/algorithmfwd.h: Change title of doc group.
	* include/bits/stl_algo.h (for_each_n): Add new C++17 algorithm from
	P0024R2.
	* include/bits/stl_numeric.h: Define doc group and add algos to it.
	* include/std/numeric (__is_random_access_iter): New internal trait.
	(reduce, transform_reduce, exclusive_scan, inclusive_scan)
	(transform_exclusive_scan, transform_inclusive_scan): Likewise.
	* testsuite/25_algorithms/for_each/for_each_n.cc: New test.
	* testsuite/26_numerics/exclusive_scan/1.cc: New test.
	* testsuite/26_numerics/inclusive_scan/1.cc: New test.
	* testsuite/26_numerics/reduce/1.cc: New test.
	* testsuite/26_numerics/transform_exclusive_scan/1.cc: New test.
	* testsuite/26_numerics/transform_inclusive_scan/1.cc: New test.
	* testsuite/26_numerics/transform_reduce/1.cc: New test.
	* testsuite/util/testsuite_iterators.h (test_container::size()): New
	member function.

From-SVN: r272459
parent 1fe39f19
2019-06-18 Jonathan Wakely <jwakely@redhat.com>
* include/bits/algorithmfwd.h: Change title of doc group.
* include/bits/stl_algo.h (for_each_n): Add new C++17 algorithm from
P0024R2.
* include/bits/stl_numeric.h: Define doc group and add algos to it.
* include/std/numeric (__is_random_access_iter): New internal trait.
(reduce, transform_reduce, exclusive_scan, inclusive_scan)
(transform_exclusive_scan, transform_inclusive_scan): Likewise.
* testsuite/25_algorithms/for_each/for_each_n.cc: New test.
* testsuite/26_numerics/exclusive_scan/1.cc: New test.
* testsuite/26_numerics/inclusive_scan/1.cc: New test.
* testsuite/26_numerics/reduce/1.cc: New test.
* testsuite/26_numerics/transform_exclusive_scan/1.cc: New test.
* testsuite/26_numerics/transform_inclusive_scan/1.cc: New test.
* testsuite/26_numerics/transform_reduce/1.cc: New test.
* testsuite/util/testsuite_iterators.h (test_container::size()): New
member function.
* include/c_global/cstddef (std::byte): Perform arithmetic operations
in unsigned int to avoid promotion (LWG 2950).
......
......@@ -154,7 +154,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/
/**
* @defgroup set_algorithms Set Operation
* @defgroup set_algorithms Set Operations
* @ingroup sorting_algorithms
*
* These algorithms are common set operations performed on sequences
......
......@@ -3867,6 +3867,39 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
return __f; // N.B. [alg.foreach] says std::move(f) but it's redundant.
}
#if __cplusplus >= 201703L
/**
* @brief Apply a function to every element of a sequence.
* @ingroup non_mutating_algorithms
* @param __first An input iterator.
* @param __n A value convertible to an integer.
* @param __f A unary function object.
* @return `__first+__n`
*
* Applies the function object `__f` to each element in the range
* `[first, first+n)`. `__f` must not modify the order of the sequence.
* If `__f` has a return value it is ignored.
*/
template<typename _InputIterator, typename _Size, typename _Function>
_InputIterator
for_each_n(_InputIterator __first, _Size __n, _Function __f)
{
auto __n2 = std::__size_to_integer(__n);
using _Cat = typename iterator_traits<_InputIterator>::iterator_category;
if constexpr (is_base_of_v<random_access_iterator_tag, _Cat>)
return std::for_each(__first, __first + __n2, __f);
else
{
while (__n2-->0)
{
__f(*__first);
++__first;
}
return __first;
}
}
#endif // C++17
/**
* @brief Find the first occurrence of a value in a sequence.
* @ingroup non_mutating_algorithms
......
......@@ -60,12 +60,16 @@
#include <debug/debug.h>
#include <bits/move.h> // For _GLIBCXX_MOVE
#if __cplusplus >= 201103L
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/** @defgroup numeric_ops Generalized Numeric operations
* @ingroup algorithms
*/
#if __cplusplus >= 201103L
/**
* @brief Create a range of sequentially increasing values.
*
......@@ -76,6 +80,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __last End of range.
* @param __value Starting value.
* @return Nothing.
* @ingroup numeric_ops
*/
template<typename _ForwardIterator, typename _Tp>
void
......@@ -94,14 +99,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
++__value;
}
}
#endif
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_ALGO
#if __cplusplus > 201703L
......@@ -112,6 +113,9 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
# define _GLIBCXX_MOVE_IF_20(_E) _E
#endif
/// @addtogroup numeric_ops
/// @{
/**
* @brief Accumulate values in a range.
*
......@@ -139,8 +143,8 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
/**
* @brief Accumulate values in a range with operation.
*
* Accumulates the values in the range [first,last) using the function
* object @p __binary_op. The initial value is @p __init. The values are
* Accumulates the values in the range `[first,last)` using the function
* object `__binary_op`. The initial value is `__init`. The values are
* processed in order.
*
* @param __first Start of range.
......@@ -390,6 +394,8 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
return ++__result;
}
// @} group numeric_ops
#undef _GLIBCXX_MOVE_IF_20
_GLIBCXX_END_NAMESPACE_ALGO
......
// { dg-options "-std=gnu++17" }
// { dg-do run { target c++17 } }
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <algorithm>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
void test01()
{
using __gnu_test::test_container;
using __gnu_test::input_iterator_wrapper;
int array[5] = { 1, 2, 3, 4, 5 };
test_container<int, input_iterator_wrapper> con(array);
int sum = 0;
struct Func
{
Func(int& i) : i(i) { }
Func(Func&&) = default;
Func& operator=(Func&&) = delete;
void operator()(int n) const { i += n; }
int& i;
};
struct Size
{
Size(short v) : val(v) { }
operator short() const { return val; }
short val;
};
auto res = std::for_each_n(con.begin(), Size(con.size()), Func(sum));
VERIFY( res.ptr == con.end().ptr );
VERIFY( sum == 15 );
}
int main()
{
test01();
}
// { dg-options "-std=gnu++17" }
// { dg-do run { target c++17 } }
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// C++17 29.8.7 [exclusive.scan]
#include <numeric>
#include <iterator>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
using __gnu_test::test_container;
using __gnu_test::input_iterator_wrapper;
using __gnu_test::output_iterator_wrapper;
/*
template<class InputIterator, class OutputIterator, class T>
OutputIterator
exclusive_scan(InputIterator, InputIterator, OutputIterator, T);
*/
void
test01()
{
int out[10];
test_container<int, output_iterator_wrapper> co(out);
test_container<int, input_iterator_wrapper> ca(a);
auto end = std::exclusive_scan(ca.begin(), ca.end(), co.begin(), 5);
static_assert(std::is_same_v<decltype(end), decltype(co.begin())>);
VERIFY( end.ptr == out+10 );
VERIFY( out[0] == 5 );
VERIFY( out[1] == 6 );
VERIFY( out[2] == 8 );
VERIFY( out[3] == 11 );
VERIFY( out[4] == 15 );
VERIFY( out[5] == 20 );
VERIFY( out[6] == 26 );
VERIFY( out[7] == 33 );
VERIFY( out[8] == 41 );
VERIFY( out[9] == 50 );
}
/*
template<class InputIterator, class OutputIterator, class T,
class BinaryOperation>
OutputIterator
exclusive_scan(InputIterator, InputIterator, OutputIterator, T,
BinaryOperation);
*/
void
test02()
{
int out[10];
test_container<int, output_iterator_wrapper> co(out);
test_container<int, input_iterator_wrapper> ca(a);
auto end = std::exclusive_scan(ca.begin(), ca.end(), co.begin(), 2,
[](int i, int j) { return 2*i + 2*j; });
static_assert(std::is_same_v<decltype(end), decltype(co.begin())>);
VERIFY( end.ptr == out+10 );
VERIFY( out[0] == 2 );
VERIFY( out[1] == 6 );
VERIFY( out[2] == 16 );
VERIFY( out[3] == 38 );
VERIFY( out[4] == 84 );
VERIFY( out[5] == 178 );
VERIFY( out[6] == 368 );
VERIFY( out[7] == 750 );
VERIFY( out[8] == 1516 );
VERIFY( out[9] == 3050 );
}
int
main()
{
test01();
test02();
}
// { dg-options "-std=gnu++17" }
// { dg-do run { target c++17 } }
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// C++17 29.8.8 [inclusive.scan]
#include <numeric>
#include <iterator>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
using __gnu_test::test_container;
using __gnu_test::input_iterator_wrapper;
using __gnu_test::output_iterator_wrapper;
/*
template<class InputIterator, class OutputIterator>
OutputIterator
inclusive_scan(InputIterator, InputIterator, OutputIterator);
*/
void
test01()
{
int out[10];
test_container<int, output_iterator_wrapper> co(out);
test_container<int, input_iterator_wrapper> ca(a);
auto end = std::inclusive_scan(ca.begin(), ca.end(), co.begin());
static_assert(std::is_same_v<decltype(end), decltype(co.begin())>);
VERIFY( end.ptr == out+10 );
VERIFY( out[0] == 1 );
VERIFY( out[1] == (1+2) );
VERIFY( out[2] == (1+2+3) );
VERIFY( out[3] == (1+2+3+4) );
VERIFY( out[4] == (1+2+3+4+5) );
VERIFY( out[5] == (1+2+3+4+5+6) );
VERIFY( out[6] == (1+2+3+4+5+6+7) );
VERIFY( out[7] == (1+2+3+4+5+6+7+8) );
VERIFY( out[8] == (1+2+3+4+5+6+7+8+9) );
VERIFY( out[9] == (1+2+3+4+5+6+7+8+9+10) );
}
/*
template<class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator
inclusive_scan(InputIterator, InputIterator, OutputIterator,
BinaryOperation);
*/
void
test02()
{
int out[10];
test_container<int, output_iterator_wrapper> co(out);
test_container<int, input_iterator_wrapper> ca(a);
auto end = std::inclusive_scan(ca.begin(), ca.end(), co.begin(),
[](int i, int j) { return 2*i + 2*j; });
static_assert(std::is_same_v<decltype(end), decltype(co.begin())>);
VERIFY( end.ptr == out+10 );
VERIFY( out[0] == 1 );
VERIFY( out[1] == (2*1+2*2) );
VERIFY( out[2] == (2*6+2*3) );
VERIFY( out[3] == (2*18+2*4) );
VERIFY( out[4] == (2*44+2*5) );
VERIFY( out[5] == (2*98+2*6));
VERIFY( out[6] == (2*208+2*7) );
VERIFY( out[7] == (2*430+2*8) );
VERIFY( out[8] == (2*876+2*9) );
VERIFY( out[9] == (2*1770+2*10) );
}
/*
template<class InputIterator, class OutputIterator, class BinaryOperation, T>
OutputIterator
inclusive_scan(InputIterator, InputIterator, OutputIterator,
BinaryOperation, T);
*/
void
test03()
{
int out[10];
test_container<int, output_iterator_wrapper> co(out);
test_container<int, input_iterator_wrapper> ca(a);
auto end = std::inclusive_scan(ca.begin(), ca.end(), co.begin(),
[](int i, int j) { return 2*i + 2*j; },
1);
static_assert(std::is_same_v<decltype(end), decltype(co.begin())>);
VERIFY( end.ptr == out+10 );
VERIFY( out[0] == 4 );
VERIFY( out[1] == (2*4+2*2) );
VERIFY( out[2] == (2*12+2*3) );
VERIFY( out[3] == (2*30+2*4) );
VERIFY( out[4] == (2*68+2*5) );
VERIFY( out[5] == (2*146+2*6) );
VERIFY( out[6] == (2*304+2*7));
VERIFY( out[7] == (2*622+2*8) );
VERIFY( out[8] == (2*1260+2*9) );
VERIFY( out[9] == (2*2538+2*10) );
}
int
main()
{
test01();
test02();
test03();
}
// { dg-options "-std=gnu++17" }
// { dg-do run { target c++17 } }
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// C++17 29.8.3 [reduce]
#include <numeric>
#include <iterator>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
/*
template<class InputIterator>
iterator_traits<InputIterator>::value_type
reduce(InputIterator, InputIterator);
*/
void
test01()
{
using __gnu_test::test_container;
using __gnu_test::input_iterator_wrapper;
int array[5] = { 1, 2, 3, 4, 5 };
test_container<int, input_iterator_wrapper> con(array);
int res = std::reduce(con.begin(), con.end());
VERIFY( res == 15 );
}
/*
template<class InputIterator, class T>
T reduce(InputIterator, InputIterator, T);
*/
void
test02()
{
bool b[] = {true, false, true, true, false, true, false, true, true, false};
int res = std::reduce(std::begin(b), std::end(b), 100);
VERIFY( res == 106 );
}
/*
template<class InputIterator, class T>
T reduce(InputIterator, InputIterator, T);
template<class InputIterator, class T, class BinaryOperation>
T reduce(InputIterator, InputIterator, T, BinaryOperation);
*/
void
test03()
{
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto res = std::reduce(std::begin(a), std::end(a), (short)11);
static_assert(std::is_same_v<decltype(res), short>);
VERIFY( res == 66 );
auto res2 = std::reduce(std::begin(a), std::end(a), -1l, std::multiplies<>());
static_assert(std::is_same_v<decltype(res2), long>);
VERIFY( res2 == -3628800 );
}
int
main()
{
test01();
test02();
test03();
}
// { dg-options "-std=gnu++17" }
// { dg-do run { target c++17 } }
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// C++17 29.8.9 [transform.exclusive.scan]
#include <numeric>
#include <iterator>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
int a[] = {1, 2, 3, 4, 5, 6, 7};
using __gnu_test::test_container;
using __gnu_test::input_iterator_wrapper;
using __gnu_test::output_iterator_wrapper;
/*
template<class InputIterator, class OutputIterator, class T,
class BinaryOperation, class UnaryOperation>
OutputIterator
transform_exclusive_scan(InputIterator, InputIterator, OutputIterator, T,
BinaryOperation, UnaryOperation);
*/
void
test01()
{
int out[7];
test_container<int, output_iterator_wrapper> co(out);
test_container<int, input_iterator_wrapper> ca(a);
auto end = std::transform_exclusive_scan(ca.begin(), ca.end(), co.begin(), 5,
std::multiplies<>(),
std::negate<>());
static_assert(std::is_same_v<decltype(end), decltype(co.begin())>);
VERIFY( end.ptr == out+7 );
VERIFY( out[0] == 5 );
VERIFY( out[1] == -5 );
VERIFY( out[2] == 10 );
VERIFY( out[3] == -30 );
VERIFY( out[4] == 120 );
VERIFY( out[5] == -600 );
VERIFY( out[6] == 3600 );
}
int
main()
{
test01();
}
// { dg-options "-std=gnu++17" }
// { dg-do run { target c++17 } }
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// C++17 29.8.10 [transform.inclusive.scan]
#include <numeric>
#include <iterator>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
int a[] = {1, 2, 3, 4, 5, 6, 7};
using __gnu_test::test_container;
using __gnu_test::input_iterator_wrapper;
using __gnu_test::output_iterator_wrapper;
/*
template<class InputIterator, class OutputIterator, class BinaryOperation,
class UnaryOperation>
OutputIterator
transform_inclusive_scan(InputIterator, InputIterator, OutputIterator,
BinaryOperation, UnaryOperation);
*/
void
test01()
{
int out[7];
test_container<int, output_iterator_wrapper> co(out);
test_container<int, input_iterator_wrapper> ca(a);
auto end = std::transform_inclusive_scan(ca.begin(), ca.end(), co.begin(),
std::multiplies<>(),
[](int i) { return i+1; });
static_assert(std::is_same_v<decltype(end), decltype(co.begin())>);
VERIFY( end.ptr == out+7 );
VERIFY( out[0] == 2 );
VERIFY( out[1] == (2*3) );
VERIFY( out[2] == (2*3*4) );
VERIFY( out[3] == (2*3*4*5) );
VERIFY( out[4] == (2*3*4*5*6) );
VERIFY( out[5] == (2*3*4*5*6*7) );
VERIFY( out[6] == (2*3*4*5*6*7*8) );
}
/*
template<class InputIterator, class OutputIterator, class BinaryOperation,
class UnaryOperation, class T>
OutputIterator
transform_inclusive_scan(InputIterator, InputIterator, OutputIterator,
BinaryOperation, UnaryOperation, T);
*/
void
test02()
{
int out[7];
test_container<int, output_iterator_wrapper> co(out);
test_container<int, input_iterator_wrapper> ca(a);
auto end = std::transform_inclusive_scan(ca.begin(), ca.end(), co.begin(),
std::multiplies<>(),
[](int i) { return i+1; },
3);
static_assert(std::is_same_v<decltype(end), decltype(co.begin())>);
VERIFY( end.ptr == out+7 );
VERIFY( out[0] == 3*2 );
VERIFY( out[1] == (3*2*3) );
VERIFY( out[2] == (3*2*3*4) );
VERIFY( out[3] == (3*2*3*4*5) );
VERIFY( out[4] == (3*2*3*4*5*6) );
VERIFY( out[5] == (3*2*3*4*5*6*7) );
VERIFY( out[6] == (3*2*3*4*5*6*7*8) );
}
int
main()
{
test01();
test02();
}
// { dg-options "-std=gnu++17" }
// { dg-do run { target c++17 } }
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// C++17 29.8.5 [transform.reduce]
#include <numeric>
#include <iterator>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double b[] = {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5};
using __gnu_test::test_container;
using __gnu_test::input_iterator_wrapper;
/*
template<class InputIterator1, class InputIterator2, class T>
T transform_reduce(InputIterator1, InputIterator1, InputIterator2, T);
*/
void
test01()
{
auto res = std::transform_reduce(std::begin(a), std::end(a), std::begin(b),
1.0f);
static_assert(std::is_same_v<decltype(res), float>);
VERIFY( res == (float)(1 + 0.5 + 1 + 1.5 + 2 + 2.5 + 3 + 3.5 + 4 + 4.5 + 5) );
test_container<int, input_iterator_wrapper> ca(a);
test_container<double, input_iterator_wrapper> cb(b);
auto res2 = std::transform_reduce(ca.begin(), ca.end(), cb.begin(),
1.0f);
static_assert(std::is_same_v<decltype(res2), float>);
VERIFY( res2 == res );
}
/*
template<class InputIterator1, class InputIterator2, class T,
class BinaryOperation1, class BinaryOperation2>
T transform_reduce(InputIterator1, InputIterator1, InputIterator2, T,
BinaryOperation1, BinaryOperation2);
*/
void
test02()
{
auto res = std::transform_reduce(std::begin(a), std::end(a), std::begin(b),
1L, std::multiplies<>(), std::plus<int>());
static_assert(std::is_same_v<decltype(res), long>);
VERIFY( res == (1L * 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10) );
test_container<int, input_iterator_wrapper> ca(a);
test_container<double, input_iterator_wrapper> cb(b);
auto res2 = std::transform_reduce(ca.begin(), ca.end(), cb.begin(),
1L, std::multiplies<>(), std::plus<int>());
static_assert(std::is_same_v<decltype(res2), long>);
VERIFY( res2 == res );
}
/*
template<class InputIterator, class T, class BinaryOperation,
class UnaryOperation>
T transform_reduce(InputIterator, InputIterator, T,
BinaryOperation, UnaryOperation);
*/
void
test03()
{
auto res = std::transform_reduce(std::begin(a), std::end(a), 10.0,
std::plus<>(),
[](int i) { return i * i; });
static_assert(std::is_same_v<decltype(res), double>);
VERIFY( res == (10.0 + 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100) );
test_container<int, input_iterator_wrapper> ca(a);
test_container<double, input_iterator_wrapper> cb(b);
auto res2 = std::transform_reduce(ca.begin(), ca.end(), 10.0,
std::plus<>(),
[](int i) { return i * i; });
static_assert(std::is_same_v<decltype(res2), double>);
VERIFY( res2 == (10.0 + 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100) );
}
int
main()
{
test01();
test02();
test03();
}
......@@ -589,6 +589,10 @@ namespace __gnu_test
ItType<T>
end()
{ return it(bounds.last); }
std::size_t
size() const
{ return bounds.last - bounds.first; }
};
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment