Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
TreeAuxiliary.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <stack>
9
10#include <ext/algorithm>
11
12#include <alib/set>
13#include <alib/tree>
14#include <alib/list>
16
17namespace tree {
18
23 template < class SymbolType >
24 static void postfixToPrefixInt ( const ext::vector < common::ranked_symbol < SymbolType > > & src, ext::vector < common::ranked_symbol < SymbolType > > & dest, unsigned & readIndex );
25
26 template < class SymbolType >
27 static ext::tree < common::ranked_symbol < SymbolType > > prefixToTreeInt ( const ext::vector < common::ranked_symbol < SymbolType > > & from, unsigned & index );
28
29 template < class SymbolType >
30 static void sortInt ( ext::tree < SymbolType > & tree );
31public:
32 template < class SymbolType >
34 template < class SymbolType >
36 template < class SymbolType >
38
39 template < class SymbolType >
41 template < class SymbolType >
43
44 template < class SymbolType >
46 template < class SymbolType >
48
49 template < class SymbolType >
51
52 template < class SymbolType >
54 template < class SymbolType >
56 template < class SymbolType >
58 template < class SymbolType >
60 template < class SymbolType >
62
63 template < class SymbolType >
65};
66
67template < class SymbolType >
68void TreeAuxiliary::sortInt ( ext::tree < SymbolType > & tree ) {
69 for ( ext::tree < SymbolType > & child : tree.getChildren ( ) ) {
70 sortInt ( child );
71 }
72 std::sort ( tree.getChildren ( ).begin ( ), tree.getChildren ( ).end ( ) );
73}
74
75template < class SymbolType >
77 sortInt ( tree );
78 return tree;
79}
80
81template < class SymbolType >
83 return ext::transform < SymbolType > ( alphabet, [&] ( const common::ranked_symbol < SymbolType > & symbol ) {
84 return symbol.getSymbol ( );
85 } );
86}
87
88template < class SymbolType >
90 return ext::transform < common::ranked_symbol < SymbolType > > ( alphabet, [&] ( const common::ranked_symbol < SymbolType > & symbol ) {
91 return common::ranked_symbol < SymbolType > ( barBase, symbol.getRank ( ) );
92 } );
93}
94
95template < class SymbolType >
98
99 for ( const ext::tree < SymbolType > & child : tree ) {
100 children.push_back ( unrankedToRanked < SymbolType > ( child ) );
101 }
102
103 unsigned size = children.size ( );
104 return ext::tree < common::ranked_symbol < SymbolType > > ( common::ranked_symbol < SymbolType > ( tree.getData ( ), size ), std::move ( children ) );
105}
106
107template < class SymbolType >
109 std::stack < ext::tree < common::ranked_symbol < SymbolType > > > tree_stack;
110
111 for ( const common::ranked_symbol < SymbolType > & x : from ) {
112 if ( x.getRank ( ) == 0 ) {
114 } else {
116
117 for ( unsigned i = 0; i < x.getRank ( ); ++i ) {
118 children.push_back ( tree_stack.top ( ) );
119 tree_stack.pop ( );
120 }
121
122 std::reverse ( children.begin ( ), children.end ( ) );
123
124 tree_stack.push ( ext::tree < common::ranked_symbol < SymbolType > > ( x, std::move ( children ) ) );
125 }
126 }
127
128 return tree_stack.top ( );
129}
130
131template < class SymbolType >
132ext::tree < common::ranked_symbol < SymbolType > > TreeAuxiliary::prefixToTreeInt ( const ext::vector < common::ranked_symbol < SymbolType > > & from, unsigned & index ) {
133 const common::ranked_symbol < SymbolType > & x = from [ index ++ ];
134
136
137 for ( unsigned i = 0; i < x.getRank ( ); ++i ) {
138 children.push_back ( prefixToTreeInt ( from, index ) );
139 }
140
141 return ext::tree < common::ranked_symbol < SymbolType > > ( x, std::move ( children ) );
142}
143
144template < class SymbolType >
146 unsigned index = 0;
147 return prefixToTreeInt ( from, index );
148}
149
150template < class SymbolType >
153
154 for ( const ext::tree < common::ranked_symbol < SymbolType > > & child : tree ) {
155 children.push_back ( rankedToUnranked < SymbolType > ( child ) );
156 }
157
158 return ext::tree < SymbolType > ( tree.getData ( ).getSymbol ( ), std::move ( children ) );
159}
160
161template < class SymbolType >
163 unsigned readIndex = src.size ( ) - 1;
164
166 postfixToPrefixInt ( src, dest, readIndex );
167
168 std::reverse ( dest.begin ( ), dest.end ( ) );
169
170 return dest;
171}
172
173template < class SymbolType >
174void TreeAuxiliary::postfixToPrefixInt ( const ext::vector < common::ranked_symbol < SymbolType > > & src, ext::vector < common::ranked_symbol < SymbolType > > & dest, unsigned & readIndex ) {
175 unsigned root = readIndex --;
176 unsigned rank = src [ root ].getRank ( );
177
178 for ( unsigned i = 0; i < rank; ++ i )
179 postfixToPrefixInt ( src, dest, readIndex );
180
181 dest.push_back ( src [ root ] );
182}
183
184template < class SymbolType >
187
188 for ( typename ext::tree < common::ranked_symbol < SymbolType > >::const_prefix_iterator iter = tree.prefix_begin ( ); iter != tree.prefix_end ( ); ++iter )
189 res.push_back ( * iter );
190
191 return res;
192}
193
194template < class SymbolType >
197
198 for ( typename ext::tree < SymbolType >::const_structure_iterator iter = tree.structure_begin ( ); iter != tree.structure_end ( ); ++iter )
199 if ( iter.getVirtual ( ) )
200 res.push_back ( bar );
201 else
202 res.push_back ( * iter );
203
204 return res;
205}
206
207template < class SymbolType >
210
211 for ( typename ext::tree < common::ranked_symbol < SymbolType > >::const_structure_iterator iter = tree.structure_begin ( ); iter != tree.structure_end ( ); ++iter )
212 if ( iter.getVirtual ( ) )
213 res.push_back ( common::ranked_symbol < SymbolType > ( barBase, iter->getRank ( ) ) );
214 else
215 res.push_back ( * iter );
216
217 return res;
218}
219
220template < class SymbolType >
223
224 for ( typename ext::tree < common::ranked_symbol < SymbolType > >::const_structure_iterator iter = tree.structure_begin ( ); iter != tree.structure_end ( ); ++iter )
225 if ( iter.getVirtual ( ) )
226 if ( ( * iter == subtreeWildcard ) )
227 res.push_back ( variablesBar );
228 else
229 res.push_back ( common::ranked_symbol < SymbolType > ( barBase, iter->getRank ( ) ) );
230 else
231 res.push_back ( * iter );
232
233 return res;
234}
235
236template < class SymbolType >
239
240 for ( typename ext::tree < common::ranked_symbol < SymbolType > >::const_structure_iterator iter = tree.structure_begin ( ); iter != tree.structure_end ( ); ++iter )
241 if ( iter.getVirtual ( ) )
242 if ( ( * iter == subtreeWildcard ) || nonlinearVariables.count ( * iter ) )
243 res.push_back ( variablesBar );
244 else
245 res.push_back ( common::ranked_symbol < SymbolType > ( barBase, iter->getRank ( ) ) );
246 else
247 res.push_back ( * iter );
248
249 return res;
250}
251
252template < class SymbolType >
255
256 for ( typename ext::tree < common::ranked_symbol < SymbolType > >::const_postfix_iterator iter = tree.postfix_begin ( ); iter != tree.postfix_end ( ); ++iter )
257 res.push_back ( * iter );
258
259 return res;
260}
261
262} /* namespace tree */
263
Definition: ranked_symbol.hpp:20
const SymbolType & getSymbol() const &
Definition: ranked_symbol.hpp:73
const size_t & getRank() const &
Definition: ranked_symbol.hpp:83
Definition: set.hpp:44
The iterator type over structure of the tree representing nodes and node_ends.
Definition: tree.hpp:152
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
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: vector.hpp:125
auto end() &
Inherited behavior of end for non-const instance.
Definition: vector.hpp:155
Definition: TreeAuxiliary.h:22
static ext::set< common::ranked_symbol< SymbolType > > computeBars(const ext::set< common::ranked_symbol< SymbolType > > &alphabet, const SymbolType &barBase)
Definition: TreeAuxiliary.h:89
static ext::tree< common::ranked_symbol< SymbolType > > unrankedToRanked(const ext::tree< SymbolType > &tree)
Definition: TreeAuxiliary.h:96
static ext::vector< common::ranked_symbol< SymbolType > > postfixToPrefix(const ext::vector< common::ranked_symbol< SymbolType > > &src)
Definition: TreeAuxiliary.h:162
static ext::tree< common::ranked_symbol< SymbolType > > prefixToTree(const ext::vector< common::ranked_symbol< SymbolType > > &from)
Definition: TreeAuxiliary.h:145
static ext::vector< common::ranked_symbol< SymbolType > > treeToPrefix(const ext::tree< common::ranked_symbol< SymbolType > > &tree)
Definition: TreeAuxiliary.h:185
static ext::vector< common::ranked_symbol< SymbolType > > treeToPostfix(const ext::tree< common::ranked_symbol< SymbolType > > &tree)
Definition: TreeAuxiliary.h:253
static ext::tree< SymbolType > sort(ext::tree< SymbolType > tree)
Definition: TreeAuxiliary.h:76
static ext::tree< common::ranked_symbol< SymbolType > > postfixToTree(const ext::vector< common::ranked_symbol< SymbolType > > &from)
Definition: TreeAuxiliary.h:108
static ext::set< SymbolType > unrankSymbols(const ext::set< common::ranked_symbol< SymbolType > > &alphabet)
Definition: TreeAuxiliary.h:82
static ext::tree< SymbolType > rankedToUnranked(const ext::tree< common::ranked_symbol< SymbolType > > &tree)
Definition: TreeAuxiliary.h:151
Definition: BarSymbol.cpp:12
int i
Definition: AllEpsilonClosure.h:118
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
return res
Definition: MinimizeByPartitioning.h:145
Definition: BackwardOccurrenceTest.h:17