Debian Codegraph @

3rd_party/range-v3/range/v3/view/for_each.hpp

raw
1 /// \file
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
14 #ifndef RANGES_V3_VIEW_FOR_EACH_HPP
15 #define RANGES_V3_VIEW_FOR_EACH_HPP
16
17 #include <utility>
18
19 #include <meta/meta.hpp>
20
21 #include <range/v3/range_fwd.hpp>
22
23 #include <range/v3/functional/bind_back.hpp>
24 #include <range/v3/functional/invoke.hpp>
25 #include <range/v3/utility/static_const.hpp>
26 #include <range/v3/view/all.hpp>
27 #include <range/v3/view/generate_n.hpp>
28 #include <range/v3/view/join.hpp>
29 #include <range/v3/view/repeat_n.hpp>
30 #include <range/v3/view/single.hpp>
31 #include <range/v3/view/transform.hpp>
32 #include <range/v3/view/view.hpp>
33
34 #include <range/v3/detail/prologue.hpp>
35
36 namespace ranges
37 {
38 /// \addtogroup group-views
39 /// @{
40
41 namespace views
42 {
43 /// Lazily applies an unary function to each element in the source
44 /// range that returns another range (possibly empty), flattening
45 /// the result.
46 struct for_each_base_fn
47 {
48 template(typename Rng, typename Fun)(
49 requires viewable_range<Rng> AND transformable_range<Rng, Fun> AND
50 joinable_range<transform_view<all_t<Rng>, Fun>>)
51 constexpr auto operator()(Rng && rng, Fun fun) const
52 {
53 return join(transform(static_cast<Rng &&>(rng), std::move(fun)));
54 }
55 };
56
57 struct for_each_fn : for_each_base_fn
58 {
59 using for_each_base_fn::operator();
60
61 template<typename Fun>
62 constexpr auto operator()(Fun fun) const
63 {
64 return make_view_closure(bind_back(for_each_base_fn{}, std::move(fun)));
65 }
66 };
67
68 /// \relates for_each_fn
69 RANGES_INLINE_VARIABLE(for_each_fn, for_each)
70 } // namespace views
71
72 struct yield_fn
73 {
74 template(typename V)(
75 requires copy_constructible<V>)
76 single_view<V> operator()(V v) const
77 {
78 return views::single(std::move(v));
79 }
80 };
81
82 /// \relates yield_fn
83 RANGES_INLINE_VARIABLE(yield_fn, yield)
84
85 struct yield_from_fn
86 {
87 template(typename Rng)(
88 requires view_<Rng>)
89 Rng operator()(Rng rng) const
90 {
91 return rng;
92 }
93 };
94
95 /// \relates yield_from_fn
96 RANGES_INLINE_VARIABLE(yield_from_fn, yield_from)
97
98 struct yield_if_fn
99 {
100 template<typename V>
101 repeat_n_view<V> operator()(bool b, V v) const
102 {
103 return views::repeat_n(std::move(v), b ? 1 : 0);
104 }
105 };
106
107 /// \relates yield_if_fn
108 RANGES_INLINE_VARIABLE(yield_if_fn, yield_if)
109
110 struct lazy_yield_if_fn
111 {
112 template(typename F)(
113 requires invocable<F &>)
114 generate_n_view<F> operator()(bool b, F f) const
115 {
116 return views::generate_n(std::move(f), b ? 1 : 0);
117 }
118 };
119
120 /// \relates lazy_yield_if_fn
121 RANGES_INLINE_VARIABLE(lazy_yield_if_fn, lazy_yield_if)
122 /// @}
123
124 /// \cond
125 template(typename Rng, typename Fun)(
126 requires viewable_range<Rng> AND views::transformable_range<Rng, Fun> AND
127 input_range<invoke_result_t<Fun &, range_reference_t<Rng>>>)
128 auto
129 operator>>=(Rng && rng, Fun fun)
130 {
131 return views::for_each(static_cast<Rng &&>(rng), std::move(fun));
132 }
133 /// \endcond
134 } // namespace ranges
135
136 #include <range/v3/detail/epilogue.hpp>
137
138 #endif