Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
BorderArray.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <cstdlib>
9
10#include <alib/vector>
11
12#include <global/GlobalData.h>
13
14#include "SubtreeJumpTable.h"
15
21
22namespace {
23
24template < class PrefixRankedPatternType >
25bool symbolCompare ( [[maybe_unused]] const PrefixRankedPatternType& pattern, size_t i, size_t j ) {
26 return pattern.getContent ( )[ i ] == pattern.getContent ( )[ j ];
27}
28
29template < class SymbolType >
30bool symbolCompare ( const tree::PrefixRankedExtendedPattern < SymbolType > & pattern, size_t i, size_t j ) {
31 const auto& symbol1 = pattern.getContent ( )[ i ];
32 const auto& symbol2 = pattern.getContent ( )[ j ];
33 return symbol1 == symbol2 || ( symbol1.getRank ( ) == symbol2.getRank ( ) && ( pattern.getNodeWildcards ( ).contains ( symbol1 ) || pattern.getNodeWildcards ( ).contains ( symbol2 ) ) );
34}
35}
36
37namespace tree::properties {
38
40
41public:
42 template < class PrefixRankedPatternType >
43 static ext::vector < size_t > construct ( const PrefixRankedPatternType & pattern );
44};
45
46template < class PrefixRankedPatternType >
47ext::vector < size_t > BorderArray::construct ( const PrefixRankedPatternType & pattern ) {
48 ext::vector < int > subtreeJumpTable = SubtreeJumpTable::compute ( pattern );
49 ext::vector < size_t > res ( pattern.getContent ( ).size ( ) + 1, 0 );
50 ext::vector < unsigned > maxs ( pattern.getContent ( ).size ( ) + 1 );
51 for ( unsigned i = 0; i <= pattern.getContent ( ).size ( ); i++ )
52 maxs [ i ] = 0;
53 res [ 0 ] = -1;
54
55 for ( unsigned ofs = 1; ofs < pattern.getContent ( ).size ( ); ofs++ ) {
56
57 unsigned i = 0; // index do patternu
58 unsigned j = ofs; // index do factoru
59
60 while ( true ) {
61 if ( i >= pattern.getContent ( ).size ( ) ) {
62 while ( j < pattern.getContent ( ).size ( ) ) {
63 maxs [ j ] = std::max ( maxs [ j ], j + 1 - ofs );
64 j++;
65 }
66 break;
67 } else if ( j >= pattern.getContent ( ).size ( ) ) { // NOLINT (bugprone-branch-clone) just a break statement
68 break;
69 } else if ( symbolCompare ( pattern, i, j ) ) {
70 maxs [ j ] = std::max ( maxs [ j ], j + 1 - ofs );
71 i++;
72 j++;
73 } else if ( pattern.getContent ( )[ i ] == pattern.getSubtreeWildcard ( ) || pattern.getContent ( )[ j ] == pattern.getSubtreeWildcard ( ) ) {
74 for ( int k = static_cast < int > ( j ); k < subtreeJumpTable [ j ] ; k++ )
75 maxs [ k ] = std::max ( maxs [ k ], k + 1 - ofs );
76
77 i = subtreeJumpTable[ i ];
78 j = subtreeJumpTable[ j ];
79 } else {
80 break;
81 }
82 }
83 }
84
85 // std::cout << maxs << std::endl;
86
87 res [ 1 ] = 0;
88 for ( size_t i = 2; i <= pattern.getContent ( ).size ( ); i++ ) {
89 res [ i ] = maxs [ i - 1 ];
90 }
91
92 return res;
93}
94
95} /* namespace tree::properties */
96
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
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
Definition: BorderArray.h:39
static ext::vector< size_t > construct(const PrefixRankedPatternType &pattern)
Definition: BorderArray.h:47
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 & max(const T &a)
Root case of maximum computation. The maximum from one value is the value itself.
Definition: algorithm.hpp:278
Definition: BadCharacterShiftTable.h:18