Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ExactSubtreeRepeatsNaive.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/map>
9#include <alib/vector>
10#include <alib/tree>
12
13#include "SubtreeJumpTable.h"
14
19#include <global/GlobalData.h>
20
21namespace tree {
22
23namespace properties {
24
29 template < class SymbolType >
31 template < class SymbolType >
33 template < class SymbolType >
35 template < class SymbolType >
36 static common::ranked_symbol < unsigned > repeatsPrefixRankedBar ( const ext::vector < common::ranked_symbol < SymbolType > > & symbols, ext::vector < common::ranked_symbol < unsigned > > & res, ext::map < std::pair < common::ranked_symbol <SymbolType >, ext::vector < common::ranked_symbol < unsigned > > >, unsigned > & data, unsigned & minId, unsigned barId, int & index );
37
38public:
43 template < class SymbolType >
45 template < class SymbolType >
47 template < class SymbolType >
49 template < class SymbolType >
51
52};
53
54template < class SymbolType >
55ext::tree < common::ranked_symbol < unsigned > > ExactSubtreeRepeatsNaive::repeats ( const ext::tree < common::ranked_symbol < SymbolType > > & node, ext::map < std::pair < common::ranked_symbol < SymbolType >, ext::vector < common::ranked_symbol < unsigned > > >, unsigned > & data, unsigned & minId ) {
57 std::pair < common::ranked_symbol < SymbolType >, ext::vector < common::ranked_symbol < unsigned > > > childRepeatsKey ( node.getData ( ), ext::vector < common::ranked_symbol < unsigned > > ( ) );
58
59 for ( const ext::tree < common::ranked_symbol < SymbolType > > & child : node.getChildren() ) {
60 children.push_back ( repeats ( child, data, minId ) );
61 childRepeatsKey.second.push_back ( children.back ( ).getData ( ) );
62 }
63
64 unsigned & uniqueRepeatId = data[childRepeatsKey];
65
66 if ( uniqueRepeatId == 0 ) uniqueRepeatId = minId++;
67
68 return ext::tree < common::ranked_symbol < unsigned > > ( common::ranked_symbol < unsigned > ( uniqueRepeatId, node.getData ( ).getRank ( ) ), std::move ( children ) );
69}
70
71template < class SymbolType >
72tree::RankedTree < unsigned > ExactSubtreeRepeatsNaive::repeats ( const tree::RankedTree < SymbolType > & tree ) {
73 unsigned minId = 1;
75
76 return tree::RankedTree < unsigned > ( repeats ( tree.getContent ( ), data, minId ) );
77}
78
79template < class SymbolType >
80common::ranked_symbol < unsigned > ExactSubtreeRepeatsNaive::repeatsPrefixRanked ( const ext::vector < common::ranked_symbol < SymbolType > > & symbols, ext::vector < common::ranked_symbol < unsigned > > & res, ext::map < std::pair < common::ranked_symbol < SymbolType >, ext::vector < common::ranked_symbol < unsigned > > >, unsigned > & data, unsigned & minId, int & index ) {
81 int begin = index;
82 std::pair < common::ranked_symbol < SymbolType >, ext::vector < common::ranked_symbol < unsigned > > > childRepeatsKey ( symbols[begin], ext::vector < common::ranked_symbol < unsigned > > ( ) );
83
84 res.push_back ( common::ranked_symbol < unsigned > ( 0, symbols[begin].getRank ( ) ) );
85
86 index++;
87
88 for ( unsigned i = 0; i < symbols[begin].getRank ( ); ++i )
89 childRepeatsKey.second.push_back ( repeatsPrefixRanked ( symbols, res, data, minId, index ) );
90
91 unsigned & uniqueRepeatId = data[childRepeatsKey];
92
93 if ( uniqueRepeatId == 0 ) uniqueRepeatId = minId++;
94
95 res[begin] = common::ranked_symbol < unsigned > ( uniqueRepeatId, symbols[begin].getRank ( ) );
96 return res[begin];
97}
98
99template < class SymbolType >
101 unsigned minId = 1;
102 int index = 0;
105
106 repeatsPrefixRanked ( tree.getContent ( ), res, data, minId, index );
107 return tree::PrefixRankedTree < unsigned > ( std::move ( res ) );
108}
109
110template < class SymbolType >
111common::ranked_symbol < unsigned > ExactSubtreeRepeatsNaive::repeatsPostfixRanked ( const ext::vector < common::ranked_symbol < SymbolType > > & symbols, ext::vector < common::ranked_symbol < unsigned > > & res, ext::map < std::pair < common::ranked_symbol < SymbolType >, ext::vector < common::ranked_symbol < unsigned > > >, unsigned > & data, unsigned & minId, int & index ) {
112 int begin = index;
113
114 std::pair < common::ranked_symbol < SymbolType >, ext::vector < common::ranked_symbol < unsigned > > > childRepeatsKey ( symbols[begin], ext::vector < common::ranked_symbol < unsigned > > ( ) );
115 res.insert ( res.begin ( ), common::ranked_symbol < unsigned > ( 0, symbols[begin].getRank ( ) ) );
116
117 index--;
118
119 for ( unsigned i = symbols.size ( ) - 1; i > symbols.size ( ) - 1 - symbols[begin].getRank ( ); --i )
120 childRepeatsKey.second.push_back ( repeatsPostfixRanked ( symbols, res, data, minId, index ) );
121
122 unsigned & uniqueRepeatId = data[childRepeatsKey];
123
124 if ( uniqueRepeatId == 0 ) uniqueRepeatId = minId++;
125
126 unsigned pos_in_res = begin - symbols.size ( ) + res.size ( );
127 res[pos_in_res] = common::ranked_symbol < unsigned > ( uniqueRepeatId, symbols[begin].getRank ( ) );
128 return res[pos_in_res];
129}
130
131template < class SymbolType >
133 unsigned minId = 1;
134 int index = tree.getContent ( ).size ( ) - 1;
135
138
139 repeatsPostfixRanked ( tree.getContent ( ), res, data, minId, index );
140 return tree::PostfixRankedTree < unsigned > ( std::move ( res ) );
141}
142
143
144template < class SymbolType >
145common::ranked_symbol < unsigned > ExactSubtreeRepeatsNaive::repeatsPrefixRankedBar ( const ext::vector < common::ranked_symbol < SymbolType > > & symbols, ext::vector < common::ranked_symbol < unsigned > > & res, ext::map < std::pair < common::ranked_symbol < SymbolType >, ext::vector < common::ranked_symbol < unsigned > > >, unsigned > & data, unsigned & minId, unsigned barId, int & index ) {
146 int begin = index;
147 std::pair < common::ranked_symbol < SymbolType >, ext::vector < common::ranked_symbol < unsigned > > > childRepeatsKey ( symbols[begin], ext::vector < common::ranked_symbol < unsigned > > ( ) );
148
149 res.push_back ( common::ranked_symbol < unsigned > ( 0, symbols[begin].getRank ( ) ) );
150
151 index++;
152
153 for ( unsigned i = 0; i < symbols[begin].getRank ( ); ++i )
154 childRepeatsKey.second.push_back ( repeatsPrefixRankedBar ( symbols, res, data, minId, barId, index ) );
155
156 unsigned & uniqueRepeatId = data[childRepeatsKey];
157
158 if ( uniqueRepeatId == 0 ) uniqueRepeatId = minId++;
159
160 res[begin] = common::ranked_symbol < unsigned > ( uniqueRepeatId, symbols[begin].getRank ( ) );
161 res.push_back ( common::ranked_symbol < unsigned > ( 0, symbols[index].getRank ( ) ) );
162 index++;
163
164 return res[begin];
165}
166
167template < class SymbolType >
169 unsigned barId = 0;
170 unsigned minId = barId + 1;
171 int index = 0;
174
175 repeatsPrefixRankedBar ( tree.getContent ( ), res, data, minId, barId, index );
176
178 for ( const common::ranked_symbol < SymbolType > & bar : tree.getBars ( ) )
179 bars.insert ( common::ranked_symbol < unsigned > ( barId, bar.getRank ( ) ) );
180
181 return tree::PrefixRankedBarTree < unsigned > ( std::move ( bars ), std::move ( res ) );
182}
183
184} /* namespace properties */
185
186} /* namespace tree */
187
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
Class introducing a tree with interface trying to be close to the interface of standard library conta...
Definition: tree.hpp:52
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Tree structure represented as linear sequece as result of postorder traversal. The representation is ...
Definition: PostfixRankedTree.h:73
Tree structure represented as linear sequece as result of preorder traversal with additional bar symb...
Definition: PrefixRankedBarTree.h:78
Tree structure represented as linear sequece as result of preorder traversal. The representation is s...
Definition: PrefixRankedTree.h:71
Tree structure represented in its natural representation. The representation is so called ranked,...
Definition: RankedTree.h:72
Definition: ExactSubtreeRepeatsNaive.h:28
int i
Definition: AllEpsilonClosure.h:118
return res
Definition: MinimizeByPartitioning.h:145
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
Definition: Node.cpp:11
Definition: BackwardOccurrenceTest.h:17