Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
OneTapeDTM.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
27#include <alib/map>
28#include <alib/set>
29#include <alib/tuple>
30
31#include <core/components.hpp>
32
35
38
39#include <core/normalize.hpp>
42
43namespace automaton {
44
45class TapeAlphabet;
46class InputAlphabet;
47class BlankSymbol;
48class States;
49class FinalStates;
50class InitialState;
51
70template < class SymbolTypeT = DefaultSymbolType, class StateTypeT = DefaultStateType >
71class OneTapeDTM final : public core::Components < OneTapeDTM < SymbolTypeT, StateTypeT >, ext::set < SymbolTypeT >, component::Set, std::tuple < TapeAlphabet, InputAlphabet >, SymbolTypeT, component::Value, BlankSymbol, ext::set < StateTypeT >, component::Set, std::tuple < States, FinalStates >, StateTypeT, component::Value, InitialState > {
72public:
73 typedef SymbolTypeT SymbolType;
74 typedef StateTypeT StateType;
75
76private:
81
82public:
93 explicit OneTapeDTM ( ext::set < StateType > states, ext::set < SymbolType > tapeAlphabet, SymbolType blankSymbol, ext::set< SymbolType > inputAlphabet, StateType initialState, ext::set < StateType > finalStates );
94
101 explicit OneTapeDTM ( StateType initial, SymbolType blank );
102
108 const StateType & getInitialState ( ) const & {
109 return this-> template accessComponent < InitialState > ( ).get ( );
110 }
111
118 return std::move ( this-> template accessComponent < InitialState > ( ).get ( ) );
119 }
120
128 bool setInitialState ( StateType state ) {
129 return this-> template accessComponent < InitialState > ( ).set ( std::move ( state ) );
130 }
131
137 const ext::set < StateType > & getStates ( ) const & {
138 return this-> template accessComponent < States > ( ).get ( );
139 }
140
147 return std::move ( this-> template accessComponent < States > ( ).get ( ) );
148 }
149
157 bool addState ( StateType state ) {
158 return this-> template accessComponent < States > ( ).add ( std::move ( state ) );
159 }
160
167 this-> template accessComponent < States > ( ).set ( std::move ( states ) );
168 }
169
177 void removeState ( const StateType & state ) {
178 this-> template accessComponent < States > ( ).remove ( state );
179 }
180
187 return this-> template accessComponent < FinalStates > ( ).get ( );
188 }
189
196 return std::move ( this-> template accessComponent < FinalStates > ( ).get ( ) );
197 }
198
206 bool addFinalState ( StateType state ) {
207 return this-> template accessComponent < FinalStates > ( ).add ( std::move ( state ) );
208 }
209
216 this-> template accessComponent < FinalStates > ( ).set ( std::move ( states ) );
217 }
218
226 void removeFinalState ( const StateType & state ) {
227 this-> template accessComponent < FinalStates > ( ).remove ( state );
228 }
229
236 return this-> template accessComponent < InputAlphabet > ( ).get ( );
237 }
238
245 return std::move ( this-> template accessComponent < InputAlphabet > ( ).get ( ) );
246 }
247
255 bool addInputSymbol ( SymbolType symbol ) {
256 return this-> template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) );
257 }
258
265 this-> template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) );
266 }
267
274 this-> template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) );
275 }
276
284 void removeInputSymbol ( const SymbolType & symbol ) {
285 this-> template accessComponent < InputAlphabet > ( ).remove ( symbol );
286 }
287
294 return this-> template accessComponent < TapeAlphabet > ( ).get ( );
295 }
296
303 return std::move ( this-> template accessComponent < TapeAlphabet > ( ).get ( ) );
304 }
305
313 bool addTapeSymbol ( SymbolType symbol ) {
314 return this-> template accessComponent < TapeAlphabet > ( ).add ( std::move ( symbol ) );
315 }
316
323 this-> template accessComponent < TapeAlphabet > ( ).add ( std::move ( symbols ) );
324 }
325
332 this-> template accessComponent < TapeAlphabet > ( ).set ( std::move ( symbols ) );
333 }
334
342 void removeTapeSymbol ( const SymbolType & symbol ) {
343 this-> template accessComponent < TapeAlphabet > ( ).remove ( symbol );
344 }
345
351 const SymbolType & getBlankSymbol ( ) const & {
352 return this-> template accessComponent < BlankSymbol > ( ).get ( );
353 }
354
361 return std::move ( this-> template accessComponent < BlankSymbol > ( ).get ( ) );
362 }
363
371 bool setBlankSymbol ( SymbolType state ) {
372 return this-> template accessComponent < BlankSymbol > ( ).set ( std::move ( state ) );
373 }
374
390 bool addTransition ( StateType from, SymbolType input, StateType to, SymbolType output, Shift shift );
391
405 bool removeTransition ( const StateType & from, const SymbolType & input, const StateType & to, const SymbolType & output, const Shift & shift );
406
413
419 ext::map < ext::pair < StateType, SymbolType >, ext::tuple < StateType, SymbolType, Shift > > && getTransitions ( ) &&;
420
428 auto operator <=> ( const OneTapeDTM & other ) const {
429 return std::tie(getStates(), getInputAlphabet(), getInitialState(), getFinalStates(), getTapeAlphabet(), getBlankSymbol(), transitions) <=> std::tie(other.getStates(), other.getInputAlphabet(), other.getInitialState(), other.getFinalStates(), other.getTapeAlphabet(), other.getBlankSymbol(), other.transitions);
430 }
431
439 bool operator == ( const OneTapeDTM & other ) const {
440 return std::tie(getStates(), getInputAlphabet(), getInitialState(), getFinalStates(), getTapeAlphabet(), getBlankSymbol(), transitions) == std::tie(other.getStates(), other.getInputAlphabet(), other.getInitialState(), other.getFinalStates(), other.getTapeAlphabet(), other.getBlankSymbol(), other.transitions);
441 }
442
451 friend ext::ostream & operator << ( ext::ostream & out, const OneTapeDTM & instance ) {
452 return out << "(OneTapeDTM"
453 << " states = " << instance.getStates ( )
454 << " inputAlphabet = " << instance.getInputAlphabet ( )
455 << " initialState = " << instance.getInitialState ( )
456 << " finalStates = " << instance.getFinalStates ( )
457 << " tapeAlphabet = " << instance.getTapeAlphabet ( )
458 << " blankSymbol = " << instance.getBlankSymbol ( )
459 << " transitions = " << instance.getTransitions ( )
460 << ")";
461 }
462};
463
464template<class SymbolType, class StateType >
465OneTapeDTM<SymbolType, StateType>::OneTapeDTM ( ext::set < StateType > states, ext::set < SymbolType > tapeAlphabet, SymbolType blankSymbol, ext::set< SymbolType > inputAlphabet, StateType initialState, ext::set < StateType > finalStates ) : core::Components < OneTapeDTM, ext::set < SymbolType >, component::Set, std::tuple < TapeAlphabet, InputAlphabet >, SymbolType, component::Value, BlankSymbol, ext::set < StateType>, component::Set, std::tuple < States, FinalStates >, StateType, component::Value, InitialState > ( std::move ( tapeAlphabet ), std::move ( inputAlphabet ), std::move ( blankSymbol ), std::move ( states ), std::move ( finalStates ), std::move ( initialState ) ) {
466
467}
468
469template<class SymbolType, class StateType >
471
472}
473
474template<class SymbolType, class StateType >
476 if (!getStates().count(from)) {
477 throw AutomatonException("State \"" + ext::to_string ( from ) + "\" doesn't exist.");
478 }
479
480 if ( getFinalStates().count ( from ) )
481 throw AutomatonException("From state \"" + ext::to_string ( from ) + "\" is final..");
482
483 if (!getTapeAlphabet().count(input)) {
484 throw AutomatonException("Tape symbol \"" + ext::to_string ( input ) + "\" doesn't exist.");
485 }
486
487 if (!getStates().count(to)) {
488 throw AutomatonException("State \"" + ext::to_string ( to ) + "\" doesn't exist.");
489 }
490
491 if (!getTapeAlphabet().count(output)) {
492 throw AutomatonException("Tape symbol \"" + ext::to_string ( output ) + "\" doesn't exist.");
493 }
494
495 ext::pair<StateType, SymbolType> key = ext::make_pair(std::move(from), std::move(input));
496
497 ext::tuple<StateType, SymbolType, Shift > value(std::move(to), std::move(output), shift);
498
499 if (transitions.find(key) != transitions.end()) {
500 if(transitions.find(key)->second == value)
501 return false;
502 else
503 throw AutomatonException("Transition (\"" + ext::to_string ( key.first ) + "\", \"" + ext::to_string ( key.second ) + "\") -> ? already exists.");
504 }
505
506 transitions.insert ( std::move(key), std::move(value) );
507 return true;
508}
509
510template<class SymbolType, class StateType >
511bool OneTapeDTM<SymbolType, StateType>::removeTransition(const StateType& from, const SymbolType& input, const StateType& to, const SymbolType& output, const Shift& shift) {
513
514 if (transitions.find(key) == transitions.end())
515 return false;
516
517 ext::tuple<StateType, SymbolType, Shift > value(to, output, shift);
518 if(transitions.find(key)->second != value)
519 throw AutomatonException("Transition (\"" + ext::to_string ( from ) + "\", \"" + ext::to_string ( input ) + "\") -> ? doesn't exists.");
520
521 transitions.erase(key);
522 return true;
523}
524
525template<class SymbolType, class StateType >
527 return transitions;
528}
529
530template<class SymbolType, class StateType >
532 return std::move ( transitions );
533}
534
535} /* namespace automaton */
536
537namespace core {
538
545template<class SymbolType, class StateType >
546class SetConstraint< automaton::OneTapeDTM<SymbolType, StateType>, SymbolType, automaton::TapeAlphabet > {
547public:
557 if ( automaton.getBlankSymbol ( ) == symbol )
558 return true;
559
560 if ( automaton.getInputAlphabet().count(symbol))
561 return true;
562
563 for ( const std::pair<const ext::pair<StateType, SymbolType>, ext::tuple<StateType, SymbolType, automaton::Shift> >& transition : automaton.getTransitions())
564 if (symbol == transition.first.second || symbol == std::get<1>(transition.second))
565 return true;
566
567 return false;
568
569 }
570
580 return true;
581 }
582
590 }
591};
592
599template<class SymbolType, class StateType >
600class SetConstraint< automaton::OneTapeDTM<SymbolType, StateType>, SymbolType, automaton::InputAlphabet > {
601public:
611 return false;
612 }
613
623 return automaton.getTapeAlphabet ( ).count ( symbol );
624 }
625
633 if (symbol == automaton.getBlankSymbol())
634 throw automaton::AutomatonException("Input symbol \"" + ext::to_string ( symbol ) + "\" cannot be blank symbol.");
635 }
636};
637
644template<class SymbolType, class StateType >
645class ElementConstraint< automaton::OneTapeDTM<SymbolType, StateType>, SymbolType, automaton::BlankSymbol > {
646public:
656 return automaton.getTapeAlphabet ( ).count ( symbol );
657 }
658
666 if (automaton.getInputAlphabet().count( symbol ))
667 throw automaton::AutomatonException("Blank symbol \"" + ext::to_string ( symbol ) + "\" cannot be in input alphabet.");
668 }
669};
670
677template<class SymbolType, class StateType >
678class SetConstraint< automaton::OneTapeDTM<SymbolType, StateType>, StateType, automaton::States > {
679public:
689 if ( automaton.getInitialState ( ) == state )
690 return true;
691
692 if ( automaton.getFinalStates ( ).count ( state ) )
693 return true;
694
695 for ( const std::pair<const ext::pair<StateType, SymbolType>, ext::tuple<StateType, SymbolType, automaton::Shift> >& transition : automaton.getTransitions ( ) )
696 if ( state == transition.first.first || state == std::get < 0 > ( transition.second ) )
697 return true;
698
699 return false;
700 }
701
711 return true;
712 }
713
721 }
722};
723
730template<class SymbolType, class StateType >
731class SetConstraint< automaton::OneTapeDTM<SymbolType, StateType>, StateType, automaton::FinalStates > {
732public:
742 return false;
743 }
744
754 return automaton.getStates ( ).count ( state );
755 }
756
764 for ( const std::pair<const ext::pair<StateType, SymbolType>, ext::tuple<StateType, SymbolType, automaton::Shift> >& transition : automaton.getTransitions ( ) )
765 if ( state == transition.first.first )
766 throw automaton::AutomatonException("State \"" + ext::to_string ( state ) + "\" cannot be marked as final as it is source of a transition.");
767 }
768};
769
776template<class SymbolType, class StateType >
777class ElementConstraint< automaton::OneTapeDTM<SymbolType, StateType>, StateType, automaton::InitialState > {
778public:
788 return automaton.getStates ( ).count ( state );
789 }
790
798 }
799};
800
806template<class SymbolType, class StateType >
807struct normalize < automaton::OneTapeDTM < SymbolType, StateType > > {
809 ext::set < DefaultSymbolType > tapeAlphabet = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getTapeAlphabet ( ) );
810 ext::set < DefaultSymbolType > alphabet = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getInputAlphabet ( ) );
811 DefaultSymbolType blankSymbol = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( value ).getBlankSymbol ( ) );
812 ext::set < DefaultStateType > states = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getStates ( ) );
813 DefaultStateType initialState = automaton::AutomatonNormalize::normalizeState ( std::move ( value ).getInitialState ( ) );
814 ext::set < DefaultStateType > finalStates = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getFinalStates ( ) );
815
816 automaton::OneTapeDTM < > res ( std::move ( states ), std::move ( tapeAlphabet ), std::move ( blankSymbol ), std::move ( alphabet ), std::move ( initialState ), std::move ( finalStates ) );
817
818 for ( std::pair < ext::pair < DefaultStateType, DefaultSymbolType >, ext::tuple < DefaultStateType, DefaultSymbolType, automaton::Shift > > && transition : ext::make_mover ( std::move ( value ).getTransitions ( ) ) ) {
819 DefaultStateType from = automaton::AutomatonNormalize::normalizeState ( std::move ( transition.first.first ) );
820 DefaultSymbolType input = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( transition.first.second ) );
821 DefaultStateType to = automaton::AutomatonNormalize::normalizeState ( std::move ( std::get < 0 > ( transition.second ) ) );
822 DefaultSymbolType output = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( std::get < 1 > ( transition.second ) ) );
823
824 res.addTransition ( std::move ( from ), std::move ( input ), std::move ( to ), std::move ( output ), std::get < 2 > ( transition.second ) );
825 }
826
827 return res;
828 }
829};
830
831} /* namespace core */
832
833extern template class automaton::OneTapeDTM < >;
834
static ext::set< DefaultSymbolType > normalizeAlphabet(ext::set< SymbolType > &&symbols)
Definition: SymbolNormalize.h:50
static DefaultSymbolType normalizeSymbol(SymbolType &&symbol)
Definition: SymbolNormalize.h:68
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 single tape turing machine. Accepts recursive languages.
Definition: OneTapeDTM.h:71
const StateType & getInitialState() const &
Definition: OneTapeDTM.h:108
const ext::set< StateType > & getFinalStates() const &
Definition: OneTapeDTM.h:186
const ext::set< SymbolType > & getTapeAlphabet() const &
Definition: OneTapeDTM.h:293
bool removeTransition(const StateType &from, const SymbolType &input, const StateType &to, const SymbolType &output, const Shift &shift)
Removes a transition from the automaton.
Definition: OneTapeDTM.h:511
const ext::map< ext::pair< StateType, SymbolType >, ext::tuple< StateType, SymbolType, Shift > > & getTransitions() const &
Definition: OneTapeDTM.h:526
void removeFinalState(const StateType &state)
Definition: OneTapeDTM.h:226
OneTapeDTM(ext::set< StateType > states, ext::set< SymbolType > tapeAlphabet, SymbolType blankSymbol, ext::set< SymbolType > inputAlphabet, StateType initialState, ext::set< StateType > finalStates)
Creates a new instance of the automaton with a concrete set of states, tape alphabet,...
Definition: OneTapeDTM.h:465
const SymbolType & getBlankSymbol() const &
Definition: OneTapeDTM.h:351
ext::set< SymbolType > && getTapeAlphabet() &&
Definition: OneTapeDTM.h:302
bool setInitialState(StateType state)
Definition: OneTapeDTM.h:128
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: OneTapeDTM.h:235
SymbolTypeT SymbolType
Definition: OneTapeDTM.h:73
bool addInputSymbol(SymbolType symbol)
Definition: OneTapeDTM.h:255
bool addFinalState(StateType state)
Definition: OneTapeDTM.h:206
StateType && getInitialState() &&
Definition: OneTapeDTM.h:117
bool setBlankSymbol(SymbolType state)
Definition: OneTapeDTM.h:371
void setInputAlphabet(ext::set< SymbolType > symbols)
Definition: OneTapeDTM.h:273
void removeState(const StateType &state)
Definition: OneTapeDTM.h:177
void removeInputSymbol(const SymbolType &symbol)
Definition: OneTapeDTM.h:284
bool operator==(const OneTapeDTM &other) const
Definition: OneTapeDTM.h:439
StateTypeT StateType
Definition: OneTapeDTM.h:74
bool addTapeSymbol(SymbolType symbol)
Definition: OneTapeDTM.h:313
ext::set< StateType > && getStates() &&
Definition: OneTapeDTM.h:146
void setFinalStates(ext::set< StateType > states)
Definition: OneTapeDTM.h:215
void addTapeSymbols(ext::set< SymbolType > symbols)
Definition: OneTapeDTM.h:322
bool addState(StateType state)
Definition: OneTapeDTM.h:157
const ext::set< StateType > & getStates() const &
Definition: OneTapeDTM.h:137
void setStates(ext::set< StateType > states)
Definition: OneTapeDTM.h:166
bool addTransition(StateType from, SymbolType input, StateType to, SymbolType output, Shift shift)
Add a transition to the automaton.
Definition: OneTapeDTM.h:475
SymbolType && getBlankSymbol() &&
Definition: OneTapeDTM.h:360
void removeTapeSymbol(const SymbolType &symbol)
Definition: OneTapeDTM.h:342
void setTapeAlphabet(ext::set< SymbolType > symbols)
Definition: OneTapeDTM.h:331
void addInputSymbols(ext::set< SymbolType > symbols)
Definition: OneTapeDTM.h:264
ext::set< StateType > && getFinalStates() &&
Definition: OneTapeDTM.h:195
friend ext::ostream & operator<<(ext::ostream &out, const OneTapeDTM &instance)
Definition: OneTapeDTM.h:451
ext::set< SymbolType > && getInputAlphabet() &&
Definition: OneTapeDTM.h:244
Definition: components.hpp:181
static void valid(const automaton::OneTapeDTM< SymbolType, StateType > &automaton, const SymbolType &symbol)
Definition: OneTapeDTM.h:665
static bool available(const automaton::OneTapeDTM< SymbolType, StateType > &automaton, const SymbolType &symbol)
Definition: OneTapeDTM.h:655
static void valid(const automaton::OneTapeDTM< SymbolType, StateType > &, const StateType &)
Definition: OneTapeDTM.h:797
static bool available(const automaton::OneTapeDTM< SymbolType, StateType > &automaton, const StateType &state)
Definition: OneTapeDTM.h:787
Definition: components.hpp:25
static bool available(const automaton::OneTapeDTM< SymbolType, StateType > &, const SymbolType &)
Definition: OneTapeDTM.h:579
static bool used(const automaton::OneTapeDTM< SymbolType, StateType > &automaton, const SymbolType &symbol)
Definition: OneTapeDTM.h:556
static void valid(const automaton::OneTapeDTM< SymbolType, StateType > &, const SymbolType &)
Definition: OneTapeDTM.h:589
static bool available(const automaton::OneTapeDTM< SymbolType, StateType > &automaton, const SymbolType &symbol)
Definition: OneTapeDTM.h:622
static void valid(const automaton::OneTapeDTM< SymbolType, StateType > &automaton, const SymbolType &symbol)
Definition: OneTapeDTM.h:632
static bool used(const automaton::OneTapeDTM< SymbolType, StateType > &, const SymbolType &)
Definition: OneTapeDTM.h:610
static bool available(const automaton::OneTapeDTM< SymbolType, StateType > &automaton, const StateType &state)
Definition: OneTapeDTM.h:753
static void valid(const automaton::OneTapeDTM< SymbolType, StateType > &automaton, const StateType &state)
Definition: OneTapeDTM.h:763
static bool used(const automaton::OneTapeDTM< SymbolType, StateType > &, const StateType &)
Definition: OneTapeDTM.h:741
static bool available(const automaton::OneTapeDTM< SymbolType, StateType > &, const StateType &)
Definition: OneTapeDTM.h:710
static void valid(const automaton::OneTapeDTM< SymbolType, StateType > &, const StateType &)
Definition: OneTapeDTM.h:720
static bool used(const automaton::OneTapeDTM< SymbolType, StateType > &automaton, const StateType &state)
Definition: OneTapeDTM.h:688
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: 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
Class extending the tuple class from the standard library. Original reason is to allow printing of th...
Definition: tuple.hpp:42
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
Definition: ToGrammar.h:31
Shift
Definition: Shift.h:15
Definition: components.hpp:13
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
reference_mover< T > make_mover(T &param)
Move adaptor construction function specialized to lvalue reference parameter.
Definition: iterator.hpp:468
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
Definition: FordFulkerson.hpp:16
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
static automaton::OneTapeDTM< > eval(automaton::OneTapeDTM< SymbolType, StateType > &&value)
Definition: OneTapeDTM.h:808
Definition: normalize.hpp:13