3 #include <unordered_map>
27 template <Automaton Aut>
31 "minimize: moore: requires Boolean weights");
33 "minimize: moore: requires free labelset");
47 using set_t = std::vector<state_t>;
52 constexpr
static const char*
me() {
return "minimize-moore"; }
55 constexpr
static class_t class_invalid = -1;
56 unsigned num_classes_ = 0;
73 class_to_set_.clear();
74 state_to_class_.clear();
76 transition_map_.clear();
82 assert(!
set.empty());
84 if (number == class_invalid)
85 number = num_classes_++;
88 state_to_class_[s] = number;
90 if (number < class_to_set_.size())
91 class_to_set_[number] = std::move(set);
94 assert(number == class_to_set_.size());
95 class_to_set_.emplace_back(std::move(set));
105 const auto&
map = transition_map_[s];
106 auto dst =
map.find(l);
107 if (dst == std::end(
map))
108 return class_invalid;
110 return state_to_class_[dst->second.dst];
117 , gs_(a_->labelset()->generators())
130 return class_to_set_;
139 make_class({a_->pre()});
140 make_class({a_->post()});
142 set_t nonfinals, finals;
143 for (
auto s : a_->states())
145 finals.emplace_back(s);
147 nonfinals.emplace_back(s);
148 if (! nonfinals.empty())
149 make_class(std::move(nonfinals));
150 if (! finals.empty())
151 make_class(std::move(finals));
158 for (
class_t c = 0; c < num_classes_; ++c)
160 const set_t& c_states = class_to_set_[c];
166 for (
auto s : c_states)
168 auto c2 = out_class(s, l);
169 target_class_to_c_states[c2].emplace_back(s);
175 if (2 <= target_class_to_c_states.size())
179 for (
auto& p : target_class_to_c_states)
181 make_class(std::move(p.second), num);
198 template <Automaton Aut>
200 std::enable_if_t<!is_free_boolean<Aut>(), quotient_t<Aut>>
203 raise(
"minimize: invalid algorithm"
204 " (non-Boolean or non-free labelset):",
auto map(const std::tuple< Ts... > &ts, Fun f) -> decltype(map_tuple_(f, ts, make_index_sequence< sizeof...(Ts)>()))
Map a function on a tuple, return tuple of the results.
std::vector< set_t > class_to_set_t
std::unordered_map< state_t, class_t > state_to_class_t
minimizer(const Aut &a)
Build the minimizer. Computes the classes.
ATTRIBUTE_NORETURN std::enable_if_t<!is_free_boolean< Aut >), Aut > minimize(const Aut &, brzozowski_tag)
Handling of errors for dyn::minimize.
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Request the set implementation (bool weights).
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
state_t_of< automaton_t > state_t
bool is_trim(const Aut &a)
Whether all its states are useful.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Provide a variadic mul on top of a binary mul(), and one().
Request for Moore implementation of minimize (B and free).
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
automaton_t a_
Input automaton, supplied at construction time.
bool is_deterministic(const Aut &aut, state_t_of< Aut > s)
Whether state s is deterministic in aut.
const labelset_t_of< Aut >::genset_t gs_
The generators.
class_t make_class(set_t &&set, class_t number=class_invalid)
Make a new class with the given set of states.
label_t_of< automaton_t > label_t
static constexpr const char * me()
std::vector< state_t > set_t
std::unordered_map< class_t, set_t > target_class_to_states_t
state_to_class_t state_to_class_
class_to_set_t class_to_set_
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
class_to_set_t & classes()
The partition, as a list of classes.
std::vector< class_t > classes_t
class_t out_class(state_t s, label_t l)
The destination class of s with l in a.
transition_map_t transition_map_
void build_classes_()
Build the initial classes, and split until fix point.