5 #include <unordered_map>
6 #include <unordered_set>
10 #include <boost/heap/fibonacci_heap.hpp>
14 #include <vcsn/algos/fwd.hh>
32 bool has_one = labelset_t_of<Aut>::has_one()>
37 using weight_t =
typename weightset_t::value_t;
60 ,
ws_(*aut->weightset())
69 aut_dirty_ = make_shared_ptr<aut_dirty_t>(dirty_ctx);
70 aut_proper_ = make_shared_ptr<aut_proper_t>(proper_ctx);
77 pcopier([](state_t) {
return true; },
78 [&aut](transition_t t) {
79 return !aut->labelset()->is_one(aut->label_of(t));
82 dcopier([&aut](state_t s) {
83 return (!
in(aut, s, aut->labelset()->one()).empty()
84 || !
out(aut, s, aut->labelset()->one()).empty());
86 [&aut](transition_t t) {
87 return aut->labelset()->is_one(aut->label_of(t));
91 const auto& dorigins = dcopier.state_map();
92 const auto& porigins = pcopier.state_map();
93 for (
const auto& dp : dorigins)
95 auto pp = porigins.find(dp.first);
96 assert(pp != porigins.end());
97 d2p_[dp.second] = pp->second;
98 p2d_[pp->second] = dp.second;
109 auto dirty_s =
p2d_[proper_s];
117 p->update(in_dirty, in_proper + in_dirty,
118 out_dirty, out_proper + out_dirty);
131 auto proper_s =
d2p_[s];
141 const char* sep =
"";
142 for (
auto i =
todo_.ordered_begin(), end =
todo_.ordered_end();
145 std::cerr << sep << *i;
156 std::cerr <<
"update heap (" << s <<
" : ";
161 todo_.update(i->second);
164 std::cerr <<
") => ";
201 using state_weight_t = std::pair<state_dirty_t, weight_t>;
202 auto closure = std::vector<state_weight_t>{};
214 star =
ws_.star(weight);
216 closure.emplace_back(src, weight);
244 for (
auto pair: closure)
246 auto src =
pair.first;
259 for (
auto pair: closure)
261 auto src =
pair.first;
286 std::cerr <<
" -" << removed <<
"+" << added
287 <<
" (-" << removed_ <<
"+" << added_ <<
")";
337 auto neighbors = std::unordered_set<state_proper_t>{};
338 while (!
todo_.empty())
342 std::cerr <<
"Before: ";
346 auto p =
todo_.top();
349 std::cerr <<
"Remove: " << p;
352 auto dirty_s =
p2d_[proper_s];
369 neighbors.emplace(
d2p_[n]);
372 neighbors.emplace(
d2p_[n]);
378 for (
auto n: neighbors)
380 for (
auto n: neighbors)
383 std::cerr <<
" #tr: "
390 std::cerr <<
"After: ";
404 std::cerr <<
"Total transitions -" << removed_
405 <<
"+" << added_ <<
'\n';
433 using heap_t = boost::heap::fibonacci_heap<profile_t>;
436 std::unordered_map<state_proper_t, typename heap_t::handle_type>
handles_;
441 std::vector<state_proper_t>
d2p_;
442 std::vector<state_dirty_t>
p2d_;
445 template <Automaton Aut>
std::vector< typename Cont::value_type > make_vector(const Cont &cont)
The content of cont as a vector.
aut_proper_t get_proper()
Return the proper part. Needed by lazy algorithm.
auto transitions(const Aut &aut) -> decltype(all_transitions(aut, is_special_t< Aut >
All the transition indexes between visible states.
static int debug_level()
Debug level set in the user's environment.
state_t_of< aut_proper_t > state_proper_t
std::unordered_map< state_proper_t, typename heap_t::handle_type > handles_
Map: state -> heap-handle.
typename weightset_t::value_t weight_t
aut_dirty_t aut_dirty_
The sponteanous part.
void show_heap_() const
Show the heap, for debugging.
value_impl< detail::weight_tag > weight
This class contains the core of the proper algorithm.
state_t_of< aut_dirty_t > state_dirty_t
aut_proper_t aut_proper_
The proper part.
const weightset_t & ws_
Shorthand to the weightset.
labelset_t_of< automaton_t > labelset_t
boost::heap::fibonacci_heap< profile_t > heap_t
Max-heap to decide the order of state-elimination.
std::shared_ptr< detail::mutable_automaton_impl< Context >> mutable_automaton
fresh_automaton_t_of< Aut, proper_ctx_t > aut_proper_t
Proper automaton.
This is used by some epsilon removal algorithms.
aut_proper_t operator()()
detail::copier< AutIn, AutOut > make_copier(const AutIn &in, AutOut &out, bool safe=true)
Build an automaton copier.
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
epsilon_remover_separate(const automaton_t &aut, bool)
typename Aut::element_type::template fresh_automaton_t< Context > fresh_automaton_t_of
Given an automaton type, the type of its copies.
aut_proper_t operator()()
Remove all the states with incoming spontaneous transitions.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
size_t states_size(const Aut &aut)
The largest state number, plus one.
weightset_t_of< automaton_t > weightset_t
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
void remover_on(state_proper_t proper_s)
Wrapper around remover_on() which does a lookup of the dirty state.
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
std::remove_cv_t< Aut > automaton_t
pair_automaton< Aut > pair(const Aut &aut, bool keep_initials=false)
void remover_on(state_proper_t proper_s, state_dirty_t dirty_s)
For each state (s), for each incoming epsilon-transitions (t), if (t) is a loop, the star of its weig...
std::remove_cv_t< Aut > automaton_t
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
fresh_automaton_t_of< automaton_t > aut_proper_t
void build_heap_()
Build the profiles and the heap for states with incoming spontaneous transitions. ...
std::ostream & dot(const Aut &aut, std::ostream &out=std::cout, format fmt={}, bool mathjax=false)
Print an automaton in Graphviz's Dot format.
aut_proper_t get_proper()
std::vector< state_dirty_t > p2d_
void update_heap_(state_proper_t s)
Update the heap for s.
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
std::vector< state_proper_t > d2p_
epsilon_remover_separate(const automaton_t &aut, bool prune=true)
Get ready to eliminate spontaneous transitions.
mutable_automaton< dirty_ctx_t > aut_dirty_t
Spontaneous automaton.
void update_profile_(state_proper_t proper_s)
Update the profile of s.
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
auto copy(const AutIn &input, KeepState keep_state, KeepTrans keep_trans) -> decltype(keep_state(input->null_state()), keep_trans(input->null_transition()), make_fresh_automaton< AutIn, AutOut >(input))
A copy of input keeping only its states that are accepted by keep_state, and transitions accepted by ...
bool state_has_spontaneous_in(state_proper_t proper_s) const
Whether the given state has incoming spontaneous transitions.
value_impl< detail::label_tag > label
label_t_of< aut_proper_t > label_proper_t
int debug_
Debug level. The higher, the more details are reported.
profile_t * profile_(state_proper_t s) const
The profile of state s, or nullptr if it is not to be processed.
auto all_in(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions entering state s.
auto make_proper_context(const context< LabelSet, WeightSet > &ctx) -> proper_context< context< LabelSet, WeightSet >>
From a context, its non-nullable context.
bool prune_
Whether to prune states that become inaccessible.