Browse Source

removed extra ptr indirection from LevenshteinDistance

Steve Yen 2 years ago
parent
commit
b6c97ddbfe
3 changed files with 16 additions and 16 deletions
  1. 9 9
      search/levenshtein.go
  2. 4 4
      search/levenshtein_test.go
  3. 3 3
      search/searcher/search_fuzzy.go

+ 9 - 9
search/levenshtein.go

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

+ 4 - 4
search/levenshtein_test.go

@@ -38,7 +38,7 @@ func TestLevenshteinDistance(t *testing.T) {
 	}
 
 	for _, test := range tests {
-		actual := LevenshteinDistance(&test.a, &test.b)
+		actual := LevenshteinDistance(test.a, test.b)
 		if actual != test.dist {
 			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 {
-		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 {
 			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"
 	for i := 0; i < b.N; i++ {
 		for _, t := range benchmarkTerms {
-			LevenshteinDistance(&a, &t)
+			LevenshteinDistance(a, t)
 		}
 	}
 }
@@ -113,7 +113,7 @@ func BenchmarkLevenshteinDistanceMax(b *testing.B) {
 	a := "water"
 	for i := 0; i < b.N; i++ {
 		for _, t := range benchmarkTerms {
-			LevenshteinDistanceMax(&a, &t, 2)
+			LevenshteinDistanceMax(a, t, 2)
 		}
 	}
 }

+ 3 - 3
search/searcher/search_fuzzy.go

@@ -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 {
 		return nil, err
 	}
@@ -76,7 +76,7 @@ func NewFuzzySearcher(indexReader index.IndexReader, term string, prefix, fuzzin
 	}, 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)
 	var fieldDict index.FieldDict
 	if len(prefixTerm) > 0 {
@@ -93,7 +93,7 @@ func findFuzzyCandidateTerms(indexReader index.IndexReader, term *string, fuzzin
 	// enumerate terms and check levenshtein distance
 	tfd, err := fieldDict.Next()
 	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 {
 			rv = append(rv, tfd.Term)
 			if tooManyClauses(len(rv)) {