reduce.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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_REDUCE_H
  10. #define _LIBCPP___NUMERIC_REDUCE_H
  11. #include <__config>
  12. #include <__functional/operations.h>
  13. #include <__iterator/iterator_traits.h>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. #if _LIBCPP_STD_VER > 14
  19. template <class _InputIterator, class _Tp, class _BinaryOp>
  20. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp reduce(_InputIterator __first, _InputIterator __last,
  21. _Tp __init, _BinaryOp __b) {
  22. for (; __first != __last; ++__first)
  23. __init = __b(__init, *__first);
  24. return __init;
  25. }
  26. template <class _InputIterator, class _Tp>
  27. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp reduce(_InputIterator __first, _InputIterator __last,
  28. _Tp __init) {
  29. return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
  30. }
  31. template <class _InputIterator>
  32. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 typename iterator_traits<_InputIterator>::value_type
  33. reduce(_InputIterator __first, _InputIterator __last) {
  34. return _VSTD::reduce(__first, __last, typename iterator_traits<_InputIterator>::value_type{});
  35. }
  36. #endif
  37. _LIBCPP_END_NAMESPACE_STD
  38. #endif // _LIBCPP___NUMERIC_REDUCE_H