Fix: Init rng in TDigest.FromBytes
- Id
- daf074f14c0e15eea2f5037394d28c466474ca23
- Author
- Nova
- Commit time
- 2025-08-14T17:41:13+07:00
Modified serialization.go
}
for i := 0; i < int(numCentroids); i++ {
- count, err := decodeUint(buf)
+ count, err := binary.ReadUvarint(buf)
if err != nil {
return nil, err
}
}
t.summary.means = t.summary.means[:numCentroids]
t.summary.counts = t.summary.counts[:numCentroids]
+ if t.rng == nil {
+ t.rng = newLocalRNG(1) // sensible default as in newWithoutSummary
+ }
idx := 16
var x float64
return errors.New("buffer has unread data")
}
return nil
-}
-
-func encodeUint(buf *bytes.Buffer, n uint64) error {
- var b [binary.MaxVarintLen64]byte
-
- l := binary.PutUvarint(b[:], n)
-
- _, err := buf.Write(b[:l])
-
- return err
-}
-
-func decodeUint(buf *bytes.Reader) (uint64, error) {
- v, err := binary.ReadUvarint(buf)
- return v, err
}
Modified serialization_test.go
package tdigest
import (
"bytes"
"encoding/base64"
+ "encoding/binary"
"math"
"math/rand"
"reflect"
testUints := []uint64{0, 10, 100, 1000, 10000, 65535, 2147483647, 2 * math.MaxUint32}
buf := new(bytes.Buffer)
+ var b [binary.MaxVarintLen64]byte
for _, i := range testUints {
- err := encodeUint(buf, i)
+ l := binary.PutUvarint(b[:], i)
+ _, err := buf.Write(b[:l])
if err != nil {
t.Error(err)
}
readBuf := bytes.NewReader(buf.Bytes())
for _, i := range testUints {
- j, err := decodeUint(readBuf)
+ j, err := binary.ReadUvarint(readBuf)
if err != nil {
t.Error(err)
}