Add connection pool implementation (part 3)

Signed-off-by: Pavel Korotkov <pavel@nspcc.ru>
This commit is contained in:
Pavel Korotkov 2021-04-05 20:13:49 +03:00 committed by Pavel Korotkov
parent a44551d42b
commit f7007f2085

View file

@ -23,15 +23,15 @@ func (wl *workList) pop() int {
} }
func NewSampler(probabilities []float64, source rand.Source) *Sampler { func NewSampler(probabilities []float64, source rand.Source) *Sampler {
generator := &Sampler{} sampler := &Sampler{}
var ( var (
small workList small workList
large workList large workList
) )
n := len(probabilities) n := len(probabilities)
generator.randomGenerator = rand.New(source) sampler.randomGenerator = rand.New(source)
generator.probabilities = make([]float64, n) sampler.probabilities = make([]float64, n)
generator.alias = make([]int, n) sampler.alias = make([]int, n)
// Compute scaled probabilities. // Compute scaled probabilities.
p := make([]float64, n) p := make([]float64, n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
@ -46,8 +46,8 @@ func NewSampler(probabilities []float64, source rand.Source) *Sampler {
} }
for len(large) > 0 && len(small) > 0 { for len(large) > 0 && len(small) > 0 {
l, g := small.pop(), large.pop() l, g := small.pop(), large.pop()
generator.probabilities[l] = p[l] sampler.probabilities[l] = p[l]
generator.alias[l] = g sampler.alias[l] = g
p[g] = (p[g] + p[l]) - 1 p[g] = (p[g] + p[l]) - 1
if p[g] < 1 { if p[g] < 1 {
small.push(g) small.push(g)
@ -57,13 +57,13 @@ func NewSampler(probabilities []float64, source rand.Source) *Sampler {
} }
for len(large) > 0 { for len(large) > 0 {
g := large.pop() g := large.pop()
generator.probabilities[g] = 1 sampler.probabilities[g] = 1
} }
for len(small) > 0 { for len(small) > 0 {
l := small.pop() l := small.pop()
generator.probabilities[l] = 1 sampler.probabilities[l] = 1
} }
return generator return sampler
} }
func (g *Sampler) Next() int { func (g *Sampler) Next() int {