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:
1 parent
9962827926
commit
a4c395a4de
2 files changed
+59
-72
No files matched your search
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
@@ -157,6 +158,22 @@ func max(a, b int) int {
|
|||||||
return b
|
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 generic code
|
||||||
// We don't need no thought control
|
// We don't need no thought control
|
||||||
// No dark templates in compiler
|
// No dark templates in compiler
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,72 +24,6 @@ type File struct {
|
|||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ByName []*File
|
|
||||||
|
|
||||||
func (a ByName) Len() int { return len(a) }
|
|
||||||
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
|
|
||||||
func (a ByName) Less(i, j int) bool {
|
|
||||||
return strings.ToLower(a[i].Name()) < strings.ToLower(a[j].Name())
|
|
||||||
}
|
|
||||||
|
|
||||||
type BySize []*File
|
|
||||||
|
|
||||||
func (a BySize) Len() int { return len(a) }
|
|
||||||
func (a BySize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
func (a BySize) Less(i, j int) bool { return a[i].Size() < a[j].Size() }
|
|
||||||
|
|
||||||
type ByTime []*File
|
|
||||||
|
|
||||||
func (a ByTime) Len() int { return len(a) }
|
|
||||||
func (a ByTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
func (a ByTime) Less(i, j int) bool { return a[i].ModTime().Before(a[j].ModTime()) }
|
|
||||||
|
|
||||||
type ByDir []*File
|
|
||||||
|
|
||||||
func (a ByDir) Len() int { return len(a) }
|
|
||||||
func (a ByDir) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
|
|
||||||
func (a ByDir) Less(i, j int) bool {
|
|
||||||
if a[i].IsDir() == a[j].IsDir() {
|
|
||||||
return i < j
|
|
||||||
}
|
|
||||||
return a[i].IsDir()
|
|
||||||
}
|
|
||||||
|
|
||||||
type ByNum []*File
|
|
||||||
|
|
||||||
func (a ByNum) Len() int { return len(a) }
|
|
||||||
func (a ByNum) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
|
|
||||||
func (a ByNum) Less(i, j int) bool {
|
|
||||||
nums1, rest1, numFirst1 := extractNums(a[i].Name())
|
|
||||||
nums2, rest2, numFirst2 := extractNums(a[j].Name())
|
|
||||||
|
|
||||||
if numFirst1 != numFirst2 {
|
|
||||||
return i < j
|
|
||||||
}
|
|
||||||
|
|
||||||
if numFirst1 {
|
|
||||||
if nums1[0] != nums2[0] {
|
|
||||||
return nums1[0] < nums2[0]
|
|
||||||
}
|
|
||||||
nums1 = nums1[1:]
|
|
||||||
nums2 = nums2[1:]
|
|
||||||
}
|
|
||||||
|
|
||||||
for k := 0; k < len(nums1) && k < len(nums2); k++ {
|
|
||||||
if rest1[k] != rest2[k] {
|
|
||||||
return i < j
|
|
||||||
}
|
|
||||||
if nums1[k] != nums2[k] {
|
|
||||||
return nums1[k] < nums2[k]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return i < j
|
|
||||||
}
|
|
||||||
|
|
||||||
func getFilesSorted(path string) []*File {
|
func getFilesSorted(path string) []*File {
|
||||||
fi, err := readdir(path)
|
fi, err := readdir(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -99,20 +32,57 @@ func getFilesSorted(path string) []*File {
|
|||||||
|
|
||||||
switch gOpts.sortby {
|
switch gOpts.sortby {
|
||||||
case "name":
|
case "name":
|
||||||
sort.Sort(ByName(fi))
|
sortFilesStable(fi, func(i, j int) bool {
|
||||||
|
return strings.EqualFold(fi[i].Name(), fi[j].Name())
|
||||||
|
})
|
||||||
case "size":
|
case "size":
|
||||||
sort.Sort(BySize(fi))
|
sortFilesStable(fi, func(i, j int) bool {
|
||||||
|
return fi[i].Size() < fi[j].Size()
|
||||||
|
})
|
||||||
case "time":
|
case "time":
|
||||||
sort.Sort(ByTime(fi))
|
sortFilesStable(fi, func(i, j int) bool {
|
||||||
|
return fi[i].ModTime().Before(fi[j].ModTime())
|
||||||
|
})
|
||||||
default:
|
default:
|
||||||
log.Printf("unknown sorting type: %s", gOpts.sortby)
|
log.Printf("unknown sorting type: %s", gOpts.sortby)
|
||||||
}
|
}
|
||||||
|
|
||||||
if gOpts.dirfirst {
|
if gOpts.dirfirst {
|
||||||
sort.Stable(ByDir(fi))
|
sortFilesStable(fi, func(i, j int) bool {
|
||||||
|
if fi[i].IsDir() == fi[j].IsDir() {
|
||||||
|
return i < j
|
||||||
|
}
|
||||||
|
return fi[i].IsDir()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
//TODO this should be optional
|
//TODO this should be optional
|
||||||
sort.Stable(ByNum(fi))
|
sortFilesStable(fi, func(i, j int) bool {
|
||||||
|
nums1, rest1, numFirst1 := extractNums(fi[i].Name())
|
||||||
|
nums2, rest2, numFirst2 := extractNums(fi[j].Name())
|
||||||
|
|
||||||
|
if numFirst1 != numFirst2 {
|
||||||
|
return i < j
|
||||||
|
}
|
||||||
|
|
||||||
|
if numFirst1 {
|
||||||
|
if nums1[0] != nums2[0] {
|
||||||
|
return nums1[0] < nums2[0]
|
||||||
|
}
|
||||||
|
nums1 = nums1[1:]
|
||||||
|
nums2 = nums2[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
for k := 0; k < len(nums1) && k < len(nums2); k++ {
|
||||||
|
if rest1[k] != rest2[k] {
|
||||||
|
return i < j
|
||||||
|
}
|
||||||
|
if nums1[k] != nums2[k] {
|
||||||
|
return nums1[k] < nums2[k]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return i < j
|
||||||
|
})
|
||||||
|
|
||||||
return fi
|
return fi
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user