1 #ifndef VCSN_CORE_MUTABLE_AUTOMATON_HH
2 # define VCSN_CORE_MUTABLE_AUTOMATON_HH
8 # include <boost/range/irange.hpp>
21 template <
typename Context>
22 class mutable_automaton_impl
31 using kind_t =
typename context_t::kind_t;
41 using label_t =
typename labelset_t::value_t;
43 using weight_t =
typename weightset_t::value_t;
92 *
this = std::move(that);
99 ctx_ = std::move(that.ctx_);
101 std::swap(
states_, that.states_);
117 std::string
vname(
bool full =
true)
const
119 return "mutable_automaton<" +
context().vname(full) +
">";
219 if (succ.size() <= pred.size())
222 std::find_if(begin(succ), end(succ),
234 std::find_if(begin(pred), end(pred),
286 auto tsucc =
std::find(succ.begin(), succ.end(), t);
287 assert(tsucc != succ.end());
288 *tsucc = std::move(succ.back());
298 auto tpred =
std::find(pred.begin(), pred.end(), t);
299 assert(tpred != pred.end());
300 *tpred = std::move(pred.back());
348 const std::string& =
"text",
354 static constexpr
bool
450 auto ts =
outin(s, d);
451 for (
auto t:
tr_cont_t{std::begin(ts), std::end(ts)})
490 states_[src].succ.emplace_back(t);
491 states_[dst].pred.emplace_back(t);
509 template <
typename A>
512 const A& aut,
typename A::element_type::transition_t t,
515 auto l = aut->label_of(t);
516 auto w = aut->weight_of(t);
519 l = aut->labelset()->transpose(l);
520 w = aut->weightset()->transpose(w);
524 weightset()->conv(*aut->weightset(), w));
551 assert(src !=
post());
553 assert(dst !=
pre());
621 template <
typename A>
624 const A& aut,
typename A::element_type::transition_t t,
627 auto l = aut->label_of(t);
628 auto w = aut->weight_of(t);
631 l = aut->labelset()->transpose(l);
632 w = aut->weightset()->transpose(w);
636 weightset()->conv(*aut->weightset(), w));
642 constexpr
char langle =
'<';
643 constexpr
char rangle =
'>';
645 std::ostringstream o;
694 template <
typename Pred>
699 {boost::irange<state_t>(
b, e),
700 [
this,pred] (
state_t s) ->
bool
703 return ((ss.
succ.empty()
724 template <
typename Pred>
741 template <
typename Pred>
801 template <
typename Pred>
806 return {
states_[s].succ, pred};
844 template <
typename Pred>
849 return {
states_[s].pred, pred};
888 template <
typename Context>
889 mutable_automaton<Context>
892 return make_shared_ptr<mutable_automaton<Context>>(ctx);
896 #endif // !VCSN_CORE_MUTABLE_AUTOMATON_HH
weight_t add_transition(state_t src, state_t dst, label_t l)
Same as above, with weight one.
ATTRIBUTE_PURE weight_t get_final_weight(state_t s) const
ATTRIBUTE_PURE weight_t get_initial_weight(state_t s) const
transitions_output_t all_transitions(Pred pred) const
All the transition indexes between all states (including pre and post), that validate pred...
size_t num_finals() const
label_t prepost_label_
Label for initial and final transitions.
Restrict the interface of a container to begin/end.
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
bool has_transition(transition_t t) const
std::vector< unsigned > free_store_t
A list of unused indexes in the states/transitions tables.
states_output_t all_states(Pred pred) const
All states including pre()/post() that validate pred.
labelset_t_of< context_t > labelset_t
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
bool has_state(state_t s) const
label_t prepost_label() const
transitions_output_t transitions() const
All the transition indexes between visible states.
size_t num_states() const
transition_t new_transition_copy(state_t src, state_t dst, const A &aut, typename A::element_type::transition_t t, bool transpose=false)
Copy the label of a transition between two states, creating a new transition.
size_t num_all_states() const
label_t label_of(transition_t t) const
weight_t add_weight(transition_t t, weight_t w)
static constexpr state_t null_state()
Invalid state.
void set_weight(weight_t &k)
void del_transition_from_src(transition_t t)
Remove t from the outgoing transitions of the source state.
unsigned state_t
Lightweight state handle (or index).
container_filter_range< boost::integer_range< transition_t >> transitions_output_t
static constexpr transition_t null_transition()
Invalid transition.
typename context_t::kind_t kind_t
static constexpr state_t pre()
weight_t add_final(state_t s, weight_t w)
std::ostream & print_state_name(state_t s, std::ostream &o, const std::string &="text", bool=true) const
const context_t & context() const
variadic_mul_mixin< detail::b_impl > b
bool has_transition(state_t src, state_t dst, label_t l) const
std::shared_ptr< detail::mutable_automaton_impl< Context >> mutable_automaton
std::vector< transition_t > tr_cont_t
All the incoming/outgoing transition handles of a state.
container_filter_range< const tr_cont_t & > outin(state_t s, state_t d) const
Indexes of visible transitions from state s to state d.
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.
mutable_automaton< context_t > automaton_nocv_t
The (shared pointer) type to use if we have to create an automaton of the same (underlying) type...
Data stored for each state.
container_filter_range< const tr_cont_t & > out(state_t s, const label_t &l) const
Indexes of all transitions leaving state s on label l.
container_filter_range< boost::integer_range< state_t >> states_output_t
container_filter_range< const tr_cont_t & > all_in(state_t s, Pred pred) const
Indexes of transitions entering state s that validate the predicate.
weight_t weight_of(transition_t t) const
state_t src_of(transition_t t) const
std::vector< stored_transition_t > tr_store_t
All the automaton's transitions.
container_filter_range< const tr_cont_t & > initial_transitions() const
Indexes of transitions to visible initial states.
void del_transition(state_t src, state_t dst, label_t l)
Remove the transition (src, l, dst).
void del_transition(state_t s, state_t d)
Remove all the transitions between s and d.
void set_final(state_t s, weight_t w)
state_t dst_of(transition_t t) const
weightset_t_of< context_t > weightset_t
transition_t new_transition(state_t src, state_t dst, label_t l)
Same as above, with weight one.
mutable_automaton_impl(const context_t &ctx)
typename context_t::labelset_ptr labelset_ptr
mutable_automaton_impl & operator=(mutable_automaton_impl &&that)
static std::string sname()
typename context_t::weightset_ptr weightset_ptr
static constexpr bool state_has_name(state_t)
const labelset_ptr & labelset() const
container_filter_range< const tr_cont_t & > all_out(state_t s, Pred pred) const
Indexes of transitions leaving state s that validate the predicate.
container_filter_range< const tr_cont_t & > final_transitions() const
Indexes of transitions from visible final states.
transition_t set_transition(state_t src, state_t dst, label_t l)
Same as above, with unit weight.
const weightset_ptr & weightset() const
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
size_t num_initials() const
void del_state(state_t s)
transition_t get_transition(state_t src, state_t dst, label_t l) const
mutable_automaton_impl(mutable_automaton_impl &&that)
container_filter_range< const tr_cont_t & > out(state_t s) const
Indexes of visible transitions leaving state s.
std::vector< stored_state_t > st_store_t
All the automaton's states.
states_output_t states() const
All states excluding pre()/post().
void del_transition(transition_t t)
states_output_t all_states() const
All states including pre()/post().
states_output_t state_range(state_t b, state_t e, Pred pred) const
The range of state numbers in [b .
typename weightset_t::value_t weight_t
Transition weight.
weight_t add_initial(state_t s, weight_t w)
weight_t lmul_weight(transition_t t, weight_t w)
weight_t add_transition(state_t src, state_t dst, label_t l, weight_t w)
Add a transition between two states.
Provide a variadic mul on top of a binary mul(), and one().
bool is_final(state_t s) const
mutable_automaton_impl()=delete
void unset_initial(state_t s)
void del_transition_from_dst(transition_t t)
Remove t from the ingoing transition of the destination state.
Aut transpose(const transpose_automaton< Aut > &aut)
container_range< const tr_cont_t & > all_in(state_t s) const
Indexes of all transitions arriving to state s.
container_range< const tr_cont_t & > all_out(state_t s) const
Indexes of all transitions leaving state s.
transition_t new_transition(state_t src, state_t dst, label_t l, weight_t w)
Create a transition between two states.
std::string vname(bool full=true) const
container_filter_range< const tr_cont_t & > in(state_t s, const label_t &l) const
Indexes of visible transitions arriving to state s on label l.
weight_t rmul_weight(transition_t t, weight_t w)
size_t num_transitions() const
container_filter_range< const tr_cont_t & > in(state_t s) const
Indexes of visible transitions arriving to state s.
void set_label(label_t &l)
void set_initial(state_t s)
states_output_t state_range(state_t b, state_t e) const
The range of state numbers in [b .. e].
weight_t set_weight(transition_t t, weight_t w)
transition_t set_transition(state_t src, state_t dst, label_t l, weight_t w)
Set a transition between two states.
std::ostream & print_state(state_t s, std::ostream &o) const
void unset_final(state_t s)
void del_transition_container(tr_cont_t &tc, bool from_succ)
weight_t add_transition_copy(state_t src, state_t dst, const A &aut, typename A::element_type::transition_t t, bool transpose=false)
Add a transition between two states, copying the label from the given transition. ...
bool is_initial(state_t s) const
static constexpr state_t post()
unsigned transition_t
Lightweight transition handle (or index).
transitions_output_t all_transitions() const
All the transition indexes between all states (including pre and post).
free_store_t transitions_fs_
Free indexes in transitions_.
typename labelset_t::value_t label_t
Transition label.
void set_initial(state_t s, weight_t w)
void set_final(state_t s)
std::string format_transition(transition_t t) const
context_t ctx_
The algebraic type of this automaton.
free_store_t states_fs_
Free indexes in states_.
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.