Skip to content

Commit 265f3af

Browse files
committed
matchfinder: penalize score for overlapping matches
1 parent a8d524a commit 265f3af

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

matchfinder/m4.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,21 @@ func (q *M4) FindMatches(dst []Match, src []byte) []Match {
167167
}
168168
}
169169

170-
if q.score(currentMatch) <= q.score(matches[0]) {
170+
if currentMatch.End-currentMatch.Start < q.MinLength {
171+
continue
172+
}
173+
174+
overlapPenalty := 0
175+
if matches[0] != (absoluteMatch{}) {
176+
overlapPenalty = 275
177+
if currentMatch.Start <= matches[1].End {
178+
// This match would completely replace the previous match,
179+
// so there is no penalty for overlap.
180+
overlapPenalty = 0
181+
}
182+
}
183+
184+
if q.score(currentMatch) <= q.score(matches[0])+overlapPenalty {
171185
continue
172186
}
173187

matchfinder/multihash.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,21 @@ func (q *MultiHash) FindMatches(dst []Match, src []byte) []Match {
172172
}
173173
}
174174

175-
if currentMatch == (absoluteMatch{}) || q.score(currentMatch) <= q.score(matches[0]) {
175+
if currentMatch.End-currentMatch.Start < q.MinLength {
176+
continue
177+
}
178+
179+
overlapPenalty := 0
180+
if matches[0] != (absoluteMatch{}) {
181+
overlapPenalty = 275
182+
if currentMatch.Start <= matches[1].End {
183+
// This match would completely replace the previous match,
184+
// so there is no penalty for overlap.
185+
overlapPenalty = 0
186+
}
187+
}
188+
189+
if q.score(currentMatch) <= q.score(matches[0])+overlapPenalty {
176190
continue
177191
}
178192

0 commit comments

Comments
 (0)