Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ForwardOccurrenceTest.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <climits>
9
17
18namespace tree {
19
20namespace exact {
21
23public:
24 template < class SymbolType >
25 static size_t occurrence ( const PrefixRankedBarTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedBarTree < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex = 0 );
26 template < class SymbolType >
27 static size_t occurrence ( const PrefixRankedBarTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedBarPattern < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex = 0 );
28 template < class SymbolType >
29 static size_t occurrence ( const PrefixRankedBarTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const tree::PrefixRankedBarTree < unsigned > & repeats, const PrefixRankedBarNonlinearPattern < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex = 0 );
30
31 template < class SymbolType >
32 static size_t occurrence ( const PrefixRankedTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedTree < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex = 0 );
33 template < class SymbolType >
34 static size_t occurrence ( const PrefixRankedTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedPattern < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex = 0 );
35 template < class SymbolType >
36 static size_t occurrence ( const PrefixRankedTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedExtendedPattern < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex = 0 );
37 template < class SymbolType >
38 static size_t occurrence ( const PrefixRankedTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const tree::PrefixRankedTree < unsigned > & repeats, const PrefixRankedNonlinearPattern < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex = 0 );
39};
40
41template < class SymbolType >
42size_t ForwardOccurrenceTest::occurrence ( const PrefixRankedBarTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedBarTree < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex ) {
43 return occurrence ( subject, subjectSubtreeJumpTable, tree::PrefixRankedBarPattern < SymbolType > ( pattern ), subjectPosition, patternStartIndex );
44}
45
46template < class SymbolType >
47size_t ForwardOccurrenceTest::occurrence ( const PrefixRankedBarTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedBarPattern < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex ) {
48 // offset to the subject
49 size_t offset = subjectPosition + patternStartIndex;
50 size_t j = patternStartIndex;
51
52 while ( ( j < pattern.getContent ( ).size ( ) ) && ( offset < subject.getContent ( ).size ( ) ) ) {
53 if ( subject.getContent ( )[offset] == pattern.getContent ( )[j] ) {
54 // match of symbol
55 offset = offset + 1;
56 j = j + 1;
57 } else if ( ( pattern.getContent ( )[j] == pattern.getSubtreeWildcard ( ) ) && ( ! subject.getBars ( ).count ( subject.getContent ( )[offset] ) ) ) { //the second part of the condition is needed to handle S |S
58 // match of variable with subtree
59 offset = subjectSubtreeJumpTable[offset];
60 j = j + 2;
61 } else {
62 break;
63 }
64 }
65
66 return j;
67}
68
69template < class SymbolType >
70size_t ForwardOccurrenceTest::occurrence ( const PrefixRankedBarTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const tree::PrefixRankedBarTree < unsigned > & repeats, const PrefixRankedBarNonlinearPattern < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex ) {
71
72 // to represent state of variable to subtree repeat
73 ext::map < common::ranked_symbol < SymbolType >, unsigned > variablesSetting;
74
75 // offset to the subject
76 unsigned offset = subjectPosition + patternStartIndex;
77 unsigned j = patternStartIndex;
78
79 while ( ( j < pattern.getContent ( ).size ( ) ) && ( offset < subject.getContent ( ).size ( ) ) ) {
80 if ( subject.getContent ( )[offset] == pattern.getContent ( )[j] ) {
81 // match of symbol
82 offset = offset + 1;
83 j = j + 1;
84 } else if ( ( pattern.getContent ( )[j] == pattern.getSubtreeWildcard ( ) || pattern.getNonlinearVariables ( ).count ( pattern.getContent ( )[j] ) ) && ( ! subject.getBars ( ).count ( subject.getContent ( )[offset] ) ) ) { //the second part of the condition is needed to handle S |S
85 // check nonlinear variable
86 if ( pattern.getNonlinearVariables ( ).count ( pattern.getContent ( )[ j ] ) ) {
87 auto setting = variablesSetting.find ( pattern.getContent ( )[ j ] );
88
89 if ( setting != variablesSetting.end ( ) && repeats.getContent ( )[ offset ].getSymbol ( ) != setting->second )
90 break;
91
92 variablesSetting.insert ( std::make_pair ( pattern.getContent ( )[ j ], repeats.getContent( )[ offset ].getSymbol ( ) ) );
93 }
94
95 // match of variable with subtree
96 offset = subjectSubtreeJumpTable[offset];
97 j = j + 2;
98 } else {
99 break;
100 }
101 }
102
103 return j;
104}
105
106template < class SymbolType >
107size_t ForwardOccurrenceTest::occurrence ( const PrefixRankedTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedTree < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex ) {
108 return occurrence ( subject, subjectSubtreeJumpTable, tree::PrefixRankedPattern < SymbolType > ( pattern ), subjectPosition, patternStartIndex );
109}
110
111template < class SymbolType >
112size_t ForwardOccurrenceTest::occurrence ( const PrefixRankedTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedPattern < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex ) {
113 return occurrence ( subject, subjectSubtreeJumpTable, tree::PrefixRankedExtendedPattern < SymbolType > ( pattern ), subjectPosition, patternStartIndex );
114}
115
116template < class SymbolType >
117size_t ForwardOccurrenceTest::occurrence ( const PrefixRankedTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedExtendedPattern < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex ) {
118 // offset to the subject
119 unsigned offset = subjectPosition + patternStartIndex;
120 size_t j = patternStartIndex;
121
122 while ( ( j < pattern.getContent ( ).size ( ) ) && ( offset < subject.getContent ( ).size ( ) ) ) {
123 if ( ( subject.getContent ( )[offset] == pattern.getContent ( )[j] /* match of symbol */ )
124 || ( pattern.getContent ( )[j].getRank ( ) == subject.getContent ( )[offset].getRank ( ) && pattern.getNodeWildcards ( ).contains ( pattern.getContent ( )[j] ) /* match node wildcard */ ) )
125 offset = offset + 1;
126 else if ( pattern.getContent ( )[j] == pattern.getSubtreeWildcard ( ) )
127 // match of variable with subtree
128 offset = subjectSubtreeJumpTable[offset];
129 else
130 break;
131
132 j = j + 1;
133 }
134
135 return j;
136}
137
138template < class SymbolType >
139size_t ForwardOccurrenceTest::occurrence ( const PrefixRankedTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const tree::PrefixRankedTree < unsigned > & repeats, const PrefixRankedNonlinearPattern < SymbolType > & pattern, size_t subjectPosition, size_t patternStartIndex ) {
140 // to represent state of variable to subtree repeat
141 ext::map < common::ranked_symbol < SymbolType >, unsigned > variablesSetting;
142
143 // offset to the subject
144 unsigned offset = subjectPosition + patternStartIndex;
145 unsigned j = patternStartIndex;
146
147 while ( ( j < pattern.getContent ( ).size ( ) ) && ( offset < subject.getContent ( ).size ( ) ) ) {
148 if ( subject.getContent ( )[offset] == pattern.getContent ( )[j] )
149 // match of symbol
150 offset = offset + 1;
151 else if ( pattern.getContent ( )[j] == pattern.getSubtreeWildcard ( ) || pattern.getNonlinearVariables ( ).count ( pattern.getContent ( )[ j ] ) ) {
152 // check nonlinear variable
153 if ( pattern.getNonlinearVariables ( ).count ( pattern.getContent ( )[ j ] ) ) {
154 auto setting = variablesSetting.find ( pattern.getContent ( )[ j ] );
155
156 if ( setting != variablesSetting.end ( ) && repeats.getContent ( )[ offset ].getSymbol ( ) != setting->second )
157 break;
158
159 variablesSetting.insert ( std::make_pair ( pattern.getContent ( )[ j ], repeats.getContent( )[ offset ].getSymbol ( ) ) );
160 }
161
162 // match of variable with subtree
163 offset = subjectSubtreeJumpTable[offset];
164 } else
165 break;
166
167 j = j + 1;
168 }
169
170 return j;
171}
172
173} /* namespace exact */
174
175} /* namespace tree */
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
std::pair< iterator, bool > insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: map.hpp:118
auto end() &
Inherited behavior of end for non-const instance.
Definition: map.hpp:215
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Nonlinear tree pattern represented as linear sequece as result of preorder traversal with additional ...
Definition: PrefixRankedBarNonlinearPattern.h:91
const ext::set< common::ranked_symbol< SymbolType > > & getNonlinearVariables() const &
Definition: PrefixRankedBarNonlinearPattern.h:277
const common::ranked_symbol< SymbolType > & getSubtreeWildcard() const &
Definition: PrefixRankedBarNonlinearPattern.h:259
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedBarNonlinearPattern.h:434
Tree pattern represented as linear sequece as result of preorder traversal with additional bar symbol...
Definition: PrefixRankedBarPattern.h:85
const common::ranked_symbol< SymbolType > & getSubtreeWildcard() const &
Definition: PrefixRankedBarPattern.h:202
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedBarPattern.h:334
Tree structure represented as linear sequece as result of preorder traversal with additional bar symb...
Definition: PrefixRankedBarTree.h:78
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedBarTree.h:273
const ext::set< common::ranked_symbol< SymbolType > > & getBars() const &
Definition: PrefixRankedBarTree.h:156
Tree pattern represented as linear sequece as result of preorder traversal. The representation is so ...
Definition: PrefixRankedExtendedPattern.h:80
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedExtendedPattern.h:293
const ext::set< common::ranked_symbol< SymbolType > > & getNodeWildcards() const &
Definition: PrefixRankedExtendedPattern.h:157
const common::ranked_symbol< SymbolType > & getSubtreeWildcard() const &
Definition: PrefixRankedExtendedPattern.h:184
Nonlinear tree pattern represented as linear sequece as result of preorder traversal....
Definition: PrefixRankedNonlinearPattern.h:82
const ext::set< common::ranked_symbol< SymbolType > > & getNonlinearVariables() const &
Definition: PrefixRankedNonlinearPattern.h:208
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedNonlinearPattern.h:333
const common::ranked_symbol< SymbolType > & getSubtreeWildcard() const &
Definition: PrefixRankedNonlinearPattern.h:190
Tree pattern represented as linear sequece as result of preorder traversal. The representation is so ...
Definition: PrefixRankedPattern.h:77
Tree structure represented as linear sequece as result of preorder traversal. The representation is s...
Definition: PrefixRankedTree.h:71
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedTree.h:235
Definition: ForwardOccurrenceTest.h:22
static size_t occurrence(const PrefixRankedBarTree< SymbolType > &subject, const ext::vector< int > &subjectSubtreeJumpTable, const PrefixRankedBarTree< SymbolType > &pattern, size_t subjectPosition, size_t patternStartIndex=0)
Definition: ForwardOccurrenceTest.h:42
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: BackwardOccurrenceTest.h:17