Horizon
Loading...
Searching...
No Matches
adjacent_difference.hpp
Go to the documentation of this file.
1
2// Range v3 library
3//
4// Copyright Eric Niebler 2004
5// Copyright Gonzalo Brito Gadeschi 2014
6//
7// Use, modification and distribution is subject to the
8// Boost Software License, Version 1.0. (See accompanying
9// file LICENSE_1_0.txt or copy at
10// http://www.boost.org/LICENSE_1_0.txt)
11//
12// Project home: https://github.com/ericniebler/range-v3
13//
14// Implementation based on the code in libc++
15// http://http://libcxx.llvm.org/
16
17#ifndef RANGES_V3_NUMERIC_ADJACENT_DIFFERENCE_HPP
18#define RANGES_V3_NUMERIC_ADJACENT_DIFFERENCE_HPP
19
20#include <meta/meta.hpp>
21
22#include <range/v3/algorithm/result_types.hpp>
33#include <range/v3/utility/static_const.hpp>
34
35#include <range/v3/detail/prologue.hpp>
36
37namespace ranges
38{
41 // clang-format off
44 template(typename I, typename O, typename BOp, typename P)(
45 concept (differenceable_)(I, O, BOp, P),
46 invocable<P&, iter_value_t<I>> AND
47 copy_constructible<uncvref_t<invoke_result_t<P&, iter_value_t<I>>>> AND
48 movable<uncvref_t<invoke_result_t<P&, iter_value_t<I>>>> AND
49 output_iterator<O, invoke_result_t<P&, iter_value_t<I>>> AND
50 invocable<
51 BOp&,
52 invoke_result_t<
53 P&,
54 iter_value_t<I>>,
55 invoke_result_t<P&, iter_value_t<I>>> AND
56 output_iterator<
57 O,
58 invoke_result_t<
59 BOp&,
60 invoke_result_t<P&, iter_value_t<I>>,
61 invoke_result_t<P&, iter_value_t<I>>>>);
62
65 template<typename I, typename O, typename BOp = minus, typename P = identity>
66 CPP_concept differenceable =
67 input_iterator<I> &&
68 CPP_concept_ref(ranges::differenceable_, I, O, BOp, P);
69 // clang-format on
70
71 template<typename I, typename O>
72 using adjacent_difference_result = detail::in_out_result<I, O>;
73
75 {
76 template(typename I, typename S, typename O, typename S2, typename BOp = minus,
77 typename P = identity)(
80 adjacent_difference_result<I, O> operator()(I first,
81 S last,
82 O result,
83 S2 end_result,
84 BOp bop = BOp{},
85 P proj = P{}) const
86 {
87 // BUGBUG think about the use of coerce here.
88 using V = iter_value_t<I>;
89 using X = invoke_result_t<P &, V>;
90 coerce<V> v;
91 coerce<X> x;
92
93 if(first != last && result != end_result)
94 {
95 auto t1(x(invoke(proj, v(*first))));
96 *result = t1;
97 for(++first, ++result; first != last && result != end_result;
98 ++first, ++result)
99 {
100 auto t2(x(invoke(proj, v(*first))));
101 *result = invoke(bop, t2, t1);
102 t1 = std::move(t2);
103 }
104 }
105 return {first, result};
106 }
107
108 template(typename I, typename S, typename O, typename BOp = minus,
109 typename P = identity)(
111 adjacent_difference_result<I, O> //
112 operator()(I first, S last, O result, BOp bop = BOp{}, P proj = P{}) const
113 {
114 return (*this)(std::move(first),
115 std::move(last),
116 std::move(result),
117 unreachable,
118 std::move(bop),
119 std::move(proj));
120 }
121
122 template(typename Rng, typename ORef, typename BOp = minus, typename P = identity,
123 typename I = iterator_t<Rng>, typename O = uncvref_t<ORef>)(
125 adjacent_difference_result<borrowed_iterator_t<Rng>, O> //
126 operator()(Rng && rng, ORef && result, BOp bop = BOp{}, P proj = P{}) const
127 {
128 return (*this)(begin(rng),
129 end(rng),
130 static_cast<ORef &&>(result),
131 std::move(bop),
132 std::move(proj));
133 }
134
135 template(typename Rng, typename ORng, typename BOp = minus, typename P = identity,
136 typename I = iterator_t<Rng>, typename O = iterator_t<ORng>)(
138 adjacent_difference_result<borrowed_iterator_t<Rng>, borrowed_iterator_t<ORng>>
139 operator()(Rng && rng, ORng && result, BOp bop = BOp{}, P proj = P{}) const
140 {
141 return (*this)(begin(rng),
142 end(rng),
143 begin(result),
144 end(result),
145 std::move(bop),
146 std::move(proj));
147 }
148 };
149
150 RANGES_INLINE_VARIABLE(adjacent_difference_fn, adjacent_difference)
152} // namespace ranges
153
154#include <range/v3/detail/epilogue.hpp>
155
156#endif
The differenceable_ concept.
The differenceable concept.
The range concept.
The sentinel_for concept.
decltype(begin(declval(Rng &))) iterator_t
Definition access.hpp:698
Tiny meta-programming library.
Definition adjacent_difference.hpp:75
Definition arithmetic.hpp:78
Definition identity.hpp:25
Definition arithmetic.hpp:35