Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions internal/strdist/strdist.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,28 @@ func StandardCost(ar, br rune) Cost {
return Cost{SwapAB: 1, DeleteA: 1, InsertB: 1}
}

// Distance returns the edit distance between two strings. The cost per edit is
// given by the costFunc argument.
//
// There is an optional cut argument that when set will finish the computation
// as early as possible once the final cost is certain to be >= cut. There is
// no guarantee about the exact cost returned when this is the case other than
// being >= cut. In particular, when cut is used, the function is not symmetric
// on a and b.
func Distance(a, b string, f CostFunc, cut int64) int64 {
if a == b {
return 0
}
lst := make([]CostInt, len(b)+1)
bl := 0
for bi, br := range b {
bl++
for _, br := range b {
cost := f(-1, br)
if cost.InsertB == Inhibit || lst[bi] == Inhibit {
lst[bi+1] = Inhibit
if cost.InsertB == Inhibit || lst[bl] == Inhibit {
lst[bl+1] = Inhibit
} else {
lst[bi+1] = lst[bi] + cost.InsertB
lst[bl+1] = lst[bl] + cost.InsertB
}
bl++
}
lst = lst[:bl+1]
// Not required, but caching means preventing the fast path
Expand All @@ -59,6 +67,9 @@ func Distance(a, b string, f CostFunc, cut int64) int64 {
lst[0] = last + cost.DeleteA
}
stop := true
if lst[0] < CostInt(cut) {
stop = false
}
i := 0
for _, br := range b {
i++
Expand Down Expand Up @@ -87,7 +98,6 @@ func Distance(a, b string, f CostFunc, cut int64) int64 {
if debug {
debugf("... %v", lst)
}
_ = stop
if cut != 0 && stop {
break
}
Expand Down
7 changes: 7 additions & 0 deletions internal/strdist/strdist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var distanceTests = []distanceTest{
{f: uniqueCost, r: 6, a: "abc", b: "b"},
{f: uniqueCost, r: 6, a: "abc", b: "c"},
{f: uniqueCost, r: 9, a: "abc", b: ""},
{f: uniqueCost, r: 6, cut: 6, a: "abc", b: ""},
{f: uniqueCost, r: 5, a: "abc", b: "abcd"},
{f: uniqueCost, r: 5, a: "abc", b: "dabc"},
{f: uniqueCost, r: 10, a: "abc", b: "adbdc"},
Expand All @@ -58,7 +59,13 @@ var distanceTests = []distanceTest{
{f: strdist.GlobCost, r: 1, a: "a**f/hij", b: "abc/def/hik"},
{f: strdist.GlobCost, r: 2, a: "a**fg", b: "abc/def/hik"},
{f: strdist.GlobCost, r: 0, a: "a**f/hij/klm", b: "abc/d**m"},
{f: strdist.GlobCost, r: 1, a: "**a", b: ""},
Comment thread
letFunny marked this conversation as resolved.
{f: strdist.GlobCost, r: 0, a: "/*a/", b: "/a/"},
{f: strdist.GlobCost, r: 3, a: "abc", b: ""},
{f: strdist.GlobCost, r: 1, cut: 1, a: "abc", b: ""},
// Not symmetric.
{f: strdist.GlobCost, r: 2, cut: 3, a: "ab", b: ""},
{f: strdist.GlobCost, r: 2, cut: 1, a: "", b: "ab"},
}

func (s *S) TestDistance(c *C) {
Expand Down
Loading