Skip to content

Commit

Permalink
walk vals
Browse files Browse the repository at this point in the history
  • Loading branch information
neurlang authored and Your Name committed Jun 14, 2024
1 parent 43e5338 commit 4da3075
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions walk.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,58 @@
package levenshtein

func Walk[T Number](mat []T, width uint, to func(uint, uint)) {
WalkVals(mat, width, func(prev T, this T, x uint, y uint) bool {
to(x, y)
return false
})
}

func WalkVals[T Number](mat []T, width uint, to func(prev T, this T, x uint, y uint) bool) {
pos := uint(len(mat) - 1)
x := (pos % width)
y := (pos / width)
for x > 0 && y > 0 {
here := x + width*y
diag := x - 1 + width*(y-1)
up := x + width*(y-1)
left := x - 1 + width*(y)
if diag >= 0 {
if mat[diag] < mat[left] && mat[diag] < mat[up] {
x--
y--
to(x, y)
if to(mat[here], mat[diag], x, y) {
return
}
continue
}
}
if left >= 0 && up >= 0 {
if mat[up] < mat[left] {
y--
to(x, y)
if to(mat[here], mat[up], x, y) {
return
}
continue
} else {
x--
to(x, y)
if to(mat[here], mat[left], x, y) {
return
}
continue
}
}
if left >= 0 {
x--
to(x, y)
if to(mat[here], mat[left], x, y) {
return
}
continue
}
if up >= 0 {
y--
to(x, y)
if to(mat[here], mat[up], x, y) {
return
}
continue
}
}
Expand Down

0 comments on commit 4da3075

Please sign in to comment.