8#include <ext/algorithm>
11#include <string/LinearString.h>
46 template <
class SymbolType >
47 struct graphStructuredStack {
54 graphStructuredStack ( std::shared_ptr < Element > value ) : m_value (
std::move ( value ) ) { }
56 std::shared_ptr < Element > m_value;
58 static std::strong_ordering compareElements ( std::shared_ptr < Element > first, std::shared_ptr < Element >
second ) {
59 if ( first ==
nullptr ||
second ==
nullptr )
62 std::strong_ordering
res = first->m_data <=>
second->m_data;
66 return compareElements ( first->m_parent,
second->m_parent );
69 std::strong_ordering operator <=> (
const graphStructuredStack < SymbolType > & other )
const {
70 return compareElements ( m_value, other.m_value );
74 template <
class Type >
76 std::shared_ptr < typename graphStructuredStack < Type >::Element > stackNode =
stack.m_value;
79 finalContent.push_front ( stackNode->m_data );
80 stackNode = stackNode->m_parent;
101 template <
class SymbolType,
class StateType >
120 template <
class SymbolType,
class StateType >
123 template <
class SymbolType,
class StateType >
142 template <
class SymbolType,
class StateType >
155 template <
class SymbolType >
158 template <
class Type >
159 static bool canPop ( graphStructuredStack < Type > pushdownStore,
const ext::vector < Type > & pop );
176 template <
class SymbolType,
class StateType >
193 template <
class SymbolType,
class StateType >
210 template <
class SymbolType,
class StateType >
228 template <
class SymbolType,
class StateType >
246 template <
class SymbolType,
class StateType >
249 template <
class SymbolType,
class StateType >
267 template <
class SymbolType,
class StateType >
286 template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
305 template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
324 template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
343 template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
362 template <
class InputSymbolType,
class OutputSymbolType,
class PushdownStoreSymbolType,
class StateType >
380 template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
386template <
class SymbolType,
class StateType >
392 for (
const SymbolType & symbol :
string.getContent ( ) ) {
393 if (
automaton.getFinalStates ( ).contains ( state ) )
394 occurrences.insert (
i );
401 if ( transition ==
automaton.getTransitions ( ).end ( ) )
404 state = transition->second;
408 if (
automaton.getFinalStates ( ).contains ( state ) )
409 occurrences.insert (
i );
419template <
class SymbolType,
class StateType >
428 if (
automaton.getFinalStates ( ).contains ( state ) )
429 occurrences.insert (
i );
434 for (
const SymbolType & symbol :
string.getContent ( ) ) {
437 for (
const StateType & state : states ) {
440 for (
const auto & transition : transitions )
441 next.insert ( transition.second );
448 if (
automaton.getFinalStates ( ).contains ( state ) )
449 occurrences.insert (
i );
460template <
class SymbolType,
class StateType >
469 if (
automaton.getFinalStates ( ).contains ( state ) )
470 occurrences.insert (
i );
475 for (
const SymbolType & symbol :
string.getContent ( ) ) {
478 for (
const StateType & state : states ) {
481 if ( transitions.empty ( ) )
continue;
483 for (
const auto & transition : transitions )
484 next.insert ( transition.second );
495 states = epsilonNext;
498 if (
automaton.getFinalStates ( ).contains ( state ) )
499 occurrences.insert (
i );
510template <
class SymbolType,
class StateType >
514 states.reserve (
node.getData ( ).getRank ( ) );
527 states.push_back (
res.second );
531 return ext::make_pair (
false, label::FailStateLabel::instance < StateType > ( ) );
535 if ( it ==
automaton.getTransitions ( ).end ( ) )
return ext::make_pair (
false, label::FailStateLabel::instance < StateType > ( ) );
539 if (
automaton.getFinalStates ( ).contains ( state ) )
548template <
class SymbolType,
class StateType >
559template <
class SymbolType,
class StateType >
563 childStates.reserve (
node.getChildren ( ).size ( ) );
571 return ext::make_pair (
false, label::FailStateLabel::instance < StateType > ( ) );
573 childStates.push_back (
res.second );
576 const auto & it =
automaton.getTransitions ( ).find (
node.getData ( ) );
578 if ( it ==
automaton.getTransitions ( ).end ( ) )
579 return ext::make_pair (
false, label::FailStateLabel::instance < StateType > ( ) );
586 for (
const StateType & childState : childStates ) {
589 if ( itChildState ==
automaton.getTransitions ( ).end ( ) ) {
592 return ext::make_pair (
false, label::FailStateLabel::instance < StateType > ( ) );
595 state = itChildState->second;
604 if (
automaton.getFinalStates ( ).contains ( state ) )
610template <
class SymbolType,
class StateType >
621template <
class SymbolType,
class StateType >
636 states.insert (
res.second );
640 return ext::make_pair (
false, label::FailStateLabel::instance < StateType > ( ) );
644 if ( it ==
automaton.getTransitions ( ).end ( ) )
return ext::make_pair (
false, label::FailStateLabel::instance < StateType > ( ) );
648 if (
automaton.getFinalStates ( ).contains ( state ) )
657template <
class SymbolType,
class StateType >
668template <
class SymbolType,
class StateType >
672 resStates.reserve (
node.getData ( ).getRank ( ) );
680 std::pair < bool, ext::set < StateType > > childStates = calculateStates (
automaton, child, occ,
i );
682 if ( childStates.second.empty ( ) )
685 resStates.push_back ( childStates.second );
690 for (
const auto & transition :
automaton.getTransitions ( ) ) {
691 if ( transition.first.first !=
node.getData ( ) )
694 unsigned rank = transition.first.first.getRank ( );
697 for ( j = 0; j < rank; j++ )
698 if ( ! resStates [ j ].
contains ( transition.first.second [ j ] ) )
702 states.insert ( transition.second );
706 if (
automaton.getFinalStates ( ).contains ( state ) )
715template <
class SymbolType,
class StateType >
726template <
class SymbolType >
728 unsigned popSize = pop.size ( );
729 unsigned pushdownStoreSize = pushdownStore.size ( );
731 if ( pushdownStoreSize < popSize )
return false;
733 for (
unsigned i = 0;
i < popSize;
i++ )
734 if ( pop[
i] != pushdownStore[pushdownStoreSize -
i - 1] )
742template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
751 for (
const InputSymbolType & symbol :
string.getContent ( ) ) {
752 if (
automaton.getFinalStates ( ).contains ( state ) )
760 if ( transition ==
automaton.getTransitions ( ).end ( ) )
765 if ( !canPop ( pushdownStore, operation.first ) )
768 for (
unsigned j = 0; j < operation.first.size ( ); j++ ) pushdownStore.pop_back ( );
770 for (
const auto & push :
ext::make_reverse ( operation.second ) ) pushdownStore.push_back ( push );
772 state = transition->second;
776 if (
automaton.getFinalStates ( ).contains ( state ) )
787template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
796 for (
const InputSymbolType & symbol :
string.getContent ( ) ) {
797 if (
automaton.getFinalStates ( ).contains ( state ) )
803 if (
automaton.getCallInputAlphabet ( ).contains ( symbol ) ) {
806 if ( transition ==
automaton.getCallTransitions ( ).end ( ) )
809 pushdownStore.push_back ( transition->second.second );
810 state = transition->second.first;
811 }
else if (
automaton.getReturnInputAlphabet ( ).contains ( symbol ) ) {
812 auto transition =
automaton.getReturnTransitions ( ).find (
ext::make_tuple ( state, symbol, pushdownStore.back ( ) ) );
814 if ( transition ==
automaton.getReturnTransitions ( ).end ( ) )
817 if ( pushdownStore.back ( ) !=
automaton.getBottomOfTheStackSymbol ( ) ) pushdownStore.pop_back ( );
819 state = transition->second;
820 }
else if (
automaton.getLocalInputAlphabet ( ).contains ( symbol ) ) {
823 if ( transition ==
automaton.getLocalTransitions ( ).end ( ) )
826 state = transition->second;
834 if (
automaton.getFinalStates ( ).contains ( state ) )
845template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
854 if (
automaton.getFinalStates ( ).contains ( state ) )
860 for (
auto symbolIter =
string.getContent ( ).
begin ( ); symbolIter !=
string.getContent ( ).
end ( ); ) {
862 bool transitionFound =
false;
866 if ( callTransition !=
automaton.getCallTransitions ( ).end ( ) ) {
873 if ( callTransition !=
automaton.getCallTransitions ( ).end ( ) ) {
874 pushdownStore.push_back ( callTransition->second.second );
875 state = callTransition->second.first;
876 transitionFound =
true;
879 if ( ! transitionFound ) {
882 if ( returnTransition !=
automaton.getReturnTransitions ( ).end ( ) ) {
889 if ( returnTransition !=
automaton.getReturnTransitions ( ).end ( ) ) {
890 pushdownStore.pop_back ( );
891 state = returnTransition->second;
892 transitionFound =
true;
896 if ( ! transitionFound ) {
899 if ( localTransition !=
automaton.getLocalTransitions ( ).end ( ) ) {
906 if ( localTransition !=
automaton.getLocalTransitions ( ).end ( ) ) {
907 state = localTransition->second;
908 transitionFound =
true;
912 if ( ! transitionFound )
915 if (
automaton.getFinalStates ( ).contains ( state ) )
924 bool transitionFound =
false;
928 if ( callTransition !=
automaton.getCallTransitions ( ).end ( ) ) {
929 pushdownStore.push_back ( callTransition->second.second );
930 state = callTransition->second.first;
931 transitionFound =
true;
934 if ( ! transitionFound ) {
937 if ( returnTransition !=
automaton.getReturnTransitions ( ).end ( ) ) {
938 pushdownStore.pop_back ( );
939 state = returnTransition->second;
940 transitionFound =
true;
944 if ( ! transitionFound ) {
947 if ( localTransition !=
automaton.getLocalTransitions ( ).end ( ) ) {
948 state = localTransition->second;
949 transitionFound =
true;
953 if ( ! transitionFound )
956 if (
automaton.getFinalStates ( ).contains ( state ) )
968template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
976 for (
auto symbolIter =
string.getContent ( ).
begin ( ); symbolIter !=
string.getContent ( ).
end ( ); ) {
978 if (
automaton.getFinalStates ( ).contains ( state ) )
979 occ.insert ( std::distance (
string.getContent ( ).
begin ( ), symbolIter ) );
986 auto transitions =
automaton.getTransitionsFromState ( state );
987 auto transition = transitions.begin ( );
989 for ( ; transition != transitions.end ( ); transition++ ) {
990 if ( ( std::get < 1 > ( transition->first ) != * symbolIter ) && !std::get < 1 > ( transition->first ).is_epsilon ( ) )
continue;
992 if ( canPop ( pushdownStore, std::get < 2 > ( transition->first ) ) )
break;
995 if ( transition == transitions.end ( ) )
999 common::Streams::log <<
"Transition: " << transition->first <<
" to " << transition->second << std::endl;
1001 for (
unsigned j = 0; j < std::get < 2 > ( transition->first ).size ( ); j++ ) pushdownStore.pop_back ( );
1003 for (
const auto & symbol :
ext::make_reverse ( transition->second.second ) ) pushdownStore.push_back ( symbol );
1005 state = transition->second.first;
1007 if ( !std::get < 1 > ( transition->first ).is_epsilon ( ) ) {
1012 if (
automaton.getFinalStates ( ).contains ( state ) )
1013 occ.insert (
string.getContent ( ).size ( ) );
1025template <
class Type >
1026bool Run::canPop ( graphStructuredStack < Type > pushdownStore,
const ext::vector < Type > & pop ) {
1027 std::shared_ptr < typename graphStructuredStack < Type >::Element >
node = pushdownStore.m_value;
1028 for (
const Type & symbol : pop ) {
1029 if (
node ==
nullptr ||
node->m_data != symbol )
1039template <
class InputSymbolType,
class OutputSymbolType,
class PushdownStoreSymbolType,
class StateType >
1045 graphStructuredStack < OutputSymbolType > initialOutput (
nullptr );
1046 bftQueue . emplace_back (
automaton . getInitialState (),
ext::set < unsigned > {},
string . getContent () .
begin (), std::move ( initialStack ), std::move ( initialOutput ) );
1050 while ( ! bftQueue . empty () ) {
1051 auto configuration = std::move ( bftQueue . front () );
1052 bftQueue . pop_front ();
1054 StateType state = std::move ( std::get < 0 > ( configuration ) );
1057 graphStructuredStack < PushdownStoreSymbolType >
stack = std::get < 3 > ( configuration );
1058 graphStructuredStack < OutputSymbolType > output = std::get < 4 > ( configuration );
1063 if ( symbolIter ==
string . getContent () .
end () ) {
1069 for (
const auto & transition :
automaton . getTransitionsFromState ( state ) ) {
1070 if ( ( symbolIter ==
string.getContent ( ).
end ( ) || std::get<1>(transition . first) != * symbolIter ) && ! std::get<1>(transition . first) . is_epsilon ( ) )
1073 const auto & pop = std::get<2>(transition . first);
1075 if ( ! canPop (
stack, pop ) )
1078 std::shared_ptr < typename graphStructuredStack < PushdownStoreSymbolType >::Element > stackNodeCopy =
stack.m_value;
1079 std::shared_ptr < typename graphStructuredStack < OutputSymbolType >::Element > outputNodeCopy = output.m_value;
1081 for (
unsigned j = 0; j < pop . size (); j ++ ) {
1082 stackNodeCopy = stackNodeCopy -> m_parent;
1086 stackNodeCopy = std::make_shared < typename graphStructuredStack < PushdownStoreSymbolType >::Element > ( stackNodeCopy, elem );
1089 for (
const auto & elem : std::get<2>(transition .
second) ) {
1090 outputNodeCopy = std::make_shared < typename graphStructuredStack < OutputSymbolType >::Element > ( outputNodeCopy, elem );
1094 if ( ! std::get<1>(transition . first) . is_epsilon ( ) )
1097 bftQueue . emplace_back ( std::get<0>(transition .
second), occurrences, nextSymbolIter, graphStructuredStack < PushdownStoreSymbolType > ( stackNodeCopy ), graphStructuredStack < OutputSymbolType > ( outputNodeCopy ) );
1106template <
class InputSymbolType,
class PushdownStoreSymbolType,
class StateType >
1116 while ( ! bftQueue . empty () ) {
1117 auto configuration = std::move ( bftQueue . front () );
1118 bftQueue . pop_front ();
1120 StateType state = std::move ( std::get < 0 > ( configuration ) );
1123 graphStructuredStack < PushdownStoreSymbolType >
stack = std::get < 3 > ( configuration );
1128 if ( symbolIter ==
string . getContent () .
end () ) {
1134 for (
const auto & transition :
automaton . getTransitionsFromState ( state ) ) {
1135 if ( ( symbolIter ==
string.getContent ( ).
end ( ) || std::get<1>(transition . first) != * symbolIter ) && ! std::get<1>(transition . first) . is_epsilon ( ) )
1138 const auto & pop = std::get<2>(transition . first);
1140 if ( ! canPop (
stack, pop ) )
1143 std::shared_ptr < typename graphStructuredStack < PushdownStoreSymbolType >::Element > stackNodeCopy =
stack.m_value;
1145 for (
unsigned j = 0; j < pop . size (); j ++ ) {
1146 stackNodeCopy = stackNodeCopy -> m_parent;
1150 stackNodeCopy = std::make_shared < typename graphStructuredStack < PushdownStoreSymbolType >::Element > ( stackNodeCopy, elem );
1154 if ( ! std::get<1>(transition . first) . is_epsilon ( ) )
1157 bftQueue . emplace_back ( std::get<0>(transition .
second), occurrences, nextSymbolIter, graphStructuredStack < PushdownStoreSymbolType > ( stackNodeCopy ) );
Deterministic Z-Automaton in Arc-Factored Normal Form. Computation model for unranked regular tree la...
Definition: ArcFactoredDeterministicZAutomaton.h:65
Deterministic finite automaton. Accepts regular languages.
Definition: DFA.h:71
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: DFTA.h:74
Deterministic pushdown automaton. Accepts subset of context free languages.
Definition: DPDA.h:78
Epsilon nondeterministic finite automaton. Accepts regular languages.
Definition: EpsilonNFA.h:74
Nondeterministic finite automaton. Accepts regular languages.
Definition: NFA.h:66
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: NFTA.h:72
Deterministic real time height deterministic pushdown automaton. Accepts subset of context free langu...
Definition: RealTimeHeightDeterministicDPDA.h:89
Deterministic unordered finite tree automaton without epsilon transitions. Accepts regular tree langu...
Definition: UnorderedDFTA.h:72
Deterministic visibly pushdown automaton. Accepts subset of context free languages.
Definition: VisiblyPushdownDPDA.h:86
static ext::set< StateType > epsilonClosure(const automaton::EpsilonNFA< SymbolType, StateType > &fsm, const StateType &q)
Definition: EpsilonClosure.h:125
Implementation of automaton run over its input.
Definition: Run.h:44
static bool verbose
Verbose flag. Some algorithms print additional runtime information about internal datastructures or t...
Definition: GlobalData.h:24
static ext::reference_wrapper< ext::ostream > log
Standard loging stream. Mapped to descriptor 4.
Definition: GlobalData.h:78
Definition: ranked_symbol.hpp:20
Definition: symbol_or_epsilon.hpp:24
Class extending the deque class from the standard library. Original reason is to allow printing of th...
Definition: deque.hpp:44
Definition: multiset.hpp:44
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Class introducing a tree with interface trying to be close to the interface of standard library conta...
Definition: tree.hpp:52
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
typename std::vector< T, Alloc >::const_iterator const_iterator
The type of constant values iterator.
Definition: vector.hpp:67
Linear string.
Definition: LinearString.h:57
Tree structure represented in its natural representation. The representation is so called ranked,...
Definition: RankedTree.h:72
Tree structure represented in its natural representation. The representation is so called ranked,...
Definition: UnorderedRankedTree.h:70
Tree represented in its natural representation. The representation is so called unranked,...
Definition: UnrankedTree.h:69
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
p second
Definition: ToRegExpAlgebraic.h:126
int i
Definition: AllEpsilonClosure.h:118
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
return closure
Definition: AllEpsilonClosure.h:142
return res
Definition: MinimizeByPartitioning.h:145
Definition: ToGrammar.h:31
reverser< T > make_reverse(T &&container)
Reverese adaptor construction function.
Definition: iterator.hpp:544
bool contains(InputIt first, InputIt last, const Element &elem)
Linear version of search in a range of values.
Definition: algorithm.hpp:170
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
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
void end()
Definition: measurements.cpp:19
Type
Definition: MeasurementTypes.hpp:20
Definition: FordFulkerson.hpp:16
Definition: BackwardOccurrenceTest.h:17
std::shared_ptr< Element > m_parent
Definition: Run.h:50
SymbolType m_data
Definition: Run.h:51
Element(std::shared_ptr< Element > parent, SymbolType data)
Definition: Run.h:49