-
Notifications
You must be signed in to change notification settings - Fork 6
/
sorting.go
47 lines (39 loc) · 1.01 KB
/
sorting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package physics
import (
"sort"
"github.com/egonelbre/exp/bit"
)
type sorterZ struct {
order []int
get func(i int) int
}
func (s *sorterZ) Len() int { return len(s.order) }
func (s *sorterZ) Swap(i, j int) { s.order[i], s.order[j] = s.order[j], s.order[i] }
func (s *sorterZ) Less(i, j int) bool {
vi := int64(s.get(s.order[i]))
vj := int64(s.get(s.order[j]))
return bit.ZEncode(vi) < bit.ZEncode(vj)
}
func SortByZ(items []int, get func(i int) int) {
sort.Sort(&sorterZ{
order: items,
get: get,
})
}
type sorterZCount struct {
order []int
get func(i int) int
}
func (s *sorterZCount) Len() int { return len(s.order) }
func (s *sorterZCount) Swap(i, j int) { s.order[i], s.order[j] = s.order[j], s.order[i] }
func (s *sorterZCount) Less(i, j int) bool {
vi := int64(s.get(s.order[i]))
vj := int64(s.get(s.order[j]))
return bit.Count(bit.ZEncode(vi)) < bit.Count(bit.ZEncode(vj))
}
func SortByZCount(items []int, get func(i int) int) {
sort.Sort(&sorterZCount{
order: items,
get: get,
})
}