Awali
Another Weighted Automata library
accessible.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 /* @file accessible.hh
18  *
19  * This files contains static functions computing accessible states.
20  */
21 #ifndef AWALI_ALGOS_ACCESSIBLE_HH
22 #define AWALI_ALGOS_ACCESSIBLE_HH
23 
24 #include <deque>
25 #include <queue>
26 #include <map>
27 #include <unordered_set>
28 
29 #include <awali/sttc/algos/copy.hh>
33 #include <awali/sttc/misc/set.hh>
34 
35 namespace awali {
36  namespace sttc {
37 
38 
39  /*--------------------------------------------------.
40  | Sets of accessible, coaccessible, useful states. |
41  `--------------------------------------------------*/
42 
43  // The set of accessible states, including pre(), and possibly post().
44  //breadth first search
45  template <typename Set, typename Aut>
46  void
47  fill_with_accessible_states(Set& res, const Aut& aut, bool include_pre_post=false) {
48  // Reachable states.
49  const auto& a = *aut;
50  if(include_pre_post)
51  res.emplace(a.pre());
52 
53  // States work list.
54  using worklist_t = std::queue<state_t>;
55  worklist_t todo;
56  todo.emplace(a.pre());
57 
58  while (!todo.empty())
59  {
60  const state_t src = todo.front();
61  todo.pop();
62 
63  for (auto tr : a.all_out(src))
64  {
65  state_t dst = a.dst_of(tr);
66  // If we have not seen it already, explore its successors.
67  if(dst != a.post() || include_pre_post)
68  if (res.emplace(dst).second)
69  todo.emplace(dst);
70  }
71  }
72  }
73 
84  template <typename Aut>
85  std::set<state_t>
86  accessible_states(const Aut& aut, bool include_pre_post=false)
87  {
88  // Reachable states.
89  std::set<state_t> res;
90  fill_with_accessible_states(res, aut, include_pre_post);
91  return res;
92  }
93 
94  template <typename Set, typename Aut>
95  void
96  fill_with_coaccessible_states(Set& res, const Aut& aut, bool include_pre_post=false) {
98  }
99 
100  // The set of coaccessible states, including post(), and possibly pre().
111  template <typename Aut>
112  std::set<state_t>
113  coaccessible_states(const Aut& aut, bool include_pre_post=false)
114  {
115  return accessible_states(transpose_view(aut), false);
116  }
117 
118  // The set of coaccessible states, including post(), and possibly pre().
129  template <typename Aut>
130  std::set<state_t>
131  useful_states(const Aut& aut, bool include_pre_post=false)
132  {
133  auto accessible = accessible_states(aut, false);
134  auto coaccessible = coaccessible_states(aut, false);
136  }
137 
138 
139  /*----------------------------------------------------.
140  | Number of accessible, coaccessible, useful states. |
141  `----------------------------------------------------*/
142 
151  template <typename Aut>
152  size_t
153  num_accessible_states(const Aut& aut)
154  {
155  std::unordered_set<state_t> set;
157  return set.size();
158  }
159 
168  template <typename Aut>
169  size_t
170  num_coaccessible_states(const Aut& aut)
171  {
172  std::unordered_set<state_t> set;
174  return set.size();
175  }
176 
185  template <typename Aut>
186  size_t
187  num_useful_states(const Aut& aut)
188  {
189  auto set = useful_states(aut);
190  return set.size();
191  }
192 
193 
194  /*-----------------------------------------------.
195  | accessible, coaccessible, useful subautomata. |
196  `-----------------------------------------------*/
197 
210  template <typename Aut>
211  typename Aut::element_type::automaton_nocv_t
212  accessible(const Aut& aut, bool keep_history=true)
213  {
214  return copy(aut, accessible_states(aut), keep_history);
215  }
216 
224  template <typename Aut>
225  void
226  accessible_here(Aut& aut)
227  {
228  return sub_automaton(aut, accessible_states(aut));
229  }
230 
243  template <typename Aut>
244  typename Aut::element_type::automaton_nocv_t
245  coaccessible(const Aut& aut, bool keep_history=true)
246  {
247  return copy(aut, coaccessible_states(aut), keep_history);
248  }
249 
257  template <typename Aut>
258  void
260  {
261  return sub_automaton(aut, coaccessible_states(aut));
262  }
263 
276  template <typename Aut>
277  typename Aut::element_type::automaton_nocv_t
278  trim(const Aut& aut, bool keep_history=true)
279  {
280  return copy(aut, useful_states(aut), keep_history);
281  }
282 
290  template <typename Aut>
291  void
292  trim_here(Aut& aut)
293  {
294  return sub_automaton(aut, useful_states(aut));
295  }
296 
297  /*----------------------------------------------------------------.
298  | is_trim, is_accessible, is_coaccessible, is_empty, is_useless. |
299  `----------------------------------------------------------------*/
300 
307  template <typename Aut>
308  bool is_trim(const Aut& aut)
309  {
310  return num_useful_states(aut) == aut->num_states();
311  }
312 
320  template <typename Aut>
321  bool is_useless(const Aut& aut)
322  {
323  return num_useful_states(aut) == 0;
324  }
325 
333  template <typename Aut>
334  bool is_accessible(const Aut& aut)
335  {
336  return num_accessible_states(aut) == aut->num_states();
337  }
338 
346  template <typename Aut>
347  bool is_coaccessible(const Aut& aut)
348  {
349  return num_coaccessible_states(aut) == aut->num_states();
350  }
351 
352  template <typename Aut>
353  bool is_empty(const Aut& aut) ATTRIBUTE_PURE;
354 
364  template <typename Aut>
365  bool is_empty(const Aut& aut)
366  {
367  return aut->num_states() == 0;
368  }
369 
370  }
371 }//end of ns awali::stc
372 
373 #endif // !AWALI_ALGOS_ACCESSIBLE_HH
std::set< T, Compare, Alloc > intersection(const std::set< T, Compare, Alloc > &set1, const std::set< T, Compare, Alloc > &set2)
The intersection of two sets.
bool is_empty(const Aut &aut) ATTRIBUTE_PURE
Test whether the automaton has no state.
Definition: accessible.hh:365
size_t num_useful_states(const Aut &aut)
Number of useful states.
Definition: accessible.hh:187
bool is_accessible(const Aut &aut)
Test whether every state of the automaton is accessible.
Definition: accessible.hh:334
void coaccessible_here(Aut &aut)
In-place coaccessible subautomaton.
Definition: accessible.hh:259
void trim_here(Aut &aut)
In-place trim subautomaton.
Definition: accessible.hh:292
void fill_with_accessible_states(Set &res, const Aut &aut, bool include_pre_post=false)
Definition: accessible.hh:47
void accessible_here(Aut &aut)
In-place accessible subautomaton.
Definition: accessible.hh:226
void sub_automaton(Aut &aut, Pred keep_state)
Definition: sub_automaton.hh:32
size_t num_coaccessible_states(const Aut &aut)
Number of coaccessible states.
Definition: accessible.hh:170
bool is_useless(const Aut &aut)
Test whether the automaton has useful states.
Definition: accessible.hh:321
Aut::element_type::automaton_nocv_t trim(const Aut &aut, bool keep_history=true)
Trim subautomaton.
Definition: accessible.hh:278
std::set< state_t > useful_states(const Aut &aut, bool include_pre_post=false)
List of useful states.
Definition: accessible.hh:131
std::set< state_t > coaccessible_states(const Aut &aut, bool include_pre_post=false)
List of coaccessible states.
Definition: accessible.hh:113
Aut::element_type::automaton_nocv_t accessible(const Aut &aut, bool keep_history=true)
Accessible subautomaton.
Definition: accessible.hh:212
AutOut copy(const AutIn &input, Pred keep_state, bool keep_history=true, bool transpose=false)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:189
bool is_coaccessible(const Aut &aut)
Test whether every state of the automaton is coaccessible.
Definition: accessible.hh:347
bool is_trim(const Aut &aut)
Test whether the automaton is trim.
Definition: accessible.hh:308
Aut::element_type::automaton_nocv_t coaccessible(const Aut &aut, bool keep_history=true)
Coaccessible subautomaton.
Definition: accessible.hh:245
std::set< state_t > accessible_states(const Aut &aut, bool include_pre_post=false)
List of accessible states.
Definition: accessible.hh:86
std::shared_ptr< internal::transpose_view_impl< Aut > > transpose_view(std::shared_ptr< Aut > aut)
Definition: transpose_view.hh:265
size_t num_accessible_states(const Aut &aut)
Number of accessible states.
Definition: accessible.hh:153
void fill_with_coaccessible_states(Set &res, const Aut &aut, bool include_pre_post=false)
Definition: accessible.hh:96
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21