Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
Compaction.h
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <stack>
27#include <alib/tuple>
28
31#include <automaton/FSM/NFA.h>
32#include <automaton/FSM/DFA.h>
33
34namespace automaton {
35
36namespace transform {
37
43 template < class T >
45
46public:
54 template < class T >
55 requires isDFA < T > || isNFA < T >
56 static Compaction::CompactAutomaton < T > convert ( const T & automaton);
57
61 template < class T >
62 requires isCompactDFA < T > || isCompactNFA < T >
63 static T convert ( const T & automaton );
64};
65
66template < class T >
67requires isCompactDFA < T > || isCompactNFA < T >
69 return automaton;
70}
71
72template < class T >
73requires isDFA < T > || isNFA < T >
74Compaction::CompactAutomaton < T > Compaction::convert ( const T & automaton) {
75 using SymbolType = typename T::SymbolType;
76 using StateType = typename T::StateType;
77
78 CompactAutomaton < T > res(automaton.getInitialState());
79 res.setInputAlphabet(automaton.getInputAlphabet());
80
82
83 std::stack<ext::tuple<StateType, StateType, SymbolType>> stack; // state x lastFork x symbol we used to go to that state
84 for(const auto& transition: automaton.getTransitionsFromState(automaton.getInitialState()))
85 stack.push(ext::make_tuple(transition.second, automaton.getInitialState(), transition.first.second));
86
87 if(automaton.getFinalStates().count(automaton.getInitialState()))
88 res.addFinalState(automaton.getInitialState());
89
90 while(!stack.empty()) {
91 StateType q = std::move(std::get<0>(stack.top()));
92 StateType lastFork = std::move(std::get<1>(stack.top()));
93 SymbolType symbol = std::move(std::get<2>(stack.top()));
94 stack.pop();
95
96 ext::vector < SymbolType > path { std::move ( symbol ) };
97
98 decltype ( automaton.getTransitionsFromState ( q ) ) transitions;
99 // only 1 child and nonfinal
100 while((transitions = automaton.getTransitionsFromState(q)).size() == 1 && automaton.getFinalStates().count(q) == 0) {
101 const std::pair<ext::pair<StateType, SymbolType>, StateType>& transition = * transitions.begin();
102 path.push_back(transition.first.second);
103 q = transition.second;
104 }
105
106 for(const std::pair<const ext::pair<StateType, SymbolType>, StateType>& transition : transitions )
107 if(visited.insert(ext::make_tuple(transition.second, q, transition.first.second)).second)
108 stack.push(ext::make_tuple(transition.second, q, transition.first.second));
109
110 // fork or final state
111 res.addState(q);
112
113 if(automaton.getFinalStates().count(q))
114 res.addFinalState(q);
115
116 res.addTransition ( std::move ( lastFork ), std::move ( path ), std::move ( q ) );
117
118 }
119
120 return res;
121}
122
123} /* namespace transform */
124
125} /* namespace automaton */
126
Compact deterministic finite automaton. Accepts regular languages. The automaton has a list of symbol...
Definition: CompactDFA.h:73
Compact nondeterministic finite automaton. Accepts regular languages. The automaton has a list of sym...
Definition: CompactNFA.h:78
Definition: Compaction.h:42
static Compaction::CompactAutomaton< T > convert(const T &automaton)
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Definition: set.hpp:44
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: vector.hpp:125
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
q
Definition: SingleInitialStateEpsilonTransition.h:85
ext::set< ext::tuple< StateType, StateType, SymbolType > > visited
Definition: Compaction.h:81
typename T::StateType StateType
Definition: Compaction.h:76
return res
Definition: AutomataConcatenation.h:102
std::stack< ext::tuple< StateType, StateType, SymbolType > > stack
Definition: Compaction.h:83
Definition: ToGrammar.h:31
ContainerType< ResType > transform(const ContainerType< InType, Ts ... > &in, Callback transform)
In container tranformation of all elements according to the tranform.
Definition: algorithm.hpp:150
constexpr auto make_tuple(Elements &&... args)
Helper of extended tuple construction. The tuple is constructed from values pack, types are deduced.
Definition: tuple.hpp:203