-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.go
48 lines (43 loc) · 1.08 KB
/
util.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
48
package lshensemble
import (
"encoding/binary"
"sort"
)
type hashKeyFunc func([]uint64) string
func hashKeyFuncGen(hashValueSize int) hashKeyFunc {
return func(sig []uint64) string {
s := make([]byte, hashValueSize*len(sig))
buf := make([]byte, 8)
for i, v := range sig {
binary.LittleEndian.PutUint64(buf, v)
copy(s[i*hashValueSize:(i+1)*hashValueSize], buf[:hashValueSize])
}
return string(s)
}
}
type sizeCount struct {
size int
count int
}
func computeSizeDistribution(domains <-chan *DomainRecord) (sizes, counts []int) {
m := make(map[int]int)
for d := range domains {
if _, exists := m[d.Size]; !exists {
m[d.Size] = 0
}
m[d.Size]++
}
sizeCounts := make([]sizeCount, 0, len(m))
for size := range m {
sizeCounts = append(sizeCounts, sizeCount{size, m[size]})
}
sort.Slice(sizeCounts, func(i, j int) bool {
return sizeCounts[i].size < sizeCounts[j].size
})
sizes, counts = make([]int, len(sizeCounts)), make([]int, len(sizeCounts))
for i := range sizeCounts {
sizes[i] = sizeCounts[i].size
counts[i] = sizeCounts[i].count
}
return sizes, counts
}