Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
SuffixAutomaton.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 <ext/iostream>
27
28#include <alib/string>
29#include <alib/set>
30
32
34
35#include <automaton/FSM/DFA.h>
36
37namespace indexes {
38
39namespace stringology {
40
41class GeneralAlphabet;
42
49template < class SymbolType = DefaultSymbolType >
50class SuffixAutomaton final {
55
59 unsigned m_backboneLength;
60
61public:
68 explicit SuffixAutomaton ( automaton::DFA < SymbolType, unsigned > automaton, unsigned backboneLength );
69
76
83
90 return m_automaton.getInputAlphabet ( );
91 }
92
99 return std::move ( m_automaton ).getInputAlphabet ( );
100 }
101
107 bool removeSymbolFromAlphabet ( const SymbolType & symbol ) {
108 return m_automaton.removeInputSymbol ( symbol );
109 }
110
116 unsigned getBackboneLength ( ) const {
117 return m_backboneLength;
118 }
119
127 auto operator <=> ( const SuffixAutomaton & other ) const {
128 return std::tie ( getAutomaton ( ) ) <=> std::tie ( other.getAutomaton ( ) );
129 }
130
138 bool operator == ( const SuffixAutomaton & other ) const {
139 return std::tie ( getAutomaton ( ) ) == std::tie ( other.getAutomaton ( ) );
140 }
141
150 friend ext::ostream & operator << ( ext::ostream & out, const SuffixAutomaton & instance ) {
151 return out << "(SuffixAutomaton " << instance.m_automaton << ")";
152 }
153
159 explicit operator automaton::DFA < SymbolType, unsigned > ( ) const;
160};
161
162} /* namespace stringology */
163
164} /* namespace indexes */
165
166namespace indexes {
167
168namespace stringology {
169
170template < class SymbolType >
171SuffixAutomaton < SymbolType >::SuffixAutomaton ( automaton::DFA < SymbolType, unsigned > automaton, unsigned backboneLength ) : m_automaton ( std::move ( automaton ) ), m_backboneLength ( backboneLength ) {
172}
173
174template < class SymbolType >
176 return m_automaton;
177}
178
179template < class SymbolType >
181 return std::move ( m_automaton );
182}
183
184template < class SymbolType >
186 return getAutomaton ( );
187}
188
189} /* namespace stringology */
190
191} /* namespace indexes */
192
193namespace core {
194
200template < class SymbolType >
201struct normalize < indexes::stringology::SuffixAutomaton < SymbolType > > {
203 ext::set < DefaultSymbolType > alphabet = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( std::move ( value ).getAutomaton ( ) ).getInputAlphabet ( ) );
204 ext::set < unsigned > states = std::move ( std::move ( value ).getAutomaton ( ) ).getStates ( );
205 unsigned initialState = std::move ( std::move ( value ).getAutomaton ( ) ).getInitialState ( );
206 ext::set < unsigned > finalStates = std::move ( std::move ( value ).getAutomaton ( ) ).getFinalStates ( );
207
208 automaton::DFA < DefaultStateType, unsigned > res ( std::move ( states ), std::move ( alphabet ), initialState, std::move ( finalStates ) );
209
210 for ( std::pair < ext::pair < unsigned, SymbolType >, unsigned > && transition : ext::make_mover ( std::move ( std::move ( value ).getAutomaton ( ) ).getTransitions ( ) ) ) {
211 unsigned from = transition.first.first;
212 DefaultSymbolType input = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( transition.first.second ) );
213 unsigned to = transition.second;
214
215 res.addTransition ( from, std::move ( input ), to );
216 }
217
218 return indexes::stringology::SuffixAutomaton < > ( std::move ( res ), std::move ( value ).getBackboneLength ( ) );
219 }
220};
221
222} /* namespace core */
223
static ext::set< DefaultSymbolType > normalizeAlphabet(ext::set< SymbolType > &&symbols)
Definition: SymbolNormalize.h:50
static DefaultSymbolType normalizeSymbol(SymbolType &&symbol)
Definition: SymbolNormalize.h:68
Deterministic finite automaton. Accepts regular languages.
Definition: DFA.h:71
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
Suffix automaton string index. Automaton representation of all suffixes. The automaton is general det...
Definition: SuffixAutomaton.h:50
bool removeSymbolFromAlphabet(const SymbolType &symbol)
Definition: SuffixAutomaton.h:107
ext::set< SymbolType > && getAlphabet() &&
Definition: SuffixAutomaton.h:98
unsigned getBackboneLength() const
Definition: SuffixAutomaton.h:116
SuffixAutomaton(automaton::DFA< SymbolType, unsigned > automaton, unsigned backboneLength)
Definition: SuffixAutomaton.h:171
const ext::set< SymbolType > & getAlphabet() const &
Definition: SuffixAutomaton.h:89
friend ext::ostream & operator<<(ext::ostream &out, const SuffixAutomaton &instance)
Definition: SuffixAutomaton.h:150
bool operator==(const SuffixAutomaton &other) const
Definition: SuffixAutomaton.h:138
const automaton::DFA< SymbolType, unsigned > & getAutomaton() const &
Definition: SuffixAutomaton.h:175
auto operator<=>(const SuffixAutomaton &other) const
Definition: SuffixAutomaton.h:127
Definition: Object.h:16
Definition: BarSymbol.cpp:12
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
return res
Definition: MinimizeByPartitioning.h:145
Definition: ToGrammar.h:31
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
Definition: CompressedBitParallelTreeIndex.h:40
Definition: ArithmeticCompression.h:18
static indexes::stringology::SuffixAutomaton< > eval(indexes::stringology::SuffixAutomaton< SymbolType > &&value)
Definition: SuffixAutomaton.h:202
Definition: normalize.hpp:13