Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
PrefixRankedTree.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 PrefixRankedTree;
32
33} /* namespace tree */
34
35
36#include <ext/algorithm>
37
38#include <alib/set>
39#include <alib/vector>
40#include <alib/tree>
41#include <alib/deque>
42
43#include <core/components.hpp>
45
46#include <tree/TreeException.h>
47
48#include <core/normalize.hpp>
50
51#include <string/LinearString.h>
52
53#include "RankedTree.h"
54
55namespace tree {
56
57class GeneralAlphabet;
58
70template < class SymbolType >
71class PrefixRankedTree final : public core::Components < PrefixRankedTree < SymbolType >, ext::set < common::ranked_symbol < SymbolType > >, component::Set, GeneralAlphabet > {
76
82 void arityChecksum ( const ext::vector < common::ranked_symbol < SymbolType > > & data );
83
84public:
92
99
106
112 explicit PrefixRankedTree ( const PostfixRankedTree < SymbolType > & other );
113
120 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
121 }
122
129 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
130 }
131
138 this->template accessComponent < GeneralAlphabet > ( ).add ( symbols );
139 }
140
147
153 ext::vector < common::ranked_symbol < SymbolType > > && getContent ( ) &&;
154
162 void setContent ( ext::vector < common::ranked_symbol < SymbolType > > data );
163
167 bool isEmpty ( ) const;
168
176 auto operator <=> ( const PrefixRankedTree & other ) const {
177 return std::tie ( m_Data, getAlphabet() ) <=> std::tie ( other.m_Data, other.getAlphabet() );
178 }
179
187 bool operator == ( const PrefixRankedTree & other ) const {
188 return std::tie ( m_Data, getAlphabet() ) == std::tie ( other.m_Data, other.getAlphabet() );
189 }
190
199 friend ext::ostream & operator << ( ext::ostream & out, const PrefixRankedTree & instance ) {
200 out << "(PrefixRankedTree";
201 out << " alphabet = " << instance.getAlphabet ( );
202 out << " content = " << instance.getContent ( );
203 out << ")";
204 return out;
205 }
206
214 }
215};
216
217template < class SymbolType >
219 setContent ( std::move ( data ) );
220}
221
222template < class SymbolType >
224}
225
226template < class SymbolType >
228}
229
230template < class SymbolType >
232}
233
234template < class SymbolType >
236 return this->m_Data;
237}
238
239template < class SymbolType >
241 return std::move ( this->m_Data );
242}
243
244template < class SymbolType >
246 arityChecksum ( data );
247
248 ext::set < common::ranked_symbol < SymbolType > > minimalAlphabet ( data.begin ( ), data.end ( ) );
249 std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const common::ranked_symbol < SymbolType > & ) {
250 throw TreeException ( "Input symbols not in the alphabet." );
251 } ) );
252
253 this->m_Data = std::move ( data );
254}
255
256template < class SymbolType >
258 if ( std::accumulate ( data.begin ( ), data.end ( ), 1, [ ] ( int current, const common::ranked_symbol < SymbolType > & symbol ) {
259 return current + symbol.getRank ( ) - 1;
260 } ) != 0 )
261 throw TreeException ( "The string does not form a tree" );
262}
263
264template < class SymbolType >
266 return this->m_Data.empty ( );
267}
268
269} /* namespace tree */
270
271namespace core {
272
278template < class SymbolType >
279class SetConstraint< tree::PrefixRankedTree < SymbolType >, common::ranked_symbol < SymbolType >, tree::GeneralAlphabet > {
280public:
290 const ext::vector < common::ranked_symbol < SymbolType > > & content = tree.getContent ( );
291
292 return std::find ( content.begin ( ), content.end ( ), symbol ) != content.end ( );
293 }
294
304 return true;
305 }
306
314 }
315};
316
322template < class SymbolType >
323struct normalize < tree::PrefixRankedTree < SymbolType > > {
327
328 return tree::PrefixRankedTree < > ( std::move ( alphabet ), std::move ( content ) );
329 }
330};
331
332} /* namespace core */
333
334extern template class tree::PrefixRankedTree < >;
335
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 bool available(const tree::PrefixRankedTree< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: PrefixRankedTree.h:303
static void valid(const tree::PrefixRankedTree< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: PrefixRankedTree.h:313
static bool used(const tree::PrefixRankedTree< SymbolType > &tree, const common::ranked_symbol< SymbolType > &symbol)
Definition: PrefixRankedTree.h:289
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
Tree structure represented as linear sequece as result of preorder traversal. The representation is s...
Definition: PrefixRankedTree.h:71
void setContent(ext::vector< common::ranked_symbol< SymbolType > > data)
Definition: PrefixRankedTree.h:245
ext::set< common::ranked_symbol< SymbolType > > && getAlphabet() &&
Definition: PrefixRankedTree.h:128
PrefixRankedTree(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: PrefixRankedTree.h:218
friend ext::ostream & operator<<(ext::ostream &out, const PrefixRankedTree &instance)
Definition: PrefixRankedTree.h:199
bool operator==(const PrefixRankedTree &other) const
Definition: PrefixRankedTree.h:187
void extendAlphabet(const ext::set< common::ranked_symbol< SymbolType > > &symbols)
Definition: PrefixRankedTree.h:137
bool isEmpty() const
Definition: PrefixRankedTree.h:265
const ext::set< common::ranked_symbol< SymbolType > > & getAlphabet() const &
Definition: PrefixRankedTree.h:119
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedTree.h:235
Tree structure represented in its natural representation. The representation is so called ranked,...
Definition: RankedTree.h:72
static ext::vector< common::ranked_symbol< SymbolType > > postfixToPrefix(const ext::vector< common::ranked_symbol< SymbolType > > &src)
Definition: TreeAuxiliary.h:162
static ext::vector< common::ranked_symbol< SymbolType > > treeToPrefix(const ext::tree< common::ranked_symbol< SymbolType > > &tree)
Definition: TreeAuxiliary.h:185
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::PrefixRankedTree< > eval(tree::PrefixRankedTree< SymbolType > &&value)
Definition: PrefixRankedTree.h:324
Definition: normalize.hpp:13