Skip to content

Commit

Permalink
Merge pull request #21 from PhoenixRion/export-updates
Browse files Browse the repository at this point in the history
Add count function
  • Loading branch information
goller authored Oct 30, 2019
2 parents 5d87a75 + b6d5054 commit 3fbb3e3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tdigest.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ func (t *TDigest) Centroids() CentroidList {
return cl
}

func (t *TDigest) Count() float64 {
t.process()
count := 0.0
for _, centroid := range t.processed {
count += centroid.Weight
}
return count
}

func (t *TDigest) updateCumulative() {
if n := t.processed.Len() + 1; n <= cap(t.cumulative) {
t.cumulative = t.cumulative[:n]
Expand Down
37 changes: 37 additions & 0 deletions tdigest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,43 @@ func init() {
}
}


func TestTdigest_Count(t *testing.T) {
tests := []struct {
name string
data []float64
digest *tdigest.TDigest
want float64
}{
{
name: "empty",
data: []float64{},
want: 0,
},
{
name: "not empty",
data: []float64{5, 4},
want: 2,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
td := tt.digest
if td == nil {
td = tdigest.NewWithCompression(1000)
for _, x := range tt.data {
td.Add(x, 1)
}
}
got := td.Count()
if got != tt.want {
t.Errorf("unexpected count, got %g want %g", got, tt.want)
}
})
}
}

func TestTdigest_Quantile(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 3fbb3e3

Please sign in to comment.