Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
FullAndLinearIndexPatterns.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 >
68 ext::vector < unsigned > rev ( fullAndLinearIndex.getJumps ( ).size ( ), static_cast < unsigned > ( -1 ) );
69
71 treePatternParts.push_back ( ext::vector < common::ranked_symbol < SymbolType > > ( ) );
72 for ( const common::ranked_symbol < SymbolType > & symbol : pattern.getContent ( ) ) {
73 if ( pattern.getSubtreeWildcard ( ) == symbol ) {
74 treePatternParts.push_back ( ext::vector < common::ranked_symbol < SymbolType > > ( ) );
75 } else {
76 treePatternParts.back ( ).push_back ( symbol );
77 }
78 }
79
80 ext::vector < std::pair < unsigned, unsigned > > prevOcc = FullAndLinearIndexPatterns::FindOccurrences < StringIndexQueryAlgo > ( fullAndLinearIndex.getStringIndex ( ) , treePatternParts [ 0 ] );
81
82 for ( unsigned i = 1; i < treePatternParts.size ( ); ++ i ) {
83 for ( std::pair < unsigned, unsigned > & occurrence : prevOcc )
84 occurrence.second = fullAndLinearIndex.getJumps ( ) [ occurrence.second ];
85
86 if ( ! treePatternParts [ i ].empty ( ) )
87 prevOcc = MergeOccurrences ( prevOcc, FullAndLinearIndexPatterns::FindOccurrences < StringIndexQueryAlgo > ( fullAndLinearIndex.getStringIndex ( ), treePatternParts [ i ] ), rev );
88 }
89
91 for ( const std::pair < unsigned, unsigned > & occurrence : prevOcc ) {
92 res.insert ( occurrence.first );
93 }
94
95 return res;
96}
97
98template < class SymbolType, template < typename > class StringIndex, class StringIndexQueryAlgo >
100 ext::vector < unsigned > rev ( fullAndLinearIndex.getJumps ( ).size ( ), static_cast < unsigned > ( -1 ) );
101
103 treePatternParts.push_back ( ext::vector < common::ranked_symbol < SymbolType > > ( ) );
104 for ( typename ext::vector < common::ranked_symbol < SymbolType > >::const_iterator symbolIter = pattern.getContent ( ).begin ( ); symbolIter != pattern.getContent ( ).end ( ); ++symbolIter ) {
105 if ( pattern.getSubtreeWildcard ( ) == * symbolIter ) {
106 treePatternParts.push_back ( ext::vector < common::ranked_symbol < SymbolType > > ( ) );
107 ++ symbolIter;
108 } else {
109 treePatternParts.back ( ).push_back ( * symbolIter );
110 }
111 }
112
113 ext::vector < std::pair < unsigned, unsigned > > prevOcc = FullAndLinearIndexPatterns::FindOccurrences < StringIndexQueryAlgo > ( fullAndLinearIndex.getStringIndex ( ), treePatternParts [ 0 ] );
114
115 for ( unsigned i = 1; i < treePatternParts.size ( ); ++ i ) {
116 for ( std::pair < unsigned, unsigned > & occurrence : prevOcc )
117 occurrence.second = fullAndLinearIndex.getJumps ( ) [ occurrence.second ];
118
119 if ( ! treePatternParts [ i ].empty ( ) )
120 prevOcc = MergeOccurrences ( prevOcc, FullAndLinearIndexPatterns::FindOccurrences < StringIndexQueryAlgo > ( fullAndLinearIndex.getStringIndex ( ), treePatternParts [ i ] ), rev );
121 }
122
124 for ( const std::pair < unsigned, unsigned > & occurrence : prevOcc ) {
125 res.insert ( occurrence.first );
126 }
127
128 return res;
129}
130
131} /* namespace query */
132
133} /* namespace arbology */
134
Definition: FullAndLinearIndexPatterns.h:24
static ext::set< unsigned > query(const indexes::arbology::FullAndLinearIndex< SymbolType, StringIndex > &fullAndLinearIndex, const tree::PrefixRankedPattern< SymbolType > &pattern)
Definition: FullAndLinearIndexPatterns.h:67
Definition: ranked_symbol.hpp:20
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: FullAndLinearIndex.h:49
const StringIndex< common::ranked_symbol< SymbolType > > & getStringIndex() const &
Definition: FullAndLinearIndex.h:179
const ext::vector< int > & getJumps() const &
Definition: FullAndLinearIndex.h:189
Linear string.
Definition: LinearString.h:57
Definition: PositionHeapFactors.h:24
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: 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: 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