Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:09:55

0001 /// \file
0002 // Range v3 library
0003 //
0004 //  Copyright Eric Niebler 2014-present
0005 //
0006 //  Use, modification and distribution is subject to the
0007 //  Boost Software License, Version 1.0. (See accompanying
0008 //  file LICENSE_1_0.txt or copy at
0009 //  http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 // Project home: https://github.com/ericniebler/range-v3
0012 //
0013 
0014 #ifndef RANGES_V3_VIEW_FOR_EACH_HPP
0015 #define RANGES_V3_VIEW_FOR_EACH_HPP
0016 
0017 #include <utility>
0018 
0019 #include <meta/meta.hpp>
0020 
0021 #include <range/v3/range_fwd.hpp>
0022 
0023 #include <range/v3/functional/bind_back.hpp>
0024 #include <range/v3/functional/invoke.hpp>
0025 #include <range/v3/utility/static_const.hpp>
0026 #include <range/v3/view/all.hpp>
0027 #include <range/v3/view/generate_n.hpp>
0028 #include <range/v3/view/join.hpp>
0029 #include <range/v3/view/repeat_n.hpp>
0030 #include <range/v3/view/single.hpp>
0031 #include <range/v3/view/transform.hpp>
0032 #include <range/v3/view/view.hpp>
0033 
0034 #include <range/v3/detail/prologue.hpp>
0035 
0036 namespace ranges
0037 {
0038     /// \addtogroup group-views
0039     /// @{
0040 
0041     namespace views
0042     {
0043         /// Lazily applies an unary function to each element in the source
0044         /// range that returns another range (possibly empty), flattening
0045         /// the result.
0046         struct for_each_base_fn
0047         {
0048             template(typename Rng, typename Fun)(
0049                 requires viewable_range<Rng> AND transformable_range<Rng, Fun> AND
0050                     joinable_range<transform_view<all_t<Rng>, Fun>>)
0051             constexpr auto operator()(Rng && rng, Fun fun) const
0052             {
0053                 return join(transform(static_cast<Rng &&>(rng), std::move(fun)));
0054             }
0055         };
0056 
0057         struct for_each_fn : for_each_base_fn
0058         {
0059             using for_each_base_fn::operator();
0060 
0061             template<typename Fun>
0062             constexpr auto operator()(Fun fun) const
0063             {
0064                 return make_view_closure(bind_back(for_each_base_fn{}, std::move(fun)));
0065             }
0066         };
0067 
0068         /// \relates for_each_fn
0069         RANGES_INLINE_VARIABLE(for_each_fn, for_each)
0070     } // namespace views
0071 
0072     struct yield_fn
0073     {
0074         template(typename V)(
0075             requires copy_constructible<V>)
0076         single_view<V> operator()(V v) const
0077         {
0078             return views::single(std::move(v));
0079         }
0080     };
0081 
0082     /// \relates yield_fn
0083     RANGES_INLINE_VARIABLE(yield_fn, yield)
0084 
0085     struct yield_from_fn
0086     {
0087         template(typename Rng)(
0088             requires view_<Rng>)
0089         Rng operator()(Rng rng) const
0090         {
0091             return rng;
0092         }
0093     };
0094 
0095     /// \relates yield_from_fn
0096     RANGES_INLINE_VARIABLE(yield_from_fn, yield_from)
0097 
0098     struct yield_if_fn
0099     {
0100         template<typename V>
0101         repeat_n_view<V> operator()(bool b, V v) const
0102         {
0103             return views::repeat_n(std::move(v), b ? 1 : 0);
0104         }
0105     };
0106 
0107     /// \relates yield_if_fn
0108     RANGES_INLINE_VARIABLE(yield_if_fn, yield_if)
0109 
0110     struct lazy_yield_if_fn
0111     {
0112         template(typename F)(
0113             requires invocable<F &>)
0114         generate_n_view<F> operator()(bool b, F f) const
0115         {
0116             return views::generate_n(std::move(f), b ? 1 : 0);
0117         }
0118     };
0119 
0120     /// \relates lazy_yield_if_fn
0121     RANGES_INLINE_VARIABLE(lazy_yield_if_fn, lazy_yield_if)
0122     /// @}
0123 
0124     /// \cond
0125     template(typename Rng, typename Fun)(
0126         requires viewable_range<Rng> AND views::transformable_range<Rng, Fun> AND
0127             input_range<invoke_result_t<Fun &, range_reference_t<Rng>>>)
0128         auto
0129         operator>>=(Rng && rng, Fun fun)
0130     {
0131         return views::for_each(static_cast<Rng &&>(rng), std::move(fun));
0132     }
0133     /// \endcond
0134 } // namespace ranges
0135 
0136 #include <range/v3/detail/epilogue.hpp>
0137 
0138 #endif