Bugfix: Really fix the crash in bm.c, do memory checking after each allocation, corre...
[2dmatching.git] / bird2.c
blob9e1ffec1c7906dbb95c56094c0f05af98543ba4d
1 #include "code.h"
3 int naive3( char **text, char **pattern, int m, int n, int textrow, int textcolumn, int patternrow )
5 unsigned int i, j, k;
7 for ( i = textcolumn; i < n - m + 1; i++ ) {
9 k = i;
11 for ( j = 0; j < m; j++ ) {
13 if ( pattern[patternrow][j] == text[textrow][k] )
14 k++;
15 else
16 break;
18 if ( j == m )
19 return k-m;
20 else
21 j = 0;
23 /* No match found on this row */
24 return -1;
27 int naive3b( char **text, char **pattern, int m, int n, int textrow, int textcolumn, int patternrow )
29 unsigned int j;
31 for ( j = 0; j < m; j++ ) {
33 if ( pattern[patternrow][j] == text[textrow][textcolumn] )
34 textcolumn++;
35 else
36 return -1;
38 /* If reached through here, report success */
39 return 0;
42 unsigned int bird2( char **text, char **pattern, int m, int n )
44 /* x and y can be -1 */
45 int x;
47 unsigned int i, row, matches = 0;
49 /* Iterate rows */
50 for ( row = 0; row < n - m + 1; row++ ) {
52 x = 0;
54 /* Iterate through results on the same row */
55 while ( x != -1 ) {
57 x = naive3 ( text, pattern, m, n, row, x, 0 );
59 /* If a row is found, check the m - 1 rows underneath it for matches */
60 if ( x != -1 ) {
62 for ( i = 1; i < m; i++) {
63 /* Optimization: instead of checking against n chars on
64 subsequent rows, just check against the next m chars after x without sliding the window */
65 if ( naive3b ( text, pattern, m , n, row + i, x, i ) == -1 )
66 break;
69 if ( i == m )
70 matches++;
71 /* After each attempt,increase the counter +1 */
72 x++;
76 return matches;