Refactor sorting types to only implement Len and Swap once (#43)

Using "trick" similar to `sort.Slice` that's comming in `go1.8`.
This commit is contained in:
Karol Woźniak authored and gokcehan committed 2016-11-10 01:01:30 +03:00
1 parent 9962827926
commit a4c395a4de
2 files changed
+59 -72

No files matched your search

+17
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"log"
"path/filepath"
"sort"
"strconv"
"strings"
"unicode"
@@ -157,6 +158,22 @@ func max(a, b int) int {
return b
}
type filesSortable struct {
files []*File
less func(i, j int) bool
}
func (f filesSortable) Len() int { return len(f.files) }
func (f filesSortable) Swap(i, j int) { f.files[i], f.files[j] = f.files[j], f.files[i] }
func (f filesSortable) Less(i, j int) bool { return f.less(i, j) }
// TODO: Replace with `sort.SliceStable` once available
func sortFilesStable(files []*File, less func(i, j int) bool) {
sort.Stable(filesSortable{files: files, less: less})
}
// We don't need no generic code
// We don't need no thought control
// No dark templates in compiler