Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
BackwardOccurrenceTest.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <climits>
9
16
17namespace tree {
18
19namespace exact {
20
22public:
23 template < class SymbolType >
24 static ext::pair < int, int > occurrence ( const PrefixRankedBarTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedBarTree < SymbolType > & pattern, size_t subjectPosition );
25 template < class SymbolType >
26 static ext::pair < int, int > occurrence ( const PrefixRankedBarTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedBarPattern < SymbolType > & pattern, size_t subjectPosition );
27 template < class SymbolType >
28 static ext::pair < int, int > occurrence ( const PrefixRankedBarTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const tree::PrefixRankedBarTree < unsigned > & repeats, const PrefixRankedBarNonlinearPattern < SymbolType > & pattern, size_t subjectPosition );
29
30};
31
32template < class SymbolType >
33ext::pair < int, int > BackwardOccurrenceTest::occurrence ( const PrefixRankedBarTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedBarTree < SymbolType > & pattern, size_t subjectPosition ) {
34 return occurrence ( subject, subjectSubtreeJumpTable, tree::PrefixRankedBarPattern < SymbolType > ( pattern ), subjectPosition );
35}
36
37template < class SymbolType >
38ext::pair < int, int > BackwardOccurrenceTest::occurrence ( const PrefixRankedBarTree < SymbolType > & subject, const ext::vector < int > & subjectSubtreeJumpTable, const PrefixRankedBarPattern < SymbolType > & pattern, size_t subjectPosition ) {
39 // index to the pattern
40 int j = pattern.getContent ( ).size ( ) - 1;
41
42 // offset to the subject
43 int offset = subjectPosition;
44
45 while ( ( j >= 0 ) && ( offset >= 0 ) ) {
46 if ( subject.getContent ( )[offset] == pattern.getContent ( )[j] ) {
47 // match of symbol
48 offset = offset - 1;
49 j = j - 1;
50 } else if ( ( pattern.getContent ( )[j] == pattern.getVariablesBar ( ) ) && ( subject.getBars ( ).count ( subject.getContent ( )[offset] ) ) ) { //the second part of the condition is needed to handle S |S
51 // match of variable with subtree
52 offset = subjectSubtreeJumpTable[offset];
53 j = j - 2;
54 } else {
55 break;
56 }
57 }
58
59 return ext::make_pair ( j, offset );
60}
61
62template < class SymbolType >
64
65 // to represent state of variable to subtree repeat
66 ext::map < common::ranked_symbol < SymbolType >, unsigned > variablesSetting;
67
68 // index to the pattern
69 int j = pattern.getContent ( ).size ( ) - 1;
70
71 // offset to the subject
72 int offset = subjectPosition;
73
74 while ( ( j >= 0 ) && ( offset >= 0 ) ) {
75 if ( subject.getContent ( )[offset] == pattern.getContent ( )[j] ) {
76 // match of symbol
77 offset = offset - 1;
78 j = j - 1;
79 } else if ( ( pattern.getContent ( )[j] == pattern.getVariablesBar ( ) ) && ( subject.getBars ( ).count ( subject.getContent ( )[offset] ) ) ) { //the second part of the condition is needed to handle S |S
80 // else match of variable with subtree
81 offset = subjectSubtreeJumpTable[offset];
82 j = j - 2;
83
84 // check nonlinear variable
85 if ( pattern.getNonlinearVariables ( ).count ( pattern.getContent ( )[ j + 1 ] ) ) {
86 auto setting = variablesSetting.find ( pattern.getContent ( )[ j + 1 ] );
87
88 if ( setting != variablesSetting.end ( ) && repeats.getContent ( )[ offset + 1 ].getSymbol ( ) != setting->second )
89 break;
90
91 variablesSetting.insert ( std::make_pair ( pattern.getContent ( )[ j + 1 ], repeats.getContent( )[ offset + 1 ].getSymbol ( ) ) );
92 }
93 } else {
94 break;
95 }
96 }
97
98 return ext::make_pair ( j, offset );
99}
100
101} /* namespace exact */
102
103} /* namespace tree */
104
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 pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
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 ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedBarNonlinearPattern.h:434
const common::ranked_symbol< SymbolType > & getVariablesBar() const &
Definition: PrefixRankedBarNonlinearPattern.h:295
Tree pattern represented as linear sequece as result of preorder traversal with additional bar symbol...
Definition: PrefixRankedBarPattern.h:85
const common::ranked_symbol< SymbolType > & getVariablesBar() const &
Definition: PrefixRankedBarPattern.h:220
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
Definition: BackwardOccurrenceTest.h:21
static ext::pair< int, int > occurrence(const PrefixRankedBarTree< SymbolType > &subject, const ext::vector< int > &subjectSubtreeJumpTable, const PrefixRankedBarTree< SymbolType > &pattern, size_t subjectPosition)
Definition: BackwardOccurrenceTest.h:33
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: BackwardOccurrenceTest.h:17