Awali
Another Weighted Automata library
cont_filter.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_MISC_CONT_FILTER_HH
18 # define AWALI_MISC_CONT_FILTER_HH
19 
20 #include<iterator>
21 #include<functional>
22 
23 
24 namespace awali {
25  namespace sttc {
26  namespace internal {
27 
28  template <typename Container>
30  using value_t = typename Container::value_type;
31  using pred_t = std::function<bool(const value_t&)>;
32  using value_type = unsigned;
33  using reference = unsigned&;
34  using difference_type = int;
35  using pointer = unsigned*;
36  using iterator_category = std::forward_iterator_tag ;
37  it_indice_filter(Container cont, unsigned current, unsigned end, pred_t pred) :
38  current(current), length(end), pred(pred), cont(cont) {}
39 
40  it_indice_filter(unsigned end) :
41  current(end), length(end) {}
42 
43  unsigned current;
44  unsigned length;
46  Container cont;
47 
48  unsigned operator*() {
49  return current;
50  }
51 
53  do {
54  ++current;
55  } while(current!=length && !pred(cont[current]));
56  return *this;
57  }
58 
59  bool operator!=(const it_indice_filter& it) const {
60  return current != it.current;
61  }
62  bool operator==(const it_indice_filter& it) const {
63  return current == it.current;
64  }
65  };
66 
67 
68  template <typename Container>
69  struct indice_filter {
70  using value_t = typename Container::value_type;
71  using pred_t = std::function<bool(const value_t&)>;
73 
74  indice_filter(const Container& cont, pred_t pred) :
75  cont(cont), pred(pred), shift(0) {}
76  indice_filter(const Container& cont, pred_t pred, unsigned shift) :
77  cont(cont), pred(pred), shift(shift) {}
78  const Container& cont;
80  unsigned shift;
81 
83  unsigned b=shift;
84  unsigned e= cont.size();
85  while(b!=e && !pred(cont[b])) ++b;
86  return const_iterator{cont, b, e, pred};
87  }
88 
89  const_iterator end() const {
90  return const_iterator{(unsigned)cont.size()};
91  }
92  bool empty() const {
93  return begin()==end();
94  }
95 
96  unsigned size() const {
97  unsigned s=0;
98  for( const_iterator b=begin(), e= end(); b!=e ; ++b) ++s;
99  return s;
100  }
101  };
102 
103 
104  template <typename Container>
105  struct it_filter {
106  using value_type = typename Container::value_type;
107  using pred_t = std::function<bool(const value_type&)>;
109  using difference_type = int;
110  using pointer = value_type*;
111  using iterator_category = std::forward_iterator_tag ;
112  using it_t = typename Container::const_iterator;
113 
115  current(current), end_(end), pred(pred) {}
116 
118  current(end), end_(end) {}
119 
123 
124  const value_type& operator*() const {
125  return *current;
126  }
127 
129  do {
130  ++current;
131  } while(current!=end_ && !pred(*current));
132  return *this;
133  }
134 
135  bool operator!=(const it_filter& it) const {
136  return current != it.current;
137  }
138  bool operator==(const it_filter& it) const {
139  return current == it.current;
140  }
141  };
142 
143 
144  template <typename Container>
145  struct cont_filter {
146  using value_type = typename Container::value_type;
147  using pred_t = std::function<bool(const value_type&)>;
149  using it_t = typename Container::const_iterator;
150 
151  cont_filter(const Container& cont, pred_t pred) :
152  cont(cont), pred(pred) {}
153  const Container& cont;
155 
157  it_t b=cont.begin(), e= cont.end();
158  while(b!=e && !pred(*b)) ++b;
159  return const_iterator{b, e, pred};
160  }
161 
162  const_iterator end() const {
163  return const_iterator{cont.end()};
164  }
165 
166  bool empty() const {
167  return begin()==end();
168  }
169 
170  unsigned size() const {
171  unsigned s=0;
172  for( const_iterator b=begin(), e= end(); b!=e ; ++b) ++s;
173  return s;
174  }
175 
176  };
177 
178  template <typename Iterator>
179  struct it_cont {
180  using value_type = typename Iterator::value_type;
181 
182  it_cont(Iterator begin, Iterator end) :
183  begin_(begin), end_(end) {}
184  Iterator begin_;
185  Iterator end_;
186 
187 
188  Iterator begin() const {
189  return begin_;
190  }
191 
192  Iterator end() const {
193  return end_;
194  }
195 
196  bool empty() const {
197  return begin_==end_;
198  }
199 
200  unsigned size() const {
201  unsigned s=0;
202  for( Iterator b=begin_, e= end_; b!=e ; ++b) ++s;
203  return s;
204  }
205 
206  };
207  }
208  }
209 }//end of ns awali::stc
210 
211 #endif // !AWALI_MISC_CONT_FILTER_HH
The Boolean semring.
Definition: b.hh:38
Main namespace of Awali.
Definition: ato.hh:22
Definition: cont_filter.hh:145
const_iterator end() const
Definition: cont_filter.hh:162
const_iterator begin() const
Definition: cont_filter.hh:156
cont_filter(const Container &cont, pred_t pred)
Definition: cont_filter.hh:151
pred_t pred
Definition: cont_filter.hh:154
typename Container::value_type value_type
Definition: cont_filter.hh:146
const Container & cont
Definition: cont_filter.hh:153
bool empty() const
Definition: cont_filter.hh:166
typename Container::const_iterator it_t
Definition: cont_filter.hh:149
unsigned size() const
Definition: cont_filter.hh:170
std::function< bool(const value_type &)> pred_t
Definition: cont_filter.hh:147
Definition: cont_filter.hh:69
indice_filter(const Container &cont, pred_t pred)
Definition: cont_filter.hh:74
const_iterator begin() const
Definition: cont_filter.hh:82
bool empty() const
Definition: cont_filter.hh:92
std::function< bool(const value_t &)> pred_t
Definition: cont_filter.hh:71
indice_filter(const Container &cont, pred_t pred, unsigned shift)
Definition: cont_filter.hh:76
typename Container::value_type value_t
Definition: cont_filter.hh:70
const_iterator end() const
Definition: cont_filter.hh:89
unsigned size() const
Definition: cont_filter.hh:96
pred_t pred
Definition: cont_filter.hh:79
const Container & cont
Definition: cont_filter.hh:78
unsigned shift
Definition: cont_filter.hh:80
Definition: cont_filter.hh:179
unsigned size() const
Definition: cont_filter.hh:200
Iterator end() const
Definition: cont_filter.hh:192
Iterator begin() const
Definition: cont_filter.hh:188
typename Iterator::value_type value_type
Definition: cont_filter.hh:180
bool empty() const
Definition: cont_filter.hh:196
Iterator begin_
Definition: cont_filter.hh:184
it_cont(Iterator begin, Iterator end)
Definition: cont_filter.hh:182
Iterator end_
Definition: cont_filter.hh:185
Definition: cont_filter.hh:105
value_type * pointer
Definition: cont_filter.hh:110
bool operator!=(const it_filter &it) const
Definition: cont_filter.hh:135
const value_type & operator*() const
Definition: cont_filter.hh:124
it_filter(it_t end)
Definition: cont_filter.hh:117
std::forward_iterator_tag iterator_category
Definition: cont_filter.hh:111
it_filter & operator++()
Definition: cont_filter.hh:128
typename Container::value_type value_type
Definition: cont_filter.hh:106
it_filter(it_t current, it_t end, pred_t pred)
Definition: cont_filter.hh:114
value_type & reference
Definition: cont_filter.hh:108
int difference_type
Definition: cont_filter.hh:109
bool operator==(const it_filter &it) const
Definition: cont_filter.hh:138
it_t current
Definition: cont_filter.hh:120
pred_t pred
Definition: cont_filter.hh:122
typename Container::const_iterator it_t
Definition: cont_filter.hh:112
it_t end_
Definition: cont_filter.hh:121
std::function< bool(const value_type &)> pred_t
Definition: cont_filter.hh:107
Definition: cont_filter.hh:29
unsigned & reference
Definition: cont_filter.hh:33
int difference_type
Definition: cont_filter.hh:34
it_indice_filter(unsigned end)
Definition: cont_filter.hh:40
Container cont
Definition: cont_filter.hh:46
typename Container::value_type value_t
Definition: cont_filter.hh:30
unsigned * pointer
Definition: cont_filter.hh:35
bool operator==(const it_indice_filter &it) const
Definition: cont_filter.hh:62
it_indice_filter & operator++()
Definition: cont_filter.hh:52
unsigned operator*()
Definition: cont_filter.hh:48
std::forward_iterator_tag iterator_category
Definition: cont_filter.hh:36
bool operator!=(const it_indice_filter &it) const
Definition: cont_filter.hh:59
std::function< bool(const value_t &)> pred_t
Definition: cont_filter.hh:31
unsigned length
Definition: cont_filter.hh:44
unsigned current
Definition: cont_filter.hh:43
pred_t pred
Definition: cont_filter.hh:45
unsigned value_type
Definition: cont_filter.hh:32
it_indice_filter(Container cont, unsigned current, unsigned end, pred_t pred)
Definition: cont_filter.hh:37