17 #ifndef AWALI_ALGOS_STANDARD_HH
18 # define AWALI_ALGOS_STANDARD_HH
31 namespace awali {
namespace sttc {
38 template <
typename Aut>
43 a->num_initials() == 1
44 && a->weightset()->is_one(a->weight_of(*(a->initial_transitions().begin())))
46 && a->in(a->dst_of(*(a->initial_transitions().begin()))).empty();
56 template <
typename Aut>
62 const auto& ws = *aut->weightset();
63 const auto& inits = aut->initial_transitions();
64 std::vector<transition_t> initials{begin(inits), end(inits)};
69 auto ini = aut->add_state();
70 for (
auto ti: initials)
73 auto i = aut->dst_of(ti);
74 auto wi = aut->weight_of(ti);
75 for (
auto t: aut->all_out(i))
76 aut->add_transition(ini, aut->dst_of(t), aut->label_of(t),
77 ws.mul(wi, aut->weight_of(t)));
78 aut->del_transition(ti);
82 if (aut->all_in(i).empty())
87 aut->set_initial(ini);
90 template <
typename Aut>
93 auto out =
copy(aut, keep_history,
false,
true);
108 template <
typename Aut,
109 typename Context = context_t_of<Aut>>
111 :
public Context::const_visitor
122 constexpr
static const char*
me() {
return "standard"; }
128 , keep_history_(keep_history)
135 res_->set_history(history_);
137 res_->set_initial(initial_);
138 history_->add_state(initial_,
"i");
139 return std::move(res_);
150 initial_ = res_->add_state();
155 auto i = res_->add_state();
162 auto i = res_->add_state();
163 auto f = res_->add_state();
165 res_->new_transition(i, f, e.value());
176 for (
auto t: res_->final_transitions())
177 res.insert(res_->src_of(t));
184 e.head()->accept(*
this);
186 for (
auto c: e.tail())
189 for (
auto t: res_->all_out(initial_))
192 res_->add_transition(initial,
196 res_->del_state(initial_);
209 e.head()->accept(*
this);
213 for (
auto c: e.tail())
216 auto ftr_ = res_->final_transitions();
218 using transs_t = std::vector<transition_t>;
219 transs_t ftr{ begin(ftr_), end(ftr_) };
240 auto s1 = res_->src_of(t1);
241 auto w1 = res_->weight_of(t1);
242 res_->del_transition(t1);
243 for (
auto t2: res_->all_out(initial_))
244 res_->set_transition(s1,
247 ws_.mul(w1, res_->weight_of(t2)));
249 res_->del_state(initial_);
258 e.sub()->accept(*
this);
260 weight_t w = ws_.star(res_->get_final_weight(initial_));
263 for (
auto ti: res_->out(initial_))
265 res_->lmul_weight(ti, w);
266 for (
auto tf: res_->final_transitions())
267 if (res_->src_of(tf) != initial_
278 ws_.mul(res_->weight_of(tf), res_->weight_of(ti)));
280 for (
auto tf: res_->final_transitions())
282 res_->rmul_weight(tf, w);
283 res_->set_final(initial_, w);
289 e.sub()->accept(*
this);
291 weight_t we = res_->get_final_weight(initial_);
295 for (
auto ti: res_->out(initial_))
297 res_->lmul_weight(ti, w);
298 for (
auto tf: res_->final_transitions())
299 if (res_->src_of(tf) != initial_
310 ws_.mul(res_->weight_of(tf), res_->weight_of(ti)));
312 for (
auto tf: res_->final_transitions())
314 res_->rmul_weight(tf, w);
315 res_->set_final(initial_, ws_.plus(we));
320 e.sub()->accept(*
this);
321 res_->add_final(initial_, ws_.one());
326 e.sub()->accept(*
this);
327 for (
auto t: res_->all_out(initial_))
328 res_->lmul_weight(t, e.weight());
334 e.sub()->accept(*
this);
335 for (
auto t: res_->final_transitions())
337 res_->rmul_weight(t, e.weight());
343 state_t initial_ = automaton_t::element_type::null_state();
360 template <
typename Aut,
361 typename Context = context_t_of<Aut>>
363 standard(
const Context& ctx,
const typename Context::ratexp_t& e,
bool keep_history=
true)
377 template <
typename RatExpSet>
379 mutable_automaton<typename RatExpSet::context_t>
380 standard(
const RatExpSet& rs,
const typename RatExpSet::ratexp_t& e,
bool keep_history=
true)
The semiring of complex numbers.
Definition: c.hh:44
Definition: ratexp.hh:280
Definition: ratexp.hh:262
Convert a ratexp to a standard automaton.
Definition: standard.hh:112
weight_t_of< context_t > weight_t
Definition: standard.hh:117
constexpr static const char * me()
Definition: standard.hh:122
AWALI_RAT_VISIT(atom, e)
Definition: standard.hh:160
AWALI_RAT_VISIT(plus, e)
Definition: standard.hh:286
AWALI_RAT_VISIT(one,)
Definition: standard.hh:153
AWALI_RAT_VISIT(rweight, e)
Definition: standard.hh:331
AWALI_RAT_VISIT(star, e)
Definition: standard.hh:255
AWALI_RAT_VISIT(sum, e)
Definition: standard.hh:181
Context context_t
Definition: standard.hh:115
AWALI_RAT_VISIT(maybe, e)
Definition: standard.hh:318
standard_visitor(const context_t &ctx, bool keep_history)
Definition: standard.hh:124
typename Context::const_visitor super_type
Definition: standard.hh:119
AWALI_RAT_VISIT(lweight, e)
Definition: standard.hh:324
automaton_t operator()(const typename context_t::ratexp_t &v)
Definition: standard.hh:132
weightset_t_of< context_t > weightset_t
Definition: standard.hh:116
std::shared_ptr< string_history > history_t
Definition: standard.hh:120
AWALI_RAT_VISIT(zero,)
Definition: standard.hh:148
states_t finals()
Definition: standard.hh:173
AWALI_RAT_VISIT(prod, e)
Definition: standard.hh:201
std::set< state_t > states_t
The current set of final states.
Definition: standard.hh:171
Aut automaton_t
Definition: standard.hh:114
Definition: ratexp.hh:176
An inner node with multiple children.
Definition: ratexp.hh:115
An inner node implementing a weight.
Definition: ratexp.hh:208
Definition: string_history.hh:29
weightset_description weightset(const std::string &k)
bool has(const std::map< Key, Value, Compare, Alloc > &s, const Key &e)
Definition: map.hh:53
std::string to_string(identities i)
bool is_standard(const Aut &a)
Definition: standard.hh:40
AutOut copy(const AutIn &input, Pred keep_state, bool keep_history=true, bool transpose=false, bool same_index=false)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:203
SharedPtr make_shared_ptr(Args &&... args)
Same as std::make_shared, but parameterized by the shared_ptr type, not the (pointed to) element_type...
Definition: memory.hh:29
typename internal::weight_t_of_impl< internal::base_t< ValueSet > >::type weight_t_of
Helper to retrieve the type of the weights of a value set.
Definition: traits.hh:81
Aut standard(Aut &aut, bool keep_history=true)
Definition: standard.hh:92
void standard_here(Aut &aut)
Turn aut into a standard automaton.
Definition: standard.hh:58
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
#define AWALI_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:75