Vcsn  2.2
Be Rational
filter.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm> // std::max
4 #include <boost/range/irange.hpp>
5 #include <vcsn/algos/copy.hh>
7 #include <vcsn/misc/crange.hh>
8 #include <vcsn/dyn/fwd.hh>
10 #include <vcsn/misc/vector.hh>
11 
12 namespace vcsn
13 {
14  namespace detail
15  {
17  template <Automaton Aut>
19  : public automaton_decorator<Aut>
20  {
21  public:
22  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 
37  using super_t::pre;
38  using super_t::post;
39  using super_t::src_of;
40  using super_t::dst_of;
41 
42  filter_automaton_impl(const automaton_t& input, const states_t& ss)
43  : super_t(input), ss_(ss)
44  {
45  ss_.emplace(input->pre());
46  ss_.emplace(input->post());
47  }
48 
50  static symbol sname()
51  {
52  static auto res = symbol{"filter_automaton<"
54  return res;
55  }
56 
57  std::ostream& print_set(std::ostream& o, format fmt = {}) const
58  {
59  o << "filter_automaton<";
60  aut_->print_set(o, fmt);
61  return o << '>';
62  }
63 
64  bool state_has_name(state_t s) const
65  {
66  assert(has(ss_, s));
67  return aut_->state_has_name(s);
68  }
69 
70  bool has_state(state_t s) const
71  {
72  return has(ss_, s) && aut_->has_state(s);
73  }
74 
75  std::ostream& print_state_name(state_t s, std::ostream& o,
76  format fmt = {},
77  bool delimit = false) const
78  {
79  assert(has(ss_, s));
80  return aut_->print_state_name(s, o, fmt, delimit);
81  }
82 
83  size_t num_states() const
84  {
85  return states().size();
86  }
87 
88  size_t num_all_states() const
89  {
90  return all_states().size();
91  }
92 
93  template <typename Pred>
94  auto all_states(Pred pred) const
95  {
96  return aut_->all_states([this, pred](state_t s)
97  {
98  return pred(s) && has(ss_, s);
99  });
100  }
101 
102  auto all_states() const
103  {
104  return all_states([](state_t) { return true; });
105  }
106 
107  auto states() const
108  {
109  return all_states([this](state_t s)
110  {
111  // When transposing post() < pre().
112  return std::max(pre(), post()) < s;
113  });
114  }
115 
118  auto all_transitions() const
119  {
120  return vcsn::detail::all_transitions
121  (aut_,
122  [this](transition_t t)
123  {
124  return (has(ss_, aut_->src_of(t))
125  && has(ss_, aut_->dst_of(t)));
126  });
127  }
128 
130  auto all_out(state_t s) const
131  {
132  return vcsn::detail::all_out(aut_, s,
133  [this](transition_t t)
134  {
135  return has(ss_, aut_->dst_of(t));
136  });
137  }
138 
140  auto all_in(state_t s) const
141  {
142  return vcsn::detail::all_in(aut_, s,
143  [this](transition_t t)
144  {
145  return has(ss_, aut_->src_of(t));
146  });
147  }
148 
149  fresh_automaton_t_of<automaton_t>
150  strip() const
151  {
152  return ::vcsn::copy(aut_, ss_);
153  }
154 
155  protected:
157  using super_t::aut_;
158 
159  private:
161  states_t ss_;
162  };
163  }
164 
165  template <Automaton Aut>
166  using filter_automaton =
167  std::shared_ptr<detail::filter_automaton_impl<Aut>>;
168 
170  template <Automaton Aut>
171  filter_automaton<Aut>
172  filter(const Aut& aut, const std::unordered_set<state_t_of<Aut>>& ss)
173  {
174  return make_shared_ptr<filter_automaton<Aut>>(aut, ss);
175  }
176 
177  namespace dyn
178  {
179  namespace detail
180  {
182  template <Automaton Aut, typename Unsigneds>
183  automaton
184  filter(const automaton& aut, const std::vector<unsigned>& states)
185  {
186  const auto& a = aut->as<Aut>();
187  auto ss = std::unordered_set<state_t_of<Aut>>{};
188  // FIXME: this is wrong, of course.
189  for (auto s: states)
190  ss.emplace(s + 2);
191  return make_automaton(::vcsn::filter(a, ss));
192  }
193  }
194  }
195 }
std::unordered_set< state_t > states_t
States set that we want to filter.
Definition: filter.hh:33
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: filter.hh:57
Hide some states of an automaton.
Definition: filter.hh:18
state_t_of< automaton_t > state_t
Definition: filter.hh:25
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)...))
Definition: a-star.hh:8
static constexpr auto pre(Args &&...args) -> decltype(element_type::pre(std::forward< Args >(args)...))
label_t_of< automaton_t > label_t
Definition: filter.hh:27
static constexpr auto post(Args &&...args) -> decltype(element_type::post(std::forward< Args >(args)...))
automaton_t aut_
The wrapped automaton, possibly const.
An input/output format for valuesets.
Definition: format.hh:11
states_t ss_
The states we keep.
Definition: filter.hh:161
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:56
static symbol sname()
Static name.
Definition: filter.hh:50
auto dst_of(Args &&...args) const -> decltype(aut_-> dst_of(std::forward< Args >(args)...))
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
Aggregate an automaton, and forward calls to it.
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:57
symbol sname()
Definition: name.hh:67
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:54
std::vector< transition_t > tr_cont_t
Definition: filter.hh:35
filter_automaton_impl(const automaton_t &input, const states_t &ss)
Definition: filter.hh:42