Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
RHDPDACommon.h
Go to the documentation of this file.
1
6#pragma once
7
10#include <alib/set>
11#include <alib/map>
14#include <queue>
15
16namespace automaton {
17
18namespace determinize {
19
20template < class StateType >
23 for(const ext::pair < StateType, StateType > & entry : localOperation) {
24 id.insert(entry.second);
25 }
26 return id;
27}
28
29template < class StateType >
32 for(const StateType& state : states) {
33 id.insert(ext::make_pair(state, state));
34 }
35 return id;
36}
37
38template < class InputSymbolType, class StateType, class N >
39ext::set < ext::pair < StateType, StateType > > retInitial ( const ext::set < ext::pair < StateType, StateType > > & S, const InputSymbolType & input, const N & nondeterministic ) {
41
42 for ( const auto & entry : S ) {
43 const StateType & q = entry.first;
44 const StateType & q2 = entry.second;
45
46 for ( const auto & transition : nondeterministic.getReturnTransitions ( ).equal_range ( ext::make_tuple ( q2, input, nondeterministic.getBottomOfTheStackSymbol ( ) ) ) ) {
47 const StateType & q1 = transition.second;
48
49 S1.insert ( ext::make_pair ( q, q1 ) );
50 }
51 }
52
53 return S1;
54}
55
56template < class InputSymbolType, class DeterministicPushdownStoreSymbolType, class StateType, class N >
57ext::set < ext::pair < StateType, StateType > > ret ( const ext::set < ext::pair < StateType, StateType > > & S, const DeterministicPushdownStoreSymbolType & pdaSymbol, const InputSymbolType & input, const N & nondeterministic ) {
59
60 for(const auto& transition : nondeterministic.getCallTransitions()) {
61 if(pdaSymbol.second != std::get<1>(transition.first)) continue;
62
63 const StateType & q = std::get<0>(transition.first);
64 const StateType & q1 = transition.second.first;
65 const auto & gamma = transition.second.second;
66
67 for(const auto& entry : S.equal_range ( ext::slice_comp ( q1 ) ) ) {
68 const StateType& q2 = entry.second;
69
70 for ( const auto & returnTransition : nondeterministic.getReturnTransitions ( ).equal_range ( ext::make_tuple ( q2, input, gamma ) ) ) {
71 const StateType & qI = returnTransition.second;
72 update.insert(ext::make_pair(q, qI));
73 }
74 }
75 }
76
77 const ext::set<ext::pair<StateType, StateType>>& S1 = pdaSymbol.first;
78
80 for(const auto& entry : S1) {
81 const StateType& q = entry.first;
82 const StateType& q3 = entry.second;
83 for(const auto& entry2 : update.equal_range ( ext::slice_comp ( q3 ) ) ) {
84 const StateType& qI = entry2.second;
85
86 S2.insert(ext::make_pair(q, qI));
87 }
88 }
89
90 return S2;
91}
92
93template < class InputSymbolType, class StateType, class N >
94ext::set < ext::pair < StateType, StateType > > call ( const ext::set < ext::pair < StateType, StateType > > & S, const InputSymbolType & input, const N & nondeterministic ) {
96
97 for(const StateType& q : retrieveDSubSet ( S ) ) {
98 for ( const auto & to : nondeterministic.getCallTransitions ( ).equal_range ( ext::make_pair ( q, input ) ) ) {
99 const StateType& q1 = to.second.first;
100
101 R1.insert(q1);
102 }
103 }
104
105 return createIdentity ( std::move ( R1 ) );
106}
107
108template < class InputSymbolType, class StateType, class N >
109ext::set < ext::pair < StateType, StateType > > local ( const ext::set < ext::pair < StateType, StateType > > & S, const InputSymbolType & input, const N & nondeterministic ) {
111
112 for ( const auto & entry : S ) {
113 const StateType & q = entry.first;
114 const StateType & q2 = entry.second;
115
116 for ( const auto & transition : nondeterministic.getLocalTransitions ( ).equal_range ( ext::make_pair ( q2, input ) ) ) {
117 const StateType & q1 = transition.second;
118 S1.insert(ext::make_pair(q, q1));
119 }
120 }
121
122 return S1;
123}
124
125template < class DeterministicStateType, class DeterministicPushdownStoreSymbolType >
126void updateTopSymbols ( ext::map < DeterministicStateType, ext::set < DeterministicStateType > > & localClosure, ext::map < DeterministicStateType, ext::set < DeterministicPushdownStoreSymbolType > > & topSymbols, std::queue < ext::pair<DeterministicStateType, DeterministicPushdownStoreSymbolType> > & dirtyStateSymbols, const DeterministicStateType & state, const ext::set < DeterministicPushdownStoreSymbolType > & newSymbols ) {
128 std::set_difference ( newSymbols.begin ( ), newSymbols.end ( ), topSymbols [ state ].begin ( ), topSymbols [ state ].end ( ), std::inserter ( missingSymbols, missingSymbols.end ( ) ) );
129
130 if ( missingSymbols.empty ( ) )
131 return;
132
133 topSymbols [ state ].insert ( missingSymbols.begin ( ), missingSymbols.end ( ) );
134
135 for ( const DeterministicStateType & adv : localClosure [ state ] )
136 updateTopSymbols ( localClosure, topSymbols, dirtyStateSymbols, adv, missingSymbols );
137
138 for ( DeterministicPushdownStoreSymbolType && symbol : ext::make_mover ( missingSymbols ) )
139 dirtyStateSymbols.push ( ext::make_pair ( state, std::move ( symbol ) ) );
140}
141
142} /* namespace determinize */
143
144} /* namespace automaton */
145
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
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
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: set.hpp:99
auto end() &
Inherited behavior of end for non-const instance.
Definition: set.hpp:129
auto equal_range(K &&key) const &
Make range of elements with key equal to the key.
Definition: set.hpp:200
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
void updateTopSymbols(ext::map< DeterministicStateType, ext::set< DeterministicStateType > > &localClosure, ext::map< DeterministicStateType, ext::set< DeterministicPushdownStoreSymbolType > > &topSymbols, std::queue< ext::pair< DeterministicStateType, DeterministicPushdownStoreSymbolType > > &dirtyStateSymbols, const DeterministicStateType &state, const ext::set< DeterministicPushdownStoreSymbolType > &newSymbols)
Definition: RHDPDACommon.h:126
ext::set< ext::pair< StateType, StateType > > call(const ext::set< ext::pair< StateType, StateType > > &S, const InputSymbolType &input, const N &nondeterministic)
Definition: RHDPDACommon.h:94
ext::set< ext::pair< StateType, StateType > > ret(const ext::set< ext::pair< StateType, StateType > > &S, const DeterministicPushdownStoreSymbolType &pdaSymbol, const InputSymbolType &input, const N &nondeterministic)
Definition: RHDPDACommon.h:57
ext::set< ext::pair< StateType, StateType > > createIdentity(const ext::set< StateType > &states)
Definition: RHDPDACommon.h:30
ext::set< StateType > retrieveDSubSet(const ext::set< ext::pair< StateType, StateType > > &localOperation)
Definition: RHDPDACommon.h:21
ext::set< ext::pair< StateType, StateType > > retInitial(const ext::set< ext::pair< StateType, StateType > > &S, const InputSymbolType &input, const N &nondeterministic)
Definition: RHDPDACommon.h:39
q
Definition: SingleInitialStateEpsilonTransition.h:85
Definition: ToGrammar.h:31
reference_mover< T > make_mover(T &param)
Move adaptor construction function specialized to lvalue reference parameter.
Definition: iterator.hpp:468
SliceComp< Ts ... > slice_comp(const Ts &... inst)
Definition: functional.hpp:95
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
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79