Browse code

Work with row pointers

John Hawthorn authored on 28/12/2019 01:47:32
Showing 1 changed files

... ...
@@ -108,6 +108,9 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi
108 108
 	score_t match_bonus[MATCH_MAX_LEN];
109 109
 	score_t D[n][m], M[n][m];
110 110
 
111
+	score_t *last_D, *last_M;
112
+	score_t *curr_D, *curr_M;
113
+
111 114
 	/*
112 115
 	 * D[][] Stores the best score for this position ending with a match.
113 116
 	 * M[][] Stores the best possible score at this position.
... ...
@@ -118,6 +121,9 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi
118 121
 		score_t prev_score = SCORE_MIN;
119 122
 		score_t gap_score = i == n - 1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER;
120 123
 
124
+		curr_D = &D[i][0];
125
+		curr_M = &M[i][0];
126
+
121 127
 		for (int j = 0; j < m; j++) {
122 128
 			if (lower_needle[i] == lower_haystack[j]) {
123 129
 				score_t score = SCORE_MIN;
... ...
@@ -125,18 +131,21 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi
125 131
 					score = (j * SCORE_GAP_LEADING) + match_bonus[j];
126 132
 				} else if (j) { /* i > 0 && j > 0*/
127 133
 					score = max(
128
-					    M[i - 1][j - 1] + match_bonus[j],
134
+					    last_M[j - 1] + match_bonus[j],
129 135
 
130 136
 					    /* consecutive match, doesn't stack with match_bonus */
131
-					    D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE);
137
+					    last_D[j - 1] + SCORE_MATCH_CONSECUTIVE);
132 138
 				}
133
-				D[i][j] = score;
134
-				M[i][j] = prev_score = max(score, prev_score + gap_score);
139
+				curr_D[j] = score;
140
+				curr_M[j] = prev_score = max(score, prev_score + gap_score);
135 141
 			} else {
136
-				D[i][j] = SCORE_MIN;
137
-				M[i][j] = prev_score = prev_score + gap_score;
142
+				curr_D[j] = SCORE_MIN;
143
+				curr_M[j] = prev_score = prev_score + gap_score;
138 144
 			}
139 145
 		}
146
+
147
+		last_D = curr_D;
148
+		last_M = curr_M;
140 149
 	}
141 150
 
142 151
 #ifdef DEBUG_VERBOSE