Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
PrefixRankedNonlinearPattern.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 PrefixRankedNonlinearPattern;
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>
52
53#include <string/LinearString.h>
55
56#include "PrefixRankedTree.h"
57#include "PrefixRankedPattern.h"
58#include "RankedTree.h"
59#include "RankedPattern.h"
61
62namespace tree {
63
64class GeneralAlphabet;
65class SubtreeWildcard;
66class NonlinearAlphabet;
67
81template < class SymbolType >
82class PrefixRankedNonlinearPattern final : public core::Components < PrefixRankedNonlinearPattern < SymbolType >, ext::set < common::ranked_symbol < SymbolType > >, component::Set, std::tuple < GeneralAlphabet, NonlinearAlphabet >, common::ranked_symbol < SymbolType >, component::Value, SubtreeWildcard > {
87
93 void arityChecksum ( const ext::vector < common::ranked_symbol < SymbolType > > & data );
94
95public:
105
114
122
129
136
143
150
157
164 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
165 }
166
173 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
174 }
175
182 this->template accessComponent < GeneralAlphabet > ( ).add ( symbols );
183 }
184
191 return this->template accessComponent < SubtreeWildcard > ( ).get ( );
192 }
193
200 return std::move ( this->template accessComponent < SubtreeWildcard > ( ).get ( ) );
201 }
202
209 return this->template accessComponent < NonlinearAlphabet > ( ).get ( );
210 }
211
218 return std::move ( this->template accessComponent < NonlinearAlphabet > ( ).get ( ) );
219 }
220
227
233 ext::vector < common::ranked_symbol < SymbolType > > && getContent ( ) &&;
234
242 void setContent ( ext::vector < common::ranked_symbol < SymbolType > > data );
243
247 bool isEmpty ( ) const;
248
256 auto operator <=> ( const PrefixRankedNonlinearPattern & other ) const {
257 return std::tie ( m_Data, getAlphabet ( ), getSubtreeWildcard ( ), getNonlinearVariables ( ) ) <=> std::tie ( other.m_Data, other.getAlphabet ( ), other.getSubtreeWildcard ( ), other.getNonlinearVariables ( ) );
258 }
259
267 bool operator == ( const PrefixRankedNonlinearPattern & other ) const {
268 return std::tie ( m_Data, getAlphabet ( ), getSubtreeWildcard ( ), getNonlinearVariables ( ) ) == std::tie ( other.m_Data, other.getAlphabet ( ), other.getSubtreeWildcard ( ), other.getNonlinearVariables ( ) );
269 }
270
280 out << "(PrefixRankedNonlinearPattern";
281 out << " alphabet = " << instance.getAlphabet ( );
282 out << " content = " << instance.getContent ( );
283 out << " nonlinearVariables = " << instance.getNonlinearVariables ( );
284 out << " subtreeWildcard = " << instance.getSubtreeWildcard ( );
285 out << ")";
286 return out;
287 }
288
296 }
297};
298
299template < class SymbolType >
301 setContent ( std::move ( data ) );
302}
303
304template < class SymbolType >
306}
307
308template < class SymbolType >
310}
311
312template < class SymbolType >
314}
315
316template < class SymbolType >
318}
319
320template < class SymbolType >
322}
323
324template < class SymbolType >
326}
327
328template < class SymbolType >
330}
331
332template < class SymbolType >
334 return this->m_Data;
335}
336
337template < class SymbolType >
339 return std::move ( this->m_Data );
340}
341
342template < class SymbolType >
344 arityChecksum ( data );
345
346 ext::set < common::ranked_symbol < SymbolType > > minimalAlphabet ( data.begin ( ), data.end ( ) );
347 std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const common::ranked_symbol < SymbolType > & ) {
348 throw TreeException ( "Input symbols not in the alphabet." );
349 } ) );
350
351 this->m_Data = std::move ( data );
352}
353
354template < class SymbolType >
356 if ( std::accumulate ( data.begin ( ), data.end ( ), 1, [ ] ( int current, const common::ranked_symbol < SymbolType > & symbol ) {
357 return current + symbol.getRank ( ) - 1;
358 } ) != 0 )
359 throw TreeException ( "The string does not form a tree" );
360}
361
362template < class SymbolType >
364 return this->m_Data.empty ( );
365}
366
367} /* namespace tree */
368
369namespace core {
370
376template < class SymbolType >
377class SetConstraint< tree::PrefixRankedNonlinearPattern < SymbolType >, common::ranked_symbol < SymbolType >, tree::GeneralAlphabet > {
378public:
388 const ext::vector < common::ranked_symbol < SymbolType > > & m_content = pattern.getContent ( );
389
390 return std::find ( m_content.begin ( ), m_content.end ( ), symbol ) != m_content.end ( ) || pattern.template accessComponent < tree::SubtreeWildcard > ( ).get ( ) == symbol;
391 }
392
402 return true;
403 }
404
412 }
413};
414
420template < class SymbolType >
421class SetConstraint< tree::PrefixRankedNonlinearPattern < SymbolType >, common::ranked_symbol < SymbolType >, tree::NonlinearAlphabet > {
422public:
432 return false;
433 }
434
444 return pattern.template accessComponent < tree::GeneralAlphabet > ( ).get ( ).count ( symbol );
445 }
446
456 if ( symbol.getRank ( ) != 0 )
457 throw tree::TreeException ( "Nonlinear variable has nonzero arity" );
458
459 if ( pattern.template accessComponent < tree::SubtreeWildcard > ( ).get ( ) == symbol )
460 throw tree::TreeException ( "Symbol " + ext::to_string ( symbol ) + "cannot be set as nonlinear variable since it is already subtree wildcard" );
461 }
462};
463
469template < class SymbolType >
470class ElementConstraint< tree::PrefixRankedNonlinearPattern < SymbolType >, common::ranked_symbol < SymbolType >, tree::SubtreeWildcard > {
471public:
481 return pattern.template accessComponent < tree::GeneralAlphabet > ( ).get ( ).count ( symbol );
482 }
483
493 if ( symbol.getRank ( ) != 0 )
494 throw tree::TreeException ( "SubtreeWildcard symbol has nonzero arity" );
495
496 if ( pattern.template accessComponent < tree::NonlinearAlphabet > ( ).get ( ).count ( symbol ) )
497 throw tree::TreeException ( "Symbol " + ext::to_string ( symbol ) + "cannot be set as subtree wildcard since it is already nonlinear variable" );
498 }
499};
500
506template < class SymbolType >
507struct normalize < tree::PrefixRankedNonlinearPattern < SymbolType > > {
509 common::ranked_symbol < DefaultSymbolType > wildcard = alphabet::SymbolNormalize::normalizeRankedSymbol ( std::move ( value ).getSubtreeWildcard ( ) );
510 ext::set < common::ranked_symbol < DefaultSymbolType > > nonlinearAlphabet = alphabet::SymbolNormalize::normalizeRankedAlphabet ( std::move ( value ).getNonlinearVariables ( ) );
513
514 return tree::PrefixRankedNonlinearPattern < > ( std::move ( wildcard ), std::move ( nonlinearAlphabet ), std::move ( alphabet ), std::move ( content ) );
515 }
516};
517
518} /* namespace core */
519
520extern template class tree::PrefixRankedNonlinearPattern < >;
521
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 void valid(const tree::PrefixRankedNonlinearPattern< SymbolType > &pattern, const common::ranked_symbol< SymbolType > &symbol)
Definition: PrefixRankedNonlinearPattern.h:492
static bool available(const tree::PrefixRankedNonlinearPattern< SymbolType > &pattern, const common::ranked_symbol< SymbolType > &symbol)
Definition: PrefixRankedNonlinearPattern.h:480
Definition: components.hpp:25
static void valid(const tree::PrefixRankedNonlinearPattern< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: PrefixRankedNonlinearPattern.h:411
static bool available(const tree::PrefixRankedNonlinearPattern< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: PrefixRankedNonlinearPattern.h:401
static bool used(const tree::PrefixRankedNonlinearPattern< SymbolType > &pattern, const common::ranked_symbol< SymbolType > &symbol)
Definition: PrefixRankedNonlinearPattern.h:387
static bool available(const tree::PrefixRankedNonlinearPattern< SymbolType > &pattern, const common::ranked_symbol< SymbolType > &symbol)
Definition: PrefixRankedNonlinearPattern.h:443
static bool used(const tree::PrefixRankedNonlinearPattern< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: PrefixRankedNonlinearPattern.h:431
static void valid(const tree::PrefixRankedNonlinearPattern< SymbolType > &pattern, const common::ranked_symbol< SymbolType > &symbol)
Definition: PrefixRankedNonlinearPattern.h:455
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
Nonlinear tree pattern represented as linear sequece as result of preorder traversal....
Definition: PrefixRankedNonlinearPattern.h:82
PrefixRankedNonlinearPattern(common::ranked_symbol< SymbolType > subtreeWildcard, ext::set< common::ranked_symbol< SymbolType > > nonlinearVariables, 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, wildcard, and nonlinear variab...
Definition: PrefixRankedNonlinearPattern.h:300
ext::set< common::ranked_symbol< SymbolType > > && getNonlinearVariables() &&
Definition: PrefixRankedNonlinearPattern.h:217
const ext::set< common::ranked_symbol< SymbolType > > & getNonlinearVariables() const &
Definition: PrefixRankedNonlinearPattern.h:208
void setContent(ext::vector< common::ranked_symbol< SymbolType > > data)
Definition: PrefixRankedNonlinearPattern.h:343
bool operator==(const PrefixRankedNonlinearPattern &other) const
Definition: PrefixRankedNonlinearPattern.h:267
bool isEmpty() const
Definition: PrefixRankedNonlinearPattern.h:363
common::ranked_symbol< SymbolType > && getSubtreeWildcard() &&
Definition: PrefixRankedNonlinearPattern.h:199
ext::set< common::ranked_symbol< SymbolType > > && getAlphabet() &&
Definition: PrefixRankedNonlinearPattern.h:172
friend ext::ostream & operator<<(ext::ostream &out, const PrefixRankedNonlinearPattern &instance)
Definition: PrefixRankedNonlinearPattern.h:279
const ext::vector< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: PrefixRankedNonlinearPattern.h:333
const ext::set< common::ranked_symbol< SymbolType > > & getAlphabet() const &
Definition: PrefixRankedNonlinearPattern.h:163
const common::ranked_symbol< SymbolType > & getSubtreeWildcard() const &
Definition: PrefixRankedNonlinearPattern.h:190
void extendAlphabet(const ext::set< common::ranked_symbol< SymbolType > > &symbols)
Definition: PrefixRankedNonlinearPattern.h:181
Tree pattern represented as linear sequece as result of preorder traversal. The representation is so ...
Definition: PrefixRankedPattern.h:77
Tree structure represented as linear sequece as result of preorder traversal. The representation is s...
Definition: PrefixRankedTree.h:71
Nonlinear tree pattern represented in its natural representation. The representation is so called ran...
Definition: RankedNonlinearPattern.h:74
Tree pattern represented in its natural representation. The representation is so called ranked,...
Definition: RankedPattern.h:72
Tree structure represented in its natural representation. The representation is so called ranked,...
Definition: RankedTree.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
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
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::PrefixRankedNonlinearPattern< > eval(tree::PrefixRankedNonlinearPattern< SymbolType > &&value)
Definition: PrefixRankedNonlinearPattern.h:508
Definition: normalize.hpp:13