17 #ifndef AWALI_ALGOS_COMPOSE_HH
18 # define AWALI_ALGOS_COMPOSE_HH
21 # include <unordered_map>
34 namespace awali {
namespace sttc {
38 template<
typename T>
struct aff{};
47 template <
typename... T,
size_t I>
58 template <
size_t I,
size_t J,
typename Tuple1,
typename Tuple2>
71 template <
typename Lhs,
typename Rhs,
size_t I,
size_t J>
74 static_assert(Lhs::element_type::context_t::is_lat,
75 "requires labels_are_tuples");
76 static_assert(Rhs::element_type::context_t::is_lat,
77 "requires labels_are_tuples");
85 using I_labelset_t =
typename l_labelset_t::template valueset_t<I>;
86 using J_labelset_t =
typename r_labelset_t::template valueset_t<J>;
111 return {concat_and_remove<I,J>(ll.sets(),rl.sets())};
117 return {
make_labelset_(*lhs->context().labelset(), *rhs->context().labelset()),
118 join(*lhs->weightset(), *rhs->weightset())};
124 auto i = map_.find(ps);
125 if (i == std::end(map_))
127 res = output_->add_state();
140 map_[ps] = output_->pre();
142 map_[std::make_pair(lhs_->post(),rhs_->post())] = output_->post();
143 const auto& ws = *output_->context().weightset();
144 while (!todo_.empty())
149 auto it1 = lhs_->all_out(ps.first).begin();
150 auto end1 = lhs_->all_out(ps.first).end();
153 if(is_epsilon<I_labelset_t>(std::get<I>(lhs_->label_of(*it1))))
154 for(
auto tr:lhs_->all_out(ps.first)) {
157 output_->add_transition(src, dst,
159 lhs_->weight_of(tr));
162 auto it2 = rhs_->all_out(ps.second).begin();
163 auto end2 = rhs_->all_out(ps.second).end();
165 const auto& tr2=*it2;
166 if(is_epsilon<J_labelset_t>(std::get<J>(rhs_->label_of(tr2)))) {
169 output_->add_transition(src, dst,
171 rhs_->weight_of(tr2));
177 std::get<J>(rhs_->label_of(tr2))))
180 std::get<I>(lhs_->label_of(*it1))))
185 std::get<J>(rhs_->label_of(*it2)))) {
188 std::get<J>(rhs_->label_of(*it2)))) {
189 state_t q1 = lhs_->dst_of(*it1);
190 state_t q2 = rhs_->dst_of(*it2);
192 output_->add_transition(src, dst,
195 ws.mul(lhs_->weight_of(*it1),rhs_->weight_of(*it2)));
208 auto history = std::make_shared<tuple_history<std::tuple<Lhs,Rhs>>>(std::make_tuple(lhs_,rhs_));
209 output_->set_history(history);
210 for (
const auto& p: map_)
211 history->add_state(p.second, p.first);
217 using label_t =
typename labelset_t::value_t;
218 using weight_t =
typename weightset_t::value_t;
220 std::unordered_map<pair_state_t, state_t> map_;
221 std::stack<pair_state_t> todo_;
252 template <
size_t I,
size_t J,
typename TDC1,
typename TDC2>
254 composeIJ(TDC1& tdc1, TDC2& tdc2,
bool keep_history=
true)
257 auto l = outsplit<I>(tdc1, keep_history);
280 template <
typename TDC1,
typename TDC2>
282 compose(
const TDC1& tdc1,
const TDC2& tdc2,
bool keep_history=
true)
285 return composeIJ<1,0>(tdc1, tdc2, keep_history);
300 template <
typename Aut,
typename Tdc>
302 eval_tdc(
const Aut& aut,
const Tdc& tdc,
bool keep_history=
true)
303 -> decltype(projection<1>(tdc))
305 auto l = partial_identity<1>(aut,keep_history);
306 auto r = composeIJ<0,0>(l, tdc, keep_history);
307 return projection<0>(
r,keep_history);
carries the algebraic settings of automata
Definition: context.hh:40
Build the (accessible part of the) composition.
Definition: compose.hh:73
weightset_t_of< Lhs > l_weightset_t
Definition: compose.hh:83
weightset_t_of< Rhs > r_weightset_t
Definition: compose.hh:84
typename rem_in_tupleset< r_labelset_t, J >::type minusJ_labelset_t
Definition: compose.hh:88
typename l_labelset_t::template valueset_t< I > I_labelset_t
Definition: compose.hh:85
mutable_automaton< context_t > automaton_t
The type of the resulting automaton.
Definition: compose.hh:97
std::pair< state_t, state_t > pair_state_t
Result state type.
Definition: compose.hh:100
static labelset_t make_labelset_(const l_labelset_t &ll, const r_labelset_t &rl)
Definition: compose.hh:108
labelset_t_of< Lhs > l_labelset_t
Definition: compose.hh:81
state_t state(const pair_state_t &ps)
Definition: compose.hh:121
Rhs crhs_t
Definition: compose.hh:80
typename rem_in_tupleset< l_labelset_t, I >::type minusI_labelset_t
Definition: compose.hh:87
Lhs clhs_t
Definition: compose.hh:79
automaton_t compose()
The (accessible part of the) product of lhs_ and rhs_.
Definition: compose.hh:137
labelset_t_of< Rhs > r_labelset_t
Definition: compose.hh:82
join_t< weightset_t_of< context_t_of< Lhs > >, weightset_t_of< context_t_of< Rhs > >> weightset_t
Definition: compose.hh:93
static context_t make_context_(const Lhs &lhs, const Rhs &rhs)
Definition: compose.hh:115
sttc::context< labelset_t, weightset_t > context_t
Definition: compose.hh:94
void set_history()
Definition: compose.hh:207
typename r_labelset_t::template valueset_t< J > J_labelset_t
Definition: compose.hh:86
composer(const Lhs &lhs, const Rhs &rhs)
Definition: compose.hh:102
typename concat_tupleset< minusI_labelset_t, minusJ_labelset_t >::type labelset_t
The type of context of the result.
Definition: compose.hh:91
The semiring of rational numbers.
Definition: q.hh:41
The semiring of floating Numbers.
Definition: r.hh:34
A ValueSet which is a Cartesian product of ValueSets.
Definition: tupleset.hh:80
const valuesets_t & sets() const
The componants valuesets, as a tuple.
Definition: tupleset.hh:152
auto concat_and_remove(const Tuple1 &t1, const Tuple2 &t2) -> typename std::concat_tuple< typename rem_in_tuple< I >::template type< Tuple1 >, typename rem_in_tuple< J >::template type< Tuple2 >>::type
Definition: compose.hh:60
Definition: compose.hh:38
Definition: tupleset.hh:969
Definition: compose.hh:45
Definition: projection.hh:52
RatExpSet::ratexp_t less_than(const RatExpSet &rs, const typename RatExpSet::ratexp_t &v)
Definition: less_than.hh:166
auto composeIJ(TDC1 &tdc1, TDC2 &tdc2, bool keep_history=true) -> typename internal::composer< TDC1, TDC2, I, J >::automaton_t
Composition of two transducers on given tapes.
Definition: compose.hh:254
auto compose(const TDC1 &tdc1, const TDC2 &tdc2, bool keep_history=true) -> typename internal::composer< TDC1, TDC2, 1, 0 >::automaton_t
Composition of two transducers.
Definition: compose.hh:282
RatExpSet::ratexp_t equals(const RatExpSet &rs, const typename RatExpSet::ratexp_t &v)
Definition: equal_visit.hh:153
decltype(join(std::declval< ValueSets >()...)) join_t
Computation of the join of some value sets.
Definition: context.hh:206
auto join(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< join_t< Ctx1, Ctx2 >>
The union of two ratexpsets.
Definition: ratexpset.hh:448
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:911
auto eval_tdc(const Aut &aut, const Tdc &tdc, bool keep_history=true) -> decltype(projection< 1 >(tdc))
Evaluation of an automaton by a transducer.
Definition: compose.hh:302
void proper_here(Aut &aut, direction_t dir=BACKWARD, bool prune=true)
Eliminate spontaneous transitions in place.
Definition: proper.hh:427
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
Definition: sub_tuple.hh:111
static auto get(const Tuple &t) -> type< Tuple >
Definition: sub_tuple.hh:119
static type get(const tp_t &t)
Definition: compose.hh:53
typename tuple_to_tupleset< typename rem_in_tuple< I >::template type< std::tuple< T... > >>::type type
Definition: compose.hh:50