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