strings: use int instead of u16 in strings.levenshtein_distance; it is ~ same performance, but has less constraints

This commit is contained in:
Delyan Angelov 2024-12-18 21:06:18 +02:00
parent e8ee207434
commit 78ed1f703a
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -1,7 +1,7 @@
module strings module strings
@[inline] @[inline]
fn min(a u16, b u16, c u16) u16 { fn min(a int, b int, c int) int {
mut m := a mut m := a
if b < m { if b < m {
m = b m = b
@ -49,11 +49,9 @@ pub fn levenshtein_distance(a string, b string) int {
if a == b { if a == b {
return 0 return 0
} }
mut row := []int{len: a.len + 1, init: index}
mut row := []u16{len: a.len + 1, init: u16(index)}
for i := 1; i < b.len + 1; i++ { for i := 1; i < b.len + 1; i++ {
mut prev := u16(i) mut prev := i
for j := 1; j < a.len + 1; j++ { for j := 1; j < a.len + 1; j++ {
mut current := row[j - 1] // match mut current := row[j - 1] // match
if b[i - 1] != a[j - 1] { if b[i - 1] != a[j - 1] {
@ -65,7 +63,6 @@ pub fn levenshtein_distance(a string, b string) int {
} }
row[a.len] = prev row[a.len] = prev
} }
return row[a.len] return row[a.len]
} }