@@ -15,8 +15,7 @@ import (
1515
1616type DDSketch struct {
1717 mapping.IndexMapping
18- store store.Store
19- zeroCount int32
18+ store store.Store
2019}
2120
2221func NewDDSketch (indexMapping mapping.IndexMapping , store store.Store ) * DDSketch {
@@ -71,18 +70,14 @@ func (s *DDSketch) Add(value float64) error {
7170
7271// Adds a value to the sketch with a int32 count.
7372func (s * DDSketch ) AddWithCount (value float64 , count int32 ) error {
74- if value < 0 || value > s .MaxIndexableValue () {
73+ if value < s . MinIndexableValue () || value > s .MaxIndexableValue () {
7574 return errors .New ("input value is outside the range that is tracked by the sketch" )
7675 }
7776 if count < 0 {
78- return errors .New ("The count cannot be negative. " )
77+ return errors .New ("count cannot be negative" )
7978 }
8079
81- if value > s .MinIndexableValue () {
82- s .store .AddWithCount (s .Index (value ), count )
83- } else {
84- s .zeroCount += count
85- }
80+ s .store .AddWithCount (s .Index (value ), count )
8681 return nil
8782}
8883
@@ -97,21 +92,27 @@ func (s *DDSketch) Copy() *DDSketch {
9792// Return the value at the specified quantile. Return a non-nil error if the quantile is invalid
9893// or if the sketch is empty.
9994func (s * DDSketch ) GetValueAtQuantile (quantile float64 ) (float64 , error ) {
95+ key , err := s .GetIndexAtQuantile (quantile )
96+ if err != nil {
97+ return math .NaN (), err
98+ }
99+ return s .Value (key ), nil
100+ }
101+
102+ // Return the index at the specified quantile. Return a non-nil error if the quantile is invalid
103+ // or if the sketch is empty.
104+ func (s * DDSketch ) GetIndexAtQuantile (quantile float64 ) (int , error ) {
100105 if quantile < 0 || quantile > 1 {
101- return math . NaN () , errors .New ("The quantile must be between 0 and 1. " )
106+ return 0 , errors .New ("quantile must be between 0 and 1" )
102107 }
103108
104109 count := s .GetCount ()
105110 if count == 0 {
106- return math . NaN () , errors .New ("no such element exists" )
111+ return 0 , errors .New ("no such element exists" )
107112 }
108113
109114 rank := quantile * float64 (count - 1 )
110- if rank < float64 (s .zeroCount ) {
111- return 0 , nil
112- } else {
113- return s .Value (s .store .KeyAtRank (rank - float64 (s .zeroCount ))), nil
114- }
115+ return s .store .KeyAtRank (rank ), nil
115116}
116117
117118// Return the values at the respective specified quantiles. Return a non-nil error if any of the quantiles
@@ -130,47 +131,41 @@ func (s *DDSketch) GetValuesAtQuantiles(quantiles []float64) ([]float64, error)
130131
131132// Return the total number of values that have been added to this sketch.
132133func (s * DDSketch ) GetCount () int32 {
133- return s .zeroCount + s . store .TotalCount ()
134+ return s .store .TotalCount ()
134135}
135136
136137// Return true iff no value has been added to this sketch.
137138func (s * DDSketch ) IsEmpty () bool {
138- return s .zeroCount == 0 && s . store .IsEmpty ()
139+ return s .store .IsEmpty ()
139140}
140141
141142// Return the maximum value that has been added to this sketch. Return a non-nil error if the sketch
142143// is empty.
143144func (s * DDSketch ) GetMaxValue () (float64 , error ) {
144- if ! s .store .IsEmpty () {
145- maxIndex , _ := s .store .MaxIndex ()
146- return s .Value (maxIndex ), nil
147- } else {
148- return 0 , nil
145+ maxIndex , err := s .store .MaxIndex ()
146+ if err != nil {
147+ return math .NaN (), err
149148 }
149+ return s .Value (maxIndex ), nil
150150}
151151
152152// Return the minimum value that has been added to this sketch. Returns a non-nil error if the sketch
153153// is empty.
154154func (s * DDSketch ) GetMinValue () (float64 , error ) {
155- if s .zeroCount > 0 {
156- return 0 , nil
157- } else {
158- minIndex , err := s .store .MinIndex ()
159- if err != nil {
160- return math .NaN (), err
161- }
162- return s .Value (minIndex ), nil
155+ minIndex , err := s .store .MinIndex ()
156+ if err != nil {
157+ return math .NaN (), err
163158 }
159+ return s .Value (minIndex ), nil
164160}
165161
166162// Merges the other sketch into this one. After this operation, this sketch encodes the values that
167163// were added to both this and the other sketches.
168164func (s * DDSketch ) MergeWith (other * DDSketch ) error {
169165 if ! s .IndexMapping .Equals (other .IndexMapping ) {
170- return errors .New ("Cannot merge sketches with different index mappings. " )
166+ return errors .New ("cannot merge sketches with different index mappings" )
171167 }
172168 s .store .MergeWith (other .store )
173- s .zeroCount += other .zeroCount
174169 return nil
175170}
176171
0 commit comments