Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
PostfixRankedTree.h
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
27
28namespace tree {
29
30template < class SymbolType = DefaultSymbolType >
31class PostfixRankedTree;
32
33} /* namespace tree */
34
35#include <numeric>
36
37#include <ext/algorithm>
38
39#include <alib/set>
40#include <alib/vector>
41#include <alib/tree>
42#include <alib/deque>
43
44#include <core/components.hpp>
46
47#include <tree/TreeException.h>
49
50#include <core/normalize.hpp>
52
53#include <string/LinearString.h>
54
55#include "RankedTree.h"
56
57namespace tree {
58
59class GeneralAlphabet;
60
72template < class SymbolType >
73class PostfixRankedTree final : public core::Components < PostfixRankedTree < SymbolType >, ext::set < common::ranked_symbol < SymbolType > >, component::Set, GeneralAlphabet > {
78
84 void arityChecksum ( const ext::vector < common::ranked_symbol < SymbolType > > & data );
85
86public:
94
101
108
115 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
116 }
117
124 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
125 }
126
133 this->template accessComponent < GeneralAlphabet > ( ).add ( symbols );
134 }
135
142
148 ext::vector < common::ranked_symbol < SymbolType > > && getContent ( ) &&;
149
157 void setContent ( ext::vector < common::ranked_symbol < SymbolType > > data );
158
162 bool isEmpty ( ) const;
163
171 auto operator <=> ( const PostfixRankedTree & other ) const {
172 return std::tie ( m_Data, getAlphabet ( ) ) <=> std::tie ( other.m_Data, other.getAlphabet ( ) );
173 }
174
182 bool operator == ( const PostfixRankedTree & other ) const {
183 return std::tie ( m_Data, getAlphabet ( ) ) == std::tie ( other.m_Data, other.getAlphabet ( ) );
184 }
185
194 friend ext::ostream & operator << ( ext::ostream & out, const PostfixRankedTree & instance ) {
195 out << "(PostfixRankedTree";
196 out << " alphabet = " << instance.getAlphabet ( );
197 out << " content = " << instance.getContent ( );
198 out << ")";
199 return out;
200 }
201
209 }
210};
211
212template < class SymbolType >
214 setContent ( std::move ( data ) );
215}
216
217template < class SymbolType >
219}
220
221template < class SymbolType >
223}
224
225template < class SymbolType >
227 return this->m_Data;
228}
229
230template < class SymbolType >
232 return std::move ( this->m_Data );
233}
234
235template < class SymbolType >
237 arityChecksum ( data );
238
239 ext::set < common::ranked_symbol < SymbolType > > minimalAlphabet ( data.begin ( ), data.end ( ) );
240 std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const common::ranked_symbol < SymbolType > & ) {
241 throw TreeException ( "Input symbols not in the alphabet." );
242 } ) );
243
244 this->m_Data = std::move ( data );
245}
246
247template < class SymbolType >
249 if ( std::accumulate ( data.begin ( ), data.end ( ), 1, [ ] ( int current, const common::ranked_symbol < SymbolType > & symbol ) {
250 return current + symbol.getRank ( ) - 1;
251 } ) != 0 )
252 throw TreeException ( "The string does not form a tree" );
253}
254
255template < class SymbolType >
257 return this->m_Data.empty ( );
258}
259
260} /* namespace tree */
261
262namespace core {
263
269template < class SymbolType >
270class SetConstraint < tree::PostfixRankedTree < SymbolType >, common::ranked_symbol < SymbolType >, tree::GeneralAlphabet > {
271public:
281 const ext::vector < common::ranked_symbol < SymbolType > > & content = tree.getContent ( );
282
283 return std::find ( content.begin ( ), content.end ( ), symbol ) != content.end ( );
284 }
285
295 return true;
296 }
297
305 }
306
307};
308
314template < class SymbolType >
315struct normalize < tree::PostfixRankedTree < SymbolType > > {
319
320 return tree::PostfixRankedTree < > ( std::move ( alphabet ), std::move ( content ) );
321 }
322};
323
324} /* namespace core */
325
326extern template class tree::PostfixRankedTree < >;
327
static ext::vector< common::ranked_symbol< DefaultSymbolType > > normalizeRankedSymbols(ext::vector< common::ranked_symbol< SymbolType > > &&symbols)
Definition: SymbolNormalize.h:113
static ext::set< common::ranked_symbol< DefaultSymbolType > > normalizeRankedAlphabet(ext::set< common::ranked_symbol< SymbolType > > &&symbols)
Definition: SymbolNormalize.h:59
Definition: ranked_symbol.hpp:20
Definition: components.hpp:181
static void valid(const tree::PostfixRankedTree< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: PostfixRankedTree.h:304
static bool used(const tree::PostfixRankedTree< SymbolType > &tree, const common::ranked_symbol< SymbolType > &symbol)
Definition: PostfixRankedTree.h:280
static bool available(const tree::PostfixRankedTree< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: PostfixRankedTree.h:294
Definition: setComponents.hpp:26
Output iterator calling a callback function on assignment.
Definition: iterator.hpp:923
Definition: ostream.h:14
Definition: set.hpp:44
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: set.hpp:99
auto end() &
Inherited behavior of end for non-const instance.
Definition: set.hpp:129
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
Linear string.
Definition: LinearString.h:57
Tree structure represented as linear sequece as result of postorder traversal. The representation is ...
Definition: PostfixRankedTree.h:73
bool operator==(const PostfixRankedTree &other) const
Definition: PostfixRankedTree.h:182
const ext::set< common::ranked_symbol< SymbolType > > & getAlphabet() const &
Definition: PostfixRankedTree.h:114
ext::set< common::ranked_symbol< SymbolType > > && getAlphabet() &&
Definition: PostfixRankedTree.h:123
bool isEmpty() const
Definition: PostfixRankedTree.h:256
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PostfixRankedTree.h:226
void setContent(ext::vector< common::ranked_symbol< SymbolType > > data)
Definition: PostfixRankedTree.h:236
void extendAlphabet(const ext::set< common::ranked_symbol< SymbolType > > &symbols)
Definition: PostfixRankedTree.h:132
friend ext::ostream & operator<<(ext::ostream &out, const PostfixRankedTree &instance)
Definition: PostfixRankedTree.h:194
PostfixRankedTree(ext::set< common::ranked_symbol< SymbolType > > alphabet, ext::vector< common::ranked_symbol< SymbolType > > data)
Creates a new instance of the tree with concrete alphabet and content.
Definition: PostfixRankedTree.h:213
Tree structure represented in its natural representation. The representation is so called ranked,...
Definition: RankedTree.h:72
static ext::vector< common::ranked_symbol< SymbolType > > treeToPostfix(const ext::tree< common::ranked_symbol< SymbolType > > &tree)
Definition: TreeAuxiliary.h:253
Definition: TreeException.h:15
Definition: BarSymbol.cpp:12
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
Definition: Permutation.hpp:18
Definition: normalize.hpp:10
Definition: sigHandler.cpp:20
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
void end()
Definition: measurements.cpp:19
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
Definition: BackwardOccurrenceTest.h:17
static tree::PostfixRankedTree< > eval(tree::PostfixRankedTree< SymbolType > &&value)
Definition: PostfixRankedTree.h:316
Definition: normalize.hpp:13