Skip to content
Snippets Groups Projects
Commit 684b86f8 authored by Jan Travnicek's avatar Jan Travnicek
Browse files

use require in compaction algo and implement it for NFa

parent fda815ea
No related branches found
No related tags found
No related merge requests found
Pipeline #35865 passed
......@@ -31,6 +31,7 @@
#include <automaton/FSM/CompactNFA.h>
#include <automaton/FSM/NFA.h>
#include <automaton/FSM/DFA.h>
#include <automaton/Automaton.h>
 
namespace automaton {
 
......@@ -41,6 +42,9 @@ namespace transform {
* The compact automaton allows to use strings in the transition function. The transitions are compacted.
*/
class Compaction {
template < class T >
using CompactAutomaton = typename std::conditional < isDFA < T >, automaton::CompactDFA < automaton::SymbolTypeOfAutomaton < T >, automaton::StateTypeOfAutomaton < T > >, automaton::CompactNFA < automaton::SymbolTypeOfAutomaton < T >, automaton::StateTypeOfAutomaton < T > > >::type;
public:
/**
* Compaction of a finite automaton.
......@@ -49,41 +53,27 @@ public:
* @param automaton automaton to compact
* @return compact nondeterministic FA equivalent to @p automaton
*/
template < class SymbolType, class StateType >
static automaton::CompactDFA < SymbolType, StateType > convert( const automaton::DFA < SymbolType, StateType > & automaton);
/**
* @overload
*/
template < class SymbolType, class StateType >
static automaton::CompactNFA < SymbolType, StateType > convert( const automaton::NFA < SymbolType, StateType > & automaton);
/**
* @overload
*/
template < class SymbolType, class StateType >
static automaton::CompactDFA < SymbolType, StateType > convert( const automaton::CompactDFA < SymbolType, StateType > & automaton);
template < class T >
static ext::require < isDFA < T > || isNFA < T >, Compaction::CompactAutomaton < T > > convert ( const T & automaton);
 
/**
* @overload
*/
template < class SymbolType, class StateType >
static automaton::CompactNFA < SymbolType, StateType > convert( const automaton::CompactNFA < SymbolType, StateType > & automaton);
template < class T >
static ext::require < isCompactDFA < T > || isCompactNFA < T >, T > convert ( const T & automaton );
};
 
template < class SymbolType, class StateType >
automaton::CompactDFA < SymbolType, StateType > Compaction::convert(const automaton::CompactDFA < SymbolType, StateType > & automaton) {
template < class T >
ext::require < isCompactDFA < T > || isCompactNFA < T >, T > Compaction::convert ( const T & automaton ) {
return automaton;
}
 
template < class SymbolType, class StateType >
automaton::CompactNFA < SymbolType, StateType > Compaction::convert(const automaton::CompactNFA < SymbolType, StateType > & automaton) {
return automaton;
}
template < class T >
ext::require < isDFA < T > || isNFA < T >, Compaction::CompactAutomaton < T > > Compaction::convert ( const T & automaton) {
using SymbolType = automaton::SymbolTypeOfAutomaton < T >;
using StateType = automaton::StateTypeOfAutomaton < T >;
 
template < class SymbolType, class StateType >
automaton::CompactDFA < SymbolType, StateType > Compaction::convert(const automaton::DFA < SymbolType, StateType > & automaton) {
automaton::CompactDFA < SymbolType, StateType > res(automaton.getInitialState());
CompactAutomaton < T > res(automaton.getInitialState());
res.setInputAlphabet(automaton.getInputAlphabet());
 
ext::set < ext::tuple < StateType, StateType, SymbolType > > visited;
......@@ -103,7 +93,7 @@ automaton::CompactDFA < SymbolType, StateType > Compaction::convert(const automa
 
ext::vector < SymbolType > path { symbol };
 
ext::iterator_range < typename ext::map < ext::pair < StateType, SymbolType >, StateType >::const_iterator > transitions;
decltype ( automaton.getTransitionsFromState ( q ) ) transitions;
// only 1 child and nonfinal
while((transitions = automaton.getTransitionsFromState(q)).size() == 1 && automaton.getFinalStates().count(q) == 0) {
const std::pair<ext::pair<StateType, SymbolType>, StateType>& transition = * transitions.begin();
......@@ -128,13 +118,6 @@ automaton::CompactDFA < SymbolType, StateType > Compaction::convert(const automa
return res;
}
 
template < class SymbolType, class StateType >
automaton::CompactNFA < SymbolType, StateType > Compaction::convert(const automaton::NFA < SymbolType, StateType > & automaton) {
automaton::CompactNFA < SymbolType, StateType > res(automaton.getInitialState());
// TODO
return res;
}
} /* namespace transform */
 
} /* namespace automaton */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment