accumulate.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  21. _Tp
  22. accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
  23. {
  24. for (; __first != __last; ++__first)
  25. #if _LIBCPP_STD_VER >= 20
  26. __init = _VSTD::move(__init) + *__first;
  27. #else
  28. __init = __init + *__first;
  29. #endif
  30. return __init;
  31. }
  32. template <class _InputIterator, class _Tp, class _BinaryOperation>
  33. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  34. _Tp
  35. accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
  36. {
  37. for (; __first != __last; ++__first)
  38. #if _LIBCPP_STD_VER >= 20
  39. __init = __binary_op(_VSTD::move(__init), *__first);
  40. #else
  41. __init = __binary_op(__init, *__first);
  42. #endif
  43. return __init;
  44. }
  45. _LIBCPP_END_NAMESPACE_STD
  46. _LIBCPP_POP_MACROS
  47. #endif // _LIBCPP___NUMERIC_ACCUMULATE_H