Browse code

Change match into if/else

This is marginally faster and I still think it reads very well (maybe
better).

John Hawthorn authored on 14/06/2016 06:55:58
Showing 1 changed files

... ...
@@ -117,9 +117,10 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi
117 117
 	for (int i = 0; i < n; i++) {
118 118
 		score_t prev_score = SCORE_MIN;
119 119
 		score_t gap_score = i == n - 1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER;
120
+
120 121
 		for (int j = 0; j < m; j++) {
121
-			score_t score = SCORE_MIN;
122 122
 			if (tolower(needle[i]) == tolower(haystack[j])) {
123
+				score_t score = SCORE_MIN;
123 124
 				if (!i) {
124 125
 					score = (j * SCORE_GAP_LEADING) + match_bonus[j];
125 126
 				} else if (j) { /* i > 0 && j > 0*/
... ...
@@ -129,9 +130,12 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi
129 130
 					    /* consecutive match, doesn't stack with match_bonus */
130 131
 					    D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE);
131 132
 				}
133
+				D[i][j] = score;
134
+				M[i][j] = prev_score = max(score, prev_score + gap_score);
135
+			} else {
136
+				D[i][j] = SCORE_MIN;
137
+				M[i][j] = prev_score = prev_score + gap_score;
132 138
 			}
133
-			D[i][j] = score;
134
-			M[i][j] = prev_score = max(score, prev_score + gap_score);
135 139
 		}
136 140
 	}
137 141