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