accumulate.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_PUSH_MACROS
  17. #include <__undef_macros>
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. template <class _InputIterator, class _Tp>
  20. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
  21. accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) {
  22. for (; __first != __last; ++__first)
  23. #if _LIBCPP_STD_VER >= 20
  24. __init = std::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_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
  32. accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) {
  33. for (; __first != __last; ++__first)
  34. #if _LIBCPP_STD_VER >= 20
  35. __init = __binary_op(std::move(__init), *__first);
  36. #else
  37. __init = __binary_op(__init, *__first);
  38. #endif
  39. return __init;
  40. }
  41. _LIBCPP_END_NAMESPACE_STD
  42. _LIBCPP_POP_MACROS
  43. #endif // _LIBCPP___NUMERIC_ACCUMULATE_H