Horizon
filter.hpp
Go to the documentation of this file.
1 // Range v3 library
3 //
4 // Copyright Eric Niebler 2013-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_FILTER_HPP
15 #define RANGES_V3_VIEW_FILTER_HPP
16 
17 #include <range/v3/range_fwd.hpp>
18 
22 #include <range/v3/utility/static_const.hpp>
24 
25 #include <range/v3/detail/prologue.hpp>
26 
27 namespace ranges
28 {
31  template<typename Rng, typename Pred>
32  struct filter_view : remove_if_view<Rng, logical_negate<Pred>>
33  {
34  filter_view() = default;
35  constexpr filter_view(Rng rng, Pred pred)
36  : filter_view::remove_if_view{std::move(rng), not_fn(std::move(pred))}
37  {}
38  };
39 
40 #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
41  template(typename Rng, typename Pred)(
42  requires input_range<Rng> AND indirect_unary_predicate<Pred, iterator_t<Rng>> AND
43  view_<Rng> AND std::is_object<Pred>::value) //
44  filter_view(Rng &&, Pred)
45  ->filter_view<views::all_t<Rng>, Pred>;
46 #endif
47 
48  namespace views
49  {
50  struct filter_fn;
51 
55  {
56  template(typename Rng, typename Pred)(
57  requires viewable_range<Rng> AND input_range<Rng> AND
59  constexpr filter_view<all_t<Rng>, Pred> operator()(Rng && rng, Pred pred) //
60  const
61  {
62  return filter_view<all_t<Rng>, Pred>{all(static_cast<Rng &&>(rng)),
63  std::move(pred)};
64  }
65  };
66 
68  {
69  using cpp20_filter_base_fn::operator();
70 
71  template<typename Pred>
72  constexpr auto operator()(Pred pred) const
73  {
74  return make_view_closure(
75  bind_back(cpp20_filter_base_fn{}, std::move(pred)));
76  }
77  };
78 
82  {
83  using cpp20_filter_base_fn::operator();
84 
85  template(typename Rng, typename Pred, typename Proj)(
86  requires viewable_range<Rng> AND input_range<Rng> AND
87  indirect_unary_predicate<Pred, projected<iterator_t<Rng>, Proj>>)
89  operator()(Rng && rng, Pred pred, Proj proj) const
90  {
92  all(static_cast<Rng &&>(rng)),
93  compose(std::move(pred), std::move(proj))};
94  }
95  };
96 
130  {
131  using filter_base_fn::operator();
132 
133  template<typename Pred>
134  constexpr auto operator()(Pred pred) const
135  {
136  return make_view_closure(bind_back(filter_base_fn{}, std::move(pred)));
137  }
138 
139  template(typename Pred, typename Proj)(
140  requires (!range<Pred>))
141  constexpr auto operator()(Pred pred, Proj proj) const
142  {
143  return make_view_closure(
144  bind_back(filter_base_fn{}, std::move(pred), std::move(proj)));
145  }
146  };
147 
151  } // namespace views
152 
153  namespace cpp20
154  {
155  namespace views
156  {
158  }
159  template(typename V, typename Pred)(
160  requires input_range<V> AND indirect_unary_predicate<Pred, iterator_t<V>> AND
161  view_<V> AND std::is_object<Pred>::value) //
162  using filter_view = ranges::filter_view<V, Pred>;
163  } // namespace cpp20
165 } // namespace ranges
166 
167 #include <range/v3/detail/epilogue.hpp>
168 #include <range/v3/detail/satisfy_boost_range.hpp>
169 RANGES_SATISFY_BOOST_RANGE(::ranges::filter_view)
170 
171 #endif
CPP_concept indirect_unary_predicate
\concept indirect_unary_predicate
Definition: concepts.hpp:632
decltype(begin(declval(Rng &))) iterator_t
Definition: access.hpp:698
RANGES_INLINE_VARIABLE(detail::to_container_fn< detail::from_range< std::vector >>, to_vector) template< template< typename... > class ContT > auto to(RANGES_HIDDEN_DETAIL(detail
For initializing a container of the specified type with the elements of an Range.
Definition: conversion.hpp:399
defer< bind_back, Fn, Ts... > bind_back
Definition: meta.hpp:994
compose< quote< not_ >, Fn > not_fn
Logically negate the result of invocable Fn.
Definition: meta.hpp:3009
join< transform< L, detail::filter_< Pred > >> filter
Returns a new meta::list where only those elements of L that satisfy the Callable Pred such that invo...
Definition: meta.hpp:2818
Definition: compose.hpp:33
Definition: filter.hpp:33
Definition: remove_if.hpp:48
Given a source range and a unary predicate, present a view of the elements that satisfy the predicate...
Definition: filter.hpp:55
Definition: filter.hpp:68
Given a source range, unary predicate, and optional projection, present a view of the elements that s...
Definition: filter.hpp:82
Definition: filter.hpp:130