-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf2.cpp
74 lines (59 loc) · 2.03 KB
/
cf2.cpp
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
#define NEW_SIGMA 16
#include "common.h"
// cf_new
void cf2 (const uchar *P, const int m, const uchar *T, const int n, const int sigma, const int k) {
int *hp = (int*)malloc(sizeof(int)*sigma);
int *ht = (int*)malloc(sizeof(int)*sigma);
int sum = 0;
for ( int i = 0; i < sigma; ++i) {
hp[ i ] = ht[ i ] = 0;
}
for ( int i = 0; i < m; ++i) {
++hp[ map[P[i]] ]; // pattern
++ht[ map[T[i]] ]; // initial window
}
for(int i = m; i < n; i++) {
sum = 0;
for(int j = 0; j < sigma; j++) {
sum += abs(hp[j] - ht[j]);
}
if ( sum <= k*2 ) {
++v_count;
}
--ht[ map[T[i-m] ]];
++ht[ map[T[i] ]];
}
free(hp);
free(ht);
}
/* MAIN */
int main(int argc, char *argv[]) {
if ( argc < 4 ) return EXIT_FAILURE;
t_timemes t_data_acc = {0}, t_pre = {0}, t_run = {0};
t_init(t_data_acc); t_init(t_pre); t_init(t_run);
t_start(t_pre);
const char *patterns_filename = argv[1], *patt_size_pch = argv[2], *filename = argv[3];
unsigned int patt_size = atoi(patt_size_pch), text_size = 0;
uchar *text = NULL, *pattern = NULL;
FILE * file = NULL;
text_size = read_file_content<uchar>(&text, filename);
if (text == NULL) return EXIT_FAILURE;
pattern = (uchar*)calloc (patt_size+1, CH_SIZE);
if (pattern == NULL) { printf("Error: Can't allocate memory for pattern."); free(text); return EXIT_FAILURE; }
file = fopen (patterns_filename, "rb");
if (!file) { printf("Error: Can't open patterns file."); free(text); free(pattern); return EXIT_FAILURE; }
indexes = (bool*)calloc(text_size, BOOL_SIZE);
int sigma = CreateMapByHistogramBinPack<uchar>(text, text_size);
while (fread (pattern, CH_SIZE, patt_size, file)==patt_size) {
t_start(t_run);
cf2(pattern, patt_size, text, text_size, sigma, K_DIFF);
t_stop(t_run);
}
for(unsigned long l = 0; l < text_size; ++l) if( indexes[l] ) ++counter;
print_resultn(t_get_seconds(t_run), v_count, counter, sigma, Q_GRAM, K_DIFF);
free(text);
free(pattern);
free(indexes);
fclose(file);
return EXIT_SUCCESS;
}