-
Notifications
You must be signed in to change notification settings - Fork 659
/
weighted_uniform.go
69 lines (58 loc) · 1.65 KB
/
weighted_uniform.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package sampler
import (
"errors"
"math"
safemath "github.com/ava-labs/avalanchego/utils/math"
)
var (
errWeightsTooLarge = errors.New("total weight is too large")
_ Weighted = (*weightedUniform)(nil)
)
// Sampling is performed by indexing into the array to find the correct index.
//
// Initialization takes O(Sum(weights)) time. This results in an exponential
// initialization time. Therefore, the time to execute this operation can be
// extremely long. Initialization takes O(Sum(weights)) space, causing this
// algorithm to be unable to handle large inputs.
//
// Sampling is performed in O(1) time. However, if the Sum(weights) is large,
// this operation can still be relatively slow due to poor cache locality.
type weightedUniform struct {
indices []int
maxWeight uint64
}
func (s *weightedUniform) Initialize(weights []uint64) error {
totalWeight := uint64(0)
for _, weight := range weights {
newWeight, err := safemath.Add64(totalWeight, weight)
if err != nil {
return err
}
totalWeight = newWeight
}
if totalWeight > s.maxWeight || totalWeight > math.MaxInt32 {
return errWeightsTooLarge
}
size := int(totalWeight)
if size > cap(s.indices) {
s.indices = make([]int, size)
} else {
s.indices = s.indices[:size]
}
offset := 0
for i, weight := range weights {
for j := uint64(0); j < weight; j++ {
s.indices[offset] = i
offset++
}
}
return nil
}
func (s *weightedUniform) Sample(value uint64) (int, error) {
if uint64(len(s.indices)) <= value {
return 0, ErrOutOfRange
}
return s.indices[int(value)], nil
}