Bugfix: Really fix the crash in bm.c, do memory checking after each allocation, corre...
[2dmatching.git] / bird.c
blob0d0433cecfa62b0438f3aff5af8eb38a2a1dea79
1 #include "code.h"
3 unsigned int bird( char **text, char **pattern, int m, int n )
4 {
5 /* x and y can be -1 */
6 int x, y;
8 unsigned int i, row, matches = 0;
10 /* Iterate rows */
11 for ( row = 0; row < n - m + 1; row++ ) {
13 x = 0;
15 /* Iterate through results on the same row */
16 while ( x != -1 ) {
18 x = aho( text, pattern, m, n, row, x, 0, n - m + 1 );
20 /* If a row is found, check the m - 1 rows underneath it for matches */
21 if ( x != -1 ) {
23 for ( i = 1; i < m; i++) {
24 /* Optimization: instead of checking against n chars on
25 subsequent rows, just check against the next m chars after x */
26 y = aho( text, pattern, m, n, row + i, x, i, x + m );
28 if ( y != x )
29 break;
31 /* If we reached to the final line above x without breaking, we have a match */
32 else if ( i == m -1)
33 matches++;
35 /* After each attempt,increase the counter + 1 */
36 x++;
40 return matches;