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