Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
CompactSuffixAutomatonTerminatingSymbol.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <ext/iostream>
9
10#include <alib/string>
11#include <alib/map>
12
14
16#include <core/xmlApi.hpp>
21
23
25
26namespace indexes {
27
28namespace stringology {
29
30class GeneralAlphabet;
31
32template < class SymbolType = DefaultSymbolType >
36
37public:
39 m_string = str;
40 }
41
43 return m_string;
44 }
45
47 return std::move ( m_string );
48 }
49
50 void setNumberOfVertices(int n) {
51 m_delta.resize(n);
52 }
53
54 void insertVertex(unsigned vertexNumber, const ext::map<ext::pair< int,int>,int> & edges) {
55 for(const auto & edge : edges ) {
56 m_delta[vertexNumber].insert({{edge.first.first - 1, edge.first.second - 1}, edge.second}); // to match indexing the string from 0
57 }
58 }
59
61 return m_delta;
62 }
63
65 return std::move ( m_delta );
66 }
67
68 bool addTransition ( unsigned from, size_t startIndex, size_t endIndex, unsigned to ) {
69 return m_delta [ from ].insert ( ext::make_pair ( ext::make_pair ( startIndex, endIndex ), to ) ).second;
70 }
71
73 m_delta = std::move ( delta );
74 }
75
76/* void print() {
77 cout << "PRINT" << endl;
78 cout << m_delta.size() << endl;
79 for(int i = 0;i<m_delta.size();i++) {
80 cout << "Vrchol " << i << endl;
81 for(auto it = m_delta[i].begin(); it != m_delta[i].end(); ++it) {
82 cout << it->first.first << " " << it->first.second << endl;
83 cout << " -- ";
84 for(int j = it->first.first;j<=it->first.second;j++) {
85 cout << m_string[j];
86 }
87 cout << " --> " << it->second << endl;
88 }
89 }
90 }*/
91
92 void GetAllPathsLen(unsigned state, unsigned curLen, ext::set < unsigned > & res) const {
93 if(m_delta[state].empty ( ))
94 res.insert(m_string.size ( ) - curLen);
95
96 for(auto it = m_delta[state].begin();it!=m_delta[state].end();++it) {
97 GetAllPathsLen(it->second,curLen+it->first.second-it->first.first+1,res);
98 }
99 }
100
101 unsigned GetNextState(unsigned state,const ext::vector <SymbolType > & pattern, unsigned & index) const {
102 for(auto it = m_delta[state].begin();it!=m_delta[state].end();++it) {
103 if( m_string[it->first.first] != pattern[index])//hledám hranu, která má první znak stejný
104 continue;
105
106 for(unsigned i = it->first.first;i<=it->first.second;i++) { //chci projít všechny znaky, jestli se shodují se vzorem
107 if(index == pattern.size()) {
108 index += (it->first.second-i+1);
109 return it->second;
110 }
111
112 if(m_string[i] != pattern[index++]) { //pokud se neshodují - konec
113 throw "notfound";
114 }
115 }
116 return it->second;
117 }
118
119 throw "notfound";
120 }
121
123 return out << "(CompactSuffixAutomatonTerminatingSymbol " << instance.m_string << ", " << instance.m_delta << ")";
124 }
125
127 return std::tie ( getString ( ), getTransitions ( ) ) <=> std::tie ( other.getString ( ), other.getTransitions ( ) );
128 }
129
131 return std::tie ( getString ( ), getTransitions ( ) ) == std::tie ( other.getString ( ), other.getTransitions ( ) );
132 }
133
134 explicit operator automaton::CompactDFA < SymbolType, unsigned > ( ) const;
135};
136
137template < class SymbolType >
140 res.setInputAlphabet ( ext::set < SymbolType > ( getString ( ).begin ( ), getString ( ).end ( ) ) );
141
143 for ( size_t state = 0; state < getTransitions ( ).size ( ); ++ state )
144 states.insert ( state );
145 res.setStates ( std::move ( states ) );
146
147 res.addFinalState ( 1 );
148
149 for ( size_t state = 0; state < getTransitions ( ).size ( ); ++ state )
150 for ( const std::pair < const ext::pair < size_t, size_t >, unsigned > & transition : getTransitions ( ) [ state ] )
151 res.addTransition ( state, ext::vector < SymbolType > ( getString ( ).begin ( ) + transition.first.first, getString ( ).begin ( ) + transition.first.second + 1 ), transition.second );
152
153 return res;
154}
155
156} /* namespace stringology */
157
158} /* namespace indexes */
159
160namespace core {
161
162template < class SymbolType >
163struct normalize < indexes::stringology::CompactSuffixAutomatonTerminatingSymbol < SymbolType > > {
166 res.setString ( alphabet::SymbolNormalize::normalizeSymbols ( std::move ( value ).getString ( ) ) );
167 res.setTransitions ( std::move ( value ).getTransitions ( ) );
168
169 return res;
170 }
171};
172
173template < class SymbolType >
174struct xmlApi < indexes::stringology::CompactSuffixAutomatonTerminatingSymbol < SymbolType > > {
176 static bool first ( const ext::deque < sax::Token >::const_iterator & input );
177 static std::string xmlTagName ( );
179};
180
181template < class SymbolType >
187 res.setString ( std::move ( string ) );
188 res.setTransitions ( std::move ( delta ) );
189
191 return res;
192}
193
194template < class SymbolType >
197}
198
199template < class SymbolType >
201 return "CompactSuffixAutomatonTerminatingSymbol";
202}
203
204template < class SymbolType >
206 output.emplace_back ( xmlTagName ( ), sax::Token::TokenType::START_ELEMENT );
207 core::xmlApi < ext::vector < SymbolType > >::compose ( output, index.getString ( ) );
209 output.emplace_back ( xmlTagName ( ), sax::Token::TokenType::END_ELEMENT );
210}
211
212} /* namespace core */
213
static ext::vector< DefaultSymbolType > normalizeSymbols(ext::vector< SymbolType > &&symbols)
Definition: SymbolNormalize.h:86
Compact deterministic finite automaton. Accepts regular languages. The automaton has a list of symbol...
Definition: CompactDFA.h:73
Class extending the deque class from the standard library. Original reason is to allow printing of th...
Definition: deque.hpp:44
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 vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
reverse_iterator insert(const_reverse_iterator pos, const T &value)
Inserts the value on position given by iterator pos.
Definition: vector.hpp:229
auto end() &
Inherited behavior of end for non-const instance.
Definition: vector.hpp:155
Definition: CompactSuffixAutomatonTerminatingSymbol.h:33
const ext::vector< ext::map< ext::pair< size_t, size_t >, unsigned > > & getTransitions() const &
Definition: CompactSuffixAutomatonTerminatingSymbol.h:60
const ext::vector< SymbolType > & getString() const &
Definition: CompactSuffixAutomatonTerminatingSymbol.h:42
unsigned GetNextState(unsigned state, const ext::vector< SymbolType > &pattern, unsigned &index) const
Definition: CompactSuffixAutomatonTerminatingSymbol.h:101
friend ext::ostream & operator<<(ext::ostream &out, const CompactSuffixAutomatonTerminatingSymbol &instance)
Definition: CompactSuffixAutomatonTerminatingSymbol.h:122
void GetAllPathsLen(unsigned state, unsigned curLen, ext::set< unsigned > &res) const
Definition: CompactSuffixAutomatonTerminatingSymbol.h:92
void setTransitions(ext::vector< ext::map< ext::pair< size_t, size_t >, unsigned > > delta)
Definition: CompactSuffixAutomatonTerminatingSymbol.h:72
bool addTransition(unsigned from, size_t startIndex, size_t endIndex, unsigned to)
Definition: CompactSuffixAutomatonTerminatingSymbol.h:68
bool operator==(const CompactSuffixAutomatonTerminatingSymbol &other) const
Definition: CompactSuffixAutomatonTerminatingSymbol.h:130
void insertVertex(unsigned vertexNumber, const ext::map< ext::pair< int, int >, int > &edges)
Definition: CompactSuffixAutomatonTerminatingSymbol.h:54
ext::vector< ext::map< ext::pair< size_t, size_t >, unsigned > > && getTransitions() &&
Definition: CompactSuffixAutomatonTerminatingSymbol.h:64
void setString(const ext::vector< SymbolType > &str)
Definition: CompactSuffixAutomatonTerminatingSymbol.h:38
void setNumberOfVertices(int n)
Definition: CompactSuffixAutomatonTerminatingSymbol.h:50
ext::vector< SymbolType > && getString() &&
Definition: CompactSuffixAutomatonTerminatingSymbol.h:46
auto operator<=>(const CompactSuffixAutomatonTerminatingSymbol &other) const
Definition: CompactSuffixAutomatonTerminatingSymbol.h:126
static void popToken(ext::deque< Token >::iterator &input, Token::TokenType type, const std::string &data)
Definition: FromXMLParserHelper.cpp:39
static bool isToken(ext::deque< Token >::const_iterator input, Token::TokenType type, const std::string &data)
Definition: FromXMLParserHelper.cpp:29
int i
Definition: AllEpsilonClosure.h:118
return res
Definition: MinimizeByPartitioning.h:145
Definition: normalize.hpp:10
Definition: CapacityEdge.hpp:18
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
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
Definition: CompressedBitParallelTreeIndex.h:40
void end()
Definition: measurements.cpp:19
Definition: ArithmeticCompression.h:18
static indexes::stringology::CompactSuffixAutomatonTerminatingSymbol< > eval(indexes::stringology::CompactSuffixAutomatonTerminatingSymbol< SymbolType > &&value)
Definition: CompactSuffixAutomatonTerminatingSymbol.h:164
Definition: normalize.hpp:13
Definition: xmlApi.hpp:27