Skip to content

Commit

Permalink
Implement new serial algorithms from Parallelism TS (P0024R2)
Browse files Browse the repository at this point in the history
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.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@272459 138bc75d-0d04-0410-961f-82ee72b054a4
  • Loading branch information
redi committed Jun 18, 2019
1 parent 6c552ff commit b93041f
Show file tree
Hide file tree
Showing 13 changed files with 1,159 additions and 9 deletions.
17 changes: 17 additions & 0 deletions libstdc++-v3/ChangeLog
@@ -1,5 +1,22 @@
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).

Expand Down
2 changes: 1 addition & 1 deletion libstdc++-v3/include/bits/algorithmfwd.h
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions libstdc++-v3/include/bits/stl_algo.h
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions libstdc++-v3/include/bits/stl_numeric.h
Expand Up @@ -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.
*
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -390,6 +394,8 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
return ++__result;
}

// @} group numeric_ops

#undef _GLIBCXX_MOVE_IF_20

_GLIBCXX_END_NAMESPACE_ALGO
Expand Down

0 comments on commit b93041f

Please sign in to comment.