Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Concepts
UnorderedDFTA.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
26#include <ostream>
27
28#include <alib/map>
29#include <alib/set>
30#include <alib/multiset>
31
32#include <core/components.hpp>
34
37
39
40#include <core/normalize.hpp>
43
44namespace automaton {
45
46class InputAlphabet;
47class States;
48class FinalStates;
49
71template < class SymbolTypeT = DefaultSymbolType, class StateTypeT = DefaultStateType >
72class UnorderedDFTA final : public core::Components < UnorderedDFTA < SymbolTypeT, StateTypeT >, ext::set < common::ranked_symbol < SymbolTypeT > >, component::Set, InputAlphabet, ext::set < StateTypeT >, component::Set, std::tuple < States, FinalStates > > {
73public:
74 typedef SymbolTypeT SymbolType;
75 typedef StateTypeT StateType;
76
77private:
82
83public:
87 explicit UnorderedDFTA ( );
88
97
103 const ext::set < StateType > & getStates ( ) const & {
104 return this->template accessComponent < States > ( ).get ( );
105 }
106
113 return std::move ( this->template accessComponent < States > ( ).get ( ) );
114 }
115
123 bool addState ( StateType state ) {
124 return this->template accessComponent < States > ( ).add ( std::move ( state ) );
125 }
126
133 this->template accessComponent < States > ( ).set ( std::move ( states ) );
134 }
135
143 void removeState ( const StateType & state ) {
144 this->template accessComponent < States > ( ).remove ( state );
145 }
146
153 return this->template accessComponent < FinalStates > ( ).get ( );
154 }
155
162 return std::move ( this->template accessComponent < FinalStates > ( ).get ( ) );
163 }
164
172 bool addFinalState ( StateType state ) {
173 return this->template accessComponent < FinalStates > ( ).add ( std::move ( state ) );
174 }
175
182 this->template accessComponent < FinalStates > ( ).set ( std::move ( states ) );
183 }
184
192 void removeFinalState ( const StateType & state ) {
193 this->template accessComponent < FinalStates > ( ).remove ( state );
194 }
195
202 return this->template accessComponent < InputAlphabet > ( ).get ( );
203 }
204
211 return std::move ( this->template accessComponent < InputAlphabet > ( ).get ( ) );
212 }
213
222 return this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) );
223 }
224
231 this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) );
232 }
233
240 this->template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) );
241 }
242
251 this->template accessComponent < InputAlphabet > ( ).remove ( symbol );
252 }
253
268
280 bool removeTransition ( const common::ranked_symbol < SymbolType > & symbol, const ext::multiset < StateType > & states, const StateType & next );
281
288 return transitions;
289 }
290
297 return std::move ( transitions );
298 }
299
305
306 for ( const auto & transition : getTransitions ( ) ) {
307 if ( transition.second == q )
308 res.insert ( std::make_pair ( transition.first, q ) );
309 }
310
311 return res;
312 }
313
319
320 for ( const auto & transition : getTransitions ( ) ) {
321 if ( std::find ( transition.first.second.begin ( ), transition.first.second.end ( ), q ) != transition.first.second.end ( ) )
322 res.insert ( transition );
323 }
324
325 return res;
326 }
327
335 auto operator <=> ( const UnorderedDFTA & other ) const {
336 return std::tie(getStates(), getInputAlphabet(), getFinalStates(), transitions) <=> std::tie(other.getStates(), other.getInputAlphabet(), other.getFinalStates(), other.transitions);
337 }
338
346 bool operator == ( const UnorderedDFTA & other ) const {
347 return std::tie(getStates(), getInputAlphabet(), getFinalStates(), transitions) == std::tie(other.getStates(), other.getInputAlphabet(), other.getFinalStates(), other.transitions);
348 }
349
358 friend ext::ostream & operator << ( ext::ostream & out, const UnorderedDFTA & instance ) {
359 return out << "(UnorderedDFTA"
360 << " states = " << instance.getStates ( )
361 << " inputAlphabet = " << instance.getInputAlphabet ( )
362 << " finalStates = " << instance.getFinalStates ( )
363 << " transitions = " << instance.getTransitions ( )
364 << ")";
365 }
366};
367
373template < class T >
374class isUnorderedDFTA_impl : public std::false_type {};
375
384template < class SymbolType, class StateType >
385class isUnorderedDFTA_impl < UnorderedDFTA < SymbolType, StateType > > : public std::true_type {};
386
392template < class T >
394
395template<class SymbolType, class StateType >
396UnorderedDFTA < SymbolType, StateType >::UnorderedDFTA ( ext::set < StateType > states, ext::set < common::ranked_symbol < SymbolType > > inputAlphabet, ext::set < StateType > finalStates ) : core::Components < UnorderedDFTA, ext::set < common::ranked_symbol < SymbolType > >, component::Set, InputAlphabet, ext::set < StateType >, component::Set, std::tuple < States, FinalStates > > ( std::move ( inputAlphabet ), std::move ( states ), std::move ( finalStates ) ) {
397}
398
399template<class SymbolType, class StateType >
401}
402
403template<class SymbolType, class StateType >
405 if ( prevStates.size ( ) != symbol.getRank ( ) )
406 throw AutomatonException("Number of states doesn't match rank of the symbol");
407
408 if (! getInputAlphabet().count(symbol))
409 throw AutomatonException("Input symbol \"" + ext::to_string ( symbol ) + "\" doesn't exist.");
410
411 if (! getStates().count(next))
412 throw AutomatonException("State \"" + ext::to_string ( next ) + "\" doesn't exist.");
413
414 for ( const StateType & it : prevStates) {
415 if (! getStates().count(it))
416 throw AutomatonException("State \"" + ext::to_string ( it ) + "\" doesn't exist.");
417 }
418
419 ext::pair<common::ranked_symbol < SymbolType >, ext::multiset<StateType> > key = ext::make_pair ( std::move ( symbol ), std::move ( prevStates ) );
420 if ( transitions.find ( key ) != transitions.end ( ) ) {
421 if ( transitions.find ( key )->second == next )
422 return false;
423 else
424 throw AutomatonException("Transition already exists");
425 }
426
427 transitions.insert ( std::move ( key ), std::move ( next ) );
428 return true;
429}
430
431template<class SymbolType, class StateType >
434
435 if ( transitions.find ( key ) == transitions.end ( ) )
436 return false;
437
438 if ( transitions.find ( key )->second != next )
439 throw AutomatonException("Transition does not exist");
440
441 transitions.erase(key);
442 return true;
443}
444
445} /* namespace automaton */
446
447namespace core {
448
455template<class SymbolType, class StateType >
456class SetConstraint< automaton::UnorderedDFTA < SymbolType, StateType >, common::ranked_symbol < SymbolType >, automaton::InputAlphabet > {
457public:
467 for ( const std::pair<const ext::pair<common::ranked_symbol < SymbolType >, ext::multiset<StateType> >, StateType>& t : automaton.getTransitions())
468 if (t.first.first == symbol)
469 return true;
470
471 return false;
472 }
473
483 return true;
484 }
485
493 }
494};
495
502template<class SymbolType, class StateType >
503class SetConstraint< automaton::UnorderedDFTA < SymbolType, StateType >, StateType, automaton::States > {
504public:
514 if ( automaton.getFinalStates ( ).count ( state ) )
515 return true;
516
517 for ( const std::pair<const ext::pair<common::ranked_symbol < SymbolType >, ext::multiset<StateType> >, StateType>& t : automaton.getTransitions())
518 if(ext::contains(t.first.second.begin(), t.first.second.end(), state ) || t.second == state)
519 return true;
520
521 return false;
522 }
523
533 return true;
534 }
535
543 }
544};
545
552template<class SymbolType, class StateType >
553class SetConstraint< automaton::UnorderedDFTA < SymbolType, StateType >, StateType, automaton::FinalStates > {
554public:
564 return false;
565 }
566
576 return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state );
577 }
578
586 }
587};
588
594template < class SymbolType, class StateType >
595struct normalize < automaton::UnorderedDFTA < SymbolType, StateType > > {
598 ext::set < DefaultStateType > states = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getStates ( ) );
599 ext::set < DefaultStateType > finalStates = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getFinalStates ( ) );
600
601 automaton::UnorderedDFTA < > res ( std::move ( states ), std::move ( alphabet ), std::move ( finalStates ) );
602
603 for ( std::pair < ext::pair < common::ranked_symbol < SymbolType >, ext::multiset < StateType > >, StateType > && transition : ext::make_mover ( std::move ( value ).getTransitions ( ) ) ) {
605 ext::multiset < DefaultStateType > from = automaton::AutomatonNormalize::normalizeStates ( std::move ( transition.first.second ) );
606 DefaultStateType to = automaton::AutomatonNormalize::normalizeState ( std::move ( transition.second ) );
607
608 res.addTransition ( std::move ( input ), std::move ( from ), std::move ( to ) );
609 }
610
611 return res;
612 }
613};
614
615} /* namespace core */
616
617extern template class automaton::UnorderedDFTA < >;
618
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
Definition: AutomatonException.h:15
static DefaultStateType normalizeState(StateType &&state)
Definition: AutomatonNormalize.h:76
static ext::multiset< DefaultStateType > normalizeStates(ext::multiset< StateType > &&states)
Definition: AutomatonNormalize.h:49
Deterministic unordered finite tree automaton without epsilon transitions. Accepts regular tree langu...
Definition: UnorderedDFTA.h:72
bool addFinalState(StateType state)
Definition: UnorderedDFTA.h:172
bool removeTransition(const common::ranked_symbol< SymbolType > &symbol, const ext::multiset< StateType > &states, const StateType &next)
Removes a transition from the automaton.
Definition: UnorderedDFTA.h:432
void addInputSymbols(ext::set< common::ranked_symbol< SymbolType > > symbols)
Definition: UnorderedDFTA.h:230
const ext::map< ext::pair< common::ranked_symbol< SymbolType >, ext::multiset< StateType > >, StateType > & getTransitions() const &
Definition: UnorderedDFTA.h:287
ext::set< common::ranked_symbol< SymbolType > > && getInputAlphabet() &&
Definition: UnorderedDFTA.h:210
ext::map< ext::pair< common::ranked_symbol< SymbolType >, ext::multiset< StateType > >, StateType > getTransitionsToState(const StateType &q) const
Definition: UnorderedDFTA.h:303
bool addTransition(common::ranked_symbol< SymbolType > symbol, ext::multiset< StateType > prevStates, StateType next)
Add a transition to the automaton.
Definition: UnorderedDFTA.h:404
ext::map< ext::pair< common::ranked_symbol< SymbolType >, ext::multiset< StateType > >, StateType > getTransitionsFromState(const StateType &q) const
Definition: UnorderedDFTA.h:317
const ext::set< StateType > & getFinalStates() const &
Definition: UnorderedDFTA.h:152
void setStates(ext::set< StateType > states)
Definition: UnorderedDFTA.h:132
ext::map< ext::pair< common::ranked_symbol< SymbolType >, ext::multiset< StateType > >, StateType > && getTransitions() &&
Definition: UnorderedDFTA.h:296
const ext::set< StateType > & getStates() const &
Definition: UnorderedDFTA.h:103
const ext::set< common::ranked_symbol< SymbolType > > & getInputAlphabet() const &
Definition: UnorderedDFTA.h:201
void setInputAlphabet(ext::set< common::ranked_symbol< SymbolType > > symbols)
Definition: UnorderedDFTA.h:239
bool operator==(const UnorderedDFTA &other) const
Definition: UnorderedDFTA.h:346
friend ext::ostream & operator<<(ext::ostream &out, const UnorderedDFTA &instance)
Definition: UnorderedDFTA.h:358
void removeInputSymbol(const common::ranked_symbol< SymbolType > &symbol)
Definition: UnorderedDFTA.h:250
bool addState(StateType state)
Definition: UnorderedDFTA.h:123
ext::set< StateType > && getStates() &&
Definition: UnorderedDFTA.h:112
SymbolTypeT SymbolType
Definition: UnorderedDFTA.h:74
UnorderedDFTA()
Creates a new instance of the automaton.
Definition: UnorderedDFTA.h:400
void removeFinalState(const StateType &state)
Definition: UnorderedDFTA.h:192
ext::set< StateType > && getFinalStates() &&
Definition: UnorderedDFTA.h:161
StateTypeT StateType
Definition: UnorderedDFTA.h:75
bool addInputSymbol(common::ranked_symbol< SymbolType > symbol)
Definition: UnorderedDFTA.h:221
auto operator<=>(const UnorderedDFTA &other) const
Definition: UnorderedDFTA.h:335
void setFinalStates(ext::set< StateType > states)
Definition: UnorderedDFTA.h:181
void removeState(const StateType &state)
Definition: UnorderedDFTA.h:143
Definition: UnorderedDFTA.h:374
Definition: ranked_symbol.hpp:20
Definition: components.hpp:181
static void valid(const automaton::UnorderedDFTA< SymbolType, StateType > &, const common::ranked_symbol< SymbolType > &)
Definition: UnorderedDFTA.h:492
static bool used(const automaton::UnorderedDFTA< SymbolType, StateType > &automaton, const common::ranked_symbol< SymbolType > &symbol)
Definition: UnorderedDFTA.h:466
static bool available(const automaton::UnorderedDFTA< SymbolType, StateType > &, const common::ranked_symbol< SymbolType > &)
Definition: UnorderedDFTA.h:482
static void valid(const automaton::UnorderedDFTA< SymbolType, StateType > &, const StateType &)
Definition: UnorderedDFTA.h:542
static bool used(const automaton::UnorderedDFTA< SymbolType, StateType > &automaton, const StateType &state)
Definition: UnorderedDFTA.h:513
static bool available(const automaton::UnorderedDFTA< SymbolType, StateType > &, const StateType &)
Definition: UnorderedDFTA.h:532
static void valid(const automaton::UnorderedDFTA< SymbolType, StateType > &, const StateType &)
Definition: UnorderedDFTA.h:585
static bool available(const automaton::UnorderedDFTA< SymbolType, StateType > &automaton, const StateType &state)
Definition: UnorderedDFTA.h:575
static bool used(const automaton::UnorderedDFTA< SymbolType, StateType > &, const StateType &)
Definition: UnorderedDFTA.h:563
Definition: setComponents.hpp:26
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Definition: multiset.hpp:44
Definition: ostream.h:14
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Definition: set.hpp:44
Definition: Object.h:16
Definition: BarSymbol.cpp:12
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
return res
Definition: MinimizeByPartitioning.h:145
q
Definition: SingleInitialStateEpsilonTransition.h:85
Definition: ToGrammar.h:31
constexpr bool isUnorderedDFTA
Definition: UnorderedDFTA.h:393
Definition: normalize.hpp:10
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
reference_mover< T > make_mover(T &param)
Move adaptor construction function specialized to lvalue reference parameter.
Definition: iterator.hpp:468
bool contains(InputIt first, InputIt last, const Element &elem)
Linear version of search in a range of values.
Definition: algorithm.hpp:170
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
static automaton::UnorderedDFTA< > eval(automaton::UnorderedDFTA< SymbolType, StateType > &&value)
Definition: UnorderedDFTA.h:596
Definition: normalize.hpp:13