Vcsn  2.3a
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 
25  template <typename Aut>
26  using path_t_of = std::vector<transition_t_of<Aut>>;
27 
28  namespace detail
29  {
30 
31  /*---------.
32  | Sizes. |
33  `---------*/
34 
38  template <Automaton Aut>
39  size_t states_size(const Aut& aut)
40  {
41  // Cannot be empty.
42  return aut->all_states().back() + 1;
43  }
44 
48  template <Automaton Aut>
49  size_t transitions_size(const Aut& aut)
50  {
51  return (aut->all_transitions().empty()
52  ? 0
53  : aut->all_transitions().back() + 1);
54  }
55 
56 
57  /*------------------------.
58  | Outgoing transitions. |
59  `------------------------*/
60 
64  template <Automaton Aut>
65  auto all_out(const Aut& aut, state_t_of<Aut> s)
66  {
67  return aut->all_out(s);
68  }
69 
74  template <Automaton Aut, typename Pred>
75  auto all_out(const Aut& aut, state_t_of<Aut> s, Pred pred)
76  {
77  return make_container_filter_range(aut->all_out(s), pred);
78  }
79 
83  template <Automaton Aut>
84  auto out(const Aut& aut, state_t_of<Aut> s)
85  {
86  return all_out(aut, s,
87  [&aut](transition_t_of<Aut> t)
88  {
89  return aut->dst_of(t) != aut->post();
90  });
91  }
92 
96  template <Automaton Aut>
97  auto out(const Aut& aut, state_t_of<Aut> s, label_t_of<Aut> l)
98  {
99  return all_out(aut, s,
100  [&aut,l](transition_t_of<Aut> t)
101  {
102  return aut->labelset()->equal(aut->label_of(t), l);
103  });
104  }
105 
106  /*------------------------.
107  | Incoming transitions. |
108  `------------------------*/
109 
113  template <Automaton Aut>
114  auto all_in(const Aut& aut, state_t_of<Aut> s)
115  {
116  return aut->all_in(s);
117  }
118 
123  template <Automaton Aut, typename Pred>
124  auto all_in(const Aut& aut, state_t_of<Aut> s, Pred pred)
125  {
126  return make_container_filter_range(aut->all_in(s), pred);
127  }
128 
132  template <Automaton Aut>
133  auto in(const Aut& aut, state_t_of<Aut> s)
134  {
135  return all_in(aut, s,
136  [&aut](transition_t_of<Aut> t)
137  {
138  return aut->src_of(t) != aut->pre();
139  });
140  }
141 
145  template <Automaton Aut>
146  auto in(const Aut& aut, state_t_of<Aut> s, label_t_of<Aut> l)
147  {
148  return all_in(aut, s,
149  [&aut,l](transition_t_of<Aut> t)
150  {
151  return aut->labelset()->equal(aut->label_of(t), l);
152  });
153  }
154 
155 
156  /*-----------------------.
157  | Special transitions. |
158  `-----------------------*/
159 
164  template <Automaton Aut>
165  auto initial_transitions(const Aut& aut)
166  -> decltype(aut->all_out(aut->pre())) // for G++ 5.2
167  {
168  return aut->all_out(aut->pre());
169  }
170 
175  template <Automaton Aut>
176  auto final_transitions(const Aut& aut)
177  -> decltype(aut->all_in(aut->post())) // for G++ 5.2
178  {
179  return aut->all_in(aut->post());
180  }
181 
182 
186  template <Automaton Aut>
187  auto outin(const Aut& aut, state_t_of<Aut> s, state_t_of<Aut> d)
188  {
189  return all_out(aut, s,
190  [aut, d](transition_t_of<Aut> t)
191  {
192  return aut->dst_of(t) == d;
193  });
194  }
195 
197  template <Automaton Aut>
198  void del_transition(const Aut& aut, state_t_of<Aut> s, state_t_of<Aut> d)
199  {
200  // Make a copy of the transition indexes, as the iterators are
201  // invalidated by del_transition(t).
202  for (auto t: make_vector(outin(aut, s, d)))
203  aut->del_transition(t);
204  }
205 
206  /*---------------.
207  | Transitions. |
208  `---------------*/
209 
211  template <Automaton Aut>
212  auto all_transitions(const Aut& aut)
213  {
214  return aut->all_transitions();
215  }
216 
219  template <Automaton Aut, typename Pred>
220  auto all_transitions(const Aut& aut, Pred pred)
221  {
222  return make_container_filter_range(aut->all_transitions(), pred);
223  }
224 
226  template <Automaton Aut>
227  bool is_special(const Aut& aut, transition_t_of<Aut> t)
228  {
229  return aut->src_of(t) != aut->pre() && aut->dst_of(t) != aut->post();
230  }
231 
234  template <Automaton Aut>
236  {
238  {
239  return is_special(aut, t);
240  }
241  const Aut& aut;
242  };
243 
245  template <Automaton Aut>
246  auto transitions(const Aut& aut)
247  -> decltype(all_transitions(aut, is_special_t<Aut>{aut})) // for G++ 5.2
248  {
249  return all_transitions(aut, is_special_t<Aut>{aut});
250  }
251  }
252 }
size_t states_size(const Aut &aut)
The largest state number, plus one.
Definition: automaton.hh:39
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:198
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:84
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
Definition: automaton.hh:65
Definition: a-star.hh:8
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:187
std::vector< transition_t_of< Aut >> path_t_of
A list of transitions representing a path.
Definition: automaton.hh:26
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:64
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:65
bool operator()(transition_t_of< Aut > t)
Definition: automaton.hh:237
std::vector< typename Cont::value_type > make_vector(const Cont &cont)
The content of cont as a vector.
Definition: vector.hh:20
size_t transitions_size(const Aut &aut)
The largest transition number, plus one.
Definition: automaton.hh:49
auto transitions(const Aut &aut) -> decltype(all_transitions(aut, is_special_t< Aut >
All the transition indexes between visible states.
Definition: automaton.hh:246
auto all_in(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions entering state s.
Definition: automaton.hh:114
auto all_transitions(const Aut &aut)
All the transition indexes between all states (including pre and post).
Definition: automaton.hh:212
std::vector< transition_t_of< Aut >> predecessors_t_of
A state-indexed vector of predecessor transitions from the path path.
Definition: automaton.hh:18
auto initial_transitions(const Aut &aut) -> decltype(aut->all_out(aut->pre()))
Indexes of transitions to (visible) initial states.
Definition: automaton.hh:165
bool is_special(const Aut &aut, transition_t_of< Aut > t)
Whether this transition is from pre or to post.
Definition: automaton.hh:227
Needed for GCC 5 and 6 that refuse deduced return type for transitions() when using a lambda...
Definition: automaton.hh:235
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
Definition: automaton.hh:133
auto final_transitions(const Aut &aut) -> decltype(aut->all_in(aut->post()))
Indexes of transitions from (visible) final states.
Definition: automaton.hh:176
container_filter_range< Cont, Pred > make_container_filter_range(const Cont &cont, Pred pred)
Definition: crange.hh:114
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:62