1 #ifndef VCSN_ALGOS_TO_EXPRESSION_HH
2 # define VCSN_ALGOS_TO_EXPRESSION_HH
17 template <
typename Aut,
18 typename Lifted = detail::lifted_automaton_t<Aut>>
20 std::function<state_t_of<Lifted>(
const Lifted&)>;
27 template <
typename Aut>
32 bool best_has_loop =
false;
33 size_t best_degree = std::numeric_limits<size_t>::max();
34 for (
auto s: a->states())
38 bool has_loop =
false;
40 for (
auto t: a->all_out(s))
41 if (a->dst_of(t) != s)
45 size_t in = a->all_in(s).size();
46 size_t degree = in * out;
48 if (degree < best_degree
49 || (degree == best_degree && has_loop < best_has_loop))
53 best_has_loop = has_loop;
66 template <typename Aut, typename Kind = typename context_t_of<Aut>::kind_t>
70 template <
typename Aut>
87 require(aut_->has_state(s),
"not a valid state: ", s);
90 auto loop = ws_.zero();
91 assert(aut_->outin(s, s).size() <= 1);
94 for (
auto t:
to_vector(aut_->outin(s, s)))
96 loop = ws_.add(loop, aut_->weight_of(t));
97 aut_->del_transition(t);
99 loop = ws_.star(loop);
102 auto outs = aut_->all_out(s);
103 for (
auto in: aut_->all_in(s))
106 (aut_->src_of(in), aut_->dst_of(out),
108 ws_.mul(aut_->weight_of(in), loop, aut_->weight_of(out)));
115 while (aut_->num_states())
116 operator()(next_state(aut_));
130 template <
typename Aut>
157 require(aut_->has_state(s),
"not a valid state: ", s);
160 auto loop = rs_.zero();
161 for (
auto t:
to_vector(aut_->outin(s, s)))
164 rs_.lmul(aut_->weight_of(t), aut_->label_of(t)));
165 aut_->del_transition(t);
167 loop = rs_.star(loop);
170 auto outs = aut_->all_out(s);
171 for (
auto in: aut_->all_in(s))
174 (aut_->src_of(in), aut_->dst_of(out),
175 rs_.mul(rs_.lmul(aut_->weight_of(in), aut_->label_of(in)),
177 rs_.lmul(aut_->weight_of(out), aut_->label_of(out))));
184 while (aut_->num_states())
185 operator()(next_state(aut_));
199 template <
typename Aut>
209 template <
typename Aut>
213 if (s == res->null_state())
221 template <
typename Aut>
242 template <
typename Aut,
typename Int>
246 const auto& a = aut->as<Aut>();
247 auto s = a->null_state();
264 template <
typename Aut,
265 typename RatExpSet = ratexpset<context_t_of<Aut>>>
266 typename RatExpSet::value_t
273 eliminate_states(next_state);
274 return aut->get_initial_weight(aut->post());
278 template <
typename Aut,
279 typename RatExpSet = ratexpset<context_t_of<Aut>>>
280 typename RatExpSet::value_t
296 template <
typename Aut,
typename String>
300 const auto& a = aut->as<Aut>();
305 ratexpset_t rs(a->context(), ratexpset_t::identities_t::trivial);
306 if (algo ==
"auto" || algo ==
"naive")
309 raise(
"to-expression: invalid algorithm: ",
str_escape(algo),
310 ": expected \"auto\", or \"naive\"");
314 (
const automaton& aut,
const std::string& algo)
321 #endif // !VCSN_ALGOS_TO_EXPRESSION_HH
state_eliminator(automaton_t &aut)
ratexp make_ratexp(const RatExpSet &rs, const typename RatExpSet::value_t &ratexp)
RatExpSet::value_t to_expression(const Aut &a, const state_chooser_t< Aut > &next_state)
Aut eliminate_state(const Aut &aut, state_t_of< Aut > s)
A copy of automaton res without the state s.
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
automaton_t & aut_
The automaton we work on.
weightset_t_of< automaton_t > weightset_t
automaton eliminate_state(const automaton &aut, int state)
Bridge.
int debug_
Debug level. The higher, the more details are reported.
state_t_of< Aut > next_naive(const Aut &a)
RatExpSet::value_t to_expression_naive(const Aut &a)
Aut & eliminate_state_here(Aut &res, state_t_of< Aut > s)
Remove state s from automaton res.
int debug_
Debug level. The higher, the more details are reported.
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
typename std::remove_cv< Aut >::type automaton_t
std::ostream & str_escape(std::ostream &os, const std::string &str)
Output a string, escaping special characters.
std::shared_ptr< detail::ratexp_base > ratexp
std::function< state_t_of< Lifted >(const Lifted &)> state_chooser_t
A state (inner) from an automaton.
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
AutOut copy(const AutIn &input, Pred keep_state)
A copy of input keeping only its states that are accepted by keep_state.
ratexp to_expression(const automaton &aut, const std::string &algo)
Bridge.
std::function< state_t(const automaton_t &)> state_chooser_t
State selector type.
detail::lifted_automaton_t< Aut > lift(const Aut &a)
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
state_t_of< automaton_t > state_t
std::vector< typename Cont::value_type > to_vector(const Cont &cont)
Return the content of cont as a vector.
state_eliminator< Aut > make_state_eliminator(Aut &a)
state_eliminator(automaton_t &aut)
void operator()(state_t s)
Eliminate state s.
void operator()(state_t s)
Eliminate state s.
void operator()(const state_chooser_t &next_state)
Eliminate all the states, in the order specified by next_state.
Provide a variadic mul on top of a binary mul(), and one().
state_t_of< automaton_t > state_t
weightset_t_of< automaton_t > weightset_t
automaton_t & aut_
The automaton we work on.
void operator()(const state_chooser_t &next_state)
Eliminate all the states, in the order specified by next_state.
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
labelset_t_of< automaton_t > ratexpset_t
typename std::remove_cv< Aut >::type automaton_t
std::function< state_t(const automaton_t &)> state_chooser_t
State selector type.
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.