20 #ifndef LIMI_AUTOMATON_H
21 #define LIMI_AUTOMATON_H
23 #include <unordered_set>
24 #include <unordered_map>
31 #include "internal/hash.h"
48 template <
class State,
class Symbol,
class Implementation>
52 using Symbol_ = Symbol;
68 delete state_printer_;
69 delete symbol_printer_;
72 using State_set = std::unordered_set<State>;
73 using Symbol_set = std::unordered_set<Symbol>;
74 using State_vector = std::vector<State>;
75 using Symbol_vector = std::vector<Symbol>;
160 inline bool is_final_state(
const State& state)
const {
return impl().int_is_final_state(state); }
168 impl().int_initial_states(states);
169 explore_epsilon(states);
178 State_vector initial;
180 states.insert(initial.begin(), initial.end());
201 void successors(
const State& state,
const Symbol& sigma, State_vector& successors1)
const {
202 impl().int_successors(state, sigma, successors1);
203 explore_epsilon(successors1);
213 inline State_set
successors(
const State_set& states,
const Symbol& sigma)
const
227 inline void successors(
const State_set& states,
const Symbol& sigma, State_set& successors1)
const
229 State_vector successors2;
230 for (
const State& state : states) {
233 successors1.insert(successors2.begin(), successors2.end());
244 inline State_vector
successors(
const State& state,
const Symbol& sigma)
const {
256 inline void next_symbols(
const State& state, Symbol_vector& symbols)
const {
257 impl().int_next_symbols(state, symbols);
258 filter_epsilon(symbols);
268 Symbol_vector result;
279 inline void next_symbols(
const State& state, Symbol_set& symbols)
const {
280 Symbol_vector result;
282 symbols.insert(result.begin(), result.end());
293 if (!state_printer_) state_printer_ = impl().int_state_printer();
294 return *state_printer_;
305 if (!symbol_printer_) symbol_printer_ = impl().int_symbol_printer();
306 return *symbol_printer_;
320 inline bool is_epsilon(
const Symbol& symbol)
const {
return impl().int_is_epsilon(symbol); }
329 for (State s:states) {
355 inline Implementation& impl() {
356 return *
static_cast<Implementation*
>(
this);
358 inline const Implementation& impl()
const {
359 return *
static_cast<const Implementation*
>(
this);
362 void filter_epsilon(Symbol_vector& symbols)
const {
363 if (collapse_epsilon) {
364 for (
auto it = symbols.begin(); it!=symbols.end(); ) {
365 if (impl().int_is_epsilon(*it))
366 it = symbols.erase(it);
373 void explore_epsilon(State_vector& states)
const {
374 if (!collapse_epsilon)
return;
376 std::deque<State> frontier;
377 frontier.insert(frontier.begin(), states.begin(), states.end());
379 while(!frontier.empty()) {
380 const State& s = frontier.front();
384 impl().int_next_symbols(s, next_symbols);
391 for (
const Symbol& sy : next_symbols) {
393 impl().int_successors(s, sy, succs);
399 for (
const State& st : succs) {
400 if (seen.insert(st).second) {
401 frontier.push_back(st);
404 frontier.pop_front();
411 #endif // LIMI_AUTOMATON_H
void next_symbols(const State &state, Symbol_vector &symbols) const
Returns possible successor symbols for a state.
Definition: automaton.h:256
The main namespace of the library.
Definition: antichain_algo.h:40
bool int_is_epsilon(const Symbol &symbol) const
Implement Determines if a symbol should be considered an epsilon transition.
State_vector successors(const State &state, const Symbol &sigma) const
Returns successors for a state.
Definition: automaton.h:244
State_vector initial_states() const
Returns a set of initial states.
Definition: automaton.h:188
void initial_states(State_set &states) const
Returns the initial states.
Definition: automaton.h:177
void int_next_symbols(const State &state, Symbol_vector &symbols) const
Implement Returns possible successor symbols for a state.
printer_base< Symbol > * int_symbol_printer() const
Optionally Implement Returns a printer for symbols.
Definition: automaton.h:101
const printer_base< State > & state_printer() const
Returns a printer for states.
Definition: automaton.h:292
bool is_epsilon(const Symbol &symbol) const
Determines if a symbol should be considered an epsilon transition.
Definition: automaton.h:320
bool is_final_state(const State &state) const
This function determines if a specific state is final.
Definition: automaton.h:160
void successors(const State &state, const Symbol &sigma, State_vector &successors1) const
Returns the successor for a specific state.
Definition: automaton.h:201
bool no_epsilon_produced
Indicates that the implementation of next_symbols(const State&,Symbol_set&) const will never produce ...
Definition: automaton.h:350
automaton(bool collapse_epsilon=false, bool no_epsilon_produced=false)
Constructor.
Definition: automaton.h:65
const printer_base< Symbol > & symbol_printer() const
Returns a printer for symbols.
Definition: automaton.h:304
void int_successors(const State &state, const Symbol &sigma, State_vector &successors) const
Implement Returns the successor for a specific state.
The default implementation of Limi::printer_base.
Definition: generics.h:104
void int_initial_states(State_vector &states) const
Implement Returns the initial states.
bool is_final_state(const State_set &states) const
Determines if one of the states is final.
Definition: automaton.h:328
void initial_states(State_vector &states) const
Returns the initial states.
Definition: automaton.h:167
bool collapse_epsilon
If true during exploration of the next state the epsilons will be fully explored. If false then epsil...
Definition: automaton.h:344
bool int_is_final_state(const State &state) const
Implement This function determines if a specific state is final.
Automata need to inherit from this class and implement certain methods.
Definition: automaton.h:49
printer_base< State > * int_state_printer() const
Optionally Implement Returns a printer for states.
Definition: automaton.h:90
void next_symbols(const State &state, Symbol_set &symbols) const
Returns possible successor symbols of a state.
Definition: automaton.h:279
void successors(const State_set &states, const Symbol &sigma, State_set &successors1) const
Returns successors for a set of states.
Definition: automaton.h:227
State_set successors(const State_set &states, const Symbol &sigma) const
Finds successors for a set of states.
Definition: automaton.h:213
Symbol_vector next_symbols(const State &state) const
Returns possible successor symbols of a state.
Definition: automaton.h:267