-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.go
47 lines (36 loc) · 975 Bytes
/
sample.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
// Copyright (c) 2020-2021 C4 Project
//
// This file is part of c4t.
// Licenced under the MIT licence; see `LICENSE`.
package corpus
import (
"math/rand"
"sort"
)
// Sample tries to select a sample of size want from this corpus.
// If want is non-positive, or the corpus is smaller than want, no sampling occurs.
func (c Corpus) Sample(rng *rand.Rand, want int) (Corpus, error) {
got := len(c)
if got == 0 {
return nil, ErrNone
}
if want <= 0 || got <= want {
return c, nil
}
return c.actuallySample(rng, want), nil
}
func (c Corpus) actuallySample(r *rand.Rand, want int) Corpus {
sample := make(Corpus, want)
names := c.Names()
for _, j := range c.sampleIndices(r, want) {
n := names[j]
sample[n] = c[n]
}
return sample
}
// sampleIndices produces a random sorted list of n indices into the corpus's name list.
func (c Corpus) sampleIndices(r *rand.Rand, n int) []int {
indices := r.Perm(len(c))[:n]
sort.Ints(indices)
return indices
}