Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
BorderArrayNaive.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/vector>
9
10#include <global/GlobalData.h>
11
12#include "SubtreeJumpTable.h"
13
19
20namespace tree {
21
22namespace properties {
23
29 template < class SymbolType >
30 static bool matches ( const tree::PrefixRankedBarNonlinearPattern < SymbolType > & pattern, const ext::vector < int > & subtreeJumpTable, int stop, int offset );
31
32 template < class SymbolType >
33 static bool matches ( const tree::PrefixRankedBarPattern < SymbolType > & pattern, const ext::vector < int > & subtreeJumpTable, int stop, int offset );
34
35 template < class SymbolType >
36 static bool matches ( const tree::PrefixRankedNonlinearPattern < SymbolType > & pattern, const ext::vector < int > & subtreeJumpTable, int stop, int offset );
37
38 template < class SymbolType >
39 static bool matches ( const tree::PrefixRankedPattern < SymbolType > & pattern, const ext::vector < int > & subtreeJumpTable, int stop, int offset );
40
41 template < class SymbolType >
42 static bool matches ( const tree::PrefixRankedExtendedPattern < SymbolType > & pattern, const ext::vector < int > & subtreeJumpTable, int stop, int offset );
43
44public:
54 template < class T >
55 static ext::vector < size_t > construct ( const T & pattern );
56};
57
58template < class T >
60 ext::vector < int > patternSubtreeJumpTable = SubtreeJumpTable::compute ( pattern );
62
63 for ( size_t i = 0; i <= pattern.getContent ( ).size ( ); i++ )
64 res.push_back ( 0 );
65
66 res[0] = -1;
67
68 for ( size_t i = 1; i <= pattern.getContent ( ).size ( ); i++ ) {
69 size_t min = i;
70
71 for ( size_t j = 1; j < i; j++ )
72 if ( matches ( pattern, patternSubtreeJumpTable, i, j ) ) {
73 min = j;
74 break;
75 }
76
77 res[i] = i - min;
78 }
79
81 common::Streams::log << res << std::endl;
82
83 return res;
84}
85
86template < class SymbolType >
87bool BorderArrayNaive::matches ( const tree::PrefixRankedBarNonlinearPattern < SymbolType > & pattern, const ext::vector < int > & subtreeJumpTable, int stop, int offset ) {
88 unsigned i = 0;
89
90 while ( offset < stop && i < pattern.getContent ( ).size ( ) )
91 if ( pattern.getContent ( )[i] == pattern.getContent ( )[offset] ) {
92 i++;
93 offset++;
94 } else if ( ( pattern.getContent ( )[i] == pattern.getSubtreeWildcard ( ) ) || ( pattern.getNonlinearVariables ( ).count ( pattern.getContent ( ) [ i ] ) )
95 || ( pattern.getContent ( )[offset] == pattern.getSubtreeWildcard ( ) ) || ( pattern.getNonlinearVariables ( ).count ( pattern.getContent ( ) [ offset ] ) ) ) {
96 i = subtreeJumpTable[i];
97 offset = subtreeJumpTable[offset];
98 } else {
99 return false;
100 }
101
102 return true;
103}
104
105template < class SymbolType >
106bool BorderArrayNaive::matches ( const tree::PrefixRankedBarPattern < SymbolType > & pattern, const ext::vector < int > & subtreeJumpTable, int stop, int offset ) {
107 unsigned i = 0;
108
109 while ( offset < stop && i < pattern.getContent ( ).size ( ) )
110 if ( pattern.getContent ( )[i] == pattern.getContent ( )[offset] ) {
111 i++;
112 offset++;
113 } else if ( ( pattern.getContent ( )[i] == pattern.getSubtreeWildcard ( ) ) || ( pattern.getContent ( )[offset] == pattern.getSubtreeWildcard ( ) ) ) {
114 i = subtreeJumpTable[i];
115 offset = subtreeJumpTable[offset];
116 } else {
117 return false;
118 }
119
120 return true;
121}
122template < class SymbolType >
123bool BorderArrayNaive::matches ( const tree::PrefixRankedNonlinearPattern < SymbolType > & pattern, const ext::vector < int > & subtreeJumpTable, int stop, int offset ) {
124 size_t i = 0;
125
126 while ( offset < stop && i < pattern.getContent ( ).size ( ) )
127 if ( pattern.getContent ( )[i] == pattern.getContent ( )[offset] ) {
128 i++;
129 offset++;
130 } else if ( ( pattern.getContent ( )[i] == pattern.getSubtreeWildcard ( ) ) || ( pattern.getNonlinearVariables ( ).count ( pattern.getContent ( ) [ i ] ) )
131 || ( pattern.getContent ( )[offset] == pattern.getSubtreeWildcard ( ) ) || ( pattern.getNonlinearVariables ( ).count ( pattern.getContent ( ) [ offset ] ) ) ) {
132 i = subtreeJumpTable[i];
133 offset = subtreeJumpTable[offset];
134 } else {
135 return false;
136 }
137
138 return true;
139}
140
141template < class SymbolType >
142bool BorderArrayNaive::matches ( const tree::PrefixRankedPattern < SymbolType > & pattern, const ext::vector < int > & subtreeJumpTable, int stop, int offset ) {
143 size_t i = 0;
144
145 while ( offset < stop && i < pattern.getContent ( ).size ( ) )
146 if ( pattern.getContent ( )[i] == pattern.getContent ( )[offset] ) {
147 i++;
148 offset++;
149 } else if ( ( pattern.getContent ( )[i] == pattern.getSubtreeWildcard ( ) ) || ( pattern.getContent ( )[offset] == pattern.getSubtreeWildcard ( ) ) ) {
150 i = subtreeJumpTable[i];
151 offset = subtreeJumpTable[offset];
152 } else {
153 return false;
154 }
155
156 return true;
157}
158
159template < class SymbolType >
160bool BorderArrayNaive::matches ( const tree::PrefixRankedExtendedPattern < SymbolType > & pattern, const ext::vector < int > & subtreeJumpTable, int stop, int offset ) {
161 size_t i = 0;
162
163 while ( offset < stop && i < pattern.getContent ( ).size ( ) ) {
164 const auto & iSymbol = pattern.getContent ( )[i];
165 const auto & oSymbol = pattern.getContent ( )[offset];
166 if ( iSymbol == oSymbol || ( iSymbol.getRank ( ) == oSymbol.getRank ( ) && ( pattern.getNodeWildcards ( ).contains ( iSymbol ) || pattern.getNodeWildcards ( ).contains ( oSymbol ) ) ) ) {
167 i++;
168 offset++;
169 } else if ( iSymbol == pattern.getSubtreeWildcard ( ) || oSymbol == pattern.getSubtreeWildcard ( ) ) {
170 i = subtreeJumpTable[i];
171 offset = subtreeJumpTable[offset];
172 } else {
173 return false;
174 }
175 }
176
177 return true;
178}
179
180} /* namespace properties */
181
182} /* namespace tree */
183
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
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 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
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedPattern.h:262
const common::ranked_symbol< SymbolType > & getSubtreeWildcard() const &
Definition: PrefixRankedPattern.h:154
Definition: BorderArrayNaive.h:28
static ext::vector< size_t > construct(const T &pattern)
Definition: BorderArrayNaive.h:59
static ext::vector< int > compute(const tree::PrefixRankedBarTree< SymbolType > &subject)
Definition: SubtreeJumpTable.h:49
int i
Definition: AllEpsilonClosure.h:118
return res
Definition: MinimizeByPartitioning.h:145
constexpr const T & min(const T &a)
Definition: algorithm.hpp:310
Definition: BackwardOccurrenceTest.h:17