-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssm.h
More file actions
104 lines (88 loc) · 2.74 KB
/
Copy pathssm.h
File metadata and controls
104 lines (88 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// Created by pc on 5/31/2024.
//
#ifndef ESTIMATINGSTRINGPATTERNALGORITHMS_SSM_H
#define ESTIMATINGSTRINGPATTERNALGORITHMS_SSM_H
#include "include/define.h"
#include "include/main_header.h"
#include "Algorithm.h"
class ssm : public Algorithm {
public:
const char *name() const override {
return "SSM";
}
void Horspool_Distance(int Horspool[], int Dist[], unsigned char *x, int m, int *dMax) {
int i;
Dist[m] = 0;
for (i = 0; i < SIGMA; i++) Horspool[i] = m;
for (i = 0; i < m - 1; i++) {
if (Horspool[x[i]] == m)
Dist[i] = i + 1;
else
Dist[i] = i - (m - Horspool[x[i]] - 1);
Horspool[x[i]] = m - i - 1;
if (Dist[i] > Dist[m]) {
Dist[m] = Dist[i];
*dMax = i;
}
}
}
//************************************************************************//
void PreProc(int Dist[], int Shift[], unsigned char *x, int m) {
/*----compute the safe shift from left to right*/
int i, rept = 1;
Shift[0] = 1;
for (i = 1; i < m; i++) {
if (x[i] != x[i - 1]) {
Shift[i] = 1;
rept = 1;
} else {
rept++;
Shift[i] = rept;
}
}
/*----compute the safe shift from right to left*/
rept = 1;
for (i = m - 1; i > 0; i--) {
if (x[i] != x[i - 1]) {
if (rept >= Shift[i - 1])
Shift[i - 1] = rept + 1;
rept = 1;
} else
rept++;
}
/*----Compute the max shift of all shifts----*/
for (i = 0; i < m; i++)
if (Shift[i] < Dist[m])
Shift[i] = Dist[m];
}
/*-------------------------------------------------------------*/
int search(unsigned char *x, int m, unsigned char *y, int n) override {
int count, q, j, Max, jMax, Pos;
int Hors[SIGMA], Sht[XSIZE], Dis[XSIZE];
unsigned char xMax;
/*Preprocessing phase-----------*/
Horspool_Distance(Hors, Dis, x, m, &Max);
PreProc(Dis, Sht, x, m);
/*Searching Phase--------------*/
count = 0;
j = m - 1;
jMax = Max - m + 1;
xMax = x[Max];
memcpy(y + n, x, m);
while (j < n) {
while (y[j + jMax] != xMax) j = j + Hors[y[j]];
q = m - 1;
Pos = 1 - m + j;
while ((q >= 0) && (x[q] == y[q + Pos]))
q--;
if ((q < 0) && (j < n)) {
count++;
q++;
}
j = j + Sht[q];
}
return count;
}
};
#endif //ESTIMATINGSTRINGPATTERNALGORITHMS_SSM_H