Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
UnorderedRankedTree.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 UnorderedRankedTree;
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
53
54namespace tree {
55
56class GeneralAlphabet;
57
69template < class SymbolType >
70class UnorderedRankedTree final : public core::Components < UnorderedRankedTree < SymbolType >, ext::set < common::ranked_symbol < SymbolType > >, component::Set, GeneralAlphabet > {
75
83 void checkAlphabet ( const ext::tree < common::ranked_symbol < SymbolType > > & data ) const;
84
92 void checkArities ( const ext::tree < common::ranked_symbol < SymbolType > > & data ) const;
93
94public:
102
109
116
123 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
124 }
125
132 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
133 }
134
141 this->template accessComponent < GeneralAlphabet > ( ).add ( symbols );
142 }
143
150
156 ext::tree < common::ranked_symbol < SymbolType > > && getContent ( ) &&;
157
165 void setTree ( ext::tree < common::ranked_symbol < SymbolType > > tree );
166
174 auto operator <=> ( const UnorderedRankedTree & other ) const {
175 return std::tie ( m_content, getAlphabet() ) <=> std::tie ( other.m_content, other.getAlphabet() );
176 }
177
185 bool operator == ( const UnorderedRankedTree & other ) const {
186 return std::tie ( m_content, getAlphabet() ) == std::tie ( other.m_content, other.getAlphabet() );
187 }
188
197 friend ext::ostream & operator << ( ext::ostream & out, const UnorderedRankedTree & instance ) {
198 out << "(UnorderedRankedTree";
199 out << " alphabet = " << instance.getAlphabet ( );
200 out << " content = " << instance.getContent ( );
201 out << ")";
202 return out;
203 }
204
210 void nicePrint ( ext::ostream & os ) const;
211};
212
213template < class SymbolType >
215 checkAlphabet ( m_content );
216 checkArities ( m_content );
217}
218
219template < class SymbolType >
221}
222
223template < class SymbolType >
225}
226
227template < class SymbolType >
229 return m_content;
230}
231
232template < class SymbolType >
234 return std::move ( m_content );
235}
236
237template < class SymbolType >
239 ext::set < common::ranked_symbol < SymbolType > > minimalAlphabet ( data.prefix_begin ( ), data.prefix_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
245template < class SymbolType >
246void UnorderedRankedTree < SymbolType >::checkArities ( const ext::tree < common::ranked_symbol < SymbolType > > & data ) const {
247 if ( data.getData ( ).getRank ( ) != data.getChildren ( ).size ( ) )
248 throw exception::CommonException ( "Invalid rank." );
249
250 for ( const ext::tree < common::ranked_symbol < SymbolType > > & child : data )
251 checkArities ( child );
252}
253
254template < class SymbolType >
256 checkAlphabet ( tree );
257 checkArities ( tree );
258
259 this->m_content = TreeAuxiliary::sort ( std::move ( tree ) );
260}
261
262template < class SymbolType >
264 m_content.nicePrint ( os );
265}
266
267} /* namespace tree */
268
269namespace core {
270
276template < class SymbolType >
277class SetConstraint< tree::UnorderedRankedTree < SymbolType >, common::ranked_symbol < SymbolType >, tree::GeneralAlphabet > {
278public:
288 const ext::tree < common::ranked_symbol < SymbolType > > & m_content = tree.getContent ( );
289 return std::find(m_content.prefix_begin(), m_content.prefix_end(), symbol) != m_content.prefix_end();
290 }
291
301 return true;
302 }
303
311 }
312};
313
319template < class SymbolType >
320struct normalize < tree::UnorderedRankedTree < SymbolType > > {
324
325 return tree::UnorderedRankedTree < > ( std::move ( alphabet ), std::move ( content ) );
326 }
327};
328
329} /* namespace core */
330
331extern template class tree::UnorderedRankedTree < >;
332
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::UnorderedRankedTree< SymbolType > &tree, const common::ranked_symbol< SymbolType > &symbol)
Definition: UnorderedRankedTree.h:287
static bool available(const tree::UnorderedRankedTree< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: UnorderedRankedTree.h:300
static void valid(const tree::UnorderedRankedTree< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: UnorderedRankedTree.h:310
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 in its natural representation. The representation is so called ranked,...
Definition: RankedTree.h:72
static ext::tree< SymbolType > sort(ext::tree< SymbolType > tree)
Definition: TreeAuxiliary.h:76
Definition: TreeException.h:15
static ext::tree< common::ranked_symbol< DefaultSymbolType > > normalizeRankedTree(ext::tree< common::ranked_symbol< SymbolType > > &&tree)
Definition: TreeNormalize.h:40
Tree structure represented in its natural representation. The representation is so called ranked,...
Definition: UnorderedRankedTree.h:70
ext::set< common::ranked_symbol< SymbolType > > && getAlphabet() &&
Definition: UnorderedRankedTree.h:131
const ext::set< common::ranked_symbol< SymbolType > > & getAlphabet() const &
Definition: UnorderedRankedTree.h:122
void extendAlphabet(const ext::set< common::ranked_symbol< SymbolType > > &symbols)
Definition: UnorderedRankedTree.h:140
UnorderedRankedTree(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: UnorderedRankedTree.h:214
friend ext::ostream & operator<<(ext::ostream &out, const UnorderedRankedTree &instance)
Definition: UnorderedRankedTree.h:197
void nicePrint(ext::ostream &os) const
Definition: UnorderedRankedTree.h:263
const ext::tree< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: UnorderedRankedTree.h:228
bool operator==(const UnorderedRankedTree &other) const
Definition: UnorderedRankedTree.h:185
void setTree(ext::tree< common::ranked_symbol< SymbolType > > tree)
Definition: UnorderedRankedTree.h:255
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::UnorderedRankedTree< > eval(tree::UnorderedRankedTree< SymbolType > &&value)
Definition: UnorderedRankedTree.h:321
Definition: normalize.hpp:13