Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
Total.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
28#include <automaton/FSM/NFA.h>
29#include <automaton/FSM/DFA.h>
30#include <automaton/TA/DFTA.h>
31#include <automaton/TA/NFTA.h>
32
35
36namespace automaton {
37
38namespace simplify {
39
44class Total {
45 template < class StateType, class Callback >
46 static void CombinationRepetitionUtil ( const ext::set < StateType > & states, ext::vector < StateType > & stateList, size_t length, Callback callback ) {
47 if ( stateList.size ( ) == length )
48 callback ( stateList );
49 else for ( const StateType & state : states ) {
50 stateList.push_back ( state );
51 CombinationRepetitionUtil ( states, stateList, length, callback );
52 stateList.pop_back ( );
53 }
54 }
55
56 template < class StateType, class Callback >
57 static void CombinationRepetition ( ext::set < StateType > states, size_t length, Callback callback ) {
59 CombinationRepetitionUtil ( states, stateList, length, callback );
60 }
61
62public:
72 template < class T >
73 requires isDFA < T > || isNFA < T >
74 static T total ( const T & automaton );
75
76 template < class T >
77 requires isDFTA < T > || isNFTA < T >
78 static T total ( const T & automaton );
79};
80
81template < class T >
82requires isDFA < T > || isNFA < T >
83T Total::total ( const T & automaton ) {
84 using StateType = typename T::StateType;
85
86 if ( automaton.isTotal ( ) )
87 return automaton;
88
89 T res ( automaton );
90 StateType nullState = common::createUnique ( label::FailStateLabel::instance < StateType > ( ), automaton.getStates ( ) );
91 res.addState ( nullState );
92
93 for ( const auto & q : res.getStates ( ) ) {
94 for ( const auto & a : res.getInputAlphabet ( ) ) {
95 if ( ! res.getTransitions ( ).contains ( ext::make_pair ( q, a ) ) ) {
96 res.addTransition ( q, a, nullState );
97 }
98 }
99 }
100
101 return res;
102}
103
104template < class T >
105requires isDFTA < T > || isNFTA < T >
106T Total::total ( const T & automaton ) {
107 using StateType = typename T::StateType;
108
109 T res ( automaton );
110 StateType nullState = common::createUnique ( label::FailStateLabel::instance < StateType > ( ), automaton.getStates ( ) );
111 res.addState ( nullState );
112
113 for ( const auto & a : res.getInputAlphabet ( ) ) {
114 unsigned rank = a.getRank ( );
115 CombinationRepetition ( res.getStates ( ), rank, [ & ] ( const ext::vector < StateType > & stateList ) {
116 if ( ! res.getTransitions ( ).contains ( ext::make_pair ( a, stateList ) ) ) {
117 res.addTransition ( a, stateList, nullState );
118 }
119 } );
120 }
121
122 return res;
123}
124
125} /* namespace simplify */
126
127} /* namespace automaton */
128
Definition: Total.h:44
static T total(const T &automaton)
Definition: Total.h:83
static T total(const T &automaton)
Definition: set.hpp:44
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
return res
Definition: MinimizeByPartitioning.h:145
q
Definition: SingleInitialStateEpsilonTransition.h:85
Definition: ToGrammar.h:31
T createUnique(T object, const Alphabets &... alphabets)
Definition: createUnique.hpp:46
int callback(struct dl_phdr_info *info, size_t, void *data)
Definition: simpleStacktrace.cpp:25
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79