Browse code

Avoid unnecessary matrix access

John Hawthorn authored on 07/09/2014 01:04:10
Showing 1 changed files

  • match.c index 1bc7998..25e08c8 100644
... ...
@@ -110,6 +110,7 @@ double calculate_score(const char *needle, const char *haystack, size_t *positio
110 110
 	}
111 111
 
112 112
 	for(int i = 0; i < n; i++){
113
+		double prev_score = SCORE_MIN;
113 114
 		for(int j = 0; j < m; j++){
114 115
 			score_t score = SCORE_MIN;
115 116
 			if(tolower(needle[i]) == tolower(haystack[j])){
... ...
@@ -125,12 +126,13 @@ double calculate_score(const char *needle, const char *haystack, size_t *positio
125 126
 			D[i][j] = score;
126 127
 			if(j){
127 128
 				if(i == n-1){
128
-					score = max(score, M[i][j-1] + SCORE_GAP_TRAILING);
129
+					score = max(score, prev_score + SCORE_GAP_TRAILING);
129 130
 				}else{
130
-					score = max(score, M[i][j-1] + SCORE_GAP_INNER);
131
+					score = max(score, prev_score + SCORE_GAP_INNER);
131 132
 				}
132 133
 			}
133 134
 			M[i][j] = score;
135
+			prev_score = score;
134 136
 		}
135 137
 	}
136 138