Vcsn  2.3
Be Rational
automaton.hh
Go to the documentation of this file.
1 #pragma once
2 
4 #include <vcsn/ctx/traits.hh> // state_t_of
5 
6 namespace vcsn
7 {
8  namespace detail
9  {
10 
11  /*---------.
12  | Sizes. |
13  `---------*/
14 
18  template <Automaton Aut>
19  size_t states_size(const Aut& aut)
20  {
21  // Cannot be empty.
22  return aut->all_states().back() + 1;
23  }
24 
28  template <Automaton Aut>
29  size_t transitions_size(const Aut& aut)
30  {
31  return (aut->all_transitions().empty()
32  ? 0
33  : aut->all_transitions().back() + 1);
34  }
35 
36 
37  /*------------------------.
38  | Outgoing transitions. |
39  `------------------------*/
40 
44  template <Automaton Aut>
45  auto all_out(const Aut& aut, state_t_of<Aut> s)
46  {
47  return aut->all_out(s);
48  }
49 
54  template <Automaton Aut, typename Pred>
55  auto all_out(const Aut& aut, state_t_of<Aut> s, Pred pred)
56  {
57  return make_container_filter_range(aut->all_out(s), pred);
58  }
59 
63  template <Automaton Aut>
64  auto out(const Aut& aut, state_t_of<Aut> s)
65  {
66  return all_out(aut, s,
67  [&aut](transition_t_of<Aut> t)
68  {
69  return aut->dst_of(t) != aut->post();
70  });
71  }
72 
76  template <Automaton Aut>
77  auto out(const Aut& aut, state_t_of<Aut> s, label_t_of<Aut> l)
78  {
79  return all_out(aut, s,
80  [&aut,l](transition_t_of<Aut> t)
81  {
82  return aut->labelset()->equal(aut->label_of(t), l);
83  });
84  }
85 
86  /*------------------------.
87  | Incoming transitions. |
88  `------------------------*/
89 
93  template <Automaton Aut>
94  auto all_in(const Aut& aut, state_t_of<Aut> s)
95  {
96  return aut->all_in(s);
97  }
98 
103  template <Automaton Aut, typename Pred>
104  auto all_in(const Aut& aut, state_t_of<Aut> s, Pred pred)
105  {
106  return make_container_filter_range(aut->all_in(s), pred);
107  }
108 
112  template <Automaton Aut>
113  auto in(const Aut& aut, state_t_of<Aut> s)
114  {
115  return all_in(aut, s,
116  [&aut](transition_t_of<Aut> t)
117  {
118  return aut->src_of(t) != aut->pre();
119  });
120  }
121 
125  template <Automaton Aut>
126  auto in(const Aut& aut, state_t_of<Aut> s, label_t_of<Aut> l)
127  {
128  return all_in(aut, s,
129  [&aut,l](transition_t_of<Aut> t)
130  {
131  return aut->labelset()->equal(aut->label_of(t), l);
132  });
133  }
134 
135 
136  /*-----------------------.
137  | Special transitions. |
138  `-----------------------*/
139 
144  template <Automaton Aut>
145  auto initial_transitions(const Aut& aut)
146  -> decltype(aut->all_out(aut->pre())) // for G++ 5.2
147  {
148  return aut->all_out(aut->pre());
149  }
150 
155  template <Automaton Aut>
156  auto final_transitions(const Aut& aut)
157  -> decltype(aut->all_in(aut->post())) // for G++ 5.2
158  {
159  return aut->all_in(aut->post());
160  }
161 
162 
166  template <Automaton Aut>
167  auto outin(const Aut& aut, state_t_of<Aut> s, state_t_of<Aut> d)
168  {
169  return all_out(aut, s,
170  [aut, d](transition_t_of<Aut> t)
171  {
172  return aut->dst_of(t) == d;
173  });
174  }
175 
177  template <Automaton Aut>
178  void del_transition(const Aut& aut, state_t_of<Aut> s, state_t_of<Aut> d)
179  {
180  // Make a copy of the transition indexes, as the iterators are
181  // invalidated by del_transition(t).
182  for (auto t: make_vector(outin(aut, s, d)))
183  aut->del_transition(t);
184  }
185 
186  /*---------------.
187  | Transitions. |
188  `---------------*/
189 
191  template <Automaton Aut>
192  auto all_transitions(const Aut& aut)
193  {
194  return aut->all_transitions();
195  }
196 
199  template <Automaton Aut, typename Pred>
200  auto all_transitions(const Aut& aut, Pred pred)
201  {
202  return make_container_filter_range(aut->all_transitions(), pred);
203  }
204 
206  template <Automaton Aut>
207  bool is_special(const Aut& aut, transition_t_of<Aut> t)
208  {
209  return aut->src_of(t) != aut->pre() && aut->dst_of(t) != aut->post();
210  }
211 
214  template <Automaton Aut>
216  {
218  {
219  return is_special(aut, t);
220  }
221  const Aut& aut;
222  };
223 
225  template <Automaton Aut>
226  auto transitions(const Aut& aut)
227  -> decltype(all_transitions(aut, is_special_t<Aut>{aut})) // for G++ 5.2
228  {
229  return all_transitions(aut, is_special_t<Aut>{aut});
230  }
231  }
232 }
std::vector< typename Cont::value_type > make_vector(const Cont &cont)
The content of cont as a vector.
Definition: vector.hh:20
auto transitions(const Aut &aut) -> decltype(all_transitions(aut, is_special_t< Aut >
All the transition indexes between visible states.
Definition: automaton.hh:226
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:65
size_t states_size(const Aut &aut)
The largest state number, plus one.
Definition: automaton.hh:19
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
Definition: automaton.hh:113
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:178
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:64
auto all_transitions(const Aut &aut)
All the transition indexes between all states (including pre and post).
Definition: automaton.hh:192
size_t transitions_size(const Aut &aut)
The largest transition number, plus one.
Definition: automaton.hh:29
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:167
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:64
Definition: a-star.hh:8
bool is_special(const Aut &aut, transition_t_of< Aut > t)
Whether this transition is from pre or to post.
Definition: automaton.hh:207
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:62
auto all_in(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions entering state s.
Definition: automaton.hh:94
bool operator()(transition_t_of< Aut > t)
Definition: automaton.hh:217
container_filter_range< Cont, Pred > make_container_filter_range(const Cont &cont, Pred pred)
Definition: crange.hh:114
auto initial_transitions(const Aut &aut) -> decltype(aut->all_out(aut->pre()))
Indexes of transitions to (visible) initial states.
Definition: automaton.hh:145
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
Definition: automaton.hh:45
auto final_transitions(const Aut &aut) -> decltype(aut->all_in(aut->post()))
Indexes of transitions from (visible) final states.
Definition: automaton.hh:156
Needed for GCC 5 and 6 that refuse deduced return type for transitions() when using a lambda...
Definition: automaton.hh:215