xref: /freebsd/contrib/llvm-project/libcxx/include/__numeric/reduce.h (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___NUMERIC_REDUCE_H
11 #define _LIBCPP___NUMERIC_REDUCE_H
12 
13 #include <__config>
14 #include <__functional/operations.h>
15 #include <__iterator/iterator_traits.h>
16 #include <__utility/move.h>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 #if _LIBCPP_STD_VER >= 17
25 template <class _InputIterator, class _Tp, class _BinaryOp>
26 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
27 reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b) {
28   for (; __first != __last; ++__first)
29     __init = __b(std::move(__init), *__first);
30   return __init;
31 }
32 
33 template <class _InputIterator, class _Tp>
34 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
35 reduce(_InputIterator __first, _InputIterator __last, _Tp __init) {
36   return std::reduce(__first, __last, __init, std::plus<>());
37 }
38 
39 template <class _InputIterator>
40 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename iterator_traits<_InputIterator>::value_type
41 reduce(_InputIterator __first, _InputIterator __last) {
42   return std::reduce(__first, __last, typename iterator_traits<_InputIterator>::value_type{});
43 }
44 #endif
45 
46 _LIBCPP_END_NAMESPACE_STD
47 
48 #endif // _LIBCPP___NUMERIC_REDUCE_H
49