This is an automated email from the ASF dual-hosted git repository.
placave pushed a commit to branch cpc-sketch
in repository https://gitbox.apache.org/repos/asf/datasketches-go.git
The following commit(s) were added to refs/heads/cpc-sketch by this push:
new 20c2420 Add more CPC Update
20c2420 is described below
commit 20c2420705f8b32fffd785ec62e61e6272b6b3f0
Author: Pierre Lacave <[email protected]>
AuthorDate: Sat Jun 29 15:27:45 2024 +0200
Add more CPC Update
---
cpc/cpc_sketch.go | 26 ++++++++++++++++++++++++--
cpc/cpc_sketch_test.go | 9 ++++++++-
2 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/cpc/cpc_sketch.go b/cpc/cpc_sketch.go
index 4e5685f..a9c6ddb 100644
--- a/cpc/cpc_sketch.go
+++ b/cpc/cpc_sketch.go
@@ -21,7 +21,9 @@ import (
"encoding/binary"
"fmt"
"github.com/twmb/murmur3"
+ "math"
"math/bits"
+ "unsafe"
)
const (
@@ -61,12 +63,32 @@ func NewCpcSketch(lgK int, seed uint64) (*CpcSketch, error)
{
}, nil
}
-func (c *CpcSketch) Update(datum int64) error {
- binary.LittleEndian.PutUint64(c.scratch[:], uint64(datum))
+func (c *CpcSketch) UpdateUint64(datum uint64) error {
+ binary.LittleEndian.PutUint64(c.scratch[:], datum)
hashLo, hashHi := hash(c.scratch[:], c.seed)
return c.hashUpdate(hashLo, hashHi)
}
+func (c *CpcSketch) UpdateInt64(datum int64) error {
+ return c.UpdateUint64(uint64(datum))
+}
+
+func (c *CpcSketch) UpdateFloat64(datum float64) error {
+ binary.LittleEndian.PutUint64(c.scratch[:], math.Float64bits(datum))
+ hashLo, hashHi := hash(c.scratch[:], c.seed)
+ return c.hashUpdate(hashLo, hashHi)
+}
+
+func (c *CpcSketch) UpdateSlice(datum []byte) error {
+ hashLo, hashHi := hash(datum, c.seed)
+ return c.hashUpdate(hashLo, hashHi)
+}
+
+func (c *CpcSketch) UpdateString(datum string) error {
+ // get a slice to the string data (avoiding a copy to heap)
+ return c.UpdateSlice(unsafe.Slice(unsafe.StringData(datum), len(datum)))
+}
+
func (c *CpcSketch) hashUpdate(hash0, hash1 uint64) error {
col := 64 - bits.LeadingZeros64(hash1)
if col < c.fiCol {
diff --git a/cpc/cpc_sketch_test.go b/cpc/cpc_sketch_test.go
index a5f7079..9062b6d 100644
--- a/cpc/cpc_sketch_test.go
+++ b/cpc/cpc_sketch_test.go
@@ -26,7 +26,14 @@ func TestCPCCheckUpdatesEstimate(t *testing.T) {
sk, err := NewCpcSketch(10, 0)
assert.NoError(t, err)
assert.Equal(t, sk.getFormat(), format_empty_hip)
- err = sk.Update(1)
+ err = sk.UpdateUint64(1)
+ assert.NoError(t, err)
+ err = sk.UpdateFloat64(2.0)
+ assert.NoError(t, err)
+ err = sk.UpdateString("3")
+ assert.NoError(t, err)
+ bytes := []byte{4, 4}
+ err = sk.UpdateSlice(bytes)
assert.NoError(t, err)
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]