-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
345 lines (285 loc) · 8.21 KB
/
common.h
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#ifndef COMMON_H
#define COMMON_H
/* Compile mode detection */
#if defined(_WIN32) || defined(_WIN64)
#define WINDOWS
#if defined(_WIN64)
#define ENV64BIT
#else
#define ENV32BIT
#endif
#endif
#if defined(__GNUC__)
#define UNIX
#if defined(__x86_64__) || defined(__ppc64__)
#define ENV64BIT
#else
#define ENV32BIT
#endif
#endif
#include <xmmintrin.h> /* SSE */
#include <emmintrin.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <math.h>
#include <limits>
#include <iostream>
#include <algorithm> /* std:sort */
#ifdef WINDOWS
#define NOMINMAX /* to disable windows.h min,max */
#include <windows.h>
#include <stdint.h> /* int64_t, etc. for Visual Studio */
#endif
/* Print results flag */
#define PRINT_RESULTSS
#define VERIFY_MEMCMPP
/* Typedefs */
#ifdef ENV64BIT
typedef int64_t word;
typedef long double ldouble;
#endif
#ifdef ENV32BIT
typedef int32_t word;
typedef double ldouble;
#endif
typedef unsigned char uchar;
/* Constants */
const unsigned int MAX_WORD_SIZE = sizeof(word)*8;
/* Sizes of types */
const size_t CH_SIZE = sizeof(char);
const size_t PCH_SIZE = sizeof(char*);
const size_t PINT_SIZE = sizeof(unsigned int*);
const size_t BOOL_SIZE = sizeof(bool);
const size_t UCH_SIZE = sizeof(uchar);
/* Other variables */
long counter = 0;
bool *indexes = 0;
#define U 2
int k = 4; /* q */
int64_t v_count = 0;
#ifndef K_DIFF
#define K_DIFF 0
#endif
#ifndef ASIZE
#define ASIZE 256
#endif
#ifndef Q_GRAM
#define Q_GRAM 0
#endif
#ifndef NEW_SIGMA
#define NEW_SIGMA 0
#endif
/* Time measurement stuff */
#ifdef UNIX
typedef struct {
timespec start;
timespec stop;
timespec sum;
} t_timemes;
#define t_init(t) t.sum.tv_nsec = 0; t.sum.tv_sec = 0
#define t_start(t) clock_gettime(CLOCK_MONOTONIC, &(t.start))
#define t_stop(t) clock_gettime(CLOCK_MONOTONIC, &(t.stop)); \
t.sum.tv_sec += t.stop.tv_sec - t.start.tv_sec; \
t.sum.tv_nsec += t.stop.tv_nsec - t.start.tv_nsec
#define t_get_seconds(t) (double)t.sum.tv_sec + (double)t.sum.tv_nsec / (double)1000000000
#else
typedef struct {
LARGE_INTEGER ticksPerSecond;
LARGE_INTEGER tick;
double start; // A point in time
double stop;
double sum;
} t_timemes;
#define t_init(t) t.sum = 0; QueryPerformanceFrequency(&(t.ticksPerSecond))
#define t_start(t) QueryPerformanceCounter(&(t.tick)); t.start = double(t.tick.QuadPart)/t.ticksPerSecond.QuadPart
#define t_stop(t) QueryPerformanceCounter(&(t.tick)); \
t.stop = double(t.tick.QuadPart)/t.ticksPerSecond.QuadPart; \
t.sum += t.stop - t.start
#define t_get_seconds(t) t.sum
#endif
/* FUNCTIONS */
/*
static inline word ilog2( ldouble n ) {
return (word) ( log( n ) / log( (ldouble)2 ) );
}
*/
#define log2 0.69314718055994529
#define ilog2(n) (word) ( log( n ) / log2 )
#define ILOG_2( x ) ilog2( x )
void print_result(double search_time, long ver_count, long matches, long sigma, int q, int kdiff) {
printf("%d\t%d\t%d\t%f\t%ld\t%ld", sigma, q, kdiff, search_time, ver_count, matches);
}
void print_resultn(double search_time, long ver_count, long matches, long sigma, int q, int kdiff) {
print_result(search_time, ver_count, matches, sigma, q, kdiff);
printf("\n");
}
unsigned int get_number_of_occurences(const uchar *T, const int n, const uchar s) {
int occurences = 0;
for(int i = 0; i < n; i++)
if(T[i]==s)
occurences++;
return occurences;
}
static int * build_verify_table( int m, int k, int r, int l )
{
int mk;
int h;
int mpk = m / k;
int c, f, * t;
t = (int*)malloc( MAX_WORD_SIZE * sizeof( int ));
for( f = 0; f < r; f++ ) {
c = ( 1 - f - mpk ) * k;
mk = ( mpk * l - 1 + f * l );
for( h = 0; h < k; h++, c-- ) {
t[ mk ] = c;
mk += mpk * l + r - 1;
}
}
return t;
}
int double_cmp(const void *a, const void *b)
{
const double *da = (const double *)a;
const double *db = (const double *)b;
return (int)(*da - *db);
}
template <class T> long read_file_content( T**buffer, const char* filename ) {
long length;
FILE * file = fopen (filename, "rb");
if (file) {
fseek (file, 0, SEEK_END);
length = ftell (file);
fseek (file, 0, SEEK_SET);
*buffer = (T*)malloc (length+1);
if (*buffer==NULL) return 0;
fread (*buffer, 1, length, file);
*(*buffer+length) = '\0';
fclose (file);
}
return length;
}
template <class T> void printf_binnary(T var) {
unsigned int size = sizeof(T)*8;
for ( int i = size - 1; i >= 0; i--) {
printf("%d", var >> i & 1);
}
}
template <class T> char * deb_printf_binnary(T var) {
unsigned int size = sizeof(T)*8;
char *str = new char[size];
char *tmp = str;
for ( int i = size - 1; i >= 0; i--) {
sprintf(tmp++, "%d", var >> i & 1);
}
return str;
}
int map [256];
bool CreateMapByHistogram_compar (unsigned long* i, unsigned long* j) { return (*i > *j); }
template<class T> int CreateMapByHistogram(T* t, int n) {
// Map array creation
int sigma = 0;
int threshold = 16; // how much sigma is the most
unsigned long symbols [256];
unsigned long * symbolsp[256];
for (int i = 0; i < 256; ++i) {
symbols[i] = 0;
symbolsp[i] = &(symbols[i]);
}
for (long i = 0; i < n; ++i)
symbols[ t[i] ] ++;
std::sort(symbolsp, &(symbolsp[255]), CreateMapByHistogram_compar);
for (int i = 0; i < 256; ++i) map[i] = NULL;
for (int i = 0; i < threshold; ++i) {
if(*symbolsp[i]) {
*symbolsp[i] = sigma++;
} else {
*symbolsp[i] = sigma-1;
}
}
for (int i = threshold; i < 256; ++i) {
*symbolsp[i] = sigma<threshold?sigma-1:threshold-1;
}
//printf("HISTOGRAM\n");
for (int i = 0; i < 256; ++i) {
map[i] = symbols[i];
//printf("%d (%c) = %d\n", i, i, map[i]);
}
return sigma;
}
template<class T> int CreateMapByHistogramBinPack(T* t, int n) {
// Map array creation
int sigma = 0;
int new_sigma = NEW_SIGMA;
unsigned long symbols [256];
unsigned long * symbolsp[256];
long sum = 0;
int binSize = n / new_sigma;
for (int i = 0; i < 256; ++i) {
symbols[i] = 0;
symbolsp[i] = &(symbols[i]);
}
for (long i = 0; i < n; ++i)
symbols[ t[i] ] ++;
std::sort(symbolsp, &(symbolsp[255]), CreateMapByHistogram_compar);
// symbolsp is now most common symbols (the most at first position)
for (int i = 0; i < 256; ++i) map[i] = NULL;
for (int i = 0; i < 256; ++i) {
sum += *symbolsp[i];
if(sum > binSize) {
sum = sum - binSize;
*symbolsp[i] = sigma++;
} else {
*symbolsp[i] = sigma;
}
}
// printf("HISTOGRAM\n");
for (int i = 0; i < 256; ++i) {
map[i] = symbols[i]; // printf("%d (%c) = %d\n", i, i, map[i]);
}
//for(int i = 0; i < new_sigma; ++i) { printf("%d:\t[ ", i); for (int j = 0; j < 256; ++j) { if( symbols[j] == i ) printf(" '%c' ", j); } printf(" ] \n"); }
return ++sigma;
}
template <unsigned int Q, unsigned int SIGMA> struct get_ {
static unsigned long long int gram(const uchar * s, const long start /* at start set to 1 */ ) {
return map[s[Q - 1]] * start + get_<Q - 1, SIGMA>::gram(s, start*SIGMA);
}
};
template <unsigned int SIGMA> struct get_<0, SIGMA> {
static unsigned long long int gram(const uchar * s, const long start /* at start set to 1 */ ) { return 0; }
};
template <unsigned int Q, unsigned int SIGMA> unsigned long long int get_q_gram(const uchar *s) {
return get_<Q, SIGMA>::gram(s, 1);
};
void KDIFFERENCES (const uchar *y, int n, int index, const uchar *x, int m, int k) {
int T[256][256];
++v_count;
// Preprocessing
// Offset aby wyrownac indeksowanie do 0
int offset = 1;
for (int i = -1; i < n; ++i) {
T[-1 + offset ][ i + offset ] = 0;
}
for (int i = -1; i < m; ++i) {
T[ i + offset ][ -1 + offset ] = i + 1;
}
// Searching
int j = 0;
for( j=0; j < n; j++) {
for(int i=0; i<m; i++) {
int p = x[i] == y[j] ? 0 : 1;
T[i + offset][j + offset] = std::min(
T[i - 1 + offset][j - 1 + offset] + p,
std::min( T[i + offset][j - 1 + offset] + 1, T[i - 1 + offset][j + offset] + 1)
);
}
if( T[m][j + offset] <= k ) {
indexes[ j + index - m + 1] = true;
}
}
}
// */
#endif