Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func (f *Fast) Quantile(phi float64) float64 {
func (f *Fast) Quantiles(dst, phis []float64) []float64 {
f.tmp = append(f.tmp[:0], f.a...)
sort.Float64s(f.tmp)
return f.quantiles(dst, phis)
}

func (f *Fast) quantiles(dst, phis []float64) []float64 {
for _, phi := range phis {
q := f.quantile(phi)
dst = append(dst, q)
Expand Down Expand Up @@ -129,3 +133,37 @@ func PutFast(f *Fast) {
}

var fastPool sync.Pool

func Quantile(fs []*Fast, phi float64) float64 {
t := combine(fs)
return t.quantile(phi)
}

func Quantiles(fs []*Fast, dst, phis []float64) []float64 {
t := combine(fs)
return t.quantiles(dst, phis)
}

func combine(fs []*Fast) Fast {
n := 0
for _, f := range fs {
n += len(f.a)
}

var t Fast
t.Reset()
t.tmp = make([]float64, 0, n)

for _, f := range fs {
t.tmp = append(t.tmp, f.a...)
if t.max < f.max {
t.max = f.max
}
if t.min > f.min {
t.min = f.min
}
}
sort.Float64s(t.tmp)

return t
}
27 changes: 27 additions & 0 deletions histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,30 @@ func TestFastRepeatableResults(t *testing.T) {
}
}
}

func TestCombine(t *testing.T) {
f1 := GetFast()
defer PutFast(f1)

f2 := GetFast()
defer PutFast(f2)

for i := 0; i < 10000; i++ {
f1.Update(float64(i))
}
for i := 10000; i < 20000; i++ {
f2.Update(float64(i))
}

q50 := Quantile([]*Fast{f1, f2}, 0.5)
if q50 < 9000 || q50 > 11000 {
t.Fatal(q50)
}
qs := Quantiles([]*Fast{f1, f2}, nil, []float64{0, 0.5, 1})
if len(qs) != 3 {
t.Fatal(len(qs))
}
if qs[0] != 0 || qs[1] != q50 || qs[2] != 19999 {
t.Fatal(qs)
}
}