Added the Baker and Bird variant. Reset code.h to the default text size
[2dmatching.git] / bird2.c
blob78ab5a4de747aa02ecdf60f0e3bae46861b85185
1 #include "code.h"
3 int naive3( int textrow, int textcolumn, int patternrow, int patterncolumn )
5 unsigned int i, j, k;
7 for(i = textcolumn; i < patterncolumn; i++) {
9 k = i;
11 for(j = 0; j < m; j++) {
13 if( pattern[patternrow][j] == text[textrow][k])
14 k++;
16 else
17 break;
20 if (j == m)
21 return k-m;
23 else
24 j = 0;
27 /* No match found on this row */
28 return -1;
31 void bird2()
33 int steps = 0;
35 /* x and y can be -1 */
36 int x, y;
38 unsigned int i, row, matches = 0;
40 /* Iterate rows */
41 for ( row = 0; row < n - m + 1; row++ ) {
43 x = 0;
45 /* Iterate through results on the same row */
46 while ( x != -1 ) {
48 x = naive3( row, x, 0, n - m );
50 steps++;
52 /* If a row is found, check the m - 1 rows underneath it for matches */
53 if ( x != -1 ) {
55 for ( i = 1; i < m; i++) {
56 /* Optimization: instead of checking against n chars on
57 subsequent rows, just check against the next m chars after x */
58 y = naive3( row + i, x, i, x + m );
60 steps++;
62 if ( y != x )
63 break;
65 /* If we reached to the final line above x without breaking, we have a match */
66 else if ( i == m -1)
67 matches++;
69 /* After each attempt,increase the counter + 1 */
70 x++;
74 printf("Bird2: %i\n",matches);