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