Horizon
Loading...
Searching...
No Matches
transform.hpp
Go to the documentation of this file.
1
2// Range v3 library
3//
4// Copyright Eric Niebler 2014-present
5//
6// Use, modification and distribution is subject to the
7// Boost Software License, Version 1.0. (See accompanying
8// file LICENSE_1_0.txt or copy at
9// http://www.boost.org/LICENSE_1_0.txt)
10//
11// Project home: https://github.com/ericniebler/range-v3
12//
13#ifndef RANGES_V3_ALGORITHM_TRANSFORM_HPP
14#define RANGES_V3_ALGORITHM_TRANSFORM_HPP
15
16#include <utility>
17
18#include <meta/meta.hpp>
19
21
22#include <range/v3/algorithm/result_types.hpp>
32#include <range/v3/utility/static_const.hpp>
33
34#include <range/v3/detail/prologue.hpp>
35
36namespace ranges
37{
40 template<typename I, typename O>
41 using unary_transform_result = detail::in_out_result<I, O>;
42
43 template<typename I1, typename I2, typename O>
44 using binary_transform_result = detail::in1_in2_out_result<I1, I2, O>;
45
46 RANGES_FUNC_BEGIN(transform)
47
48 // Single-range variant
50 template(typename I, typename S, typename O, typename F, typename P = identity)(
51 requires input_iterator<I> AND sentinel_for<S, I> AND
52 weakly_incrementable<O> AND copy_constructible<F> AND
53 indirectly_writable<O, indirect_result_t<F &, projected<I, P>>>)
54 constexpr unary_transform_result<I, O> //
55 RANGES_FUNC(transform)(I first, S last, O out, F fun, P proj = P{}) //
56 {
57 for(; first != last; ++first, ++out)
58 *out = invoke(fun, invoke(proj, *first));
59 return {first, out};
60 }
61
63 template(typename Rng, typename O, typename F, typename P = identity)(
64 requires input_range<Rng> AND weakly_incrementable<O> AND
65 copy_constructible<F> AND
66 indirectly_writable<O, indirect_result_t<F &, projected<iterator_t<Rng>, P>>>)
67 constexpr unary_transform_result<borrowed_iterator_t<Rng>, O> //
68 RANGES_FUNC(transform)(Rng && rng, O out, F fun, P proj = P{}) //
69 {
70 return (*this)(
71 begin(rng), end(rng), std::move(out), std::move(fun), std::move(proj));
72 }
73
74 // Double-range variant, 4-iterator version
76 template(typename I0,
77 typename S0,
78 typename I1,
79 typename S1,
80 typename O,
81 typename F,
82 typename P0 = identity,
83 typename P1 = identity)(
86 weakly_incrementable<O> AND copy_constructible<F> AND
88 O,
89 indirect_result_t<F &, projected<I0, P0>, projected<I1, P1>>>)
90 constexpr binary_transform_result<I0, I1, O> //
91 RANGES_FUNC(transform)(I0 begin0,
92 S0 end0,
93 I1 begin1,
94 S1 end1,
95 O out,
96 F fun,
97 P0 proj0 = P0{},
98 P1 proj1 = P1{}) //
99 {
100 for(; begin0 != end0 && begin1 != end1; ++begin0, ++begin1, ++out)
101 *out = invoke(fun, invoke(proj0, *begin0), invoke(proj1, *begin1));
102 return {begin0, begin1, out};
103 }
104
106 template(typename Rng0,
107 typename Rng1,
108 typename O,
109 typename F,
110 typename P0 = identity,
111 typename P1 = identity)(
112 requires input_range<Rng0> AND input_range<Rng1> AND
113 weakly_incrementable<O> AND copy_constructible<F> AND
115 O,
116 indirect_result_t<F &,
117 projected<iterator_t<Rng0>, P0>,
118 projected<iterator_t<Rng1>, P1>>>)
119 constexpr binary_transform_result<borrowed_iterator_t<Rng0>,
120 borrowed_iterator_t<Rng1>,
121 O> //
122 RANGES_FUNC(transform)(
123 Rng0 && rng0, Rng1 && rng1, O out, F fun, P0 proj0 = P0{}, P1 proj1 = P1{}) //
124 {
125 return (*this)(begin(rng0),
126 end(rng0),
127 begin(rng1),
128 end(rng1),
129 std::move(out),
130 std::move(fun),
131 std::move(proj0),
132 std::move(proj1));
133 }
134
135 // Double-range variant, 3-iterator version
137 template(typename I0,
138 typename S0,
139 typename I1,
140 typename O,
141 typename F,
142 typename P0 = identity,
143 typename P1 = identity)(
146 copy_constructible<F> AND
148 O,
149 indirect_result_t<F &, projected<I0, P0>, projected<I1, P1>>>)
150 RANGES_DEPRECATED(
151 "Use the variant of ranges::transform that takes an upper bound "
152 "for both input ranges")
153 binary_transform_result<I0, I1, O> //
154 RANGES_FUNC(transform)(I0 begin0,
155 S0 end0,
156 I1 begin1,
157 O out,
158 F fun,
159 P0 proj0 = P0{},
160 P1 proj1 = P1{})
161 {
162 return (*this)(std::move(begin0),
163 std::move(end0),
164 std::move(begin1),
165 unreachable,
166 std::move(out),
167 std::move(fun),
168 std::move(proj0),
169 std::move(proj1));
170 }
171
173 template(typename Rng0,
174 typename I1Ref,
175 typename O,
176 typename F,
177 typename P0 = identity,
178 typename P1 = identity)(
179 requires input_range<Rng0> AND input_iterator<uncvref_t<I1Ref>> AND
180 weakly_incrementable<O> AND copy_constructible<F> AND
182 O,
183 indirect_result_t<F &,
184 projected<iterator_t<Rng0>, P0>,
185 projected<uncvref_t<I1Ref>, P1>>>)
186 RANGES_DEPRECATED(
187 "Use the variant of ranges::transform that takes an upper bound "
188 "for both input ranges")
189 binary_transform_result<borrowed_iterator_t<Rng0>, uncvref_t<I1Ref>, O> //
190 RANGES_FUNC(transform)(Rng0 && rng0,
191 I1Ref && begin1,
192 O out,
193 F fun,
194 P0 proj0 = P0{},
195 P1 proj1 = P1{}) //
196 {
197 return (*this)(begin(rng0),
198 end(rng0),
199 static_cast<I1Ref &&>(begin1),
200 unreachable,
201 std::move(out),
202 std::move(fun),
203 std::move(proj0),
204 std::move(proj1));
205 }
206
207 RANGES_FUNC_END(transform)
208
209 namespace cpp20
210 {
211 using ranges::binary_transform_result;
212 using ranges::transform;
213 using ranges::unary_transform_result;
214 } // namespace cpp20
216} // namespace ranges
217
218#include <range/v3/detail/epilogue.hpp>
219
220#endif
The indirectly_writable concept.
The input_iterator concept.
The input_range concept.
The sentinel_for concept.
The weakly_incrementable concept.
decltype(begin(declval(Rng &))) iterator_t
Definition access.hpp:698
typename Fn::template invoke< Args... > invoke
Evaluate the invocable Fn with the arguments Args.
Definition meta.hpp:541
front< Pair > first
Retrieve the first element of the pair Pair.
Definition meta.hpp:2251
Tiny meta-programming library.
Definition identity.hpp:25