Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
PrefixBarTree.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 PrefixBarTree;
32
33} /* namespace tree */
34
35
36#include <ext/algorithm>
37
38#include <alib/deque>
39#include <alib/set>
40#include <alib/vector>
41#include <alib/tree>
42
43#include <core/components.hpp>
44
45#include <alphabet/BarSymbol.h>
46
47#include <tree/TreeException.h>
49
50#include <core/normalize.hpp>
52
53#include "UnrankedTree.h"
54
55#include <string/LinearString.h>
56
57namespace tree {
58
59class GeneralAlphabet;
60class BarSymbol;
61
76template < class SymbolType >
77class PrefixBarTree final : public core::Components < PrefixBarTree < SymbolType >, ext::set < SymbolType >, component::Set, GeneralAlphabet, SymbolType, component::Value, BarSymbol > {
82
88 void arityChecksum ( const ext::vector < SymbolType > & data );
89
90public:
99
107
115
122
129 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
130 }
131
138 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
139 }
140
146 void extendAlphabet ( const ext::set < SymbolType > & symbols ) {
147 this->template accessComponent < GeneralAlphabet > ( ).add ( symbols );
148 }
149
155 const SymbolType & getBar ( ) const & {
156 return this->template accessComponent < BarSymbol > ( ).get ( );
157 }
158
164 SymbolType && getBar ( ) && {
165 return std::move ( this->template accessComponent < BarSymbol > ( ).get ( ) );
166 }
167
173 const ext::vector < SymbolType > & getContent ( ) const &;
174
180 ext::vector < SymbolType > && getContent ( ) &&;
181
189 void setContent ( ext::vector < SymbolType > data );
190
194 bool isEmpty ( ) const;
195
203 auto operator <=> ( const PrefixBarTree & other ) const {
204 return std::tie ( m_Data, getAlphabet ( ), getBar ( ) ) <=> std::tie ( other.m_Data, other.getAlphabet ( ), other.getBar ( ) );
205 }
206
214 bool operator == ( const PrefixBarTree & other ) const {
215 return std::tie ( m_Data, getAlphabet ( ), getBar ( ) ) == std::tie ( other.m_Data, other.getAlphabet ( ), other.getBar ( ) );
216 }
217
226 friend ext::ostream & operator << ( ext::ostream & out, const PrefixBarTree & instance ) {
227 out << "(PrefixBarTree";
228 out << " alphabet = " << instance.getAlphabet ( );
229 out << " content = " << instance.getContent ( );
230 out << ")";
231 return out;
232 }
233
239 explicit operator string::LinearString < SymbolType > ( ) const {
241 }
242};
243
244template < class SymbolType >
245PrefixBarTree < SymbolType >::PrefixBarTree ( SymbolType bar, ext::set < SymbolType > alphabet, ext::vector < SymbolType > data ) : core::Components < PrefixBarTree, ext::set < SymbolType >, component::Set, GeneralAlphabet, SymbolType, component::Value, BarSymbol > ( std::move ( alphabet ), std::move ( bar ) ) {
246 setContent ( std::move ( data ) );
247}
248
249template < class SymbolType >
251}
252
253template < class SymbolType >
255}
256
257template < class SymbolType >
258PrefixBarTree < SymbolType >::PrefixBarTree ( const UnrankedTree < SymbolType > & tree ) : PrefixBarTree ( alphabet::BarSymbol::instance < SymbolType > ( ), tree ) {
259}
260
261template < class SymbolType >
263 return this->m_Data;
264}
265
266template < class SymbolType >
268 return std::move ( this->m_Data );
269}
270
271template < class SymbolType >
273 arityChecksum ( data );
274
275 ext::set < SymbolType > minimalAlphabet ( data.begin ( ), data.end ( ) );
276 std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const SymbolType & ) {
277 throw TreeException ( "Input symbols not in the alphabet." );
278 } ) );
279
280 this->m_Data = std::move ( data );
281}
282
283template < class SymbolType >
285 int arityChecksumTypes = 0;
286
287 for ( const SymbolType & symbol : data ) {
288 if ( symbol == getBar ( ) )
289 arityChecksumTypes -= 1;
290 else
291 arityChecksumTypes += 1;
292 }
293
294 if ( arityChecksumTypes != 0 ) throw TreeException ( "The string does not form a tree" );
295}
296
297template < class SymbolType >
299 return this->m_Data.empty ( );
300}
301
302} /* namespace tree */
303
304namespace core {
305
311template < class SymbolType >
312class SetConstraint< tree::PrefixBarTree < SymbolType >, SymbolType, tree::GeneralAlphabet > {
313public:
322 static bool used ( const tree::PrefixBarTree < SymbolType > & tree, const SymbolType & symbol ) {
323 const ext::vector < SymbolType > & content = tree.getContent ( );
324
325 return std::find ( content.begin ( ), content.end ( ), symbol ) != content.end ( );
326 }
327
336 static bool available ( const tree::PrefixBarTree < SymbolType > &, const SymbolType & ) {
337 return true;
338 }
339
346 static void valid ( const tree::PrefixBarTree < SymbolType > &, const SymbolType & ) {
347 }
348};
349
355template < class SymbolType >
356class ElementConstraint< tree::PrefixBarTree < SymbolType >, SymbolType, tree::BarSymbol > {
357public:
366 static bool available ( const tree::PrefixBarTree < SymbolType > & tree, const SymbolType & symbol ) {
367 return tree.template accessComponent < tree::GeneralAlphabet > ( ).get ( ).count ( symbol );
368 }
369
376 static void valid ( const tree::PrefixBarTree < SymbolType > &, const SymbolType & ) {
377 }
378};
379
385template < class SymbolType >
386struct normalize < tree::PrefixBarTree < SymbolType > > {
388 DefaultSymbolType bar = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( value ).getBar ( ) );
390 ext::vector < DefaultSymbolType > content = alphabet::SymbolNormalize::normalizeSymbols ( std::move ( value ).getContent ( ) );
391
392 return tree::PrefixBarTree < > ( std::move ( bar ), std::move ( alphabet ), std::move ( content ) );
393 }
394};
395
396} /* namespace core */
397
398extern template class tree::PrefixBarTree < >;
399
static ext::set< DefaultSymbolType > normalizeAlphabet(ext::set< SymbolType > &&symbols)
Definition: SymbolNormalize.h:50
static ext::vector< DefaultSymbolType > normalizeSymbols(ext::vector< SymbolType > &&symbols)
Definition: SymbolNormalize.h:86
static DefaultSymbolType normalizeSymbol(SymbolType &&symbol)
Definition: SymbolNormalize.h:68
Definition: components.hpp:181
static void valid(const tree::PrefixBarTree< SymbolType > &, const SymbolType &)
Definition: PrefixBarTree.h:376
static bool available(const tree::PrefixBarTree< SymbolType > &tree, const SymbolType &symbol)
Definition: PrefixBarTree.h:366
Definition: components.hpp:25
static void valid(const tree::PrefixBarTree< SymbolType > &, const SymbolType &)
Definition: PrefixBarTree.h:346
static bool available(const tree::PrefixBarTree< SymbolType > &, const SymbolType &)
Definition: PrefixBarTree.h:336
static bool used(const tree::PrefixBarTree< SymbolType > &tree, const SymbolType &symbol)
Definition: PrefixBarTree.h:322
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
Definition: Object.h:16
Linear string.
Definition: LinearString.h:57
Tree structure represented as linear sequece as result of preorder traversal with additional bar symb...
Definition: PrefixBarTree.h:77
const SymbolType & getBar() const &
Definition: PrefixBarTree.h:155
const ext::set< SymbolType > & getAlphabet() const &
Definition: PrefixBarTree.h:128
PrefixBarTree(SymbolType bar, ext::set< SymbolType > alphabet, ext::vector< SymbolType > data)
Creates a new instance of the tree with concrete alphabet, bar, and content.
Definition: PrefixBarTree.h:245
ext::set< SymbolType > && getAlphabet() &&
Definition: PrefixBarTree.h:137
SymbolType && getBar() &&
Definition: PrefixBarTree.h:164
void extendAlphabet(const ext::set< SymbolType > &symbols)
Definition: PrefixBarTree.h:146
bool operator==(const PrefixBarTree &other) const
Definition: PrefixBarTree.h:214
const ext::vector< SymbolType > & getContent() const &
Definition: PrefixBarTree.h:262
friend ext::ostream & operator<<(ext::ostream &out, const PrefixBarTree &instance)
Definition: PrefixBarTree.h:226
bool isEmpty() const
Definition: PrefixBarTree.h:298
void setContent(ext::vector< SymbolType > data)
Definition: PrefixBarTree.h:272
static ext::vector< common::ranked_symbol< SymbolType > > treeToPrefix(const ext::tree< common::ranked_symbol< SymbolType > > &tree)
Definition: TreeAuxiliary.h:185
Definition: TreeException.h:15
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: 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::PrefixBarTree< > eval(tree::PrefixBarTree< SymbolType > &&value)
Definition: PrefixBarTree.h:387
Definition: normalize.hpp:13