Skip to content

Commit 640853f

Browse files
committed
Fix: Initializing affine DP matrices
1 parent 8c15447 commit 640853f

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

include/stringcuzilla/similarity.hpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,16 @@ struct tile_scorer<first_iterator_type_, second_iterator_type_, score_type_, sub
510510
* @note Should only be called for the top row and left column of the matrix.
511511
*/
512512
void init_score(score_t &cell, sz_size_t diagonal_index) const noexcept {
513-
cell = gap_costs_.open + gap_costs_.extend * (diagonal_index - 1);
513+
cell = diagonal_index ? gap_costs_.open + gap_costs_.extend * (diagonal_index - 1) : 0;
514514
}
515515

516516
void init_gap(score_t &cell, sz_size_t diagonal_index) const noexcept {
517517
// Make sure the initial value of the gap is not smaller in magnitude than the primary.
518518
// The supplementary matrices are initialized with values of higher magnitude,
519519
// which is equivalent to discarding them. That's better than using `SIZE_MAX`
520520
// as subsequent additions won't overflow.
521-
cell = gap_costs_.open * 2 + gap_costs_.extend * diagonal_index;
521+
cell = gap_costs_.open + gap_costs_.extend +
522+
(diagonal_index ? gap_costs_.open + gap_costs_.extend * (diagonal_index - 1) : 0);
522523
}
523524

524525
/**
@@ -850,14 +851,12 @@ struct diagonal_walker<char_type_, score_type_, substituter_type_, linear_gap_co
850851

851852
// Perform a circular rotation of those buffers, to reuse the memory, this time, with a shift,
852853
// dropping the first element in the current array.
853-
score_t *temporary = previous_scores;
854+
rotate_three(previous_scores, current_scores, next_scores);
854855

855856
// ! Drop the first entry among the current scores.
856-
// ! Assuming every next diagonal is shorter by one element, we don't need a full-blown `sz_move`.
857-
// ! to shift the array by one element.
858-
previous_scores = current_scores + 1;
859-
current_scores = next_scores;
860-
next_scores = temporary;
857+
// ! Assuming every next diagonal is shorter by one element,
858+
// ! we don't need a full-blown `sz_move` to shift the array by one element.
859+
previous_scores++;
861860
}
862861

863862
// Export the scalar before `free` call.
@@ -1061,22 +1060,14 @@ struct diagonal_walker<char_type_, score_type_, substituter_type_, affine_gap_co
10611060

10621061
// Perform a circular rotation of those buffers, to reuse the memory, this time, with a shift,
10631062
// dropping the first element in the current array.
1064-
score_t *temporary = previous_scores;
1063+
rotate_three(previous_scores, current_scores, next_scores);
1064+
std::swap(current_inserts, next_inserts);
1065+
std::swap(current_deletes, next_deletes);
10651066

10661067
// ! Drop the first entry among the current scores.
1067-
// ! Assuming every next diagonal is shorter by one element, we don't need a full-blown `sz_move`.
1068-
// ! to shift the array by one element.
1069-
previous_scores = current_scores + 1;
1070-
current_scores = next_scores;
1071-
next_scores = temporary;
1072-
1073-
// ! Drop the first entry among the current insertions and deletions.
1074-
temporary = current_inserts;
1075-
current_inserts = next_inserts + 0;
1076-
next_inserts = temporary;
1077-
temporary = current_deletes;
1078-
current_deletes = next_deletes + 0;
1079-
next_deletes = temporary;
1068+
// ! Assuming every next diagonal is shorter by one element,
1069+
// ! we don't need a full-blown `sz_move` to shift the array by one element.
1070+
previous_scores++;
10801071
}
10811072

10821073
// Export the scalar before `free` call.

0 commit comments

Comments
 (0)