TermOx
filter_view.hpp
1 #ifndef TERMOX_COMMON_FILTER_VIEW_HPP
2 #define TERMOX_COMMON_FILTER_VIEW_HPP
3 #include <cstddef>
4 #include <type_traits>
5 #include <utility>
6 
7 #include <termox/common/filter_iterator.hpp>
8 
9 namespace ox {
10 
12 
14 template <typename Container, typename Filter_fn>
15 class Filter_view {
16  public:
17  Filter_view(Container& c, Filter_fn&& filter_fn)
18  : container_{c}, filter_fn_{std::forward<Filter_fn>(filter_fn)}
19  {}
20 
21  public:
22  [[nodiscard]] auto begin() const
23  {
24  return Filter_iterator{std::cbegin(container_), std::cend(container_),
25  filter_fn_};
26  }
27 
28  [[nodiscard]] auto begin()
29  {
30  return Filter_iterator{std::begin(container_), std::end(container_),
31  filter_fn_};
32  }
33 
34  [[nodiscard]] auto end() const
35  {
36  return Filter_iterator{std::cend(container_), std::cend(container_),
37  filter_fn_};
38  }
39 
40  [[nodiscard]] auto end()
41  {
42  return Filter_iterator{std::end(container_), std::end(container_),
43  filter_fn_};
44  }
45 
46  private:
47  Container& container_;
48  Filter_fn filter_fn_;
49 };
50 
52 
54 template <typename Container, typename Filter_fn>
56  public:
57  Owning_filter_view(Container c, Filter_fn&& filter_fn)
58  : container_{std::move(c)},
59  filter_fn_{std::forward<Filter_fn>(filter_fn)}
60  {}
61 
62  public:
63  [[nodiscard]] auto begin() const
64  {
65  return Filter_iterator{std::cbegin(container_), std::cend(container_),
66  filter_fn_};
67  }
68 
69  [[nodiscard]] auto begin()
70  {
71  return Filter_iterator{std::begin(container_), std::end(container_),
72  filter_fn_};
73  }
74 
75  [[nodiscard]] auto end() const
76  {
77  return Filter_iterator{std::cend(container_), std::cend(container_),
78  filter_fn_};
79  }
80 
81  [[nodiscard]] auto end()
82  {
83  return Filter_iterator{std::end(container_), std::end(container_),
84  filter_fn_};
85  }
86 
87  private:
88  Container container_;
89  Filter_fn filter_fn_;
90 };
91 
92 } // namespace ox
93 #endif // TERMOX_COMMON_FILTER_VIEW_HPP
operator++ skips underlying elements of it that do not satisfy predicate.
Definition: filter_iterator.hpp:10
Read only Container view with Filter_iterator interface.
Definition: filter_view.hpp:15
Container view with Filter_iterator interface.
Definition: filter_view.hpp:55