Awali
Another Weighted Automata library
complement.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_COMPLEMENT_HH
18 # define AWALI_ALGOS_COMPLEMENT_HH
19 
20 # include <set>
21 
22 #include <awali/sttc/algos/copy.hh>
25 #include <awali/sttc/misc/raise.hh>
26 #include <awali/sttc/weightset/fwd.hh> // b
27 
28 namespace awali { namespace sttc {
29 
30 
31  /*------------------------.
32  | complement(automaton). |
33  `------------------------*/
34 
43  template <typename Aut>
44  Aut
45  complement_here(Aut& aut)
46  {
47  static_assert(labelset_t_of<Aut>::is_free(),
48  "requires free labelset");
49  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
50  "requires Boolean weights");
51 
52  // using automaton_t = Aut;
53 
55  "complement: requires a deterministic automaton");
56  require(is_complete(aut),
57  "complement: requires a complete automaton");
58 
59  // Complement.
60  for (auto s: aut->states())
61  if (!aut->is_final(s))
62  aut->set_final(s);
63  else
64  aut->unset_final(s);
65  return aut;
66  }
67 
77  template <typename Aut>
78  auto
79  complement(const Aut& aut, bool keep_history=true)
80  -> decltype(copy(aut))
81  {
82  auto res = copy(aut, keep_history);
83  complement_here(res);
84  return res;
85  }
86 
87 }}//end of ns awali::stc
88 
89 #endif // !AWALI_ALGOS_COMPLEMENT_HH
The Boolean semring.
Definition: b.hh:38
static constexpr TOP< void > value
Definition: priority.hh:93
auto complement(const Aut &aut, bool keep_history=true) -> decltype(copy(aut))
Complementation of a deterministic complete automaton.
Definition: complement.hh:79
Aut complement_here(Aut &aut)
Complementation of a deterministic complete automaton.
Definition: complement.hh:45
bool is_deterministic(const Aut &aut)
tests whether the automaton is deterministic
Definition: determinize.hh:210
bool is_complete(const Aut &aut)
Whether aut is complete.
Definition: is_complete.hh:28
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
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22