Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ReversedBadCharacterShiftTable.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/set>
9#include <alib/map>
10
15
17
18namespace tree {
19
20namespace properties {
21
27public:
32 template < class SymbolType >
34 template < class SymbolType >
36 template < class SymbolType >
38 template < class SymbolType >
40
41};
42
43template < class SymbolType >
46}
47
48template < class SymbolType >
51
53
54 // initialisation of bcs table to the size of the pattern
55 for ( const common::ranked_symbol < SymbolType > & symbol : alphabet ) {
56 if ( ( symbol == pattern.getSubtreeWildcard ( ) ) || ( pattern.getNonlinearVariables ( ).count ( symbol ) ) || ( symbol == pattern.getVariablesBar ( ) ) ) continue;
57
58 bcs.insert ( std::make_pair ( symbol, pattern.getContent ( ).size ( ) ) );
59 }
60
61 // find the distance between the beginning of the pattern and the index
62 // of the first symbol representing the variable's bar
63 size_t firstSBarOffset = FirstVariableOffsetFront::offset ( pattern ) + 1;
64
65 // limit the shift by occurrence of the last variable's bar
66 for ( const common::ranked_symbol < SymbolType > & symbol : alphabet ) {
67 if ( ( symbol == pattern.getSubtreeWildcard ( ) ) || ( pattern.getNonlinearVariables ( ).count ( symbol ) ) || ( symbol == pattern.getVariablesBar ( ) ) ) continue;
68
69 size_t tmp = firstSBarOffset;
70
71 if ( pattern.getBars ( ).count ( symbol ) )
72 // size of the smallest subtree containing given terminal depend
73 // on the arity of the terminal
74 tmp += symbol.getRank ( ) * 2;
75 else if ( tmp >= 2 )
76 // bar symbols match the variable bar which is one symbol after
77 // the last variable, conditioned because of the case S S| where
78 // the -1 would cause shift by 0 -- illegal
79 tmp -= 1;
80
81 if ( bcs[symbol] > tmp )
82 bcs[symbol] = tmp;
83 }
84
85 // limit the shift by position of symbols within the pattern
86 for ( unsigned i = pattern.getContent ( ).size ( ) - 1; i >= 1; i-- ) { // first symbol is not concerned
87 if ( ( pattern.getContent ( )[i] == pattern.getSubtreeWildcard ( ) ) || ( pattern.getNonlinearVariables ( ).count ( pattern.getContent ( )[i] ) ) || ( pattern.getContent ( )[i] == pattern.getVariablesBar ( ) ) ) continue;
88
89 size_t tmp = i;
90
91 if ( bcs[pattern.getContent ( )[i]] > tmp )
92 bcs[pattern.getContent ( )[i]] = tmp;
93 }
94
95 return bcs;
96}
97
98template < class SymbolType >
101}
102
103template < class SymbolType >
106
108
109 // initialisation of bcs table to the size of the pattern
110 for ( const common::ranked_symbol < SymbolType > & symbol : alphabet ) {
111 if ( symbol == pattern.getSubtreeWildcard ( ) || pattern.getNonlinearVariables ( ).count ( symbol ) ) continue;
112
113 bcs.insert ( std::make_pair ( symbol, pattern.getContent ( ).size ( ) ) );
114 }
115
116 // find the distance between the beginning of the pattern and the index
117 // of the first symbol representing the variable
118 size_t firstSOffset = FirstVariableOffsetFront::offset ( pattern );
119
120 // handle situation when the pattern is just S
121 if ( firstSOffset == 0 ) firstSOffset = 1;
122
123 // limit the shift by occurrence of the last variable
124 for ( const common::ranked_symbol < SymbolType > & symbol : alphabet ) {
125 if ( symbol == pattern.getSubtreeWildcard ( ) || pattern.getNonlinearVariables ( ).count ( symbol ) ) continue;
126
127 if ( bcs[symbol] > firstSOffset )
128 bcs[symbol] = firstSOffset;
129 }
130
131 // limit the shift by position of symbols within the pattern
132 for ( unsigned i = pattern.getContent ( ).size ( ) - 1; i >= 1; i-- ) { // first symbol is not concerned
133 if ( pattern.getContent ( )[i] == pattern.getSubtreeWildcard ( ) || pattern.getNonlinearVariables ( ).count ( pattern.getContent ( )[i] ) ) continue;
134 size_t tmp = i;
135
136 if ( bcs[pattern.getContent ( )[i]] > tmp )
137 bcs[pattern.getContent ( )[i]] = tmp;
138 }
139
140 return bcs;
141}
142
143} /* namespace properties */
144
145} /* namespace tree */
146
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
Definition: set.hpp:44
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 > > & getBars() const &
Definition: PrefixRankedBarNonlinearPattern.h:232
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::set< common::ranked_symbol< SymbolType > > & getAlphabet() const &
Definition: PrefixRankedBarNonlinearPattern.h:205
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
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 ext::set< common::ranked_symbol< SymbolType > > & getAlphabet() const &
Definition: PrefixRankedNonlinearPattern.h:163
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
static size_t offset(const tree::PrefixRankedPattern< SymbolType > &pattern)
Definition: FirstVariableOffsetFront.h:33
Definition: ReversedBadCharacterShiftTable.h:26
static ext::map< common::ranked_symbol< SymbolType >, size_t > bcs(const tree::PrefixRankedBarPattern< SymbolType > &pattern)
Definition: ReversedBadCharacterShiftTable.h:44
Definition: BarSymbol.cpp:12
int i
Definition: AllEpsilonClosure.h:118
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: BackwardOccurrenceTest.h:17