5 #include <unordered_map>
6 #include <unordered_set>
10 #include <boost/lexical_cast.hpp>
11 #include <boost/heap/fibonacci_heap.hpp>
15 #include <vcsn/algos/fwd.hh>
37 bool has_one = labelset_t_of<Aut>::has_one()>
43 using weight_t =
typename weightset_t::value_t;
64 auto res = make_shared_ptr<aut_proper_t>(proper_ctx);
124 for (
auto s:
aut_->states())
129 auto ins =
in(
aut_, s).size();
133 {s, in_sp, ins, out_sp,
out});
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 <<
") => ";
166 std::cerr << std::endl;
203 using state_weight_t = std::pair<state_t, weight_t>;
204 std::vector<state_weight_t> closure;
210 star =
ws_.star(weight);
212 closure.emplace_back(src, weight);
214 aut_->del_transition(t);
237 aut_->set_weight(t, blow);
241 for (
auto pair: closure)
245 aut_->add_transition(src, dst, label, w);
249 unsigned added =
all_out(
aut_, s).size() * closure.size();
250 unsigned removed = transitions.size();
263 std::cerr <<
" -" << removed <<
"+" << added
264 <<
" (-" << removed_ <<
"+" << added_ <<
")";
290 std::unordered_set<state_t> neighbors;
291 while (!
todo_.empty())
295 std::cerr <<
"Before: ";
297 std::cerr << std::endl;
299 auto p =
todo_.top();
302 std::cerr <<
"Remove: " << p;
311 neighbors.emplace(n);
317 neighbors.emplace(n);
323 for (
auto n: neighbors)
325 for (
auto n: neighbors)
328 std::cerr <<
" #tr: "
330 <<
"/" <<
aut_->num_transitions() << std::endl;
333 std::cerr <<
"After: ";
335 std::cerr << std::endl;
338 dot(
aut_, std::cerr) << std::endl;
340 std::cerr << std::endl;
344 std::cerr <<
"Total transitions -" << removed_
345 <<
"+" << added_ << std::endl;
357 res +=
aut_->labelset()->is_one(
aut_->label_of(t));
372 using heap_t = boost::heap::fibonacci_heap<epsilon_profile<state_t>>;
375 std::unordered_map<state_t, typename heap_t::handle_type>
handles_;
386 template <Automaton Aut>
389 using automaton_t = Aut;
bool is_proper(const Aut &aut)
Test whether an automaton is proper.
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
pair_automaton< Aut > pair(const Aut &aut, bool keep_initials=false)
This class contains the core of the proper algorithm.
void update_profile_(state_t s)
The core of the (backward) epsilon-removal.
auto make_proper_context(const context< LabelSet, WeightSet > &ctx) -> proper_context< context< LabelSet, WeightSet >>
From a context, its non-nullable context.
std::shared_ptr< const detail::weight_base > weight
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
void show_heap_() const
Show the heap, for debugging.
void copy_into(const AutIn &in, AutOut &out, KeepState keep_state, KeepTrans keep_trans)
Copy selected states and transitions of an automaton.
auto all_in(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions entering state s.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
label_t empty_word_
Shorthand to the empty word.
transition_t_of< automaton_t > transition_t
std::unordered_map< state_t, typename heap_t::handle_type > handles_
Map: state -> heap-handle.
void in_situ_remover()
Nothing to do to remove the transitions in place.
label_t_of< automaton_t > label_t
labelset_t_of< automaton_t > labelset_t
epsilon_remover(automaton_t &aut, bool=true)
epsilon_profile< state_t > * profile(state_t s)
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
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.
automaton_t aut_
The automaton we work on.
fresh_automaton_t_of< automaton_t > aut_proper_t
std::vector< typename Cont::value_type > make_vector(const Cont &cont)
The content of cont as a vector.
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()()
Just a copy of the automata in the proper context, since there aren't any transitions to remove...
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
auto transitions(const Aut &aut) -> decltype(all_transitions(aut, is_special_t< Aut >
All the transition indexes between visible states.
void update_heap_(state_t s)
Update the heap for s.
int debug_
Debug level. The higher, the more details are reported.
weightset_t_of< automaton_t > weightset_t
boost::heap::fibonacci_heap< epsilon_profile< state_t >> heap_t
Max-heap to decide the order of state-elimination.
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
void build_heap_()
Build the profiles and the heap for states with incoming spontaneous transitions. ...
fresh_automaton_t_of< automaton_t, detail::proper_context< context_t_of< automaton_t >>> aut_proper_t
static int debug_level()
Debug level set in the user's environment.
epsilon_remover(automaton_t &aut, bool prune=true)
Get ready to eliminate spontaneous transitions.
typename weightset_t::value_t weight_t
const weightset_t & ws_
Shorthand to the weightset.
std::ostream & dot(const Aut &aut, std::ostream &out, format fmt={})
Print an automaton in Graphviz's Dot format.
size_t num_spontaneous_transitions_() const
The number of spontaneous transitions in aut_.
void in_situ_remover_(state_t s)
For each state (s), for each incoming epsilon-transitions (t), if (t) is a loop, the star of its weig...
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
state_t_of< automaton_t > state_t
aut_proper_t operator()()
bool prune_
Whether to prune states that become inaccessible.
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
void in_situ_remover_()
Remove all the states with incoming spontaneous transitions.
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 ...