-
Notifications
You must be signed in to change notification settings - Fork 1
/
key_list.go
49 lines (44 loc) · 1.23 KB
/
key_list.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
49
package keys
import (
"fmt"
"math/rand"
"golang.org/x/tools/container/intsets"
)
type KeyList interface {
Name() string
Desc() string
Keys() []string
Count() int
}
func NewKeyList(name string, desc string, keys []string) KeyList {
return &MemoryKeyList{name, desc, keys}
}
func SamplingKeyList(origList KeyList, sampleSize int) (KeyList, error) {
if sampleSize > origList.Count() {
return nil, fmt.Errorf("sample size %d must be <= original list count %d", sampleSize, origList.Count())
}
if sampleSize == origList.Count() {
return origList, nil
}
var origIndices intsets.Sparse
for origIndices.Len() < sampleSize {
origIndex := rand.Intn(origList.Count())
origIndices.Insert(origIndex)
}
origKeys := origList.Keys()
sampleKeys := make([]string, sampleSize)
for sampleIndex := 0; sampleIndex < sampleSize; sampleIndex++ {
var origIndex int
if origIndices.TakeMin(&origIndex) {
sampleKeys[sampleIndex] = origKeys[origIndex]
} else {
// should never happen
return nil, fmt.Errorf("unable to take key for sample index %d", sampleIndex)
}
}
return NewKeyList(
fmt.Sprintf("%d from %v", sampleSize, origList.Name()),
fmt.Sprintf("%d keys sampled from %v", sampleSize, origList.Desc()),
sampleKeys,
), nil
}