0
0
Fork 0

removed extra ptr indirection from LevenshteinDistance

This commit is contained in:
Steve Yen 2016-10-11 08:49:10 -07:00
parent de0c26718d
commit b6c97ddbfe
3 changed files with 16 additions and 16 deletions

View File

@ -18,9 +18,9 @@ import (
"math" "math"
) )
func LevenshteinDistance(a, b *string) int { func LevenshteinDistance(a, b string) int {
la := len(*a) la := len(a)
lb := len(*b) lb := len(b)
d := make([]int, la+1) d := make([]int, la+1)
var lastdiag, olddiag, temp int var lastdiag, olddiag, temp int
@ -36,7 +36,7 @@ func LevenshteinDistance(a, b *string) int {
if (d[j-1] + 1) < min { if (d[j-1] + 1) < min {
min = d[j-1] + 1 min = d[j-1] + 1
} }
if (*a)[j-1] == (*b)[i-1] { if a[j-1] == b[i-1] {
temp = 0 temp = 0
} else { } else {
temp = 1 temp = 1
@ -51,14 +51,14 @@ func LevenshteinDistance(a, b *string) int {
return d[la] return d[la]
} }
// LevenshteinDistanceMax same as levenshteinDistance but // LevenshteinDistanceMax same as LevenshteinDistance but
// attempts to bail early once we know the distance // attempts to bail early once we know the distance
// will be greater than max // will be greater than max
// in which case the first return val will be the max // in which case the first return val will be the max
// and the second will be true, indicating max was exceeded // and the second will be true, indicating max was exceeded
func LevenshteinDistanceMax(a, b *string, max int) (int, bool) { func LevenshteinDistanceMax(a, b string, max int) (int, bool) {
la := len(*a) la := len(a)
lb := len(*b) lb := len(b)
ld := int(math.Abs(float64(la - lb))) ld := int(math.Abs(float64(la - lb)))
if ld > max { if ld > max {
@ -81,7 +81,7 @@ func LevenshteinDistanceMax(a, b *string, max int) (int, bool) {
if (d[j-1] + 1) < min { if (d[j-1] + 1) < min {
min = d[j-1] + 1 min = d[j-1] + 1
} }
if (*a)[j-1] == (*b)[i-1] { if a[j-1] == b[i-1] {
temp = 0 temp = 0
} else { } else {
temp = 1 temp = 1

View File

@ -38,7 +38,7 @@ func TestLevenshteinDistance(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
actual := LevenshteinDistance(&test.a, &test.b) actual := LevenshteinDistance(test.a, test.b)
if actual != test.dist { if actual != test.dist {
t.Errorf("expected %d, got %d for %s and %s", test.dist, actual, test.a, test.b) t.Errorf("expected %d, got %d for %s and %s", test.dist, actual, test.a, test.b)
} }
@ -78,7 +78,7 @@ func TestLevenshteinDistanceMax(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
actual, exceeded := LevenshteinDistanceMax(&test.a, &test.b, test.max) actual, exceeded := LevenshteinDistanceMax(test.a, test.b, test.max)
if actual != test.dist || exceeded != test.exceeded { if actual != test.dist || exceeded != test.exceeded {
t.Errorf("expected %d %t, got %d %t for %s and %s", test.dist, test.exceeded, actual, exceeded, test.a, test.b) t.Errorf("expected %d %t, got %d %t for %s and %s", test.dist, test.exceeded, actual, exceeded, test.a, test.b)
} }
@ -104,7 +104,7 @@ func BenchmarkLevenshteinDistance(b *testing.B) {
a := "water" a := "water"
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for _, t := range benchmarkTerms { for _, t := range benchmarkTerms {
LevenshteinDistance(&a, &t) LevenshteinDistance(a, t)
} }
} }
} }
@ -113,7 +113,7 @@ func BenchmarkLevenshteinDistanceMax(b *testing.B) {
a := "water" a := "water"
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for _, t := range benchmarkTerms { for _, t := range benchmarkTerms {
LevenshteinDistanceMax(&a, &t, 2) LevenshteinDistanceMax(a, t, 2)
} }
} }
} }

View File

@ -37,7 +37,7 @@ func NewFuzzySearcher(indexReader index.IndexReader, term string, prefix, fuzzin
} }
} }
candidateTerms, err := findFuzzyCandidateTerms(indexReader, &term, fuzziness, field, prefixTerm) candidateTerms, err := findFuzzyCandidateTerms(indexReader, term, fuzziness, field, prefixTerm)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -76,7 +76,7 @@ func NewFuzzySearcher(indexReader index.IndexReader, term string, prefix, fuzzin
}, nil }, nil
} }
func findFuzzyCandidateTerms(indexReader index.IndexReader, term *string, fuzziness int, field, prefixTerm string) (rv []string, err error) { func findFuzzyCandidateTerms(indexReader index.IndexReader, term string, fuzziness int, field, prefixTerm string) (rv []string, err error) {
rv = make([]string, 0) rv = make([]string, 0)
var fieldDict index.FieldDict var fieldDict index.FieldDict
if len(prefixTerm) > 0 { if len(prefixTerm) > 0 {
@ -93,7 +93,7 @@ func findFuzzyCandidateTerms(indexReader index.IndexReader, term *string, fuzzin
// enumerate terms and check levenshtein distance // enumerate terms and check levenshtein distance
tfd, err := fieldDict.Next() tfd, err := fieldDict.Next()
for err == nil && tfd != nil { for err == nil && tfd != nil {
ld, exceeded := search.LevenshteinDistanceMax(term, &tfd.Term, fuzziness) ld, exceeded := search.LevenshteinDistanceMax(term, tfd.Term, fuzziness)
if !exceeded && ld <= fuzziness { if !exceeded && ld <= fuzziness {
rv = append(rv, tfd.Term) rv = append(rv, tfd.Term)
if tooManyClauses(len(rv)) { if tooManyClauses(len(rv)) {