Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __numeric / adjacent_difference.h
blob4b06f9f29f8d026dfd504ed93dbd6534109b654d
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___NUMERIC_ADJACENT_DIFFERENCE_H
11 #define _LIBCPP___NUMERIC_ADJACENT_DIFFERENCE_H
13 #include <__config>
14 #include <__iterator/iterator_traits.h>
15 #include <__utility/move.h>
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 # pragma GCC system_header
19 #endif
21 _LIBCPP_PUSH_MACROS
22 #include <__undef_macros>
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 template <class _InputIterator, class _OutputIterator>
27 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
28 _OutputIterator
29 adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
31 if (__first != __last)
33 typename iterator_traits<_InputIterator>::value_type __acc(*__first);
34 *__result = __acc;
35 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
37 typename iterator_traits<_InputIterator>::value_type __val(*__first);
38 #if _LIBCPP_STD_VER >= 20
39 *__result = __val - _VSTD::move(__acc);
40 #else
41 *__result = __val - __acc;
42 #endif
43 __acc = _VSTD::move(__val);
46 return __result;
49 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
50 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
51 _OutputIterator
52 adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
53 _BinaryOperation __binary_op)
55 if (__first != __last)
57 typename iterator_traits<_InputIterator>::value_type __acc(*__first);
58 *__result = __acc;
59 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
61 typename iterator_traits<_InputIterator>::value_type __val(*__first);
62 #if _LIBCPP_STD_VER >= 20
63 *__result = __binary_op(__val, _VSTD::move(__acc));
64 #else
65 *__result = __binary_op(__val, __acc);
66 #endif
67 __acc = _VSTD::move(__val);
70 return __result;
73 _LIBCPP_END_NAMESPACE_STD
75 _LIBCPP_POP_MACROS
77 #endif // _LIBCPP___NUMERIC_ADJACENT_DIFFERENCE_H