xref: /freebsd/contrib/llvm-project/libcxx/include/__numeric/partial_sum.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
14824e7fdSDimitry Andric // -*- C++ -*-
24824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
34824e7fdSDimitry Andric //
44824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
54824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
64824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
74824e7fdSDimitry Andric //
84824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
94824e7fdSDimitry Andric 
104824e7fdSDimitry Andric #ifndef _LIBCPP___NUMERIC_PARTIAL_SUM_H
114824e7fdSDimitry Andric #define _LIBCPP___NUMERIC_PARTIAL_SUM_H
124824e7fdSDimitry Andric 
134824e7fdSDimitry Andric #include <__config>
144824e7fdSDimitry Andric #include <__iterator/iterator_traits.h>
154824e7fdSDimitry Andric #include <__utility/move.h>
164824e7fdSDimitry Andric 
174824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
184824e7fdSDimitry Andric #  pragma GCC system_header
194824e7fdSDimitry Andric #endif
204824e7fdSDimitry Andric 
2106c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS
2206c3fb27SDimitry Andric #include <__undef_macros>
2306c3fb27SDimitry Andric 
244824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
254824e7fdSDimitry Andric 
264824e7fdSDimitry Andric template <class _InputIterator, class _OutputIterator>
27*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
28*cb14a3feSDimitry Andric partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
29*cb14a3feSDimitry Andric   if (__first != __last) {
304824e7fdSDimitry Andric     typename iterator_traits<_InputIterator>::value_type __t(*__first);
314824e7fdSDimitry Andric     *__result = __t;
32*cb14a3feSDimitry Andric     for (++__first, (void)++__result; __first != __last; ++__first, (void)++__result) {
3306c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
345f757f3fSDimitry Andric       __t = std::move(__t) + *__first;
354824e7fdSDimitry Andric #else
364824e7fdSDimitry Andric       __t = __t + *__first;
374824e7fdSDimitry Andric #endif
384824e7fdSDimitry Andric       *__result = __t;
394824e7fdSDimitry Andric     }
404824e7fdSDimitry Andric   }
414824e7fdSDimitry Andric   return __result;
424824e7fdSDimitry Andric }
434824e7fdSDimitry Andric 
444824e7fdSDimitry Andric template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
45*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
46*cb14a3feSDimitry Andric partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op) {
47*cb14a3feSDimitry Andric   if (__first != __last) {
484824e7fdSDimitry Andric     typename iterator_traits<_InputIterator>::value_type __t(*__first);
494824e7fdSDimitry Andric     *__result = __t;
50*cb14a3feSDimitry Andric     for (++__first, (void)++__result; __first != __last; ++__first, (void)++__result) {
5106c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
525f757f3fSDimitry Andric       __t = __binary_op(std::move(__t), *__first);
534824e7fdSDimitry Andric #else
544824e7fdSDimitry Andric       __t = __binary_op(__t, *__first);
554824e7fdSDimitry Andric #endif
564824e7fdSDimitry Andric       *__result = __t;
574824e7fdSDimitry Andric     }
584824e7fdSDimitry Andric   }
594824e7fdSDimitry Andric   return __result;
604824e7fdSDimitry Andric }
614824e7fdSDimitry Andric 
624824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD
634824e7fdSDimitry Andric 
6406c3fb27SDimitry Andric _LIBCPP_POP_MACROS
6506c3fb27SDimitry Andric 
664824e7fdSDimitry Andric #endif // _LIBCPP___NUMERIC_PARTIAL_SUM_H
67