Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
PrefixRankedPattern.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 PrefixRankedPattern;
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
50
51#include <core/normalize.hpp>
53
54#include <string/LinearString.h>
55
56#include "RankedPattern.h"
57#include "PrefixRankedTree.h"
58
59namespace tree {
60
61class GeneralAlphabet;
62class SubtreeWildcard;
63
76template < class SymbolType >
77class PrefixRankedPattern final : public core::Components < PrefixRankedPattern < SymbolType >, ext::set < common::ranked_symbol < SymbolType > >, component::Set, GeneralAlphabet, common::ranked_symbol < SymbolType >, component::Value, SubtreeWildcard > {
82
88 void arityChecksum ( const ext::vector < common::ranked_symbol < SymbolType > > & data );
89
90public:
99
107
114
121
128 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
129 }
130
137 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
138 }
139
146 this->template accessComponent < GeneralAlphabet > ( ).add ( symbols );
147 }
148
155 return this->template accessComponent < SubtreeWildcard > ( ).get ( );
156 }
157
164 return std::move ( this->template accessComponent < SubtreeWildcard > ( ).get ( ) );
165 }
166
173
179 ext::vector < common::ranked_symbol < SymbolType > > && getContent ( ) &&;
180
188 void setContent ( ext::vector < common::ranked_symbol < SymbolType > > data );
189
193 bool isEmpty ( ) const;
194
202 auto operator <=> ( const PrefixRankedPattern & other ) const {
203 return std::tie ( m_Data, getAlphabet ( ), getSubtreeWildcard ( ) ) <=> std::tie ( other.m_Data, other.getAlphabet ( ), other.getSubtreeWildcard ( ) );
204 }
205
213 bool operator == ( const PrefixRankedPattern & other ) const {
214 return std::tie ( m_Data, getAlphabet ( ), getSubtreeWildcard ( ) ) == std::tie ( other.m_Data, other.getAlphabet ( ), other.getSubtreeWildcard ( ) );
215 }
216
225 friend ext::ostream & operator << ( ext::ostream & out, const PrefixRankedPattern & instance ) {
226 out << "(PrefixRankedPattern";
227 out << " alphabet = " << instance.getAlphabet ( );
228 out << " content = " << instance.getContent ( );
229 out << " subtreeWildcard = " << instance.getSubtreeWildcard ( );
230 out << ")";
231 return out;
232 }
233
241 }
242};
243
244template < class SymbolType >
246 setContent ( std::move ( data ) );
247}
248
249template < class SymbolType >
251}
252
253template < class SymbolType >
255}
256
257template < class SymbolType >
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 < common::ranked_symbol < SymbolType > > minimalAlphabet ( data.begin ( ), data.end ( ) );
276 std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const common::ranked_symbol < 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 if ( std::accumulate ( data.begin ( ), data.end ( ), 1, [ ] ( int current, const common::ranked_symbol < SymbolType > & symbol ) {
286 return current + symbol.getRank ( ) - 1;
287 } ) != 0 )
288 throw TreeException ( "The string does not form a tree" );
289}
290
291template < class SymbolType >
293 return this->m_Data.empty ( );
294}
295
296} /* namespace tree */
297
298namespace core {
299
305template < class SymbolType >
306class SetConstraint< tree::PrefixRankedPattern < SymbolType >, common::ranked_symbol < SymbolType >, tree::GeneralAlphabet > {
307public:
318
319 return std::find ( content.begin ( ), content.end ( ), symbol ) != content.end ( ) || pattern.template accessComponent < tree::SubtreeWildcard > ( ).get ( ) == symbol;
320 }
321
331 return true;
332 }
333
341 }
342};
343
349template < class SymbolType >
350class ElementConstraint< tree::PrefixRankedPattern < SymbolType >, common::ranked_symbol < SymbolType >, tree::SubtreeWildcard > {
351public:
361 return pattern.template accessComponent < tree::GeneralAlphabet > ( ).get ( ).count ( symbol );
362 }
363
373 if( symbol.getRank() != 0 )
374 throw tree::TreeException ( "SubtreeWildcard symbol has nonzero arity" );
375 }
376};
377
383template < class SymbolType >
384struct normalize < tree::PrefixRankedPattern < SymbolType > > {
386 common::ranked_symbol < DefaultSymbolType > wildcard = alphabet::SymbolNormalize::normalizeRankedSymbol ( std::move ( value ).getSubtreeWildcard ( ) );
389
390 return tree::PrefixRankedPattern < > ( std::move ( wildcard ), std::move ( alphabet ), std::move ( content ) );
391 }
392};
393
394} /* namespace core */
395
396extern template class tree::PrefixRankedPattern < >;
397
static ext::vector< common::ranked_symbol< DefaultSymbolType > > normalizeRankedSymbols(ext::vector< common::ranked_symbol< SymbolType > > &&symbols)
Definition: SymbolNormalize.h:113
static common::ranked_symbol< DefaultSymbolType > normalizeRankedSymbol(common::ranked_symbol< SymbolType > &&symbol)
Definition: SymbolNormalize.h:81
static ext::set< common::ranked_symbol< DefaultSymbolType > > normalizeRankedAlphabet(ext::set< common::ranked_symbol< SymbolType > > &&symbols)
Definition: SymbolNormalize.h:59
static Base instance()
Factory for the symbol construction of the symbol based on given type.
Definition: WildcardSymbol.h:83
Definition: ranked_symbol.hpp:20
const size_t & getRank() const &
Definition: ranked_symbol.hpp:83
Definition: components.hpp:181
static bool available(const tree::PrefixRankedPattern< SymbolType > &pattern, const common::ranked_symbol< SymbolType > &symbol)
Definition: PrefixRankedPattern.h:360
static void valid(const tree::PrefixRankedPattern< SymbolType > &, const common::ranked_symbol< SymbolType > &symbol)
Definition: PrefixRankedPattern.h:372
Definition: components.hpp:25
static bool available(const tree::PrefixRankedPattern< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: PrefixRankedPattern.h:330
static void valid(const tree::PrefixRankedPattern< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: PrefixRankedPattern.h:340
static bool used(const tree::PrefixRankedPattern< SymbolType > &pattern, const common::ranked_symbol< SymbolType > &symbol)
Definition: PrefixRankedPattern.h:316
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 pattern represented as linear sequece as result of preorder traversal. The representation is so ...
Definition: PrefixRankedPattern.h:77
ext::set< common::ranked_symbol< SymbolType > > && getAlphabet() &&
Definition: PrefixRankedPattern.h:136
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedPattern.h:262
PrefixRankedPattern(common::ranked_symbol< SymbolType > subtreeWildcard, ext::set< common::ranked_symbol< SymbolType > > alphabet, ext::vector< common::ranked_symbol< SymbolType > > data)
Creates a new instance of the pattern with concrete alphabet, content, and wildcard.
Definition: PrefixRankedPattern.h:245
void setContent(ext::vector< common::ranked_symbol< SymbolType > > data)
Definition: PrefixRankedPattern.h:272
common::ranked_symbol< SymbolType > && getSubtreeWildcard() &&
Definition: PrefixRankedPattern.h:163
const ext::set< common::ranked_symbol< SymbolType > > & getAlphabet() const &
Definition: PrefixRankedPattern.h:127
friend ext::ostream & operator<<(ext::ostream &out, const PrefixRankedPattern &instance)
Definition: PrefixRankedPattern.h:225
const common::ranked_symbol< SymbolType > & getSubtreeWildcard() const &
Definition: PrefixRankedPattern.h:154
bool isEmpty() const
Definition: PrefixRankedPattern.h:292
void extendAlphabet(const ext::set< common::ranked_symbol< SymbolType > > &symbols)
Definition: PrefixRankedPattern.h:145
bool operator==(const PrefixRankedPattern &other) const
Definition: PrefixRankedPattern.h:213
Tree structure represented as linear sequece as result of preorder traversal. The representation is s...
Definition: PrefixRankedTree.h:71
Tree pattern represented in its natural representation. The representation is so called ranked,...
Definition: RankedPattern.h:72
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::PrefixRankedPattern< > eval(tree::PrefixRankedPattern< SymbolType > &&value)
Definition: PrefixRankedPattern.h:385
Definition: normalize.hpp:13