Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
star.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_STAR_HH
2 # define VCSN_ALGOS_STAR_HH
3 
4 # include <vcsn/algos/copy.hh>
5 # include <vcsn/algos/standard.hh> // is_standard
6 # include <vcsn/ctx/traits.hh>
7 # include <vcsn/dyn/automaton.hh> // dyn::make_automaton
8 # include <vcsn/misc/raise.hh> // require
9 
10 namespace vcsn
11 {
12  /*------.
13  | star |
14  `------*/
15 
19  template <typename Aut>
20  Aut&
21  star_here(Aut& res)
22  {
23  require(is_standard(res), __func__, ": input must be standard");
24 
25  using automaton_t = Aut;
26  using context_t = context_t_of<automaton_t>;
27  using weightset_t = weightset_t_of<context_t>;
28  using weight_t = weight_t_of<context_t>;
29  using state_t = state_t_of<automaton_t>;
30 
31  weightset_t ws(*res->context().weightset());
32 
33  state_t initial = res->dst_of(res->initial_transitions().front());
34  // The "final weight of the initial state", starred.
35  weight_t w = ws.star(res->get_final_weight(initial));
36  // Branch all the final states (but initial) to the successors
37  // of initial.
38  for (auto ti: res->out(initial))
39  {
40  res->lmul_weight(ti, w);
41  for (auto tf: res->final_transitions())
42  if (res->src_of(tf) != initial)
43  // The weight of ti has already been multiplied, on the
44  // left, by w.
45  res->add_transition
46  (res->src_of(tf),
47  res->dst_of(ti),
48  res->label_of(ti),
49  ws.mul(res->weight_of(tf), res->weight_of(ti)));
50  }
51  for (auto tf: res->final_transitions())
52  res->rmul_weight(tf, w);
53  res->set_final(initial, w);
54  return res;
55  }
56 
57 
59  template <typename Aut>
60  typename Aut::element_type::automaton_nocv_t
61  star(const Aut& aut)
62  {
63  auto res = copy(aut);
64  star_here(res);
65  return res;
66  }
67 
68  namespace dyn
69  {
70  namespace detail
71  {
73  template <typename Aut>
74  automaton
75  star(const automaton& a)
76  {
77  const auto& aut = a->as<Aut>();
78  return make_automaton(::vcsn::star(aut));
79  }
80 
82  (const automaton& aut) -> automaton);
83  }
84  }
85 }
86 
87 #endif // !VCSN_ALGOS_STAR_HH
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
bool is_standard(const Aut &a)
Whether a is standard.
Definition: standard.hh:27
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:77
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:32
automaton star(const automaton &a)
Bridge.
Definition: star.hh:75
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
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:37
Aut & star_here(Aut &res)
In place star of a standard automaton.
Definition: star.hh:21
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
Aut::element_type::automaton_nocv_t star(const Aut &aut)
Star of a standard automaton.
Definition: star.hh:61
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:39