accumulate.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #ifndef _LIBCPP___NUMERIC_ACCUMULATE_H
  10. #define _LIBCPP___NUMERIC_ACCUMULATE_H
  11. #include <__config>
  12. #include <__utility/move.h>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. _LIBCPP_BEGIN_NAMESPACE_STD
  17. template <class _InputIterator, class _Tp>
  18. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  19. _Tp
  20. accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
  21. {
  22. for (; __first != __last; ++__first)
  23. #if _LIBCPP_STD_VER > 17
  24. __init = _VSTD::move(__init) + *__first;
  25. #else
  26. __init = __init + *__first;
  27. #endif
  28. return __init;
  29. }
  30. template <class _InputIterator, class _Tp, class _BinaryOperation>
  31. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  32. _Tp
  33. accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
  34. {
  35. for (; __first != __last; ++__first)
  36. #if _LIBCPP_STD_VER > 17
  37. __init = __binary_op(_VSTD::move(__init), *__first);
  38. #else
  39. __init = __binary_op(__init, *__first);
  40. #endif
  41. return __init;
  42. }
  43. _LIBCPP_END_NAMESPACE_STD
  44. #endif // _LIBCPP___NUMERIC_ACCUMULATE_H