xref: /freebsd/contrib/llvm-project/libcxx/include/__numeric/partial_sum.h (revision 3e8eb5c7f4909209c042403ddee340b2ee7003a5)
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_PARTIAL_SUM_H
11 #define _LIBCPP___NUMERIC_PARTIAL_SUM_H
12 
13 #include <__config>
14 #include <__iterator/iterator_traits.h>
15 #include <__utility/move.h>
16 
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #  pragma GCC system_header
19 #endif
20 
21 _LIBCPP_BEGIN_NAMESPACE_STD
22 
23 template <class _InputIterator, class _OutputIterator>
24 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
25 _OutputIterator
26 partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
27 {
28     if (__first != __last)
29     {
30         typename iterator_traits<_InputIterator>::value_type __t(*__first);
31         *__result = __t;
32         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
33         {
34 #if _LIBCPP_STD_VER > 17
35             __t = _VSTD::move(__t) + *__first;
36 #else
37             __t = __t + *__first;
38 #endif
39             *__result = __t;
40         }
41     }
42     return __result;
43 }
44 
45 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
46 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
47 _OutputIterator
48 partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
49               _BinaryOperation __binary_op)
50 {
51     if (__first != __last)
52     {
53         typename iterator_traits<_InputIterator>::value_type __t(*__first);
54         *__result = __t;
55         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
56         {
57 #if _LIBCPP_STD_VER > 17
58             __t = __binary_op(_VSTD::move(__t), *__first);
59 #else
60             __t = __binary_op(__t, *__first);
61 #endif
62             *__result = __t;
63         }
64     }
65     return __result;
66 }
67 
68 _LIBCPP_END_NAMESPACE_STD
69 
70 #endif // _LIBCPP___NUMERIC_PARTIAL_SUM_H
71