Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
PrefixRankedBarTree.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 PrefixRankedBarTree;
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>
48
49#include <alphabet/BarSymbol.h>
50
51#include <core/normalize.hpp>
53
54#include <string/LinearString.h>
55
56#include "RankedTree.h"
57
58namespace tree {
59
60class GeneralAlphabet;
61class BarSymbols;
62
77template < class SymbolType >
78class PrefixRankedBarTree final : public core::Components < PrefixRankedBarTree < SymbolType >, ext::set < common::ranked_symbol < SymbolType > >, component::Set, std::tuple < GeneralAlphabet, BarSymbols > > {
83
89 void arityChecksum ( const ext::vector < common::ranked_symbol < SymbolType > > & data );
90
91public:
100
108
115 explicit PrefixRankedBarTree ( SymbolType barBase, const RankedTree < SymbolType > & tree );
116
122 explicit PrefixRankedBarTree ( const RankedTree < > & tree );
123
130 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
131 }
132
139 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
140 }
141
148 this->template accessComponent < GeneralAlphabet > ( ).add ( symbols );
149 }
150
157 return this->template accessComponent < BarSymbols > ( ).get ( );
158 }
159
166 return std::move ( this->template accessComponent < BarSymbols > ( ).get ( ) );
167 }
168
175 this->template accessComponent < BarSymbols > ( ).add ( bars );
176 }
177
184
190 ext::vector < common::ranked_symbol < SymbolType > > && getContent ( ) && ;
191
199 void setContent ( ext::vector < common::ranked_symbol < SymbolType > > data );
200
204 bool isEmpty ( ) const;
205
213 auto operator <=> ( const PrefixRankedBarTree & other ) const {
214 return std::tie ( m_Data, getAlphabet(), getBars() ) <=> std::tie ( other.m_Data, other.getAlphabet(), other.getBars() );
215 }
216
224 bool operator == ( const PrefixRankedBarTree & other ) const {
225 return std::tie ( m_Data, getAlphabet(), getBars() ) == std::tie ( other.m_Data, other.getAlphabet(), other.getBars() );
226 }
227
236 friend ext::ostream & operator << ( ext::ostream & out, const PrefixRankedBarTree & instance ) {
237 out << "(PrefixRankedBarTree";
238 out << " alphabet = " << instance.getAlphabet ( );
239 out << " bars = " << instance.getBars ( );
240 out << " content = " << instance.getContent ( );
241 out << ")";
242 return out;
243 }
244
252 }
253};
254
255template < class SymbolType >
257 setContent ( std::move ( data ) );
258}
259
260template < class SymbolType >
262}
263
264template < class SymbolType >
265PrefixRankedBarTree < SymbolType >::PrefixRankedBarTree ( SymbolType barBase, const RankedTree < SymbolType > & tree ) : PrefixRankedBarTree ( TreeAuxiliary::computeBars ( tree.getAlphabet ( ), barBase ), tree.getAlphabet ( ) + TreeAuxiliary::computeBars ( tree.getAlphabet ( ), barBase ), TreeAuxiliary::treeToPrefix ( tree.getContent ( ), barBase ) ) {
266}
267
268template < class SymbolType >
269PrefixRankedBarTree < SymbolType >::PrefixRankedBarTree ( const RankedTree < > & tree ) : PrefixRankedBarTree ( alphabet::BarSymbol::instance < SymbolType > ( ), tree ) {
270}
271
272template < class SymbolType >
274 return this->m_Data;
275}
276
277template < class SymbolType >
279 return std::move ( this->m_Data );
280}
281
282template < class SymbolType >
284 arityChecksum ( data );
285
286 ext::set < common::ranked_symbol < SymbolType > > minimalAlphabet ( data.begin ( ), data.end ( ) );
287 std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const common::ranked_symbol < SymbolType > & ) {
288 throw TreeException ( "Input symbols not in the alphabet." );
289 } ) );
290
291 this->m_Data = std::move ( data );
292}
293
294template < class SymbolType >
296 struct Data {
297 int terminals;
298 int bars;
299 int types;
300 };
301
302 Data accRes = std::accumulate ( data.begin ( ), data.end ( ), Data { 1, 1, 0 }, [ * this ] ( const Data & current, const common::ranked_symbol < SymbolType > & symbol ) {
303 if ( getBars ( ).contains ( symbol ) )
304 return Data { current.terminals, static_cast < int > ( current.bars + symbol.getRank ( ) - 1 ), current.types - 1 };
305 else
306 return Data { static_cast < int > ( current.terminals + symbol.getRank ( ) - 1 ), current.bars, current.types + 1 };
307 } );
308
309 if ( accRes.terminals != 0 || accRes.bars != 0 || accRes.types != 0 )
310 throw TreeException ( "The string does not form a tree" );
311}
312
313template < class SymbolType >
315 return this->m_Data.empty ( );
316}
317
318} /* namespace tree */
319
320namespace core {
321
327template < class SymbolType >
328class SetConstraint< tree::PrefixRankedBarTree < SymbolType >, common::ranked_symbol < SymbolType >, tree::GeneralAlphabet > {
329public:
339 const ext::vector < common::ranked_symbol < SymbolType > > & content = tree.getContent ( );
340
341 return std::find ( content.begin ( ), content.end ( ), symbol ) != content.end ( );
342 }
343
353 return true;
354 }
355
363 }
364};
365
371template < class SymbolType >
372class SetConstraint< tree::PrefixRankedBarTree < SymbolType >, common::ranked_symbol < SymbolType >, tree::BarSymbols > {
373public:
383 const ext::vector < common::ranked_symbol < SymbolType > > & content = tree.getContent ( );
384
385 return std::find ( content.begin ( ), content.end ( ), symbol ) != content.end ( );
386 }
387
397 return tree.template accessComponent < tree::GeneralAlphabet > ( ).get ( ).count ( symbol );
398 }
399
407 }
408};
409
415template < class SymbolType >
416struct normalize < tree::PrefixRankedBarTree < SymbolType > > {
421
422 return tree::PrefixRankedBarTree < > ( std::move ( bars ), std::move ( alphabet ), std::move ( content ) );
423 }
424};
425
426} /* namespace core */
427
428extern template class tree::PrefixRankedBarTree < >;
429
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::PrefixRankedBarTree< SymbolType > &tree, const common::ranked_symbol< SymbolType > &symbol)
Definition: PrefixRankedBarTree.h:396
static bool used(const tree::PrefixRankedBarTree< SymbolType > &tree, const common::ranked_symbol< SymbolType > &symbol)
Definition: PrefixRankedBarTree.h:382
static void valid(const tree::PrefixRankedBarTree< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: PrefixRankedBarTree.h:406
static bool used(const tree::PrefixRankedBarTree< SymbolType > &tree, const common::ranked_symbol< SymbolType > &symbol)
Definition: PrefixRankedBarTree.h:338
static bool available(const tree::PrefixRankedBarTree< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: PrefixRankedBarTree.h:352
static void valid(const tree::PrefixRankedBarTree< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: PrefixRankedBarTree.h:362
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 preorder traversal with additional bar symb...
Definition: PrefixRankedBarTree.h:78
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedBarTree.h:273
bool operator==(const PrefixRankedBarTree &other) const
Definition: PrefixRankedBarTree.h:224
const ext::set< common::ranked_symbol< SymbolType > > & getAlphabet() const &
Definition: PrefixRankedBarTree.h:129
void extendBars(const ext::set< common::ranked_symbol< SymbolType > > &bars)
Definition: PrefixRankedBarTree.h:174
PrefixRankedBarTree(ext::set< common::ranked_symbol< SymbolType > > bars, ext::set< common::ranked_symbol< SymbolType > > alphabet, ext::vector< common::ranked_symbol< SymbolType > > data)
Creates a new instance of the tree with concrete alphabet, bars, and content.
Definition: PrefixRankedBarTree.h:256
void setContent(ext::vector< common::ranked_symbol< SymbolType > > data)
Definition: PrefixRankedBarTree.h:283
ext::set< common::ranked_symbol< SymbolType > > && getBars() &&
Definition: PrefixRankedBarTree.h:165
bool isEmpty() const
Definition: PrefixRankedBarTree.h:314
void extendAlphabet(const ext::set< common::ranked_symbol< SymbolType > > &symbols)
Definition: PrefixRankedBarTree.h:147
ext::set< common::ranked_symbol< SymbolType > > && getAlphabet() &&
Definition: PrefixRankedBarTree.h:138
friend ext::ostream & operator<<(ext::ostream &out, const PrefixRankedBarTree &instance)
Definition: PrefixRankedBarTree.h:236
const ext::set< common::ranked_symbol< SymbolType > > & getBars() const &
Definition: PrefixRankedBarTree.h:156
Tree structure represented in its natural representation. The representation is so called ranked,...
Definition: RankedTree.h:72
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::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::PrefixRankedBarTree< > eval(tree::PrefixRankedBarTree< SymbolType > &&value)
Definition: PrefixRankedBarTree.h:417
Definition: normalize.hpp:13