Vcsn  2.2
Be Rational
automaton.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/ctx/traits.hh> // state_t_of
4 
5 namespace vcsn
6 {
7 
8 #if defined __cpp_concepts
9  template <typename Aut>
10  concept bool Automaton()
11  {
12  return requires (Aut a)
13  {
14  typename state_t_of<Aut>;
15  { a->null_state() } -> state_t_of<Aut>;
16  { a->pre() } -> state_t_of<Aut>;
17  { a->post() } -> state_t_of<Aut>;
18 
19  typename transition_t_of<Aut>;
20  { a->null_transition() } -> transition_t_of<Aut>;
21  };
22  }
23 #else
24 # define Automaton typename
25 #endif
26 
27  namespace detail
28  {
29  /*------------------------.
30  | Outgoing transitions. |
31  `------------------------*/
32 
36  template <Automaton Aut>
37  auto all_out(const Aut& aut, state_t_of<Aut> s)
38  {
39  return aut->all_out(s);
40  }
41 
46  template <Automaton Aut, typename Pred>
47  auto all_out(const Aut& aut, state_t_of<Aut> s, Pred pred)
48  {
49  return make_container_filter_range(aut->all_out(s), pred);
50  }
51 
55  template <Automaton Aut>
56  auto out(const Aut& aut, state_t_of<Aut> s)
57  {
58  return all_out(aut, s,
59  [&aut](transition_t_of<Aut> t)
60  {
61  return aut->dst_of(t) != aut->post();
62  });
63  }
64 
68  template <Automaton Aut>
69  auto out(const Aut& aut, state_t_of<Aut> s, label_t_of<Aut> l)
70  {
71  return all_out(aut, s,
72  [&aut,l](transition_t_of<Aut> t)
73  {
74  return aut->labelset()->equal(aut->label_of(t), l);
75  });
76  }
77 
78  /*------------------------.
79  | Incoming transitions. |
80  `------------------------*/
81 
85  template <Automaton Aut>
86  auto all_in(const Aut& aut, state_t_of<Aut> s)
87  {
88  return aut->all_in(s);
89  }
90 
95  template <Automaton Aut, typename Pred>
96  auto all_in(const Aut& aut, state_t_of<Aut> s, Pred pred)
97  {
98  return make_container_filter_range(aut->all_in(s), pred);
99  }
100 
104  template <Automaton Aut>
105  auto in(const Aut& aut, state_t_of<Aut> s)
106  {
107  return all_in(aut, s,
108  [&aut](transition_t_of<Aut> t)
109  {
110  return aut->src_of(t) != aut->pre();
111  });
112  }
113 
117  template <Automaton Aut>
118  auto in(const Aut& aut, state_t_of<Aut> s, label_t_of<Aut> l)
119  {
120  return all_in(aut, s,
121  [&aut,l](transition_t_of<Aut> t)
122  {
123  return aut->labelset()->equal(aut->label_of(t), l);
124  });
125  }
126 
127 
128  /*-----------------------.
129  | Special transitions. |
130  `-----------------------*/
131 
136  template <Automaton Aut>
137  auto initial_transitions(const Aut& aut)
138  -> decltype(aut->all_out(aut->pre())) // for G++ 5.2
139  {
140  return aut->all_out(aut->pre());
141  }
142 
147  template <Automaton Aut>
148  auto final_transitions(const Aut& aut)
149  -> decltype(aut->all_in(aut->post())) // for G++ 5.2
150  {
151  return aut->all_in(aut->post());
152  }
153 
154 
158  template <Automaton Aut>
159  auto outin(const Aut& aut, state_t_of<Aut> s, state_t_of<Aut> d)
160  {
161  return all_out(aut, s,
162  [aut, d](transition_t_of<Aut> t)
163  {
164  return aut->dst_of(t) == d;
165  });
166  }
167 
169  template <Automaton Aut>
170  void del_transition(const Aut& aut, state_t_of<Aut> s, state_t_of<Aut> d)
171  {
172  // Make a copy of the transition indexes, as the iterators are
173  // invalidated by del_transition(t).
174  for (auto t: make_vector(outin(aut, s, d)))
175  aut->del_transition(t);
176  }
177 
178  /*---------------.
179  | Transitions. |
180  `---------------*/
181 
183  template <Automaton Aut>
184  auto all_transitions(const Aut& aut)
185  {
186  return aut->all_transitions();
187  }
188 
191  template <Automaton Aut, typename Pred>
192  auto all_transitions(const Aut& aut, Pred pred)
193  {
194  return make_container_filter_range(aut->all_transitions(), pred);
195  }
196 
198  template <Automaton Aut>
199  bool is_special(const Aut& aut, transition_t_of<Aut> t)
200  {
201  return aut->src_of(t) != aut->pre() && aut->dst_of(t) != aut->post();
202  }
203 
206  template <Automaton Aut>
208  {
210  {
211  return is_special(aut, t);
212  }
213  const Aut& aut;
214  };
215 
217  template <Automaton Aut>
218  auto transitions(const Aut& aut)
219  -> decltype(all_transitions(aut, is_special_t<Aut>{aut})) // for G++ 5.2
220  {
221  return all_transitions(aut, is_special_t<Aut>{aut});
222  }
223  }
224 }
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
Definition: automaton.hh:37
container_filter_range< Cont, Pred > make_container_filter_range(const Cont &cont, Pred pred)
Definition: crange.hh:112
bool operator()(transition_t_of< Aut > t)
Definition: automaton.hh:209
Definition: a-star.hh:8
Needed for GCC 5 and 6 that refuse deduced return type for transitions() when using a lambda...
Definition: automaton.hh:207
auto all_in(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions entering state s.
Definition: automaton.hh:86
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:170
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:56
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:56
auto initial_transitions(const Aut &aut) -> decltype(aut->all_out(aut->pre()))
Indexes of transitions to (visible) initial states.
Definition: automaton.hh:137
std::vector< typename Cont::value_type > make_vector(const Cont &cont)
The content of cont as a vector.
Definition: vector.hh:20
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:57
auto transitions(const Aut &aut) -> decltype(all_transitions(aut, is_special_t< Aut >
All the transition indexes between visible states.
Definition: automaton.hh:218
bool is_special(const Aut &aut, transition_t_of< Aut > t)
Whether this transition is from pre or to post.
Definition: automaton.hh:199
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:54
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:159
auto final_transitions(const Aut &aut) -> decltype(aut->all_in(aut->post()))
Indexes of transitions from (visible) final states.
Definition: automaton.hh:148
auto all_transitions(const Aut &aut)
All the transition indexes between all states (including pre and post).
Definition: automaton.hh:184
#define Automaton
Definition: automaton.hh:24
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
Definition: automaton.hh:105