Awali
Another Weighted Automata library
transpose.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_TRANSPOSE_HH
18 # define AWALI_ALGOS_TRANSPOSE_HH
19 
25 
26 namespace awali { namespace sttc {
27 
28 
29  /*-----------------------.
30  | transpose(automaton). |
31  `-----------------------*/
32  namespace internal
33  {
34 
35  template <typename Aut>
36  struct trans_tuple {
37  using automaton_t = Aut;
40 
45 
46  trans_tuple(const Aut& aut, transition_t& t) :
47  src(aut->src_of(t)), dst(aut->dst_of(t)), label(aut->label_of(t)), weight(aut->weight_of(t)) {}
48 
49  };
50  }
51 
52  template <typename Aut>
53  void transpose_here(Aut& aut) {
54  //using automaton_t = Aut;
55 
56  std::vector<transition_t> oldtr;
57  std::vector<internal::trans_tuple<Aut>> trs;
58 
59 
60  for(auto t : aut->all_transitions())
61  {
62  trs.emplace_back(internal::trans_tuple<Aut>(aut, t));
63  oldtr.emplace_back(t);
64  }
65 
66  for(auto t :oldtr)
67  aut->del_transition(t);
68  for(auto t : trs){
69  if(t.src==aut->pre())
70  t.src=aut->post();
71  if(t.dst==aut->post())
72  t.dst=aut->pre();
73  aut->add_transition(t.dst, t.src, aut->labelset()->transpose(t.label), aut->weightset()->transpose(t.weight));
74  }
75  }
76 
77  template <typename Aut,
78  typename AutOut = typename Aut::element_type::automaton_nocv_t>
79  AutOut transpose(Aut& aut, bool keep_history=true) {
80  AutOut out = copy(aut, keep_history);
81  transpose_here(out);
82  if(aut->get_name().empty()) {
83  out->set_name("transpose");
84  out->set_desc("Automaton obtained by a transposition");
85  }
86  else {
87  out->set_name("transpose-"+aut->get_name());
88  out->set_desc("Transposition of "+aut->get_name());
89  }
90  return out;
91  }
92 
93 }}//end of ns awali::stc
94 
95 #endif // !AWALI_ALGOS_TRANSPOSE_HH
typename internal::label_t_of_impl< internal::base_t< ValueSet > >::type label_t_of
Helper to retrieve the type of the labels of a value set.
Definition: traits.hh:71
typename internal::weight_t_of_impl< internal::base_t< ValueSet > >::type weight_t_of
Helper to retrieve the type of the weights of a value set.
Definition: traits.hh:81
AutOut transpose(Aut &aut, bool keep_history=true)
Definition: transpose.hh:79
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 transpose_here(Aut &aut)
Definition: transpose.hh:53
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
unsigned transition_t
Definition: types.hh:22
Definition: transpose.hh:36
label_t label
Definition: transpose.hh:43
Aut automaton_t
Definition: transpose.hh:37
state_t dst
Definition: transpose.hh:42
weight_t weight
Definition: transpose.hh:44
weight_t_of< automaton_t > weight_t
Definition: transpose.hh:39
label_t_of< automaton_t > label_t
Definition: transpose.hh:38
trans_tuple(const Aut &aut, transition_t &t)
Definition: transpose.hh:46
state_t src
Definition: transpose.hh:41