Vcsn  2.2
Be Rational
prefix.hh
Go to the documentation of this file.
1 #pragma once
2 
4 #include <vcsn/dyn/automaton.hh> // dyn::make_automaton
5 #include <vcsn/dyn/fwd.hh>
7 
8 namespace vcsn
9 {
10  /*---------.
11  | suffix. |
12  `---------*/
13 
15  template <Automaton Aut>
16  Aut&
17  suffix_here(Aut& aut)
18  {
19  for (auto s : accessible_states(aut))
20  if (s != aut->pre()
21  && s != aut->post()
22  && !aut->is_initial(s))
23  aut->set_initial(s);
24  return aut;
25  }
26 
27  template <Automaton Aut>
28  auto
29  suffix(const Aut& aut)
30  ->decltype(::vcsn::copy(aut))
31  {
32  auto res = ::vcsn::copy(aut);
33  suffix_here(res);
34  return res;
35  }
36 
37  namespace dyn
38  {
39  namespace detail
40  {
42  template <Automaton Aut>
43  automaton
44  suffix(const automaton& aut)
45  {
46  const auto& a = aut->as<Aut>();
47  return make_automaton(::vcsn::suffix(a));
48  }
49  }
50  }
51 
52 
53  /*---------.
54  | prefix. |
55  `---------*/
56 
58  template <Automaton Aut>
59  Aut&
60  prefix_here(Aut& aut)
61  {
62  auto t = transpose(aut);
63  suffix_here(t);
64  return aut;
65  }
66 
67  template <Automaton Aut>
68  auto
69  prefix(const Aut& aut)
70  ->decltype(::vcsn::copy(aut))
71  {
72  auto res = ::vcsn::copy(aut);
73  prefix_here(res);
74  return res;
75  }
76 
77  namespace dyn
78  {
79  namespace detail
80  {
82  template <Automaton Aut>
83  automaton
84  prefix(const automaton& aut)
85  {
86  const auto& a = aut->as<Aut>();
87  return make_automaton(::vcsn::prefix(a));
88  }
89  }
90  }
91 
92  /*---------.
93  | factor. |
94  `---------*/
95 
97  template <Automaton Aut>
98  Aut&
99  factor_here(Aut& aut)
100  {
101  return suffix_here(prefix_here(aut));
102  }
103 
104  template <Automaton Aut>
105  auto
106  factor(const Aut& aut)
107  ->decltype(::vcsn::copy(aut))
108  {
109  auto res = ::vcsn::copy(aut);
110  factor_here(res);
111  return res;
112  }
113 
114  namespace dyn
115  {
116  namespace detail
117  {
119  template <Automaton Aut>
120  automaton
121  factor(const automaton& aut)
122  {
123  const auto& a = aut->as<Aut>();
124  return make_automaton(::vcsn::factor(a));
125  }
126  }
127  }
128 
129  /*----------.
130  | subword. |
131  `----------*/
132 
135  template <Automaton Aut>
136  Aut&
137  subword_here(Aut& aut)
138  {
139  const auto one = aut->labelset()->one();
140 
141  auto ts = std::vector<transition_t_of<Aut>>{};
142  for (auto s : aut->states())
143  {
144  ts.clear();
145  for (auto t : out(aut, s))
146  if (!aut->labelset()->is_one(aut->label_of(t)))
147  ts.emplace_back(t);
148  for (auto t : ts)
149  aut->add_transition(s, aut->dst_of(t), one, aut->weight_of(t));
150  }
151 
152  return aut;
153  }
154 
156  template <Automaton Aut>
157  auto
158  subword(const Aut& aut)
159  -> decltype(make_nullable_automaton(aut->context()))
160  {
161  auto res = make_nullable_automaton(aut->context());
162  copy_into(aut, res);
163  subword_here(res);
164  return res;
165  }
166 
167  namespace dyn
168  {
169  namespace detail
170  {
172  template <Automaton Aut>
173  automaton
174  subword(const automaton& aut)
175  {
176  const auto& a = aut->as<Aut>();
177  return make_automaton(::vcsn::subword(a));
178  }
179  }
180  }
181 } // namespace vcsn
automaton prefix(const automaton &aut)
Bridge.
Definition: prefix.hh:84
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:75
Aut & suffix_here(Aut &aut)
Make all accessible states initial.
Definition: prefix.hh:17
void copy_into(const AutIn &in, AutOut &out, KeepState keep_state, KeepTrans keep_trans)
Copy selected states and transitions of an automaton.
Definition: copy.hh:260
Definition: a-star.hh:8
auto factor(const Aut &aut) -> decltype(::vcsn::copy(aut))
Definition: prefix.hh:106
Aut & prefix_here(Aut &aut)
Make all coaccessible states final.
Definition: prefix.hh:60
auto prefix(const Aut &aut) -> decltype(::vcsn::copy(aut))
Definition: prefix.hh:69
Aut transpose(const transpose_automaton< Aut > &aut)
Definition: transpose.hh:227
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:56
automaton suffix(const automaton &aut)
Bridge.
Definition: prefix.hh:44
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
Aut & subword_here(Aut &aut)
Add spontaneous transitions for each non spontaneous transition, with same source, destination and weight.
Definition: prefix.hh:137
auto subword(const Aut &aut) -> decltype(make_nullable_automaton(aut->context()))
Apply subword_here() to a copy of aut.
Definition: prefix.hh:158
auto make_nullable_automaton(const Context &ctx)
states_t< Aut > accessible_states(const Aut &aut, bool strict=true)
The set of accessible states, including pre(), and possibly post().
Definition: accessible.hh:27
automaton factor(const automaton &aut)
Bridge.
Definition: prefix.hh:121
Aut & factor_here(Aut &aut)
Make each useful state both initial and final.
Definition: prefix.hh:99
auto suffix(const Aut &aut) -> decltype(::vcsn::copy(aut))
Definition: prefix.hh:29
automaton subword(const automaton &aut)
Bridge.
Definition: prefix.hh:174
auto copy(const AutIn &input, KeepState keep_state, KeepTrans keep_trans) -> decltype(keep_state(input->null_state()), keep_trans(input->null_transition()), make_fresh_automaton< AutIn, AutOut >(input))
A copy of input keeping only its states that are accepted by keep_state, and transitions accepted by ...
Definition: copy.hh:308