Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
crange.hh
Go to the documentation of this file.
1 #ifndef VCSN_CORE_CRANGE_HH
2 # define VCSN_CORE_CRANGE_HH
3 
4 # include <functional> // std::function
5 # include <type_traits>
6 
7 # include <boost/iterator/filter_iterator.hpp>
8 
9 namespace vcsn
10 {
12  template <typename C>
14  {
15  public:
16  using unref_C = typename std::remove_reference<C>::type;
17  using value_type = typename unref_C::value_type;
18 
19  using const_iterator = typename unref_C::const_iterator;
20  private:
21  const C cont_;
22  public:
23  container_range(const unref_C& cont)
24  : cont_(cont)
25  {}
26 
28 #define DEFINE(Name) \
29  auto Name() const -> decltype(this->cont_.Name()) \
30  { \
31  return cont_.Name(); \
32  }
43 #undef DEFINE
44  };
45 
46 
47  template <typename C>
49  {
50  public:
51  using unref_C = typename std::remove_reference<C>::type;
52  using value_type = typename unref_C::value_type;
53  using predicate_t = std::function<bool(value_type)>;
54  using const_iterator
55  = boost::filter_iterator<predicate_t, typename unref_C::const_iterator>;
56  public:
57  container_filter_range(const unref_C& cont, predicate_t predicate)
58  : cont_(cont)
59  , predicate_(predicate)
60  {}
61 
63  {
64  return {predicate_, cont_.begin(), cont_.end()};
65  }
66 
68  {
69  return {predicate_, cont_.end(), cont_.end()};
70  }
71 
76  value_type front() const
77  {
78  assert(!empty());
79  return *begin();
80  }
81 
86  value_type back() const
87  {
88  assert(!empty());
89  return cont_.back();
90  }
91 
92  bool empty() const
93  {
94  return begin() == end();
95  }
96 
97  size_t size() const
98  {
99  return std::distance(begin(), end());
100  }
101 
102  private:
103  const C cont_;
105  };
106 }
107 
108 #endif // !VCSN_CORE_CRANGE_HH
auto begin() const -> decltype(this->cont_.begin())
Definition: crange.hh:33
Restrict the interface of a container to begin/end.
Definition: crange.hh:13
typename unref_C::value_type value_type
Definition: crange.hh:52
value_type back() const
The last element.
Definition: crange.hh:86
auto empty() const -> decltype(this->cont_.empty())
Definition: crange.hh:41
typename std::remove_reference< C >::type unref_C
Definition: crange.hh:16
const_iterator begin() const
Definition: crange.hh:62
auto size() const -> decltype(this->cont_.size())
Definition: crange.hh:42
value_type front() const
The first element.
Definition: crange.hh:76
std::function< bool(value_type)> predicate_t
Definition: crange.hh:53
container_range(const unref_C &cont)
Definition: crange.hh:23
auto end() const -> decltype(this->cont_.end())
Definition: crange.hh:34
typename std::remove_reference< C >::type unref_C
Definition: crange.hh:51
size_t size() const
Definition: crange.hh:97
typename unref_C::const_iterator const_iterator
Definition: crange.hh:19
#define DEFINE(Name)
Forward function Name to the container.
Definition: crange.hh:28
auto back() const -> decltype(this->cont_.back())
The last element.
Definition: crange.hh:40
const_iterator end() const
Definition: crange.hh:67
boost::filter_iterator< predicate_t, typename unref_C::const_iterator > const_iterator
Definition: crange.hh:55
container_filter_range(const unref_C &cont, predicate_t predicate)
Definition: crange.hh:57
typename unref_C::value_type value_type
Definition: crange.hh:17
auto front() const -> decltype(this->cont_.front())
The first element.
Definition: crange.hh:37