Skip to content

Commit

Permalink
Merge pull request #51 from erizocosmico/feature/no-reflection-sort
Browse files Browse the repository at this point in the history
remove reflection-based slice sort
  • Loading branch information
smola committed Jun 26, 2017
2 parents 804bf53 + c28f6fa commit 7810094
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion classifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c *classifier) Classify(content []byte, candidates map[string]float64) []s
}

func sortLanguagesByScore(scoredLangs []*scoredLanguage) []string {
sort.SliceStable(scoredLangs, func(i, j int) bool { return scoredLangs[j].score < scoredLangs[i].score })
sort.Stable(byScore(scoredLangs))
sortedLanguages := make([]string, 0, len(scoredLangs))
for _, scoredLang := range scoredLangs {
sortedLanguages = append(sortedLanguages, scoredLang.language)
Expand Down Expand Up @@ -94,3 +94,9 @@ func (c *classifier) tokenProbability(token, language string) float64 {

return tokenProb
}

type byScore []*scoredLanguage

func (b byScore) Len() int { return len(b) }
func (b byScore) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byScore) Less(i, j int) bool { return b[j].score < b[i].score }

0 comments on commit 7810094

Please sign in to comment.