diff --git a/alib2aux/src/convert/LatexTable.cpp b/alib2aux/src/convert/LatexTable.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8d39c1642716b1838fdfb0603514d158bf0c1e8 --- /dev/null +++ b/alib2aux/src/convert/LatexTable.cpp @@ -0,0 +1,15 @@ +/* + * LatexTable.cpp + * + * Created on: 6. 11. 2017 + * Author: Jan Travnicek + */ + +#include "LatexTable.h" +#include <registration/AlgoRegistration.hpp> + +namespace { + +auto DFA = registration::AbstractRegister < convert::LatexTable, std::string, const automaton::DFA < > & > ( convert::LatexTable::convert ); + +} /* namespace */ diff --git a/alib2aux/src/convert/LatexTable.h b/alib2aux/src/convert/LatexTable.h new file mode 100644 index 0000000000000000000000000000000000000000..b03d227e3ae7703597bd2849e4734953eb0d835d --- /dev/null +++ b/alib2aux/src/convert/LatexTable.h @@ -0,0 +1,88 @@ +/* + * LatexTable.h + * + * Created on: 6. 11. 2017 + * Author: Jan Travnicek + */ + +#ifndef LATEX_TABLE_H_ +#define LATEX_TABLE_H_ + +#include <ostream> +#include <set> +#include <map> +#include <list> +#include <utility> +#include <vector> + +#include <exception/CommonException.h> +#include <string/String.h> + +#include <automaton/FSM/DFA.h> + +#include <factory/StringDataFactory.hpp> + +#include "common/converterCommon.hpp" + +#include <regexp/string/UnboundedRegExp.h> + +namespace convert { + +class LatexTable { +public: + template < class SymbolType, class StateType > + static void convert(std::ostream& out, const automaton::DFA < SymbolType, StateType > & a); + + template < class T > + static std::string convert ( const T & automaton ) { + std::stringstream ss; + convert ( ss, automaton ); + return ss.str ( ); + } +}; + +template < class SymbolType, class StateType > +void LatexTable::convert(std::ostream& out, const automaton::DFA < SymbolType, StateType > & a) { + size_t symbols = a.getInputAlphabet ( ).size ( ); + out << "\\begin{tabular}{|lr||"; + for ( size_t i = 0; i < symbols; ++ i ) + out << "c|"; + out << "l}" << std::endl; + out << "\\hline" << std::endl; + out << " & & "; + for ( const SymbolType & symbol : a.getInputAlphabet ( ) ) + out << replace ( alib::StringDataFactory::toString ( symbol ), "\"", "\\\"" ) << " & "; + out << "\\hspace*{14cm}\\" << std::endl; + out << "\\hline" << std::endl; + + for ( const StateType & source : a.getStates ( ) ) { + out << "\\hline" << std::endl; + if ( a.getFinalStates ( ).count ( source ) && a.getInitialState ( ) == source ) + out << "$\\leftrightarrow$ & "; + else if ( a.getFinalStates ( ).count ( source ) ) + out << "$\\leftarrow$ & "; + else if ( a.getInitialState ( ) == source ) + out << "$\\rightarrow$ & "; + else + out << " & "; + + out << replace ( alib::StringDataFactory::toString ( source ), "\"", "\\\"" ) << " & "; + + for ( const SymbolType & symbol : a.getInputAlphabet ( ) ) { + auto transition = a.getTransitions ( ).find ( ext::make_pair ( source, symbol ) ); + if ( transition == a.getTransitions ( ).end ( ) ) + out << "- & "; + else + out << replace ( alib::StringDataFactory::toString ( transition->second ), "\"", "\\\"" ) << " & "; + } + + out << "\\\\" << std::endl; + } + + out << "\\hline" << std::endl; + out << "\\end{tabular}" << std::endl; +} + +} /* namespace convert */ + +#endif /* LATEX_TABLE_H_ */