Vcsn  2.2
Be Rational
conjugate.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/algos/copy.hh>
4 
5 namespace vcsn
6 {
7  template <Automaton Aut>
8  Aut
9  conjugate(const Aut& aut)
10  {
11  require(aut->labelset()->has_one(),
12  __func__, ": labelset must be nullable");
13 
14  const auto& ls = *aut->labelset();
15  const auto& ws = *aut->weightset();
16 
17  auto res = make_shared_ptr<Aut>(aut->context());
18  auto copy_pref = make_copier(aut, res);
19  auto copy_suff = make_copier(aut, res);
20 
21  // Copy initial automaton.
22  copy_pref();
23  for (const auto s: aut->states())
24  {
25  copy_pref([] (state_t_of<Aut> s) { return true; },
26  // Remove all initial and final transitions.
27  [aut] (transition_t_of<Aut> t) {
28  return (aut->src_of(t) != aut->pre()
29  && aut->dst_of(t) != aut->post()); });
30  copy_suff([] (state_t_of<Aut> s) { return true; },
31  // Remove all initial and final transitions.
32  [aut] (transition_t_of<Aut> t) {
33  return (aut->src_of(t) != aut->pre())
34  && (aut->dst_of(t) != aut->post()); });
35 
36  res->set_initial(copy_pref.state_map().at(s));
37  res->set_final(copy_suff.state_map().at(s));
38 
39  // Add transitions from all final states of pref to all
40  // initial states of suff.
41  for (const auto ft: final_transitions(aut))
42  for (const auto it: initial_transitions(aut))
43  res->new_transition(copy_pref.state_map().at(aut->src_of(ft)),
44  copy_suff.state_map().at(aut->dst_of(it)),
45  ls.one(), ws.one());
46  }
47 
48  return res;
49  }
50 
51  namespace dyn
52  {
53  namespace detail
54  {
56  template <Automaton Aut>
57  automaton
58  conjugate(const automaton& a)
59  {
60  const auto& aut = a->as<Aut>();
61  return make_automaton(conjugate(aut));
62  }
63  }
64  }
65 }
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:75
Definition: a-star.hh:8
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:78
Aut conjugate(const Aut &aut)
Definition: conjugate.hh:9
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:56
auto initial_transitions(const Aut &aut) -> decltype(aut->all_out(aut->pre()))
Indexes of transitions to (visible) initial states.
Definition: automaton.hh:137
automaton conjugate(const automaton &a)
Bridge.
Definition: conjugate.hh:58
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:57
auto final_transitions(const Aut &aut) -> decltype(aut->all_in(aut->post()))
Indexes of transitions from (visible) final states.
Definition: automaton.hh:148
detail::copier< AutIn, AutOut > make_copier(const AutIn &in, AutOut &out, bool safe=true)
Build an automaton copier.
Definition: copy.hh:249