Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
AutomataIntersectionCartesianProduct.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 <ext/foreach>
27
29#include <automaton/TA/NFTA.h>
30
31namespace automaton {
32
33namespace transform {
34
41public:
51 template < class SymbolType, class StateType1, class StateType2 >
53
57 template < class SymbolType, class StateType1, class StateType2 >
59
63 template < class SymbolType, class StateType1, class StateType2 >
65};
66
67template < class SymbolType, class StateType1, class StateType2 >
69 ext::pair < StateType1, StateType2 > q0 ( first.getInitialState ( ), second.getInitialState ( ) );
71
72 for(const auto& a : first.getInputAlphabet())
73 res.addInputSymbol(a);
74 for(const auto& a : second.getInputAlphabet())
75 res.addInputSymbol(a);
76
77 for(const auto& p : first.getStates())
78 for(const auto& q : second.getStates())
79 res.addState ( ext::make_pair ( p, q ) );
80
81 for(const auto& p : first.getFinalStates())
82 for(const auto& q : second.getFinalStates())
83 res.addFinalState ( ext::make_pair ( p, q ) );
84
85 for(const ext::pair < StateType1, StateType2 > & state : res.getStates ( ) )
86 for(const auto & tp : first.getTransitionsFromState ( state.first ) )
87 for(const auto & tq : second.getTransitionsFromState ( state.second ) )
88 if(tp.first.second == tq.first.second)
89 res.addTransition ( state, tp.first.second, ext::make_pair ( tp.second, tq.second ) );
90
91 return res;
92}
93
94template < class SymbolType, class StateType1, class StateType2 >
96 ext::pair < StateType1, StateType2 > q0 ( first.getInitialState ( ), second.getInitialState ( ) );
98
99 for(const auto& a : first.getInputAlphabet())
100 res.addInputSymbol(a);
101 for(const auto& a : second.getInputAlphabet())
102 res.addInputSymbol(a);
103
104 for(const auto& p : first.getStates())
105 for(const auto& q : second.getStates())
106 res.addState ( ext::make_pair ( p, q ) );
107
108 for(const auto& p : first.getFinalStates())
109 for(const auto& q : second.getFinalStates())
110 res.addFinalState ( ext::make_pair ( p, q ) );
111
112 for(const ext::pair < StateType1, StateType2 > & state : res.getStates ( ) )
113 for(const auto & tp : first.getTransitionsFromState ( state.first ) )
114 for(const auto & tq : second.getTransitionsFromState ( state.second ) )
115 if(tp.first.second == tq.first.second)
116 res.addTransition ( state, tp.first.second, ext::make_pair ( tp.second, tq.second ) );
117
118 return res;
119}
120
121template < class SymbolType, class StateType1, class StateType2 >
124
125 for(const auto& a : first.getInputAlphabet())
126 res.addInputSymbol(a);
127 for(const auto& a : second.getInputAlphabet())
128 res.addInputSymbol(a);
129
130 for(const auto& p : first.getStates())
131 for(const auto& q : second.getStates())
132 res.addState ( ext::make_pair ( p, q ) );
133
134 for(const auto& p : first.getFinalStates())
135 for(const auto& q : second.getFinalStates())
136 res.addFinalState ( ext::make_pair ( p, q ) );
137
138 for(const auto & tp : first.getTransitions ( ) )
139 for(const auto & tq : second.getTransitions ( ) )
140 if(tp.first.first == tq.first.first) {
142 for ( ext::tuple < const StateType1 &, const StateType2 & > singleSourceState : ext::make_tuple_foreach ( tp.first.second, tq.first.second ) )
143 source.push_back ( ext::make_pair ( std::get < 0 > ( singleSourceState ), std::get < 1 > ( singleSourceState ) ) );
144
145 res.addTransition ( tp.first.first, source, ext::make_pair ( tp.second, tq.second ) );
146 }
147
148 return res;
149}
150
151} /* namespace transform */
152
153} /* namespace automaton */
154
Deterministic finite automaton. Accepts regular languages.
Definition: DFA.h:71
const ext::set< StateType > & getFinalStates() const &
Definition: DFA.h:183
const StateType & getInitialState() const &
Definition: DFA.h:105
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: DFA.h:232
const ext::set< StateType > & getStates() const &
Definition: DFA.h:134
ext::iterator_range< typename ext::map< ext::pair< StateType, SymbolType >, StateType >::const_iterator > getTransitionsFromState(const StateType &from) const
Definition: DFA.h:483
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: DFTA.h:74
const ext::map< ext::pair< common::ranked_symbol< SymbolType >, ext::vector< StateType > >, StateType > & getTransitions() const &
Definition: DFTA.h:289
const ext::set< common::ranked_symbol< SymbolType > > & getInputAlphabet() const &
Definition: DFTA.h:203
const ext::set< StateType > & getFinalStates() const &
Definition: DFTA.h:154
const ext::set< StateType > & getStates() const &
Definition: DFTA.h:105
Nondeterministic finite automaton. Accepts regular languages.
Definition: NFA.h:66
const ext::set< StateType > & getStates() const &
Definition: NFA.h:136
const StateType & getInitialState() const &
Definition: NFA.h:107
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: NFA.h:234
const ext::set< StateType > & getFinalStates() const &
Definition: NFA.h:185
auto getTransitionsFromState(const StateType &from) const
Definition: NFA.h:494
Definition: AutomataIntersectionCartesianProduct.h:40
static automaton::NFA< SymbolType, ext::pair< StateType1, StateType2 > > intersection(const automaton::NFA< SymbolType, StateType1 > &first, const automaton::NFA< SymbolType, StateType2 > &second)
Definition: AutomataIntersectionCartesianProduct.h:95
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Class extending the tuple class from the standard library. Original reason is to allow printing of th...
Definition: tuple.hpp:42
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
q
Definition: SingleInitialStateEpsilonTransition.h:85
StateType q0
Definition: SingleInitialState.h:96
return res
Definition: AutomataConcatenation.h:102
t first second
Definition: AutomataConcatenation.h:78
Definition: ToGrammar.h:31
const_tuple_foreach< Types ... > make_tuple_foreach(const Types &... args)
Function construction of foreach tuple pack helper.
Definition: foreach.hpp:280
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_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79