Vcsn  2.8
Be Rational
automaton.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
6 #include <vcsn/ctx/traits.hh> // state_t_of, transition_t_of
7 
8 namespace vcsn
9 {
10 
17  template <typename Aut>
18  using predecessors_t_of = std::vector<transition_t_of<Aut>>;
19 
27  template <typename Aut>
28  using path_t_of = std::vector<transition_t_of<Aut>>;
29 
30  namespace detail
31  {
32 
33  /*---------.
34  | Sizes. |
35  `---------*/
36 
40  template <Automaton Aut>
41  size_t states_size(const Aut& aut)
42  {
43  // Cannot be empty.
44  return aut->all_states().back() + 1;
45  }
46 
50  template <Automaton Aut>
51  size_t transitions_size(const Aut& aut)
52  {
53  return (aut->all_transitions().empty()
54  ? 0
55  : aut->all_transitions().back() + 1);
56  }
57 
58 
59  /*------------------------.
60  | Outgoing transitions. |
61  `------------------------*/
62 
66  template <Automaton Aut>
67  auto all_out(const Aut& aut, state_t_of<Aut> s)
68  {
69  return aut->all_out(s);
70  }
71 
76  template <Automaton Aut, typename Pred>
77  auto all_out(const Aut& aut, state_t_of<Aut> s, Pred pred)
78  {
79  return make_container_filter_range(aut->all_out(s), pred);
80  }
81 
85  template <Automaton Aut>
86  auto out(const Aut& aut, state_t_of<Aut> s)
87  {
88  return all_out(aut, s,
89  [&aut](transition_t_of<Aut> t)
90  {
91  return aut->dst_of(t) != aut->post();
92  });
93  }
94 
98  template <Automaton Aut>
99  auto out(const Aut& aut, state_t_of<Aut> s, label_t_of<Aut> l)
100  {
101  return all_out(aut, s,
102  [&aut,l](transition_t_of<Aut> t)
103  {
104  return aut->labelset()->equal(aut->label_of(t), l);
105  });
106  }
107 
108  /*------------------------.
109  | Incoming transitions. |
110  `------------------------*/
111 
115  template <Automaton Aut>
116  auto all_in(const Aut& aut, state_t_of<Aut> s)
117  {
118  return aut->all_in(s);
119  }
120 
125  template <Automaton Aut, typename Pred>
126  auto all_in(const Aut& aut, state_t_of<Aut> s, Pred pred)
127  {
128  return make_container_filter_range(aut->all_in(s), pred);
129  }
130 
134  template <Automaton Aut>
135  auto in(const Aut& aut, state_t_of<Aut> s)
136  {
137  return all_in(aut, s,
138  [&aut](transition_t_of<Aut> t)
139  {
140  return aut->src_of(t) != aut->pre();
141  });
142  }
143 
147  template <Automaton Aut>
148  auto in(const Aut& aut, state_t_of<Aut> s, label_t_of<Aut> l)
149  {
150  return all_in(aut, s,
151  [&aut,l](transition_t_of<Aut> t)
152  {
153  return aut->labelset()->equal(aut->label_of(t), l);
154  });
155  }
156 
157 
158  /*-----------------------.
159  | Special transitions. |
160  `-----------------------*/
161 
166  template <Automaton Aut>
167  auto initial_transitions(const Aut& aut)
168  -> decltype(aut->all_out(aut->pre())) // for G++ 5.2
169  {
170  return aut->all_out(aut->pre());
171  }
172 
177  template <Automaton Aut>
178  auto final_transitions(const Aut& aut)
179  -> decltype(aut->all_in(aut->post())) // for G++ 5.2
180  {
181  return aut->all_in(aut->post());
182  }
183 
184 
188  template <Automaton Aut>
189  auto outin(const Aut& aut, state_t_of<Aut> s, state_t_of<Aut> d)
190  {
191  return all_out(aut, s,
192  [aut, d](transition_t_of<Aut> t)
193  {
194  return aut->dst_of(t) == d;
195  });
196  }
197 
199  template <Automaton Aut>
200  void del_transition(const Aut& aut, state_t_of<Aut> s, state_t_of<Aut> d)
201  {
202  // Make a copy of the transition indexes, as the iterators are
203  // invalidated by del_transition(t).
204  for (auto t: make_vector(outin(aut, s, d)))
205  aut->del_transition(t);
206  }
207 
208  /*---------------.
209  | Transitions. |
210  `---------------*/
211 
213  template <Automaton Aut>
214  auto all_transitions(const Aut& aut)
215  {
216  return aut->all_transitions();
217  }
218 
221  template <Automaton Aut, typename Pred>
222  auto all_transitions(const Aut& aut, Pred pred)
223  {
224  return make_container_filter_range(aut->all_transitions(), pred);
225  }
226 
228  template <Automaton Aut>
229  bool is_special(const Aut& aut, transition_t_of<Aut> t)
230  {
231  return aut->src_of(t) != aut->pre() && aut->dst_of(t) != aut->post();
232  }
233 
236  template <Automaton Aut>
238  {
240  {
241  return is_special(aut, t);
242  }
243  const Aut& aut;
244  };
245 
247  template <Automaton Aut>
248  auto transitions(const Aut& aut)
249  -> decltype(all_transitions(aut, is_special_t<Aut>{aut})) // for G++ 5.2
250  {
252  }
253  }
254 }
auto all_transitions(const Aut &aut)
All the transition indexes between all states (including pre and post).
Definition: automaton.hh:214
auto initial_transitions(const Aut &aut) -> decltype(aut->all_out(aut->pre()))
Indexes of transitions to (visible) initial states.
Definition: automaton.hh:167
container_filter_range< Cont, Pred > make_container_filter_range(const Cont &cont, Pred pred)
Definition: crange.hh:114
typename detail::transition_t_of_impl< base_t< ValueSet > >::type transition_t_of
Definition: traits.hh:65
typename detail::state_t_of_impl< base_t< ValueSet > >::type state_t_of
Definition: traits.hh:64
size_t states_size(const Aut &aut)
The largest state number, plus one.
Definition: automaton.hh:41
bool operator()(transition_t_of< Aut > t)
Definition: automaton.hh:239
std::vector< typename Cont::value_type > make_vector(const Cont &cont)
The content of cont as a vector.
Definition: vector.hh:18
bool is_special(const Aut &aut, transition_t_of< Aut > t)
Whether this transition is from pre or to post.
Definition: automaton.hh:229
auto final_transitions(const Aut &aut) -> decltype(aut->all_in(aut->post()))
Indexes of transitions from (visible) final states.
Definition: automaton.hh:178
std::vector< transition_t_of< Aut > > path_t_of
A list of transitions representing a path.
Definition: automaton.hh:28
auto all_in(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions entering state s.
Definition: automaton.hh:116
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
Definition: automaton.hh:67
auto outin(const Aut &aut, state_t_of< Aut > s, state_t_of< Aut > d)
Indexes of visible transitions from state s to state d.
Definition: automaton.hh:189
Definition: a-star.hh:8
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
Definition: automaton.hh:135
Needed for GCC 5 and 6 that refuse deduced return type for transitions() when using a lambda...
Definition: automaton.hh:237
typename detail::label_t_of_impl< base_t< ValueSet > >::type label_t_of
Definition: traits.hh:62
std::vector< transition_t_of< Aut > > predecessors_t_of
A state-indexed vector of predecessor transitions from the path path.
Definition: automaton.hh:18
size_t transitions_size(const Aut &aut)
The largest transition number, plus one.
Definition: automaton.hh:51
auto transitions(const Aut &aut) -> decltype(all_transitions(aut, is_special_t< Aut >
All the transition indexes between visible states.
Definition: automaton.hh:248
void del_transition(const Aut &aut, state_t_of< Aut > s, state_t_of< Aut > d)
Remove all the transitions between s and d.
Definition: automaton.hh:200
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:86