Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ExactSubtreeMatch.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <ext/foreach>
9
10#include <alib/set>
11#include <alib/tree>
12#include <alib/deque>
14
19
20namespace arbology {
21
22namespace exact {
23
25public:
30 template < class SymbolType >
32 template < class SymbolType >
34 template < class SymbolType >
36 template < class SymbolType >
38private:
39 template < class SymbolType >
40 static bool matchHelper(const ext::tree < SymbolType > & subject, const ext::tree < SymbolType > & pattern);
41 template < class SymbolType >
42 static bool matchHelper(const ext::tree < common::ranked_symbol < SymbolType > > & subject, const ext::tree < common::ranked_symbol < SymbolType > > & pattern);
43
44 template < class SymbolType >
45 static void matchInternal(unsigned& index, ext::set<unsigned>& occ, const ext::tree < SymbolType > & subject, const ext::tree < SymbolType > & pattern);
46
47};
48
49template < class SymbolType >
50bool ExactSubtreeMatch::matchHelper ( const ext::tree < SymbolType > & subject, const ext::tree < SymbolType > & pattern ) {
51 if ( subject.getData ( ) != pattern.getData ( ) ) return false;
52
53 auto patternIter = pattern.getChildren ( ).begin ( );
54 auto subjectIter = subject.getChildren ( ).begin ( );
55
56 while ( patternIter != pattern.getChildren ( ).end ( ) && subjectIter != subject.getChildren ( ).end ( ) ) {
57 if ( ! matchHelper ( * subjectIter, * patternIter ) )
58 return false;
59
60 ++ patternIter;
61 ++ subjectIter;
62 }
63
64 return patternIter == pattern.getChildren ( ).end ( );
65}
66
67template < class SymbolType >
68bool ExactSubtreeMatch::matchHelper ( const ext::tree < common::ranked_symbol < SymbolType > > & subject, const ext::tree < common::ranked_symbol < SymbolType > > & pattern ) {
69 if ( subject.getData ( ) != pattern.getData ( ) ) return false;
70
71 // ranked symbols are the same; test for number of children is not needed
73 if ( !matchHelper ( std::get < 0 > ( childs ), std::get < 1 > ( childs ) ) ) return false;
74
75 return true;
76}
77
78template < class SymbolType >
79void ExactSubtreeMatch::matchInternal ( unsigned & index, ext::set < unsigned > & occ, const ext::tree < SymbolType > & subject, const ext::tree < SymbolType > & pattern ) {
80 if ( matchHelper ( subject, pattern ) ) occ.insert ( index );
81
82 index++;
83
84 for ( const ext::tree < SymbolType > & child : subject.getChildren ( ) )
85 matchInternal ( index, occ, child, pattern );
86}
87
88template < class SymbolType >
90 unsigned i = 0;
92
93 matchInternal ( i, occ, subject.getContent ( ), pattern.getContent ( ) );
94 return occ;
95}
96
97template < class SymbolType >
99 unsigned i = 0;
101
102 matchInternal ( i, occ, subject.getContent ( ), pattern.getContent ( ) );
103 return occ;
104}
105
106template < class SymbolType >
109
110 for ( unsigned i = 0; i + pattern.getContent ( ).size ( ) <= subject.getContent ( ).size ( ); i++ ) {
111 unsigned j = 0;
112
113 for ( ; j < pattern.getContent ( ).size ( ); j++ )
114 if ( pattern.getContent ( )[j] != subject.getContent ( )[i + j] ) break;
115
116 if ( j == pattern.getContent ( ).size ( ) )
117 occ.insert ( i );
118 }
119
120 return occ;
121}
122
123template < class SymbolType >
126
127 for ( unsigned i = 0; i + pattern.getContent ( ).size ( ) <= subject.getContent ( ).size ( ); i++ ) {
128 unsigned j = 0;
129
130 for ( ; j < pattern.getContent ( ).size ( ); j++ )
131 if ( pattern.getContent ( )[j] != subject.getContent ( )[i + j] ) break;
132
133 if ( j == pattern.getContent ( ).size ( ) )
134 occ.insert ( i );
135 }
136
137 return occ;
138}
139
140} /* namespace exact */
141
142} /* namespace arbology */
143
Definition: ExactSubtreeMatch.h:24
static ext::set< unsigned > match(const tree::UnrankedTree< SymbolType > &subject, const tree::UnrankedTree< SymbolType > &pattern)
Definition: ExactSubtreeMatch.h:89
Definition: ranked_symbol.hpp:20
Definition: set.hpp:44
Class introducing a tree with interface trying to be close to the interface of standard library conta...
Definition: tree.hpp:52
T & getData()
Getter of the value in the root node.
Definition: tree.hpp:100
ext::vector< tree > & getChildren()
Getter of children of the root node.
Definition: tree.hpp:120
Class extending the tuple class from the standard library. Original reason is to allow printing of th...
Definition: tuple.hpp:42
Tree structure represented as linear sequece as result of preorder traversal with additional bar symb...
Definition: PrefixRankedBarTree.h:78
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedBarTree.h:273
Tree structure represented as linear sequece as result of preorder traversal. The representation is s...
Definition: PrefixRankedTree.h:71
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedTree.h:235
Tree structure represented in its natural representation. The representation is so called ranked,...
Definition: RankedTree.h:72
const ext::tree< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: RankedTree.h:252
Tree represented in its natural representation. The representation is so called unranked,...
Definition: UnrankedTree.h:69
const ext::tree< SymbolType > & getContent() const &
Definition: UnrankedTree.h:217
Definition: BoyerMooreHorspool.h:22
int i
Definition: AllEpsilonClosure.h:118
const_tuple_foreach< Types ... > make_tuple_foreach(const Types &... args)
Function construction of foreach tuple pack helper.
Definition: foreach.hpp:280