Vcsn  2.4
Be Rational
crange.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <type_traits>
4 
5 #include <boost/iterator/filter_iterator.hpp>
6 
8 
9 namespace vcsn
10 {
12  template <typename C>
13  struct container_range
14  {
15  public:
17  using value_type = typename unref_C::value_type;
18 
19  using const_iterator = typename unref_C::const_iterator;
20  using iterator = typename unref_C::const_iterator;
21 
22  private:
23  const C cont_;
24  public:
25  container_range(const unref_C& cont)
26  : cont_(cont)
27  {}
28 
30 #define DEFINE(Name) \
31  auto Name() const -> decltype(this->cont_.Name()) \
32  { \
33  return cont_.Name(); \
34  }
45 #undef DEFINE
46  };
47 
48 
49  template <typename Cont, typename Pred>
51  {
52  public:
53  using container_t = Cont;
55  using value_type = typename unref_C::value_type;
56  using predicate_t = Pred;
57  using const_iterator
58  = boost::filter_iterator<predicate_t, typename unref_C::const_iterator>;
59  public:
60  container_filter_range(const unref_C& cont, predicate_t predicate)
61  : cont_(cont)
62  , predicate_(predicate)
63  {}
64 
66  {
67  return {predicate_, cont_.begin(), cont_.end()};
68  }
69 
71  {
72  return {predicate_, cont_.end(), cont_.end()};
73  }
74 
79  value_type front() const
80  {
81  assert(!empty());
82  return *begin();
83  }
84 
89  value_type back() const
90  {
91  assert(!empty());
92  return cont_.back();
93  }
94 
95  ATTRIBUTE_PURE
96  bool empty() const
97  {
98  return begin() == end();
99  }
100 
101  ATTRIBUTE_PURE
102  size_t size() const
103  {
104  return std::distance(begin(), end());
105  }
106 
107  private:
110  };
111 
112  template <typename Cont, typename Pred>
114  make_container_filter_range(const Cont& cont, Pred pred)
115  {
116  return {cont, pred};
117  }
118 }
container_filter_range< Cont, Pred > make_container_filter_range(const Cont &cont, Pred pred)
Definition: crange.hh:114
typename std::remove_reference< C >::type unref_C
Definition: crange.hh:16
auto back() const -> decltype(this->cont_.back())
The last element.
Definition: crange.hh:42
auto empty() const -> decltype(this->cont_.empty())
Definition: crange.hh:43
auto front() const -> decltype(this->cont_.front())
The first element.
Definition: crange.hh:39
typename unref_C::const_iterator iterator
Definition: crange.hh:20
ATTRIBUTE_PURE bool empty() const
Definition: crange.hh:96
const container_t cont_
Definition: crange.hh:108
std::string type(const automaton &a)
The implementation type of a.
Definition: others.cc:239
value_type back() const
The last element.
Definition: crange.hh:89
Definition: a-star.hh:8
value_type front() const
The first element.
Definition: crange.hh:79
typename unref_C::value_type value_type
Definition: crange.hh:17
typename std::remove_reference< container_t >::type unref_C
Definition: crange.hh:54
ATTRIBUTE_PURE size_t size() const
Definition: crange.hh:102
#define DEFINE(Name)
Forward function Name to the container.
Definition: crange.hh:30
auto end() const -> decltype(this->cont_.end())
Definition: crange.hh:36
auto begin() const -> decltype(this->cont_.begin())
Definition: crange.hh:35
boost::filter_iterator< predicate_t, typename unref_C::const_iterator > const_iterator
Definition: crange.hh:58
const_iterator end() const
Definition: crange.hh:70
typename unref_C::const_iterator const_iterator
Definition: crange.hh:19
typename unref_C::value_type value_type
Definition: crange.hh:55
const_iterator begin() const
Definition: crange.hh:65
container_range(const unref_C &cont)
Definition: crange.hh:25
auto size() const -> decltype(this->cont_.size())
Definition: crange.hh:44
container_filter_range(const unref_C &cont, predicate_t predicate)
Definition: crange.hh:60