9#include <string/LinearString.h>
17 template <
class SymbolType>
18 class CompactSuffixAutomatonConstructInt {
38 return newVertexNumber++;
41 void createEdge(
int fromVertex,
int first,
int second,
int toVertex) {
42 edges.
at(fromVertex).insert({{first,
second},toVertex});
47 void wkEdge(
int s,
const SymbolType & c,
int & sc,
int & kc,
int & pc) {
49 if ( w.
at (
edge.first.first ) == c) {
51 kc =
edge.first.first;
52 pc =
edge.first.second;
61 void replaceTheEdgeBy(
int s,
int k,
int p,
int sc,
int kc,
int pc,
int r) {
62 edges.
at(s).erase({kc,pc});
64 createEdge(s,kc,kc+p-k,r);
65 createEdge(r,kc+p-k+1,pc,sc);
69 void replaceTheEdgeByEdge(
int s,
int k,
int p,
int kc,
int pc,
int r) {
70 edges.
at(s).erase({kc,pc});
72 createEdge(s,kc,kc+p-k,r);
76 void replaceThewkEdge(
int s,
int k,
int p,
int rc) {
80 wkEdge(s,w.
at(k),scc,kcc,pcc);
82 edges.
at(s).erase({kcc,pcc});
87 bool thereIsACedgeFromS(
int s,
const SymbolType & c) {
90 if ( w.
at (
edge.first.first ) == c )
97 int duplicationOf(
int sc) {
98 int rc = createNode();
101 createEdge ( rc,
edge.first.first,
edge.first.second,
edge.second );
108 bool check_end_point(
int s,
int k,
int p,
const SymbolType & c) {
113 wkEdge(s,w.
at(k),sc,kc,pc);
114 return (c == w.
at(kc+p-k+1));
116 return thereIsACedgeFromS(s,c);
119 int split_edge(
int s,
int k,
int p) {
123 wkEdge(s,w.
at(k),sc,kc,pc);
125 int r = createNode();
126 replaceTheEdgeBy(s,k,p,sc,kc,pc,r);
128 int len = length.
at(s);
132 length[r] = len + (p-k+1);
143 wkEdge(s,w.
at(k),sc,kc,pc);
148 while(pc-kc <= p-k) {
152 wkEdge(s,w.
at(k),sc,kc,pc);
160 void redirect_edge(
int s,
int k,
int p,
int r) {
164 wkEdge(s,w.
at(k),sc,kc,pc);
166 replaceTheEdgeByEdge(s,k,p,kc,pc,r);
169 int extension(
int s,
int k,
int p) {
176 wkEdge(s,w.
at(k),sc,kc,pc);
183 ext::tie ( sc, kc ) = canonize(s,k,p);
187 int len = length.
at(s);
191 int lensc = length.
at(sc);
195 if(lensc == len+(p-k+1))
198 int rc = duplicationOf(sc);
199 suf[rc] = suf.
at(sc); suf[sc] = rc;
200 length[rc] = len +(p-k+1);
202 replaceThewkEdge(s,k,p,rc);
204 ext::tie ( s, k ) = canonize(suf.
at(s),k,p-1);
216 while(!check_end_point(s,k,p-1,c)) {
218 if(sc == extension(s,k,p)) {
219 redirect_edge(s,k,p-1,r);
220 ext::tie ( s, k ) = canonize(suf.
at(s),k,p-1);
223 sc = extension(s,k,p);
224 r = split_edge(s,k,p-1);
229 createEdge(r,p,INT_MAX,sink);
235 ext::tie ( s, k ) = canonize(suf.
at(s),k,p-1);
240 return separate_node(s,k,p);
244 CompactSuffixAutomatonConstructInt (
const ext::vector < SymbolType > & subject ) : wLen ( subject.size ( ) ), endChar ( subject.back ( ) ) {
246 newVertexNumber = -1;
249 for (
int i = 0;
i < wLen; ++
i )
259 void startConstruction() {
261 source = createNode();
263 for(
int j = 1;j<=m;j++)
264 createEdge(T,-j,-j,source);
266 length[source] = 0; length[T] = -1;
267 e = 0; length[sink] = INT_MAX;
276 }
while (w.
at(
i) != endChar);
279 void print (
int i )
const {
281 for(
int pi = 0;pi<newVertexNumber;pi++) {
282 std::cout <<
"Vrchol " << pi << std::endl;
284 std::cout << it->first.first <<
" " << it->first.second;
286 for(
int j = it->first.first;j<=it->first.second;j++) {
290 std::cout <<
" --> " << it->second << std::endl;
294 std::cout <<
"-------------" << std::endl;
297 for(
int pi = 0;pi<newVertexNumber;pi++)
298 std::cout <<
"Vrchol " << pi <<
" " << length.find(pi)->second << std::endl;
300 std::cout <<
"Suffix links" << std::endl;
301 for(
int pi = 0;pi<newVertexNumber;pi++)
302 std::cout <<
"Vrchol " << pi <<
" " << suf.find(pi)->second << std::endl;
305 void changeIntMaxToE() {
306 for(
auto it = edges.
begin();it!=edges.
end();++it)
307 for(
auto it2 = it->second.begin();it2!=it->second.end();++it2)
308 if(it2->first.second == INT_MAX) {
309 int k = it2->first.first;
312 it->second.
erase(it2);
313 it->second.insert({{k,e},r});
314 it2 = it->second.begin();
325 CompactSuffixAutomatonConstructInt < DefaultSymbolType > algo (subject.
getContent ( ) );
326 algo.startConstruction();
327 algo.changeIntMaxToE();
332 res.setNumberOfVertices(algo.getEdges().size()-1);
334 for (
const auto &
edge : algo.getEdges ( ) )
335 if (
edge.first != -1 )
341 template <
class SymbolType >
345 content.push_back ( endSymbol );
347 CompactSuffixAutomatonConstructInt < SymbolType > algo ( content );
348 algo.startConstruction();
349 algo.changeIntMaxToE();
353 res.setString ( content );
354 res.setNumberOfVertices(algo.getEdges().size()-1);
356 for (
const auto &
edge : algo.getEdges ( ) )
357 if (
edge.first != -1 )
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
R & at(const T &key, R &defaultValue)
Definition: map.hpp:163
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: map.hpp:185
std::pair< iterator, bool > insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: map.hpp:118
auto end() &
Inherited behavior of end for non-const instance.
Definition: map.hpp:215
size_t erase(const K &key)
Erase by key of arbitrary type.
Definition: map.hpp:340
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: vector.hpp:125
auto end() &
Inherited behavior of end for non-const instance.
Definition: vector.hpp:155
Definition: CompactSuffixAutomatonTerminatingSymbol.h:33
Definition: LinearStringTerminatingSymbol.h:30
const ext::vector< DefaultSymbolType > & getContent() const
Definition: LinearStringTerminatingSymbol.cpp:42
Linear string.
Definition: LinearString.h:57
const ext::set< SymbolType > & getAlphabet() const &
Definition: LinearString.h:103
const ext::vector< SymbolType > & getContent() const &
Definition: LinearString.h:238
Definition: ExperimentalCompactSuffixAutomatonConstruct.h:15
static indexes::stringology::CompactSuffixAutomatonTerminatingSymbol< SymbolType > construct(const string::LinearString< SymbolType > &subject)
Definition: ExperimentalCompactSuffixAutomatonConstruct.h:342
static indexes::stringology::CompactSuffixAutomatonTerminatingSymbol< DefaultSymbolType > construct(const string::LinearStringTerminatingSymbol &subject)
Definition: ExperimentalCompactSuffixAutomatonConstruct.h:324
Definition: BarSymbol.cpp:12
p second
Definition: ToRegExpAlgebraic.h:126
int i
Definition: AllEpsilonClosure.h:118
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
return res
Definition: MinimizeByPartitioning.h:145
T createUnique(T object, const Alphabets &... alphabets)
Definition: createUnique.hpp:46
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
Definition: ArithmeticCompression.h:18