Merge pull request #51 from erizocosmico/feature/no-reflection-sort

remove reflection-based slice sort
This commit is contained in:
Santiago M. Mola 2017-06-26 08:36:43 -07:00 committed by GitHub
commit 78100948c4

View File

@ -59,7 +59,7 @@ func (c *classifier) Classify(content []byte, candidates map[string]float64) []s
} }
func sortLanguagesByScore(scoredLangs []*scoredLanguage) []string { 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)) sortedLanguages := make([]string, 0, len(scoredLangs))
for _, scoredLang := range scoredLangs { for _, scoredLang := range scoredLangs {
sortedLanguages = append(sortedLanguages, scoredLang.language) sortedLanguages = append(sortedLanguages, scoredLang.language)
@ -94,3 +94,9 @@ func (c *classifier) tokenProbability(token, language string) float64 {
return tokenProb 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 }