Vcsn  2.1
Be Rational
complement.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <set>
4 
5 #include <vcsn/algos/copy.hh>
8 #include <vcsn/misc/raise.hh>
9 #include <vcsn/weightset/fwd.hh> // b
10 
11 namespace vcsn
12 {
13 
14  /*------------------------.
15  | complement(automaton). |
16  `------------------------*/
17 
18  template <typename Aut>
19  void
20  complement_here(Aut& aut)
21  {
22  using automaton_t = Aut;
24  "complement: requires free labelset");
26  "complement: requires a deterministic automaton");
27  require(is_complete(aut),
28  "complement: requires a complete automaton");
29 
30  using state_t = state_t_of<automaton_t>;
31 
32  // The final states of aut.
33  std::set<state_t> finals;
34  for (auto t: aut->final_transitions())
35  finals.insert(aut->src_of(t));
36 
37  // Complement.
38  for (auto s: aut->states())
39  if (has(finals, s))
40  aut->unset_final(s);
41  else
42  aut->set_final(s);
43  }
44 
45  template <typename Aut>
46  auto
47  complement(const Aut& aut)
48  -> decltype(copy(aut))
49  {
50  auto res = copy(aut);
51  complement_here(res);
52  return res;
53  }
54 
55  namespace dyn
56  {
57  namespace detail
58  {
60  template <typename Aut>
61  automaton
62  complement(const automaton& aut)
63  {
64  const auto& a = aut->as<Aut>();
65  return make_automaton(::vcsn::complement(a));
66  }
67  }
68  }
69 
70  /*--------------------------.
71  | complement(expression). |
72  `--------------------------*/
73 
74  namespace dyn
75  {
76  namespace detail
77  {
79  template <typename ExpSet>
82  {
83  const auto& e = exp->as<ExpSet>();
84 
85  return make_expression(e.expressionset(),
86  e.expressionset().complement(e.expression()));
87  }
88  }
89  }
90 
91 }
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:48
automaton complement(const automaton &aut)
Bridge.
Definition: complement.hh:62
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:75
AutOut copy(const AutIn &input, KeepState keep_state, KeepTrans keep_trans)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:254
bool is_complete(const Aut &aut)
Whether aut is complete.
Definition: is-complete.hh:13
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:47
void complement_here(Aut &aut)
Definition: complement.hh:20
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:75
expression make_expression(const ExpSet &rs, const typename ExpSet::value_t &r)
Definition: expression.hh:83
bool is_deterministic(const Aut &aut, state_t_of< Aut > s)
Whether state s is deterministic in aut.
ATTRIBUTE_PURE bool has(const std::deque< T, Allocator > &s, const T &e)
Whether e is member of s.
Definition: deque.hh:13
auto complement(const Aut &aut) -> decltype(copy(aut))
Definition: complement.hh:47
std::shared_ptr< detail::expression_base > expression
Definition: expression.hh:78
expression complement_expression(const expression &exp)
Bridge (complement).
Definition: complement.hh:81