Awali
Another Weighted Automata library
star.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_STAR_HH
18 # define AWALI_ALGOS_STAR_HH
19 
20 #include <awali/common/priority.hh>
22 #include <awali/sttc/algos/copy.hh>
23 #include <awali/sttc/algos/standard.hh> // is_standard
24 #include <awali/sttc/ctx/traits.hh>
25 #include <awali/sttc/misc/raise.hh> // require
26 
27 namespace awali {
28  namespace sttc {
29 
30  /*,------.
31  | star |
32  `------'*/
33 
34  namespace internal {
35 
37 
38  template<typename Aut, typename P>
39  Aut&
41  {
42  const auto& ws=*res->context().weightset();
43  // To compute the star of a non standard automaton,
44  // a state has to be added (at least to deal with the empty word).
45  // Hence, the simplest way is to standardize.
46  standard_here(res);
47 
48  state_t initial = res->dst_of(*(res->initial_transitions().begin()));
49  // The "final weight of the initial state", starred.
50  auto w = ws.star(res->get_final_weight(initial));
51  // Branch all the final states (but initial) to the successors
52  // of initial.
53  for (auto ti: res->out(initial))
54  {
55  res->lmul_weight(ti, w);
56  for (auto tf: res->final_transitions())
57  if (res->src_of(tf) != initial)
58  // The weight of ti has already been multiplied, on the
59  // left, by w.
60  res->add_transition
61  (res->src_of(tf),
62  res->dst_of(ti),
63  res->label_of(ti),
64  ws.mul(res->weight_of(tf), res->weight_of(ti)));
65  }
66  for (auto tf: res->final_transitions())
67  res->rmul_weight(tf, w);
68  res->set_final(initial, w);
69  return res;
70  }
71 
72  template<typename Aut, typename P>
73  auto
74  star_here(Aut& res, priority::TWO<P>) -> typename std::enable_if<labelset_t_of<Aut>::has_one(),Aut&>::type
75  {
76  const auto& ws=*res->weightset();
77  auto one=res->labelset()->one();
78  for (auto ft: res->final_transitions())
79  for (auto it: res->initial_transitions())
80  res->add_transition(res->src_of(ft),
81  res->dst_of(it),
82  one,
83  ws.mul(res->weight_of(ft), res->weight_of(it)));
84  state_t init=res->add_state();
85  res->set_initial(init);
86  res->set_final(init);
87  return res;
88  }
89  }
90 
91  template<typename Aut>
92  Aut&
93  star_here(Aut& res) {
95  }
96 
98  template <typename Aut>
99  typename Aut::element_type::automaton_nocv_t
100  star(const Aut& aut)
101  {
102  auto res = copy(aut);
103  star_here(res);
104  return res;
105  }
106  }
107 }//end of ns awali::sttc
108 
109 #endif // !AWALI_ALGOS_STAR_HH
static constexpr TOP< void > value
Definition: priority.hh:93
Definition: priority.hh:52
Aut & star_here(Aut &res, priority::ONE< P >)
In place star of an automaton.
Definition: star.hh:40
Aut::element_type::automaton_nocv_t star(const Aut &aut)
Star of an automaton.
Definition: star.hh:100
AutOut copy(const AutIn &input, Pred keep_state, bool keep_history=true, bool transpose=false)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:189
void standard_here(Aut &aut)
Turn aut into a standard automaton.
Definition: standard.hh:58
Aut & star_here(Aut &res)
Definition: star.hh:93
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21