Awali
Another Weighted Automata library
complete.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 #ifndef AWALI_ALGOS_COMPLETE_HH
18 # define AWALI_ALGOS_COMPLETE_HH
19 
20 # include <queue>
21 # include <unordered_map>
22 
23 #include <awali/sttc/algos/copy.hh>
25 
26 namespace awali { namespace sttc {
27 
36  template <typename Aut>
37  Aut&
38  complete_here(Aut& aut)
39  {
40  static_assert(labelset_t_of<Aut>::is_free(),
41  "requires free labelset");
42 
43  using automaton_t = Aut;
44  using letter_t = typename labelset_t_of<automaton_t>::letter_t;
45 
46  // A sink state, to allocate if needed.
47  state_t sink = aut->null_state();
48  const auto& ls = *aut->labelset();
49 
50  if (aut->num_initials() == 0) {
51  sink = aut->add_state();
52  aut->set_initial(sink);
53  }
54 
55  // The outgoing labels of a state.
56  std::unordered_set<letter_t> labels_met;
57  for (auto st : aut->states())
58  if (st != sink) {
59  for (auto tr : aut->out(st))
60  labels_met.insert(aut->label_of(tr));
61 
62  for (auto letter : ls.genset())
63  if (labels_met.find(letter)==labels_met.end()) {
64  if (sink == aut->null_state())
65  sink = aut->add_state();
66  aut->new_transition(st, sink, letter);
67  }
68 
69  labels_met.clear();
70  }
71 
72  if (sink != aut->null_state())
73  for (auto letter : ls.genset())
74  aut->new_transition(sink, sink, letter);
75 
76  return aut;
77  }
78 
88  template <typename Aut>
89  auto
90  complete(const Aut& aut, bool keep_history=true)
91  -> decltype(copy(aut, keep_history))
92  {
93  auto res = copy(aut, keep_history);
94  complete_here(res);
95  return res;
96  }
97 
98 }}//end of ns awali::stc
99 
100 #endif // !AWALI_ALGOS_COMPLETE_HH
auto complete(const Aut &aut, bool keep_history=true) -> decltype(copy(aut, keep_history))
Completion of a deterministic automaton.
Definition: complete.hh:90
Aut & complete_here(Aut &aut)
Completion of a deterministic automaton.
Definition: complete.hh:38
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
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
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21