17 #ifndef AWALI_CORE_MUTABLE_AUTOMATON_HH
18 # define AWALI_CORE_MUTABLE_AUTOMATON_HH
40 template <
typename Context>
41 class mutable_automaton_impl;
43 template <
typename Context>
45 = std::shared_ptr<internal::mutable_automaton_impl<Context>>;
49 template <
typename Context>
59 using kind_t =
typename context_t::kind_t;
65 using label_t =
typename labelset_t::value_t;
67 using weight_t =
typename weightset_t::value_t;
70 using names_t = std::shared_ptr<string_history>;
113 , prepost_label_(ctx.
labelset()->special())
120 , prepost_label_(that.prepost_label_) {
121 *
this = std::move(that);
126 ctx_ = std::move(that.ctx_);
127 prepost_label_ = std::move(that.prepost_label_);
128 std::swap(states_, that.states_);
129 std::swap(states_fs_, that.states_fs_);
130 std::swap(transitions_, that.transitions_);
131 std::swap(transitions_fs_, that.transitions_fs_);
132 history_ = that.history_;
133 names_ = that.names_;
142 return "mutable_automaton<" + context_t::sname() +
">";
146 return "mutable_automaton<" +
context().vname(
full) +
">";
167 return prepost_label_;
178 return (transitions_.size()
189 if (s >= states_.size())
241 const tr_cont_t& succ = states_[src].succ;
242 const tr_cont_t& pred = states_[dst].pred;
244 if (succ.size() <= pred.size()) {
246 std::find_if(begin(succ), end(succ),
249 return (st.
dst == dst
257 std::find_if(begin(pred), end(pred),
260 return (st.
src == src
278 if (t >= transitions_.size())
288 return transitions_[t].get_label();
292 return transitions_[t].get_weight();
303 auto& succ = states_[st.
src].succ;
304 auto tsucc =
std::find(succ.begin(), succ.end(), t);
305 assert(tsucc != succ.end());
306 *tsucc = std::move(succ.back());
314 auto& pred = states_[st.
dst].pred;
315 auto tpred =
std::find(pred.begin(), pred.end(), t);
316 assert(tpred != pred.end());
317 *tpred = std::move(pred.back());
330 transitions_fs_.insert(transitions_fs_.end(), tc.begin(), tc.end());
338 if (states_fs_.empty()) {
340 states_.resize(s + 1);
343 s = states_fs_.back();
344 states_fs_.pop_back();
361 history_ = std::make_shared<no_history>();
365 names_ = std::make_shared<string_history>();
369 if(names_->has_history(s))
370 return names_->print_state_name(s, o,
"text");
375 return o <<
'$' << (s-2);
379 const std::string& =
"text")
const {
384 if(names_->has_history(s))
385 return names_->get_state_name(s);
386 std::ostringstream o;
392 const std::string& fmt =
"text")
const {
393 if(history_->has_history(s))
394 return history_->print_state_name(s, o, fmt);
399 return history_->has_history(s);
403 return names_->has_history(s);
408 if(history_->has_history(s)) {
409 std::ostringstream o;
416 return names_->has_history(s);
420 names_->add_state(s,
n);
425 std::ostringstream os;
434 if(
n.length()>0 &&
n[0]==
'$')
445 return names_->get_name();
449 return names_->get_desc();
459 history_->remove_history(s);
460 names_->remove_history(s);
462 states_fs_.emplace_back(s);
509 transitions_fs_.emplace_back(t);
528 auto ts =
outin(s, d);
529 for (
auto t:
tr_cont_t{ts.begin(), ts.end()})
552 if (transitions_fs_.empty())
554 t = transitions_.size();
555 transitions_.resize(t + 1);
559 t = transitions_fs_.back();
560 transitions_fs_.pop_back();
568 states_[src].succ.emplace_back(t);
569 states_[dst].pred.emplace_back(t);
587 template <
typename A>
593 auto l = aut->label_of(t);
594 auto w = aut->weight_of(t);
597 l = aut->labelset()->transpose(l);
598 w = aut->weightset()->transpose(w);
629 assert(src !=
post());
631 assert(dst !=
pre());
656 template <
typename A>
662 auto l = aut->label_of(t);
663 auto w = aut->weight_of(t);
666 l = aut->labelset()->transpose(l);
667 w = aut->weightset()->transpose(w);
716 template <
typename A>
722 auto l = aut->label_of(t);
723 auto w = aut->weight_of(t);
726 l = aut->labelset()->transpose(l);
727 w = aut->weightset()->transpose(w);
740 transitions_[t].set_weight(w);
773 return (ss.
succ.empty()
774 || ss.
succ.front() != this->null_transition());
783 return (ss.
succ.empty()
784 || ss.
succ.front() != this->null_transition());
837 return this->transitions_[i].dst != this->
post();
847 return states_[s].succ;
854 return states_[s].succ;
865 return this->
labelset()->equals(this->transitions_[i].get_label(), l);
876 {
return this->transitions_[i].src != this->
pre(); });
885 return states_[s].pred;
896 return this->
labelset()->equals(this->transitions_[i].get_label(), l);
908 {
return this->transitions_[i].dst == d; });
913 template <
typename Context>
914 mutable_automaton<Context>
917 return make_shared_ptr<mutable_automaton<Context>>(ctx);
Definition: mutable_automaton.hh:51
cont_filter< tr_cont_t > transitions_s_output_t
Definition: mutable_automaton.hh:813
std::vector< stored_state_t > st_store_t
All the automaton's states.
Definition: mutable_automaton.hh:88
std::string get_state_name(state_t s) const
Definition: mutable_automaton.hh:383
void set_desc(const std::string &d)
Definition: mutable_automaton.hh:440
indice_filter< tr_store_t > transitions_output_t
Definition: mutable_automaton.hh:788
transitions_s_output_t final_transitions() const
Indexes of transitions from visible final states.
Definition: mutable_automaton.hh:824
std::shared_ptr< string_history > names_t
Definition: mutable_automaton.hh:70
void set_initial(state_t s, weight_t w)
Definition: mutable_automaton.hh:465
bool is_final(state_t s) const
Definition: mutable_automaton.hh:210
transitions_s_output_t out(state_t s) const
Indexes of visible transitions leaving state s.
Definition: mutable_automaton.hh:832
const tr_cont_t & all_out(state_t s) const
Indexes of all transitions leaving state s.
Definition: mutable_automaton.hh:844
bool has_history(state_t s) const
Definition: mutable_automaton.hh:398
state_t src_of(transition_t t) const
Definition: mutable_automaton.hh:285
void unset_initial(state_t s)
Definition: mutable_automaton.hh:477
weight_t add_transition(state_t src, state_t dst, label_t l, weight_t w)
Add a transition between two states.
Definition: mutable_automaton.hh:685
states_output_t states() const
All states excluding pre()/post().
Definition: mutable_automaton.hh:770
indice_filter< st_store_t > states_output_t
Definition: mutable_automaton.hh:765
states_output_t all_states() const
All states including pre()/post().
Definition: mutable_automaton.hh:780
transitions_output_t all_transitions() const
All the transition indexes between all states (including pre and post).
Definition: mutable_automaton.hh:805
std::vector< transition_t > tr_cont_t
All the incoming/outgoing transition handles of a state.
Definition: mutable_automaton.hh:79
weight_t weight_of(transition_t t) const
Definition: mutable_automaton.hh:291
bool has_explicit_name(state_t s) const
Definition: mutable_automaton.hh:415
std::ostream & print_state(state_t s, std::ostream &o) const
Definition: mutable_automaton.hh:368
context_t ctx_
The algebraic type of this automaton.
Definition: mutable_automaton.hh:73
void set_final(state_t s)
Definition: mutable_automaton.hh:485
void set_final(state_t s, weight_t w)
Definition: mutable_automaton.hh:481
static constexpr state_t null_state()
Definition: mutable_automaton.hh:163
state_t get_state_by_name(const std::string &name) const
Definition: mutable_automaton.hh:423
ATTRIBUTE_PURE weight_t get_initial_weight(state_t s) const
Definition: mutable_automaton.hh:216
transitions_s_output_t initial_transitions() const
Indexes of transitions to visible initial states.
Definition: mutable_automaton.hh:817
size_t num_transitions() const
Definition: mutable_automaton.hh:177
void del_transition_container(tr_cont_t &tc, bool from_succ)
Definition: mutable_automaton.hh:322
void del_transition(state_t s, state_t d)
Remove all the transitions between s and d.
Definition: mutable_automaton.hh:524
void del_state(state_t s)
Definition: mutable_automaton.hh:453
state_t dst_of(transition_t t) const
Definition: mutable_automaton.hh:286
mutable_automaton_impl(mutable_automaton_impl &&that)
Definition: mutable_automaton.hh:118
transition_t set_transition(state_t src, state_t dst, label_t l)
Same as above, with unit weight.
Definition: mutable_automaton.hh:651
ATTRIBUTE_PURE weight_t get_final_weight(state_t s) const
Definition: mutable_automaton.hh:226
void strip_history()
Definition: mutable_automaton.hh:360
const context_t & context() const
Definition: mutable_automaton.hh:149
static std::string sname()
Definition: mutable_automaton.hh:141
const labelset_ptr & labelset() const
Definition: mutable_automaton.hh:151
typename context_t::kind_t kind_t
Definition: mutable_automaton.hh:59
bool has_transition(transition_t t) const
Definition: mutable_automaton.hh:275
mutable_automaton_impl(const context_t &ctx)
Definition: mutable_automaton.hh:110
state_t max_state() const
Definition: mutable_automaton.hh:197
const std::string & get_name() const
Definition: mutable_automaton.hh:444
weight_t lmul_weight(transition_t t, weight_t w)
Definition: mutable_automaton.hh:751
static constexpr transition_t null_transition()
Definition: mutable_automaton.hh:164
transition_t new_transition(state_t src, state_t dst, label_t l)
Same as above, with weight one.
Definition: mutable_automaton.hh:607
mutable_automaton_impl()=delete
transition_t get_transition(state_t src, state_t dst, label_t l) const
Definition: mutable_automaton.hh:238
transitions_s_output_t outin(state_t s, state_t d) const
Indexes of visible transitions from state s to state d.
Definition: mutable_automaton.hh:903
size_t num_all_states() const
Definition: mutable_automaton.hh:173
weight_t rmul_weight(transition_t t, weight_t w)
Definition: mutable_automaton.hh:757
transition_t del_transition(state_t src, state_t dst, label_t l)
Remove the transition (src, l, dst).
Definition: mutable_automaton.hh:514
weight_t add_final(state_t s, weight_t w)
Definition: mutable_automaton.hh:489
bool set_name(const std::string &n)
Definition: mutable_automaton.hh:433
std::vector< state_t > free_store_t
A list of unused indexes in the states/transitions tables.
Definition: mutable_automaton.hh:92
labelset_t_of< context_t > labelset_t
Definition: mutable_automaton.hh:57
label_t label_of(transition_t t) const
Definition: mutable_automaton.hh:287
weight_t add_transition_copy(state_t src, state_t dst, const A &aut, transition_t t, bool transpose=false)
Add a transition between two states, copying the label from the given transition.
Definition: mutable_automaton.hh:718
typename context_t::labelset_ptr labelset_ptr
Definition: mutable_automaton.hh:61
Context context_t
Definition: mutable_automaton.hh:53
void strip_names()
Definition: mutable_automaton.hh:364
transitions_s_output_t in(state_t s) const
Indexes of visible transitions arriving to state s.
Definition: mutable_automaton.hh:872
std::vector< stored_transition_t > tr_store_t
All the automaton's transitions.
Definition: mutable_automaton.hh:90
typename weightset_t::value_t weight_t
Transition weight.
Definition: mutable_automaton.hh:67
mutable_automaton< context_t > automaton_nocv_t
The (shared pointer) type to use it we have to create an automaton of the same (underlying) type.
Definition: mutable_automaton.hh:56
typename labelset_t::value_t label_t
Transition label.
Definition: mutable_automaton.hh:65
size_t num_initials() const
Definition: mutable_automaton.hh:175
mutable_automaton_impl(const mutable_automaton_impl &)=delete
bool is_initial(state_t s) const
Definition: mutable_automaton.hh:205
void set_state_name(state_t s, const std::string &n)
Definition: mutable_automaton.hh:419
history_t history() const
Definition: mutable_automaton.hh:352
weightset_t_of< context_t > weightset_t
Definition: mutable_automaton.hh:58
void set_state_names_from_history()
Definition: mutable_automaton.hh:406
transitions_output_t transitions() const
All the transition indexes between visible states.
Definition: mutable_automaton.hh:792
std::ostream & print_state_name(state_t s, std::ostream &o, const std::string &="text") const
Definition: mutable_automaton.hh:378
const std::string & get_desc() const
Definition: mutable_automaton.hh:448
bool has_transition(state_t src, state_t dst, label_t l) const
Definition: mutable_automaton.hh:270
transition_t new_transition_copy(state_t src, state_t dst, const A &aut, transition_t t, bool transpose=false)
Copy the label of a transition between two states, creating a new transition.
Definition: mutable_automaton.hh:589
size_t num_finals() const
Definition: mutable_automaton.hh:176
void set_history(history_t h)
Definition: mutable_automaton.hh:356
static constexpr state_t pre()
Definition: mutable_automaton.hh:160
tr_cont_t succ
Definition: mutable_automaton.hh:84
state_t add_state()
Definition: mutable_automaton.hh:336
const weightset_ptr & weightset() const
Definition: mutable_automaton.hh:150
void del_transition(transition_t t)
Definition: mutable_automaton.hh:501
std::shared_ptr< history_base > history_t
History.
Definition: mutable_automaton.hh:69
bool has_state(state_t s) const
Definition: mutable_automaton.hh:186
void del_transition_from_dst(transition_t t)
Remove t from the ingoing transition of the destination state.
Definition: mutable_automaton.hh:312
transitions_s_output_t out(state_t s, const label_t &l) const
Indexes of all transitions leaving state s on label l.
Definition: mutable_automaton.hh:860
label_t prepost_label() const
Definition: mutable_automaton.hh:166
weight_t add_initial(state_t s, weight_t w)
Definition: mutable_automaton.hh:473
size_t num_states() const
Definition: mutable_automaton.hh:174
transition_t set_transition(state_t src, state_t dst, label_t l, weight_t w)
Set a transition between two states.
Definition: mutable_automaton.hh:623
const tr_cont_t & all_in(state_t s) const
Indexes of all transitions arriving to state s.
Definition: mutable_automaton.hh:882
void unset_final(state_t s)
Definition: mutable_automaton.hh:493
bool has_name(state_t s) const
Definition: mutable_automaton.hh:402
weight_t add_weight(transition_t t, weight_t w)
Definition: mutable_automaton.hh:745
void set_initial(state_t s)
Definition: mutable_automaton.hh:469
transition_t new_transition(state_t src, state_t dst, label_t l, weight_t w)
Create a transition between two states.
Definition: mutable_automaton.hh:544
mutable_automaton_impl & operator=(mutable_automaton_impl &&that)
Definition: mutable_automaton.hh:124
transitions_s_output_t in(state_t s, const label_t &l) const
Indexes of visible transitions arriving to state s on label l.
Definition: mutable_automaton.hh:891
static constexpr state_t post()
Definition: mutable_automaton.hh:161
tr_cont_t pred
Definition: mutable_automaton.hh:85
std::string vname(bool full=true) const
Definition: mutable_automaton.hh:145
std::ostream & print_state_history(state_t s, std::ostream &o, const std::string &fmt="text") const
Definition: mutable_automaton.hh:391
weight_t add_transition(state_t src, state_t dst, label_t l)
Same as above, with weight one.
Definition: mutable_automaton.hh:700
weight_t set_weight(transition_t t, weight_t w)
Definition: mutable_automaton.hh:735
tr_cont_t & all_out_(state_t s)
Definition: mutable_automaton.hh:851
typename context_t::weightset_ptr weightset_ptr
Definition: mutable_automaton.hh:62
transition_t set_transition_copy(state_t src, state_t dst, const A &aut, transition_t t, bool transpose=false)
Definition: mutable_automaton.hh:658
void del_transition_from_src(transition_t t)
Remove t from the outgoing transitions of the source state.
Definition: mutable_automaton.hh:301
Data stored for each state.
Definition: mutable_automaton.hh:83
The semiring of Natural numbers.
Definition: n.hh:34
specialisation of history_base
Definition: no_history.hh:28
Definition: string_history.hh:29
ATTRIBUTE_PURE auto find(const std::vector< T, Alloc > &s, const T &e) -> typename std::vector< T, Alloc >::const_iterator
Convenience wrapper around std::find.
Definition: vector.hh:81
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
AutOut transpose(Aut &aut, bool keep_history=true)
Definition: transpose.hh:79
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:915
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
auto conv(const ValueSet &vs, const std::string &str, Args &&... args) -> decltype(vs.conv(std::declval< std::istream & >(), std::forward< Args >(args)...))
Parse str via vs.conv.
Definition: stream.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
static const std::string full
Completely version of Awali as a std::string.
Definition: version.hh:42
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
unsigned transition_t
Definition: types.hh:22
Definition: cont_filter.hh:145
Definition: cont_filter.hh:69
State dst
Definition: transition.hh:36
State src
Definition: transition.hh:35
label_t get_label() const
Definition: transition.hh:39
void set_label(label_t &l)
Definition: transition.hh:40
Definition: transition.hh:65
void set_weight(weight_t &k)
Definition: transition.hh:68