Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
RankedTree.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 RankedTree;
32
33} /* namespace tree */
34
35
36#include <ext/iostream>
37#include <ext/algorithm>
38
39#include <alib/string>
40#include <alib/set>
41#include <alib/tree>
42
43#include <core/components.hpp>
45
46#include <tree/TreeException.h>
48
49#include <core/normalize.hpp>
51
52#include "../unranked/UnrankedTree.h"
53#include "PostfixRankedTree.h"
54#include "PrefixRankedTree.h"
55
56namespace tree {
57
58class GeneralAlphabet;
59
71template < class SymbolType >
72class RankedTree final : public core::Components < RankedTree < SymbolType >, ext::set < common::ranked_symbol < SymbolType > >, component::Set, GeneralAlphabet > {
77
85 void checkAlphabet ( const ext::tree < common::ranked_symbol < SymbolType > > & data ) const;
86
94 void checkArities ( const ext::tree < common::ranked_symbol < SymbolType > > & data ) const;
95
96public:
104
111
117 explicit RankedTree ( const UnrankedTree < SymbolType > & other );
118
124 explicit RankedTree ( const PostfixRankedTree < SymbolType > & other );
125
131 explicit RankedTree ( const PrefixRankedTree < SymbolType > & other );
132
139 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
140 }
141
148 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
149 }
150
157 this->template accessComponent < GeneralAlphabet > ( ).add ( symbols );
158 }
159
166
172 ext::tree < common::ranked_symbol < SymbolType > > && getContent ( ) &&;
173
181 void setTree ( ext::tree < common::ranked_symbol < SymbolType > > tree );
182
190 auto operator <=> ( const RankedTree & other ) const {
191 return std::tie ( m_content, getAlphabet() ) <=> std::tie ( other.m_content, other.getAlphabet() );
192 }
193
201 bool operator == ( const RankedTree & other ) const {
202 return std::tie ( m_content, getAlphabet() ) == std::tie ( other.m_content, other.getAlphabet() );
203 }
204
213 friend ext::ostream & operator << ( ext::ostream & out, const RankedTree & instance ) {
214 out << "(RankedTree";
215 out << " alphabet = " << instance.getAlphabet ( );
216 out << " content = " << instance.getContent ( );
217 out << ")";
218 return out;
219 }
220
226 void nicePrint ( ext::ostream & os ) const;
227};
228
229template < class SymbolType >
231 checkAlphabet ( m_content );
232 checkArities ( m_content );
233}
234
235template < class SymbolType >
237}
238
239template < class SymbolType >
240RankedTree < SymbolType >::RankedTree ( const UnrankedTree < SymbolType > & other ) : RankedTree ( TreeAuxiliary::unrankedToRanked < SymbolType > ( other.getContent ( ) ) ) {
241}
242
243template < class SymbolType >
244RankedTree < SymbolType >::RankedTree ( const PostfixRankedTree < SymbolType > & other) : RankedTree ( TreeAuxiliary::postfixToTree < SymbolType > ( other.getContent ( ) ) ) {
245}
246
247template < class SymbolType >
248RankedTree < SymbolType >::RankedTree ( const PrefixRankedTree < SymbolType > & other) : RankedTree ( TreeAuxiliary::prefixToTree < SymbolType > ( other.getContent ( ) ) ) {
249}
250
251template < class SymbolType >
253 return m_content;
254}
255
256template < class SymbolType >
258 return std::move ( m_content );
259}
260
261template < class SymbolType >
263 ext::set < common::ranked_symbol < SymbolType > > minimalAlphabet ( data.prefix_begin ( ), data.prefix_end ( ) );
264 std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const common::ranked_symbol < SymbolType > & ) {
265 throw TreeException ( "Input symbols not in the alphabet." );
266 } ) );
267}
268
269template < class SymbolType >
270void RankedTree < SymbolType >::checkArities ( const ext::tree < common::ranked_symbol < SymbolType > > & data ) const {
271 if ( data.getData ( ).getRank ( ) != data.getChildren ( ).size ( ) )
272 throw exception::CommonException ( "Invalid rank." );
273
274 for ( const ext::tree < common::ranked_symbol < SymbolType > > & child : data )
275 checkArities ( child );
276}
277
278template < class SymbolType >
280 checkAlphabet ( tree );
281 checkArities ( tree );
282
283 this->m_content = std::move ( tree );
284}
285
286template < class SymbolType >
288 m_content.nicePrint ( os );
289}
290
291} /* namespace tree */
292
293namespace core {
294
300template < class SymbolType >
301class SetConstraint< tree::RankedTree < SymbolType >, common::ranked_symbol < SymbolType >, tree::GeneralAlphabet > {
302public:
312 const ext::tree < common::ranked_symbol < SymbolType > > & m_content = tree.getContent ( );
313 return std::find(m_content.prefix_begin(), m_content.prefix_end(), symbol) != m_content.prefix_end();
314 }
315
325 return true;
326 }
327
335 }
336};
337
343template < class SymbolType >
344struct normalize < tree::RankedTree < SymbolType > > {
348
349 return tree::RankedTree < > ( std::move ( alphabet ), std::move ( content ) );
350 }
351};
352
353} /* namespace core */
354
355extern template class tree::RankedTree < >;
356
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 bool used(const tree::RankedTree< SymbolType > &tree, const common::ranked_symbol< SymbolType > &symbol)
Definition: RankedTree.h:311
static bool available(const tree::RankedTree< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: RankedTree.h:324
static void valid(const tree::RankedTree< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: RankedTree.h:334
Definition: setComponents.hpp:26
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
Output iterator calling a callback function on assignment.
Definition: iterator.hpp:923
Definition: ostream.h:14
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
const_prefix_iterator prefix_begin() const
Getter of the prefix iterator to the root node.
Definition: tree.hpp:904
const_prefix_iterator prefix_end() const
Getter of the prefix iterator one after the last node in the prefix traversal.
Definition: tree.hpp:914
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. 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
void extendAlphabet(const ext::set< common::ranked_symbol< SymbolType > > &symbols)
Definition: RankedTree.h:156
bool operator==(const RankedTree &other) const
Definition: RankedTree.h:201
friend ext::ostream & operator<<(ext::ostream &out, const RankedTree &instance)
Definition: RankedTree.h:213
ext::set< common::ranked_symbol< SymbolType > > && getAlphabet() &&
Definition: RankedTree.h:147
const ext::set< common::ranked_symbol< SymbolType > > & getAlphabet() const &
Definition: RankedTree.h:138
RankedTree(ext::set< common::ranked_symbol< SymbolType > > alphabet, ext::tree< common::ranked_symbol< SymbolType > > tree)
Creates a new instance of the tree with concrete alphabet and content.
Definition: RankedTree.h:230
void setTree(ext::tree< common::ranked_symbol< SymbolType > > tree)
Definition: RankedTree.h:279
void nicePrint(ext::ostream &os) const
Definition: RankedTree.h:287
const ext::tree< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: RankedTree.h:252
Definition: TreeException.h:15
static ext::tree< common::ranked_symbol< DefaultSymbolType > > normalizeRankedTree(ext::tree< common::ranked_symbol< SymbolType > > &&tree)
Definition: TreeNormalize.h:40
Tree represented in its natural representation. The representation is so called unranked,...
Definition: UnrankedTree.h:69
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::RankedTree< > eval(tree::RankedTree< SymbolType > &&value)
Definition: RankedTree.h:345
Definition: normalize.hpp:13