Awali
Another Weighted Automata library
factor.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_FACTOR_HH
18 #define AWALI_ALGOS_FACTOR_HH
19 
20 #include <awali/sttc/algos/copy.hh>
22 
23 namespace awali { namespace sttc {
24 
25 
26  /*-------.
27  | Prefix |
28  `-------*/
29 
31  template <typename Aut>
32  void
33  prefix_here(Aut& a)
34  {
35  for(auto s : coaccessible_states(a))
36  a->set_final(s);
37  }
38 
39  template <typename Aut>
40  typename Aut::element_type::automaton_nocv_t
41  prefix(const Aut& a, bool keep_history=true)
42  {
43  auto r = copy(a, keep_history);
44  prefix_here(r);
45  return r;
46  }
47 
48  /*-------.
49  | Suffix |
50  `-------*/
51 
53  template <typename Aut>
54  void
55  suffix_here(Aut& a)
56  {
57  for(auto s : accessible_states(a))
58  a->set_initial(s);
59  }
60 
61  template <typename Aut>
62  typename Aut::element_type::automaton_nocv_t
63  suffix(const Aut& a, bool keep_history=true)
64  {
65  auto r = copy(a, keep_history);
66  suffix_here(r);
67  return r;
68  }
69 
70  /*-------.
71  | Factor |
72  `-------*/
73 
75  template <typename Aut>
76  void
77  factor_here(Aut& a)
78  {
79  for(auto s : useful_states(a)) {
80  a->set_initial(s);
81  a->set_final(s);
82  }
83  }
84 
85  template <typename Aut>
86  typename Aut::element_type::automaton_nocv_t
87  factor(const Aut& a, bool keep_history=true)
88  {
89  auto r = copy(a, keep_history);
90  factor_here(r);
91  return r;
92  }
93 
94 }}//end of ns awali::stc
95 
96 #endif // !AWALI_ALGOS_FACTOR_HH
The semiring of floating Numbers.
Definition: r.hh:35
std::set< state_t > useful_states(const Aut &aut, bool include_pre_post=false)
List of useful states.
Definition: accessible.hh:131
Aut::element_type::automaton_nocv_t factor(const Aut &a, bool keep_history=true)
Definition: factor.hh:87
std::set< state_t > coaccessible_states(const Aut &aut, bool include_pre_post=false)
List of coaccessible states.
Definition: accessible.hh:113
void factor_here(Aut &a)
Make each useful state both initial and final.
Definition: factor.hh:77
void suffix_here(Aut &a)
Make all accessible states initial.
Definition: factor.hh:55
void prefix_here(Aut &a)
Make all coaccessible states final.
Definition: factor.hh:33
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
std::set< state_t > accessible_states(const Aut &aut, bool include_pre_post=false)
List of accessible states.
Definition: accessible.hh:86
Aut::element_type::automaton_nocv_t prefix(const Aut &a, bool keep_history=true)
Definition: factor.hh:41
Aut::element_type::automaton_nocv_t suffix(const Aut &a, bool keep_history=true)
Definition: factor.hh:63
Main namespace of Awali.
Definition: ato.hh:22