Awali
Another Weighted Automata library
sum.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_SUM_HH
18 # define AWALI_ALGOS_SUM_HH
19 
20 # include <map>
21 
22 #include <awali/sttc/algos/product.hh> // join_automata
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  namespace internal {
30 
31  /*,---------------------------.
32  | sum of standard automata. |
33  `---------------------------'*/
34 
39  template <typename A, typename B>
40  A&
41  sum_of_standard_here(A& res, const B& b)
42  {
43  require(is_standard(res), __func__, ": first automaton should be standard");
44  require(is_standard(b), __func__, ": second automaton should be standard");
45 
46  //Copy b into res (except the initial state)
47  // State in b -> state in res.
48  std::map<state_t, state_t> m;
49  state_t initial = res->dst_of(*(res->initial_transitions().begin()));
50  for (auto s: b->states())
51  m.emplace(s, b->is_initial(s) ? initial : res->add_state());
52  m.emplace(b->pre(), res->pre());
53  m.emplace(b->post(), res->post());
54 
55  // Add b.
56  for (auto t: b->all_transitions())
57  // Do not add initial transitions, the unique initial state is
58  // already declared as such, and its weight must remain 1.
59  if (b->src_of(t) != b->pre())
60  {
61  if (b->dst_of(t) == b->post())
62  res->add_transition(m[b->src_of(t)], m[b->dst_of(t)],
63  b->label_of(t), b->weight_of(t));
64  else
65  res->new_transition(m[b->src_of(t)], m[b->dst_of(t)],
66  b->label_of(t), b->weight_of(t));
67  }
68  return res;
69  }
70 
71  }
72 
73  /*,----------------.
74  | (disjoint) sum |
75  `----------------'*/
76 
80 
100  template <typename Res, typename Aut>
101  inline
102  Res&
103  sum_here(Res& res, const Aut& aut, bool sum_standard=false)
104  {
105  if(sum_standard)
107  else
108  copy_into(aut, res, false);
109  return res;
110  }
111 
134  template <typename Aut1, typename Aut2>
135  inline
136  auto
137  sum(const Aut1& aut1, const Aut2& aut2, bool sum_standard=false)
138  -> decltype(join_automata(aut1, aut2))
139  {
140  auto res = join_automata(aut1, aut2);
141  copy_into(aut1, res, false);
142  sum_here(res, aut2, sum_standard);
143  return res;
144  }
145 
146  /*,------------------------------.
147  | sum(polynomial, polynomial). |
148  `------------------------------'*/
149 
151  template <typename ValueSet>
152  inline
153  typename ValueSet::value_t
154  sum(const ValueSet& vs,
155  const typename ValueSet::value_t& lhs,
156  const typename ValueSet::value_t& rhs)
157  {
158  return vs.add(lhs, rhs);
159  }
160 
161  }
162 }//end of ns awali::stc
163 
164 #endif // !AWALI_ALGOS_SUM_HH
The Boolean semring.
Definition: b.hh:38
A & sum_of_standard_here(A &res, const B &b)
Merge transitions of b into those of res.
Definition: sum.hh:41
auto sum(const Aut1 &aut1, const Aut2 &aut2, bool sum_standard=false) -> decltype(join_automata(aut1, aut2))
sums two automata
Definition: sum.hh:137
bool is_standard(const Aut &a)
Definition: standard.hh:40
void copy_into(const AutIn &in, AutOut &out, Pred keep_state, bool keep_history=true, bool transpose=false)
Copy an automaton.
Definition: copy.hh:144
auto join_automata(Auts &&... auts) -> decltype(make_mutable_automaton(join(auts->context()...)))
Join between automata.
Definition: product.hh:39
Res & sum_here(Res &res, const Aut &aut, bool sum_standard=false)
Merge transitions of b into those of res.
Definition: sum.hh:103
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21