Bugfix: Fixed crash in bm.c by correctly allocating memory, fixed output of print_pat...
[2dmatching.git] / zhu.c
blobdc1a6bc2423fa09a025e1b55ca693740deaecf1d
1 #include "code.h"
3 unsigned int zhu( char **text, char **pattern, int m, int n, int version )
5 /* Huge optimization instead of unsigned int B */
6 #define B 128
8 double *text2 = ( double* ) malloc( n * n * sizeof( double ));
10 if( text2 == NULL ) {
11 printf("Failed to allocate array!\n");
12 exit(1);
15 double *pattern2 = ( double* ) malloc( m * sizeof( double ));
17 if( pattern2 == NULL ) {
18 printf("Failed to allocate array!\n");
19 exit(1);
22 unsigned int i, j, k, l = 0, matches;
24 /* Reduce text to 1d and calculate hash */
25 for( k = 0; k < n - m + 1; k++ )
26 for( j = 0; j < n; j++, l++ ) {
27 text2[l] = 0;
29 for( i = 0 + k; i < m + k; i++ )
30 text2[l] = text2[l] * B + text[i][j] % Q;
33 /* Reduce pattern to 1d and calculate hash */
34 for( j = 0; j < m; j++ ) {
35 pattern2[j] = 0;
37 for( i = 0; i < m; i++ )
38 pattern2[j] = pattern2[j] * B + pattern[i][j] % Q;
41 if ( version == 1 )
42 matches = search_kmp( text2, pattern2, m, n );
44 else
45 matches = search_bm( text2, pattern2, m, n );
47 free(text2);
48 free(pattern2);
50 return matches;