@@ -18,7 +18,7 @@ type Sketch struct {
1818 p uint8
1919 m uint32
2020 alpha float64
21- tmpSet set
21+ tmpSet * set
2222 sparseList * compressedList
2323 regs []uint8
2424}
@@ -45,7 +45,7 @@ func NewSketch(precision uint8, sparse bool) (*Sketch, error) {
4545 alpha : alpha (float64 (m )),
4646 }
4747 if sparse {
48- s .tmpSet = set {}
48+ s .tmpSet = newSet ( 0 )
4949 s .sparseList = newCompressedList (0 )
5050 } else {
5151 s .regs = make ([]uint8 , m )
@@ -65,7 +65,7 @@ func (sk *Sketch) Clone() *Sketch {
6565}
6666
6767func (sk * Sketch ) maybeToNormal () {
68- if uint32 (len ( sk .tmpSet ))* 100 > sk .m {
68+ if uint32 (sk .tmpSet . Len ( ))* 100 > sk .m {
6969 sk .mergeSparse ()
7070 if uint32 (sk .sparseList .Len ()) > sk .m {
7171 sk .toNormal ()
@@ -90,9 +90,7 @@ func (sk *Sketch) Merge(other *Sketch) error {
9090}
9191
9292func (sk * Sketch ) mergeSparseSketch (other * Sketch ) {
93- for k := range other .tmpSet {
94- sk .tmpSet .add (k )
95- }
93+ sk .tmpSet .Merge (other .tmpSet )
9694 for iter := other .sparseList .Iter (); iter .HasNext (); {
9795 sk .tmpSet .add (iter .Next ())
9896 }
@@ -105,10 +103,10 @@ func (sk *Sketch) mergeDenseSketch(other *Sketch) {
105103 }
106104
107105 if other .sparse () {
108- for k := range other .tmpSet {
106+ other .tmpSet . ForEach ( func ( k uint32 ) {
109107 i , r := decodeHash (k , other .p , pp )
110108 sk .insert (i , r )
111- }
109+ })
112110 for iter := other .sparseList .Iter (); iter .HasNext (); {
113111 i , r := decodeHash (iter .Next (), other .p , pp )
114112 sk .insert (i , r )
@@ -123,7 +121,7 @@ func (sk *Sketch) mergeDenseSketch(other *Sketch) {
123121}
124122
125123func (sk * Sketch ) toNormal () {
126- if len ( sk .tmpSet ) > 0 {
124+ if sk .tmpSet . Len ( ) > 0 {
127125 sk .mergeSparse ()
128126 }
129127
@@ -165,17 +163,17 @@ func (sk *Sketch) Estimate() uint64 {
165163}
166164
167165func (sk * Sketch ) mergeSparse () {
168- if len ( sk .tmpSet ) == 0 {
166+ if sk .tmpSet . Len ( ) == 0 {
169167 return
170168 }
171169
172- keys := make (uint64Slice , 0 , len ( sk .tmpSet ))
173- for k := range sk .tmpSet {
170+ keys := make (uint64Slice , 0 , sk .tmpSet . Len ( ))
171+ sk .tmpSet . ForEach ( func ( k uint32 ) {
174172 keys = append (keys , k )
175- }
173+ })
176174 sort .Sort (keys )
177175
178- newList := newCompressedList (4 * len ( sk .tmpSet ) + len ( sk .sparseList .b ))
176+ newList := newCompressedList (4 * sk .tmpSet . Len ( ) + sk .sparseList .Len ( ))
179177 for iter , i := sk .sparseList .Iter (), 0 ; iter .HasNext () || i < len (keys ); {
180178 if ! iter .HasNext () {
181179 newList .Append (keys [i ])
@@ -201,7 +199,7 @@ func (sk *Sketch) mergeSparse() {
201199 }
202200
203201 sk .sparseList = newList
204- sk .tmpSet = set {}
202+ sk .tmpSet = newSet ( 0 )
205203}
206204
207205// MarshalBinary implements the encoding.BinaryMarshaler interface.
@@ -277,7 +275,7 @@ func (sk *Sketch) UnmarshalBinary(data []byte) error {
277275 sparse := data [3 ] == byte (1 )
278276
279277 // Make a newSketch Sketch if the precision doesn't match or if the Sketch was used
280- if sk .p != p || sk .regs != nil || len ( sk .tmpSet ) > 0 || (sk .sparseList != nil && sk .sparseList .Len () > 0 ) {
278+ if sk .p != p || sk .regs != nil || sk .tmpSet . Len ( ) > 0 || (sk .sparseList != nil && sk .sparseList .Len () > 0 ) {
281279 newh , err := NewSketch (p , sparse )
282280 if err != nil {
283281 return err
@@ -292,14 +290,14 @@ func (sk *Sketch) UnmarshalBinary(data []byte) error {
292290
293291 // Unmarshal the tmp_set.
294292 tssz := binary .BigEndian .Uint32 (data [4 :8 ])
295- sk .tmpSet = make ( map [ uint32 ] struct {}, tssz )
293+ sk .tmpSet = newSet ( int ( tssz ) )
296294
297295 // We need to unmarshal tssz values in total, and each value requires us
298296 // to read 4 bytes.
299297 tsLastByte := int ((tssz * 4 ) + 8 )
300298 for i := 8 ; i < tsLastByte ; i += 4 {
301299 k := binary .BigEndian .Uint32 (data [i : i + 4 ])
302- sk .tmpSet [ k ] = struct {}{}
300+ sk .tmpSet . add ( k )
303301 }
304302
305303 // Unmarshal the sparse Sketch.
0 commit comments