4 #include <unordered_map>
5 #include <unordered_set>
22 template <Automaton Aut>
33 using set_t = std::vector<state_t>;
37 constexpr
static const char*
me() {
return "minimize-weighted"; }
40 constexpr
static class_t class_invalid = -1;
65 using class_polynomialset_t
77 cps_.add_here(
res[a_->label_of(t)],
78 state_to_class_.at(a_->dst_of(t)),
83 struct signature_hasher
99 struct signature_equal_to
104 if (as.size() != bs.size())
107 using std::begin;
using std::end;
108 for (
auto i = begin(as), i_end = end(as), j = begin(bs);
111 if (!minimizer_.ls_.equal(i->first, j->first)
112 || !minimizer_.cps_.equal(i->second, j->second))
123 signature_hasher, signature_equal_to>;
127 class_to_set_.clear();
128 state_to_class_.clear();
140 if (number == class_invalid)
141 number = num_classes_++;
144 state_to_class_[s] = number;
146 if (number < class_to_set_.size())
147 class_to_set_[number] = std::move(set);
150 assert(number == class_to_set_.size());
151 class_to_set_.emplace_back(std::move(set));
168 return class_to_set_;
177 std::unordered_set<class_t> classes;
179 const auto&
all = a_->all_states();
180 classes.insert(make_class(set_t{std::begin(
all), std::end(
all)}));
183 for (
bool go_on =
true; go_on; )
186 for (
auto i = std::begin(classes), end = std::end(classes);
191 const set_t& c_states = class_to_set_.at(c);
195 auto signature_to_state
197 signature_hasher{*
this},
198 signature_equal_to{*
this}};
199 for (
auto s: c_states)
200 signature_to_state[
signature(s)].emplace_back(s);
202 if (2 <= signature_to_state.size())
206 i = classes.erase(i);
210 for (
auto& p: signature_to_state)
212 bool singleton = p.second.size() == 1;
213 class_t c2 = make_class(std::move(p.second), c);
231 unsigned num_classes_ = 0;
std::vector< state_t > set_t
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
std::map< label_t, class_polynomial_t > signature_t
A signature: for each label, the outgoing class polynomial.
static size_t hash(class_t s)
void build_classes_()
Build the initial classes, and split until fix point.
size_t operator()(const signature_t &sig) const noexcept
state_t_of< automaton_t > state_t
class_to_set_t & classes()
The minimized automaton.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
class_t make_class(set_t &&set, class_t number=class_invalid)
Make a new class with the given set of states.
weightset_t_of< automaton_t > weightset_t
static constexpr const char * me()
automaton_t a_
Input automaton, supplied at construction time.
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
state_to_class_t state_to_class_
weight_t_of< automaton_t > weight_t
Request the unordered_map implementation.
typename class_polynomialset_t::value_t class_polynomial_t
class_to_set_t class_to_set_
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
const minimizer & minimizer_
signature_t signature(state_t s) const
The signature of state s.
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
static bool less(class_t l, class_t r)
label_t_of< automaton_t > label_t
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
std::unordered_map< state_t, class_t > state_to_class_t
Request for the weighted version of an algorithm.
Indentation relative functions.
std::set< std::pair< std::string, std::string >> class_t
A set of label ranges.
Request the set implementation (bool weights).
bool all(Bool &&...values)
Whether all the values evaluate as true.
bool operator()(const signature_t &as, const signature_t &bs) const noexcept
labelset_t_of< automaton_t > labelset_t
void hash_combine_hash(std::size_t &seed, size_t h)
static bool equal(class_t l, class_t r)
Signature of a function call.
std::unordered_map< signature_t, set_t, signature_hasher, signature_equal_to > signature_multimap
Cluster states per signature.
polynomialset< context< classset, weightset_t >> class_polynomialset_t
The output of a given letter from a given state, keeping into account classes and weights...
std::vector< set_t > class_to_set_t
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
const minimizer & minimizer_
Provide a variadic mul on top of a binary mul(), and one().
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
bool is_trim(const Aut &a)
Whether all its states are useful.
weightset_mixin< detail::r_impl > r