Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
NonlinearFullAndLinearIndexPatterns.h
Go to the documentation of this file.
1
6#pragma once
7
11#include <global/GlobalData.h>
12
14
15namespace arbology {
16
17namespace query {
18
25 template < class StringIndexQueryAlgo, class SymbolType, template < typename > class StringIndex >
26 static ext::vector < std::pair < unsigned, unsigned > > FindOccurrences ( const StringIndex < common::ranked_symbol < SymbolType > > & stringIndex, const ext::vector < common::ranked_symbol < SymbolType > > & string ) {
28 for ( unsigned occurrence : StringIndexQueryAlgo::query ( stringIndex, string::LinearString < common::ranked_symbol < SymbolType > > ( string ) ) ) {
29 res.push_back ( std::make_pair ( occurrence, occurrence + string.size ( ) ) );
30 }
31 return res;
32 }
33
34 static ext::vector < std::pair < unsigned, unsigned > > MergeOccurrences ( const ext::vector < std::pair < unsigned, unsigned > > & prevOcc, const ext::vector < std::pair < unsigned, unsigned > > & subOcc, ext::vector < unsigned > & rev ) {
36
37 for ( const std::pair < unsigned, unsigned > & occurrence : prevOcc ) {
38 rev [ occurrence.second ] = occurrence.first;
39 }
40
41 for ( const std::pair < unsigned, unsigned > & subOccurrence : subOcc ) {
42 if ( rev [ subOccurrence.first ] != static_cast < unsigned > ( -1 ) )
43 res.push_back ( std::make_pair ( rev [ subOccurrence.first ], subOccurrence.second ) );
44 }
45
46 for ( const std::pair < unsigned, unsigned > & occurrence : prevOcc ) {
47 rev [ occurrence.second ] = static_cast < unsigned > ( -1 );
48 }
49
50 return res;
51 }
52public:
59 template < class SymbolType, template < typename > class StringIndex, class StringIndexQueryAlgo = stringology::query::PositionHeapFactors >
61
62 template < class SymbolType, template < typename > class StringIndex, class StringIndexQueryAlgo = stringology::query::PositionHeapFactors >
64};
65
66template < class SymbolType, template < typename > class StringIndex, class StringIndexQueryAlgo >
69 ext::vector < unsigned > rev ( fullAndLinearIndex.getJumps ( ).size ( ), static_cast < unsigned > ( -1 ) );
70
72 treePatternParts.push_back ( ext::vector < common::ranked_symbol < SymbolType > > ( ) );
73 for ( const common::ranked_symbol < SymbolType > & symbol : pattern.getContent ( ) ) {
74 if ( pattern.getSubtreeWildcard ( ) == symbol || pattern.getNonlinearVariables ( ).count ( symbol ) ) {
75 treePatternParts.push_back ( ext::vector < common::ranked_symbol < SymbolType > > ( ) );
76 treePatternParts.back ( ).push_back ( symbol );
77 treePatternParts.push_back ( ext::vector < common::ranked_symbol < SymbolType > > ( ) );
78 } else {
79 treePatternParts.back ( ).push_back ( symbol );
80 }
81 }
82
83 ext::vector < std::pair < unsigned, unsigned > > prevOcc = NonlinearFullAndLinearIndexPatterns::FindOccurrences < StringIndexQueryAlgo > ( fullAndLinearIndex.getStringIndex ( ), treePatternParts [ 0 ] );
84
85 for ( unsigned i = 1; i < treePatternParts.size ( ); ++ i ) {
86 for ( std::pair < unsigned, unsigned > & occurrence : prevOcc ) {
87 if ( pattern.getNonlinearVariables ( ).count ( treePatternParts [ i ].back ( ) ) ) {
88 auto variableSettingIter = nonlinearVariablesMap.find ( std::make_pair ( occurrence.first, treePatternParts [ i ].back ( ) ) );
89 if ( variableSettingIter == nonlinearVariablesMap.end ( ) )
90 nonlinearVariablesMap.insert ( std::make_pair ( std::make_pair ( occurrence.first, treePatternParts [ i ].back ( ) ), fullAndLinearIndex.getRepeats ( ) [ occurrence.second ] ) );
91 else if ( variableSettingIter->second != fullAndLinearIndex.getRepeats ( ) [ occurrence.second ] )
92 occurrence.first = static_cast < unsigned > ( -1 );
93 }
94 occurrence.second = fullAndLinearIndex.getJumps ( ) [ occurrence.second ];
95 }
96
97 ++ i;
98
99 if ( ! treePatternParts [ i ].empty ( ) )
100 prevOcc = MergeOccurrences ( prevOcc, NonlinearFullAndLinearIndexPatterns::FindOccurrences < StringIndexQueryAlgo > ( fullAndLinearIndex.getStringIndex ( ), treePatternParts [ i ] ), rev );
101 }
102
104 for ( const std::pair < unsigned, unsigned > & occurrence : prevOcc ) {
105 if ( occurrence.first != static_cast < unsigned > ( -1 ) )
106 res.insert ( occurrence.first );
107 }
108
109 return res;
110}
111
112template < class SymbolType, template < typename > class StringIndex, class StringIndexQueryAlgo >
115 ext::vector < unsigned > rev ( fullAndLinearIndex.getJumps ( ).size ( ), static_cast < unsigned > ( -1 ) );
116
118 treePatternParts.push_back ( ext::vector < common::ranked_symbol < SymbolType > > ( ) );
119 for ( const common::ranked_symbol < SymbolType > & symbol : pattern.getContent ( ) ) {
120 if ( symbol == pattern.getVariablesBar ( ) )
121 continue;
122
123 if ( pattern.getSubtreeWildcard ( ) == symbol || pattern.getNonlinearVariables ( ).count ( symbol ) ) {
124 treePatternParts.push_back ( ext::vector < common::ranked_symbol < SymbolType > > ( ) );
125 treePatternParts.back ( ).push_back ( symbol );
126 treePatternParts.push_back ( ext::vector < common::ranked_symbol < SymbolType > > ( ) );
127 } else {
128 treePatternParts.back ( ).push_back ( symbol );
129 }
130 }
131
132 ext::vector < std::pair < unsigned, unsigned > > prevOcc = NonlinearFullAndLinearIndexPatterns::FindOccurrences < StringIndexQueryAlgo > ( fullAndLinearIndex.getStringIndex ( ), treePatternParts [ 0 ] );
133
134 for ( unsigned i = 1; i < treePatternParts.size ( ); ++ i ) {
135 for ( std::pair < unsigned, unsigned > & occurrence : prevOcc ) {
136 if ( pattern.getNonlinearVariables ( ).count ( treePatternParts [ i ].back ( ) ) ) {
137 auto variableSettingIter = nonlinearVariablesMap.find ( std::make_pair ( occurrence.first, treePatternParts [ i ].back ( ) ) );
138 if ( variableSettingIter == nonlinearVariablesMap.end ( ) )
139 nonlinearVariablesMap.insert ( std::make_pair ( std::make_pair ( occurrence.first, treePatternParts [ i ].back ( ) ), fullAndLinearIndex.getRepeats ( ) [ occurrence.second ] ) );
140 else if ( variableSettingIter->second != fullAndLinearIndex.getRepeats ( ) [ occurrence.second ] )
141 occurrence.first = static_cast < unsigned > ( -1 );
142 }
143 occurrence.second = fullAndLinearIndex.getJumps ( ) [ occurrence.second ];
144 }
145
146 ++ i;
147
148 if ( ! treePatternParts [ i ].empty ( ) )
149 prevOcc = MergeOccurrences ( prevOcc, NonlinearFullAndLinearIndexPatterns::FindOccurrences < StringIndexQueryAlgo > ( fullAndLinearIndex.getStringIndex ( ), treePatternParts [ i ] ), rev );
150 }
151
153 for ( const std::pair < unsigned, unsigned > & occurrence : prevOcc ) {
154 if ( occurrence.first != static_cast < unsigned > ( -1 ) )
155 res.insert ( occurrence.first );
156 }
157
158 return res;
159}
160
161} /* namespace query */
162
163} /* namespace arbology */
164
Definition: NonlinearFullAndLinearIndexPatterns.h:24
static ext::set< unsigned > query(const indexes::arbology::NonlinearFullAndLinearIndex< SymbolType, StringIndex > &fullAndLinearIndex, const tree::PrefixRankedNonlinearPattern< SymbolType > &pattern)
Definition: NonlinearFullAndLinearIndexPatterns.h:67
Definition: ranked_symbol.hpp:20
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
Definition: set.hpp:44
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Full and linear tree index. The index serves as a adaptor of string index so that searching for tree ...
Definition: NonlinearFullAndLinearIndex.h:50
const ext::vector< unsigned > & getRepeats() const &
Definition: NonlinearFullAndLinearIndex.h:220
const StringIndex< common::ranked_symbol< SymbolType > > & getStringIndex() const &
Definition: NonlinearFullAndLinearIndex.h:200
const ext::vector< int > & getJumps() const &
Definition: NonlinearFullAndLinearIndex.h:210
Linear string.
Definition: LinearString.h:57
Definition: PositionHeapFactors.h:24
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
const common::ranked_symbol< SymbolType > & getVariablesBar() const &
Definition: PrefixRankedBarNonlinearPattern.h:295
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
Definition: BoyerMooreHorspool.h:22
int i
Definition: AllEpsilonClosure.h:118
return res
Definition: MinimizeByPartitioning.h:145
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79