4 #include <boost/range/irange.hpp>
17 template <
typename Aut>
44 ss_.emplace(input->pre());
45 ss_.emplace(input->post());
51 static symbol res(
"filter_automaton<"
58 o <<
"filter_automaton<";
59 aut_->print_set(o, fmt);
63 bool state_has_name(state_t s) const
66 return aut_->state_has_name(s);
69 bool has_state(state_t s) const
71 return has(ss_, s) && aut_->has_state(s);
74 std::ostream& print_state_name(state_t s, std::ostream& o,
76 bool delimit = false) const
79 return aut_->print_state_name(s, o, fmt, delimit);
82 size_t num_states() const
84 return states().size();
87 size_t num_all_states() const
89 return all_states().size();
92 // FIXME: clang workaround.
93 template <typename Pred>
96 bool operator()(state_t s) const
98 return pred(s) && has(aut_.ss_, s);
100 const filter_automaton_impl& aut_;
104 template <typename Pred>
105 auto all_states(Pred pred) const
106 -> decltype(this->aut_->all_states(has_state_p<Pred>{*this, pred}))
108 return aut_->all_states(has_state_p<Pred>{*this, pred});
111 // FIXME: clang workaround.
114 bool operator()(state_t) const { return true; };
117 auto all_states() const
118 -> decltype(this->all_states(all_states_p{}))
120 return all_states(all_states_p{});
123 // FIXME: clang workaround.
124 struct visible_state_p
126 bool operator()(state_t s) const
128 // When transposing post() < pre().
129 return std::max(pre(), post()) < s;
131 const filter_automaton_impl& aut_;
135 -> decltype(this->all_states(visible_state_p{*this}))
137 return all_states(visible_state_p{*this});
140 // FIXME: clang workaround.
141 template <typename Pred>
142 struct has_transition_p
144 bool operator()(transition_t t) const
147 && has(aut_.ss_, aut_.src_of(t))
148 && has(aut_.ss_, aut_.dst_of(t)));
150 const filter_automaton_impl& aut_;
154 template <typename Pred>
155 auto all_transitions(Pred pred) const
156 -> decltype(this->aut_->all_transitions(has_transition_p<Pred>{*this,
159 return aut_->all_transitions(has_transition_p<Pred>{*this, pred});
162 // FIXME: clang workaround.
163 struct all_transitions_p
165 bool operator()(transition_t) const { return true; };
170 auto all_transitions() const
171 -> decltype(this->all_transitions(all_transitions_p{}))
173 return all_transitions(all_transitions_p{});
177 auto transitions() const
178 -> decltype(this->all_transitions(has_transition_p<all_transitions_p>
179 {*this, all_transitions_p{}}))
182 = has_transition_p<all_transitions_p>{*this, all_transitions_p{}};
183 return all_transitions(pred);
186 // FIXME: clang workaround.
187 template <typename Pred>
190 bool operator()(transition_t t) const
192 return pred(t) && has(aut_.ss_, aut_.dst_of(t));
194 const filter_automaton_impl& aut_;
198 template <typename Pred>
199 auto all_out(state_t s, Pred pred) const
200 -> decltype(this->aut_->all_out(s, has_dst_p<Pred>{*this, pred}))
202 return aut_->all_out(s, has_dst_p<Pred>{*this, pred});
205 auto all_out(state_t s) const
206 -> decltype(this->all_out(s, all_transitions_p{}))
208 return all_out(s, all_transitions_p{});
211 // FIXME: clang workaround.
214 bool operator()(transition_t t) const
216 return aut_.dst_of(t) != aut_.post();
218 const filter_automaton_impl& aut_;
221 auto out(state_t s) const
222 -> decltype(this->all_out(s, not_to_post_p{*this}))
224 return all_out(s, not_to_post_p{*this});
227 // FIXME: clang workaround.
230 bool operator()(transition_t t) const
232 return aut_.labelset()->equal(aut_.label_of(t), label_);
234 const filter_automaton_impl& aut_;
238 auto out(state_t s, label_t l) const
239 -> decltype(this->all_out(s, label_equal_p{*this, l}))
241 return all_out(s, label_equal_p{*this, l});
244 // FIXME: clang workaround.
245 template <typename Pred>
248 bool operator()(transition_t t) const
250 return pred(t) && has(aut_.ss_, aut_.src_of(t));
252 const filter_automaton_impl& aut_;
256 template <typename Pred>
257 auto all_in(state_t s, Pred pred) const
258 -> decltype(this->aut_->all_in(s, has_src_p<Pred>{*this, pred}))
260 return aut_->all_in(s, has_src_p<Pred>{*this, pred});
263 auto all_in(state_t s) const
264 -> decltype(this->all_in(s, all_transitions_p{}))
266 return all_in(s, all_transitions_p{});
269 // FIXME: clang workaround.
270 struct not_from_pre_p
272 bool operator()(transition_t t) const
274 return aut_.src_of(t) != aut_.pre();
276 const filter_automaton_impl& aut_;
279 auto in(state_t s) const
280 -> decltype(this->all_in(s, not_from_pre_p{*this}))
282 return all_in(s, not_from_pre_p{*this});
285 auto in(state_t s, label_t l) const
286 -> decltype(this->all_in(s, label_equal_p{*this, l}))
288 return all_in(s, label_equal_p{*this, l});
291 fresh_automaton_t_of<automaton_t>
294 return ::vcsn::copy(aut_, ss_);
298 auto initial_transitions() const
299 -> decltype(this->out(pre()))
305 auto final_transitions() const
306 -> decltype(this->in(post()))
321 template <typename Aut>
322 using filter_automaton =
323 std::shared_ptr<detail::filter_automaton_impl<Aut>>;
326 template <typename Aut>
328 filter_automaton<Aut>
329 filter(const Aut& aut, const std::unordered_set<state_t_of<Aut>>& ss)
331 return make_shared_ptr<filter_automaton<Aut>>(aut, ss);
339 template <typename Aut, typename Unsigneds>
341 filter(const automaton& aut, const std::vector<unsigned>& states)
343 const auto& a = aut->as<Aut>();
344 auto ss = std::unordered_set<state_t_of<Aut>>{};
345 // FIXME: this is wrong, of course.
348 return make_automaton(::vcsn::filter(a, ss));
std::unordered_set< state_t > states_t
States set that we want to filter.
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
static constexpr auto pre(Args &&...args) -> decltype(element_type::pre(std::forward< Args >(args)...))
static constexpr auto post(Args &&...args) -> decltype(element_type::post(std::forward< Args >(args)...))
state_t_of< automaton_t > state_t
label_t_of< automaton_t > label_t
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Aggregate an automaton, and forward calls to it.
static symbol sname()
Static name.
filter_automaton_impl(const automaton_t &input, const states_t &ss)
Hide some states of an automaton.
states_t ss_
The states we keep.
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
auto src_of(Args &&...args) const -> decltype(aut_-> src_of(std::forward< Args >(args)...))
std::ostream & print_set(std::ostream &o, format fmt={}) const
auto dst_of(Args &&...args) const -> decltype(aut_-> dst_of(std::forward< Args >(args)...))
std::vector< transition_t > tr_cont_t
transition_t_of< automaton_t > transition_t
automaton_t aut_
The wrapped automaton, possibly const.