tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
adjacent_difference.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2020 Tobias Hienzsch
3#ifndef TETL_NUMERIC_ADJACENT_DIFFERENCE_HPP
4#define TETL_NUMERIC_ADJACENT_DIFFERENCE_HPP
5
6#include <etl/_functional/minus.hpp>
7#include <etl/_iterator/iterator_traits.hpp>
8#include <etl/_utility/move.hpp>
9
10namespace etl {
11
12/// \brief Computes the differences between the second and the first of each
13/// adjacent pair of elements of the range [first, last) and writes them to the
14/// range beginning at destination + 1. An unmodified copy of *first is written
15/// to *destination.
16/// \ingroup numeric
17template <typename InputIt, typename OutputIt, typename BinaryOperation>
18constexpr auto adjacent_difference(InputIt first, InputIt last, OutputIt destination, BinaryOperation op) -> OutputIt
19{
20 if (first == last) {
21 return destination;
22 }
23
24 auto acc = *first;
25 *destination = acc;
26
27 while (++first != last) {
28 auto val = *first;
29 *++destination = op(val, etl::move(acc));
30 acc = etl::move(val);
31 }
32
33 return ++destination;
34}
35
36/// \ingroup numeric
37template <typename InputIt, typename OutputIt>
38constexpr auto adjacent_difference(InputIt first, InputIt last, OutputIt destination) -> OutputIt
39{
40 using value_t = typename etl::iterator_traits<InputIt>::value_type;
41 return etl::adjacent_difference(first, last, destination, etl::minus<value_t>());
42}
43
44} // namespace etl
45
46#endif // TETL_NUMERIC_ADJACENT_DIFFERENCE_HPP
constexpr auto adjacent_difference(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Definition adjacent_difference.hpp:38
constexpr auto adjacent_difference(InputIt first, InputIt last, OutputIt destination, BinaryOperation op) -> OutputIt
Computes the differences between the second and the first of each adjacent pair of elements of the ra...
Definition adjacent_difference.hpp:18
Definition adjacent_find.hpp:9
iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIt...
Definition iterator_traits.hpp:48
Function object for performing subtraction. Effectively calls operator- on two instances of type T....
Definition minus.hpp:15