xref: /freebsd/contrib/llvm-project/libcxx/include/__numeric/partial_sum.h (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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_PUSH_MACROS
22 #include <__undef_macros>
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 template <class _InputIterator, class _OutputIterator>
27 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
28 _OutputIterator
29 partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
30 {
31     if (__first != __last)
32     {
33         typename iterator_traits<_InputIterator>::value_type __t(*__first);
34         *__result = __t;
35         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
36         {
37 #if _LIBCPP_STD_VER >= 20
38             __t = _VSTD::move(__t) + *__first;
39 #else
40             __t = __t + *__first;
41 #endif
42             *__result = __t;
43         }
44     }
45     return __result;
46 }
47 
48 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
49 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
50 _OutputIterator
51 partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
52               _BinaryOperation __binary_op)
53 {
54     if (__first != __last)
55     {
56         typename iterator_traits<_InputIterator>::value_type __t(*__first);
57         *__result = __t;
58         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
59         {
60 #if _LIBCPP_STD_VER >= 20
61             __t = __binary_op(_VSTD::move(__t), *__first);
62 #else
63             __t = __binary_op(__t, *__first);
64 #endif
65             *__result = __t;
66         }
67     }
68     return __result;
69 }
70 
71 _LIBCPP_END_NAMESPACE_STD
72 
73 _LIBCPP_POP_MACROS
74 
75 #endif // _LIBCPP___NUMERIC_PARTIAL_SUM_H
76