mirror of
https://github.com/vlang/v.git
synced 2025-08-04 02:07:28 -04:00
strings: use int
instead of u16
in strings.levenshtein_distance; it is ~ same performance, but has less constraints
This commit is contained in:
parent
e8ee207434
commit
78ed1f703a
@ -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]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user