Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
filter.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_FILTER_HH
2 # define VCSN_ALGOS_FILTER_HH
3 
4 # include <algorithm> // std::max
5 # include <boost/range/irange.hpp>
6 # include <vcsn/algos/copy.hh>
8 # include <vcsn/core/crange.hh>
9 # include <vcsn/dyn/fwd.hh>
11 # include <vcsn/misc/vector.hh>
12 
13 namespace vcsn
14 {
15  namespace detail
16  {
18  template <typename Aut>
20  : public automaton_decorator<Aut>
21  {
22  public:
23  using automaton_t = Aut;
28 
33  using states_t = std::unordered_set<state_t>;
34 
35  using tr_cont_t = std::vector<transition_t>;
36  using states_output_t =
38 
39  using super_t::pre;
40  using super_t::post;
41  using super_t::src_of;
42  using super_t::dst_of;
43 
44  filter_automaton_impl(const automaton_t& input, const states_t& ss)
45  : super_t(input), ss_(ss)
46  {
47  ss_.emplace(input->pre());
48  ss_.emplace(input->post());
49  }
50 
52  static std::string sname()
53  {
54  return "filter_automaton<" + automaton_t::element_type::sname() + ">";
55  }
56 
58  std::string vname(bool full = true) const
59  {
60  return "filter_automaton<" + aut_->vname(full) + ">";
61  }
62 
63  bool state_has_name(state_t s) const
64  {
65  assert(has(ss_, s));
66  return aut_->state_has_name(s);
67  }
68 
69  bool has_state(state_t s) const
70  {
71  return has(ss_, s) && aut_->has_state(s);
72  }
73 
74  std::ostream& print_state_name(state_t s, std::ostream& o,
75  const std::string& fmt = "text",
76  bool delimit = false) const
77  {
78  assert(has(ss_, s));
79  return aut_->print_state_name(s, o, fmt, delimit);
80  }
81 
82  size_t num_states()
83  {
84  return states().size();
85  }
86 
87  size_t num_all_states()
88  {
89  return all_states().size();
90  }
91 
92  template <typename Pred>
93  states_output_t all_states(Pred pred) const
94  {
95  return aut_->all_states([this, pred](state_t s)
96  { return pred(s) && has(ss_, s); });
97  }
98 
100  {
101  return all_states([](state_t) { return true; });
102  }
103 
104 
106  {
107  return all_states([this](state_t s)
108  {
109  // In the case transposing
110  // pre() > post().
111  return std::max(pre(), post()) < s;
112  });
113  }
114 
115  template <typename Pred>
117  all_transitions(Pred pred) const
118  {
119  return aut_->all_transitions([this, pred] (transition_t t)
120  {
121  return (pred(t)
122  && has(ss_, src_of(t))
123  && has(ss_, dst_of(t)));
124  });
125  }
126 
131  {
132  return all_transitions([] (transition_t) { return true; });
133  }
134 
137  transitions() const
138  {
139  return all_transitions([this] (transition_t t)
140  {
141  return (src_of(t) != pre()
142  && dst_of(t) != post());
143  });
144  }
145 
146  template <typename Pred>
148  all_out(state_t s, Pred pred) const
149  {
150  return aut_->all_out(s,
151  [this, pred] (transition_t t)
152  {
153  return pred(t) && has(ss_, dst_of(t));
154  });
155  }
156 
158  all_out(state_t s) const
159  {
160  return all_out(s,
161  [] (transition_t) { return true; });
162  }
163 
165  out(state_t s) const
166  {
167  return all_out(s,
168  [this] (transition_t t)
169  {
170  return dst_of(t) != post();
171  });
172  }
173 
175  out(state_t s, const label_t& l) const
176  {
177  return all_out(s,
178  [this, l] (transition_t t)
179  {
180  return aut_.labelset()->equal(aut_->label_of(t, l));
181  });
182  }
183 
184  template <typename Pred>
186  all_in(state_t s, Pred pred) const
187  {
188  return aut_->all_in(s,
189  [this, pred] (transition_t t)
190  {
191  return pred(t) && has(ss_, src_of(t));
192  });
193  }
194 
196  all_in(state_t s) const
197  {
198  return all_in(s, [] (transition_t) { return true; });
199  }
200 
202  in(state_t s) const
203  {
204  return all_in(s,
205  [this] (transition_t t)
206  {
207  return src_of(t) != post();
208  });
209  }
210 
212  in(state_t s, const label_t& l) const
213  {
214  return all_in(s,
215  [this, l] (transition_t t)
216  {
217  return (aut_.labelset()->equal(aut_->label_of(t), l));
218  });
219  }
220 
221  typename automaton_t::element_type::automaton_nocv_t
222  strip() const
223  {
225  }
226 
230  {
231  return out(pre());
232  }
233 
237  {
238  return in(post());
239  }
240 
241  protected:
243  using super_t::aut_;
244 
245  private:
248  };
249  }
250 
251  template <typename Aut>
252  using filter_automaton =
253  std::shared_ptr<detail::filter_automaton_impl<Aut>>;
254 
256  template <typename Aut>
257  inline
259  filter(const Aut& aut, const std::unordered_set<state_t_of<Aut>>& ss)
260  {
261  return make_shared_ptr<filter_automaton<Aut>>(aut, ss);
262  }
263 
264  namespace dyn
265  {
266  namespace detail
267  {
269  template <typename Aut, typename Unsigneds>
270  automaton
271  filter(const automaton& aut, const std::vector<unsigned>& states)
272  {
273  const auto& a = aut->as<Aut>();
274  std::unordered_set<state_t_of<Aut>> ss;
275  // FIXME: this is wrong, of course.
276  for (auto s: states)
277  ss.emplace(s + 2);
278  return make_automaton(::vcsn::filter(a, ss));
279  }
280 
282  (const automaton& aut,
283  const std::vector<unsigned>& ss) -> automaton);
284  }
285  }
286 }
287 
288 #endif
container_filter_range< const tr_cont_t & > in(state_t s, const label_t &l) const
Definition: filter.hh:212
super_t::transitions_output_t all_transitions() const
All the transition indexes between all states (including pre and post).
Definition: filter.hh:130
states_output_t states() const
Definition: filter.hh:105
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
container_filter_range< const tr_cont_t & > all_out(state_t s, Pred pred) const
Definition: filter.hh:148
container_filter_range< const tr_cont_t & > out(state_t s, const label_t &l) const
Definition: filter.hh:175
label_t_of< automaton_t > label_t
Definition: filter.hh:27
states_output_t all_states(Pred pred) const
Definition: filter.hh:93
static std::string sname()
Static name.
Definition: filter.hh:52
states_output_t all_states() const
Definition: filter.hh:99
container_filter_range< const tr_cont_t & > out(state_t s) const
Definition: filter.hh:165
super_t::transitions_output_t transitions() const
All the transition indexes between visible states.
Definition: filter.hh:137
std::string sname()
Definition: name.hh:31
auto dst_of(Args &&...args) const -> decltype(aut_-> dst_of(std::forward< Args >(args)...))
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:77
container_filter_range< const tr_cont_t & > all_in(state_t s, Pred pred) const
Definition: filter.hh:186
bool has_state(state_t s) const
Definition: filter.hh:69
super_t::transitions_output_t all_transitions(Pred pred) const
Definition: filter.hh:117
states_t ss_
The states we keep.
Definition: filter.hh:247
AutOut copy(const AutIn &input, Pred keep_state)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:171
bool state_has_name(state_t s) const
Definition: filter.hh:63
Aggregate an automaton, and forward calls to it.
automaton filter(const automaton &aut, const std::vector< unsigned > &states)
Bridge.
Definition: filter.hh:271
Hide some states of an automaton.
Definition: filter.hh:19
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:33
automaton_t::element_type::automaton_nocv_t strip() const
Definition: filter.hh:222
container_filter_range< const tr_cont_t & > in(state_t s) const
Definition: filter.hh:202
size_t size() const
Definition: crange.hh:97
std::string vname(bool full=true) const
Dynamic name.
Definition: filter.hh:58
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:36
container_filter_range< const tr_cont_t & > initial_transitions() const
Indexes of transitions to visible initial states.
Definition: filter.hh:229
state_t_of< automaton_t > state_t
Definition: filter.hh:25
typename Aut::element_type::transitions_output_t transitions_output_t
std::unordered_set< state_t > states_t
States set that we want to filter.
Definition: filter.hh:33
transition_t_of< automaton_t > transition_t
Definition: filter.hh:26
auto src_of(Args &&...args) const -> decltype(aut_-> src_of(std::forward< Args >(args)...))
std::vector< transition_t > tr_cont_t
Definition: filter.hh:35
static constexpr auto pre(Args &&...args) -> decltype(automaton_t::element_type::pre(std::forward< Args >(args)...))
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
std::ostream & print_state_name(state_t s, std::ostream &o, const std::string &fmt="text", bool delimit=false) const
Definition: filter.hh:74
container_filter_range< const tr_cont_t & > all_out(state_t s) const
Definition: filter.hh:158
std::shared_ptr< detail::filter_automaton_impl< Aut >> filter_automaton
Definition: filter.hh:253
container_filter_range< const tr_cont_t & > all_in(state_t s) const
Definition: filter.hh:196
container_filter_range< const tr_cont_t & > final_transitions() const
Indexes of transitions from visible final states.
Definition: filter.hh:236
bool has(const std::map< Key, Value, Compare, Alloc > &s, const Key &e)
Definition: map.hh:35
static constexpr auto post(Args &&...args) -> decltype(automaton_t::element_type::post(std::forward< Args >(args)...))
automaton_t aut_
The wrapped automaton, possibly const.
filter_automaton< Aut > filter(const Aut &aut, const std::unordered_set< state_t_of< Aut >> &ss)
Get an automaton who is a part state set ss of aut.
Definition: filter.hh:259
filter_automaton_impl(const automaton_t &input, const states_t &ss)
Definition: filter.hh:44