5 #include <unordered_map>
6 #include <unordered_set>
10 #include <boost/heap/fibonacci_heap.hpp>
14 #include <vcsn/algos/fwd.hh>
36 bool has_one = labelset_t_of<Aut>::has_one()>
41 using weight_t =
typename weightset_t::value_t;
64 ,
ws_(*aut->weightset())
73 aut_dirty_ = make_shared_ptr<aut_dirty_t>(dirty_ctx);
74 aut_proper_ = make_shared_ptr<aut_proper_t>(proper_ctx);
81 pcopier([](state_t) {
return true; },
82 [&aut](transition_t t) {
83 return !aut->labelset()->is_one(aut->label_of(t));
86 dcopier([&aut](state_t s) {
87 return (!
in(aut, s, aut->labelset()->one()).empty()
88 || !
out(aut, s, aut->labelset()->one()).empty());
90 [&aut](transition_t t) {
91 return aut->labelset()->is_one(aut->label_of(t));
95 const auto& dorigins = dcopier.state_map();
96 const auto& porigins = pcopier.state_map();
97 for (
const auto& dp : dorigins)
99 auto pp = porigins.find(dp.first);
100 assert(pp != porigins.end());
101 d2p_[dp.second] = pp->second;
102 p2d_[pp->second] = dp.second;
113 auto dirty_s =
p2d_[proper_s];
121 p->update(in_dirty, in_proper + in_dirty,
122 out_dirty, out_proper + out_dirty);
135 auto proper_s =
d2p_[s];
145 const char* sep =
"";
146 for (
auto i =
todo_.ordered_begin(), end =
todo_.ordered_end();
149 std::cerr << sep << *i;
160 std::cerr <<
"update heap (" << s <<
" : ";
165 todo_.update(i->second);
168 std::cerr <<
") => ";
170 std::cerr << std::endl;
205 using state_weight_t = std::pair<state_dirty_t, weight_t>;
206 auto closure = std::vector<state_weight_t>{};
218 star =
ws_.star(weight);
220 closure.emplace_back(src, weight);
248 for (
auto pair: closure)
250 auto src =
pair.first;
263 for (
auto pair: closure)
265 auto src =
pair.first;
290 std::cerr <<
" -" << removed <<
"+" << added
291 <<
" (-" << removed_ <<
"+" << added_ <<
")";
341 auto neighbors = std::unordered_set<state_proper_t>{};
342 while (!
todo_.empty())
346 std::cerr <<
"Before: ";
348 std::cerr << std::endl;
350 auto p =
todo_.top();
353 std::cerr <<
"Remove: " << p;
356 auto dirty_s =
p2d_[proper_s];
373 neighbors.emplace(
d2p_[n]);
376 neighbors.emplace(
d2p_[n]);
382 for (
auto n: neighbors)
384 for (
auto n: neighbors)
387 std::cerr <<
" #tr: "
394 std::cerr <<
"After: ";
396 std::cerr << std::endl;
404 std::cerr << std::endl;
408 std::cerr <<
"Total transitions -" << removed_
409 <<
"+" << added_ << std::endl;
437 using heap_t = boost::heap::fibonacci_heap<profile_t>;
440 std::unordered_map<state_proper_t, typename heap_t::handle_type>
handles_;
445 std::vector<state_proper_t>
d2p_;
446 std::vector<state_dirty_t>
p2d_;
449 template <Automaton Aut>
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
boost::heap::fibonacci_heap< profile_t > heap_t
Max-heap to decide the order of state-elimination.
pair_automaton< Aut > pair(const Aut &aut, bool keep_initials=false)
profile_t * profile_(state_proper_t s) const
The profile of state s, or nullptr if it is not to be processed.
Container::value_type back(const Container &container)
The last member of this Container.
std::remove_cv_t< Aut > automaton_t
auto make_proper_context(const context< LabelSet, WeightSet > &ctx) -> proper_context< context< LabelSet, WeightSet >>
From a context, its non-nullable context.
state_t_of< aut_dirty_t > state_dirty_t
std::shared_ptr< const detail::weight_base > weight
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
std::shared_ptr< detail::mutable_automaton_impl< Context >> mutable_automaton
std::vector< state_proper_t > d2p_
label_t_of< aut_proper_t > label_proper_t
aut_proper_t aut_proper_
The proper part.
auto all_in(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions entering state s.
fresh_automaton_t_of< Aut, proper_ctx_t > aut_proper_t
Proper automaton.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
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...
labelset_t_of< automaton_t > labelset_t
void build_heap_()
Build the profiles and the heap for states with incoming spontaneous transitions. ...
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
std::vector< state_dirty_t > p2d_
std::shared_ptr< const detail::label_base > label
This is used by some epsilon removal algorithms.
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
std::unordered_map< state_proper_t, typename heap_t::handle_type > handles_
Map: state -> heap-handle.
void update_heap_(state_proper_t s)
Update the heap for s.
fresh_automaton_t_of< automaton_t > aut_proper_t
This class contains the core of the proper algorithm.
epsilon_remover_separate(const automaton_t &aut, bool prune=true)
Get ready to eliminate spontaneous transitions.
state_t_of< aut_proper_t > state_proper_t
std::remove_cv_t< Aut > automaton_t
void remover_on(state_proper_t proper_s)
Wrapper around remover_on() which does a lookup of the dirty state.
void show_heap_() const
Show the heap, for debugging.
std::vector< typename Cont::value_type > make_vector(const Cont &cont)
The content of cont as a vector.
int debug_
Debug level. The higher, the more details are reported.
mutable_automaton< dirty_ctx_t > aut_dirty_t
Spontaneous automaton.
const weightset_t & ws_
Shorthand to the weightset.
typename Aut::element_type::template fresh_automaton_t< Context > fresh_automaton_t_of
Given an automaton type, the type of its copies.
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
bool prune_
Whether to prune states that become inaccessible.
auto transitions(const Aut &aut) -> decltype(all_transitions(aut, is_special_t< Aut >
All the transition indexes between visible states.
aut_proper_t operator()()
weightset_t_of< automaton_t > weightset_t
aut_proper_t get_proper()
Return the proper part. Needed by lazy algorithm.
bool state_has_spontaneous_in(state_proper_t proper_s) const
Whether the given state has incoming spontaneous transitions.
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
void update_profile_(state_proper_t proper_s)
Update the profile of s.
static int debug_level()
Debug level set in the user's environment.
detail::copier< AutIn, AutOut > make_copier(const AutIn &in, AutOut &out, bool safe=true)
Build an automaton copier.
aut_proper_t operator()()
Remove all the states with incoming spontaneous transitions.
epsilon_remover_separate(const automaton_t &aut, bool)
aut_dirty_t aut_dirty_
The sponteanous part.
std::ostream & dot(const Aut &aut, std::ostream &out, format fmt={})
Print an automaton in Graphviz's Dot format.
aut_proper_t get_proper()
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
typename weightset_t::value_t weight_t
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 ...