1 #ifndef VCSN_ALGOS_CONCATENATE_HH
2 # define VCSN_ALGOS_CONCATENATE_HH
4 # include <unordered_map>
29 template <
typename A,
typename B>
36 using automaton_t = A;
37 const auto& ls = *res->labelset();
38 const auto& bls = *b->labelset();
39 const auto& ws = *res->weightset();
40 const auto& bws = *b->weightset();
43 auto ftr_ = res->final_transitions();
45 using transs_t = std::vector<transition_t_of<automaton_t>>;
46 transs_t ftr{ begin(ftr_), end(ftr_) };
48 state_t_of<B> b_initial = b->dst_of(b->initial_transitions().front());
52 m.emplace(b->post(), res->post());
53 for (
auto s: b->states())
54 if (!b->is_initial(s))
55 m.emplace(s, res->new_state());
62 for (
auto t: b->all_transitions())
63 if (b->src_of(t) != b->pre() && b->src_of(t) != b_initial)
64 res->new_transition(m[b->src_of(t)], m[b->dst_of(t)],
65 ls.conv(bls, b->label_of(t)),
66 ws.conv(bws, b->weight_of(t)));
82 auto s1 = res->src_of(t1);
83 auto w1 = res->weight_of(t1);
84 res->del_transition(t1);
85 for (
auto t2: b->all_out(b_initial))
86 res->set_transition(s1,
88 ls.conv(bls, b->label_of(t2)),
90 ws.conv(bws, b->weight_of(t2))));
96 template <
typename A,
typename B>
115 template <
typename Lhs,
typename Rhs>
119 const auto& l = lhs->as<Lhs>();
120 const auto&
r = rhs->as<Rhs>();
133 template <
typename Aut>
135 chain(
const Aut& aut,
int min,
int max)
138 make_shared_ptr<Aut>(aut->context());
152 auto s = res->new_state();
159 for (
int n = 1; n < min; ++n)
165 Aut
sum = make_shared_ptr<Aut>(aut->context());
167 auto s = sum->new_state();
171 for (
int n = 1; n <= max - min; ++n)
186 template <
typename Aut,
typename Int1,
typename Int2>
190 const auto& aut = a->as<Aut>();
205 template <
typename ValueSet>
207 typename ValueSet::value_t
209 const typename ValueSet::value_t& lhs,
210 const typename ValueSet::value_t& rhs)
212 return vs.mul(lhs, rhs);
220 template <
typename RatExpSetLhs,
typename RatExpSetRhs>
224 const auto& l = lhs->as<RatExpSetLhs>();
225 const auto&
r = rhs->as<RatExpSetRhs>();
226 auto rs =
vcsn::join(l.ratexpset(), r.ratexpset());
227 auto lr = rs.conv(l.ratexpset(), l.ratexp());
228 auto rr = rs.conv(r.ratexpset(), r.ratexp());
242 template <
typename RatExpSet>
243 typename RatExpSet::value_t
244 chain(
const RatExpSet& rs,
const typename RatExpSet::value_t&
r,
247 typename RatExpSet::value_t res;
252 res = rs.mul(
chain(rs, r, min, min), res);
263 for (
int n = 1; n < min; ++n)
264 res = rs.mul(res, r);
268 typename RatExpSet::value_t
sum = rs.one();
269 for (
int n = 1; n <= max - min; ++n)
270 sum = rs.add(sum,
chain(rs, r, n, n));
271 res = rs.mul(res, sum);
282 template <
typename RatExpSet,
typename Int1,
typename Int2>
286 const auto&
r = re->as<RatExpSet>();
306 template <
typename PolynomialSetLhs,
typename PolynomialSetRhs>
310 const auto& l = lhs->as<PolynomialSetLhs>();
311 const auto&
r = rhs->as<PolynomialSetRhs>();
312 auto rs =
join(l.polynomialset(), r.polynomialset());
313 auto lr = rs.conv(l.polynomialset(), l.polynomial());
314 auto rr = rs.conv(r.polynomialset(), r.polynomial());
328 template <
typename ValueSet>
330 typename ValueSet::value_t
332 const typename ValueSet::value_t& lhs,
333 const typename ValueSet::value_t& rhs)
335 return vs.mul(lhs, rhs);
343 template <
typename WeightSetLhs,
typename WeightSetRhs>
347 const auto& l = lhs->as<WeightSetLhs>();
348 const auto&
r = rhs->as<WeightSetRhs>();
349 auto rs =
join(l.weightset(), r.weightset());
350 auto lr = rs.conv(l.weightset(), l.weight());
351 auto rr = rs.conv(r.weightset(), r.weight());
361 #endif // !VCSN_ALGOS_CONCATENATE_HH
ratexp make_ratexp(const RatExpSet &rs, const typename RatExpSet::value_t &ratexp)
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
polynomial make_polynomial(const PolynomialSet &ps, const typename PolynomialSet::value_t &polynomial)
automaton concatenate(const automaton &lhs, const automaton &rhs)
Bridge.
A & concatenate_here(A &res, const B &b)
Append automaton b to res.
auto concatenate(const A &lhs, const B &rhs) -> decltype(join_automata(lhs, rhs))
Concatenate two standard automata.
bool is_standard(const Aut &a)
Whether a is standard.
weight multiply_weight(const weight &lhs, const weight &rhs)
Bridge.
std::shared_ptr< const detail::weight_base > weight
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
std::shared_ptr< detail::ratexp_base > ratexp
auto join_automata(Auts &&...auts) -> decltype(make_mutable_automaton(join(auts->context()...)))
Join between automata.
AutOut copy(const AutIn &input, Pred keep_state)
A copy of input keeping only its states that are accepted by keep_state.
ValueSet::value_t multiply(const ValueSet &vs, const typename ValueSet::value_t &lhs, const typename ValueSet::value_t &rhs)
Product of weights.
weight make_weight(const WeightSet &ws, const typename WeightSet::value_t &w)
Provide a variadic mul on top of a binary mul(), and one().
RatExpSet::value_t chain(const RatExpSet &rs, const typename RatExpSet::value_t &r, int min, int max)
auto sum(const A &lhs, const B &rhs) -> decltype(join_automata(lhs, rhs))
void copy_into(const AutIn &in, AutOut &out, Pred keep_state)
Copy an automaton.
auto join(const ValueSet &vs) -> ValueSet
The join of a single valueset.
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
weight multiply(const weight &lhs, const weight &rhs)
Multiply two weights.
ratexp concatenate_ratexp(const ratexp &lhs, const ratexp &rhs)
Bridge.
ratexp chain_ratexp(const ratexp &re, int min, int max)
Bridge.
Aut::element_type::automaton_nocv_t star(const Aut &aut)
Star of a standard automaton.
Aut chain(const Aut &aut, int min, int max)
std::shared_ptr< const detail::polynomial_base > polynomial
variadic_mul_mixin< detail::r_impl > r
polynomial concatenate_polynomial(const polynomial &lhs, const polynomial &rhs)
Bridge.
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
automaton chain(const automaton &a, int min, int max)
Bridge.