Debian Codegraph @

3rd_party/range-v3/range/v3/numeric/adjacent_difference.hpp

raw
1 /// \file
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>
23 #include <range/v3/functional/arithmetic.hpp>
24 #include <range/v3/functional/identity.hpp>
25 #include <range/v3/functional/invoke.hpp>
26 #include <range/v3/iterator/concepts.hpp>
27 #include <range/v3/iterator/traits.hpp>
28 #include <range/v3/iterator/unreachable_sentinel.hpp>
29 #include <range/v3/range/access.hpp>
30 #include <range/v3/range/concepts.hpp>
31 #include <range/v3/range/dangling.hpp>
32 #include <range/v3/range/traits.hpp>
33 #include <range/v3/utility/static_const.hpp>
34
35 #include <range/v3/detail/prologue.hpp>
36
37 namespace ranges
38 {
39 /// \addtogroup group-numerics
40 /// @{
41 // clang-format off
42 /// \concept differenceable_
43 /// \brief The \c differenceable_ concept
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
63 /// \concept differenceable
64 /// \brief The \c differenceable concept
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
74 struct adjacent_difference_fn
75 {
76 template(typename I, typename S, typename O, typename S2, typename BOp = minus,
77 typename P = identity)(
78 requires sentinel_for<S, I> AND sentinel_for<S2, O> AND
79 differenceable<I, O, BOp, P>)
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)(
110 requires sentinel_for<S, I> AND differenceable<I, O, BOp, P>)
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>)(
124 requires range<Rng> AND differenceable<I, O, BOp, P>)
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>)(
137 requires range<Rng> AND range<ORng> AND differenceable<I, O, BOp, P>)
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)
151 /// @}
152 } // namespace ranges
153
154 #include <range/v3/detail/epilogue.hpp>
155
156 #endif