Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
complement.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_COMPLEMENT_HH
2 # define VCSN_ALGOS_COMPLEMENT_HH
3 
4 # include <set>
5 
6 # include <vcsn/algos/copy.hh>
9 # include <vcsn/misc/raise.hh>
10 # include <vcsn/weightset/fwd.hh> // b
11 
12 namespace vcsn
13 {
14 
15  /*------------------------.
16  | complement(automaton). |
17  `------------------------*/
18 
19  template <typename Aut>
20  void
21  complement_here(Aut& aut)
22  {
23  static_assert(labelset_t_of<Aut>::is_free(),
24  "complement: requires free labelset");
25  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
26  "complement: requires Boolean weights");
27 
28  using automaton_t = Aut;
29 
31  "complement: requires a deterministic automaton");
32  require(is_complete(aut),
33  "complement: requires a complete automaton");
34 
35  using state_t = state_t_of<automaton_t>;
36 
37  // The final states of aut.
38  std::set<state_t> finals;
39  for (auto t: aut->final_transitions())
40  finals.insert(aut->src_of(t));
41 
42  // Complement.
43  for (auto s: aut->states())
44  if (!has(finals, s))
45  aut->set_final(s);
46  else
47  aut->unset_final(s);
48  }
49 
50  template <typename Aut>
51  auto
52  complement(const Aut& aut)
53  -> decltype(copy(aut))
54  {
55  auto res = copy(aut);
56  complement_here(res);
57  return res;
58  }
59 
60  namespace dyn
61  {
62  namespace detail
63  {
65  template <typename Aut>
66  automaton
67  complement(const automaton& aut)
68  {
69  const auto& a = aut->as<Aut>();
70  return make_automaton(::vcsn::complement(a));
71  }
72 
74  (const automaton& aut) -> automaton);
75  }
76  }
77 
78  /*---------------------.
79  | complement(ratexp). |
80  `---------------------*/
81 
82  namespace dyn
83  {
84  namespace detail
85  {
87  template <typename RatExpSet>
88  ratexp
90  {
91  const auto& e = exp->as<RatExpSet>();
92 
93  return make_ratexp(e.ratexpset(),
94  e.ratexpset().complement(e.ratexp()));
95  }
96 
98  (const ratexp& e) -> ratexp);
99  }
100  }
101 
102 }
103 
104 #endif // !VCSN_ALGOS_COMPLEMENT_HH
automaton complement(const automaton &aut)
Bridge.
Definition: complement.hh:67
ratexp make_ratexp(const RatExpSet &rs, const typename RatExpSet::value_t &ratexp)
Definition: ratexp.hh:85
bool is_deterministic(const Aut &aut, state_t_of< Aut > s)
Whether state s is deterministic in aut.
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:34
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:77
ratexp complement_ratexp(const ratexp &exp)
Bridge.
Definition: complement.hh:89
std::shared_ptr< detail::ratexp_base > ratexp
Definition: fwd.hh:64
bool is_complete(const Aut &aut)
Whether aut is complete.
Definition: is-complete.hh:15
AutOut copy(const AutIn &input, Pred keep_state)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:171
void complement_here(Aut &aut)
Definition: complement.hh:21
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
auto complement(const Aut &aut) -> decltype(copy(aut))
Definition: complement.hh:52
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
bool has(const std::map< Key, Value, Compare, Alloc > &s, const Key &e)
Definition: map.hh:35
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:39