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 2ee945b  expose format, flavor and start update test
2ee945b is described below

commit 2ee945b369ead81570071118ae5323df25e5c5ac
Author: Pierre Lacave <[email protected]>
AuthorDate: Sat Jul 20 19:32:06 2024 +0200

    expose format, flavor and start update test
---
 cpc/cpc_sketch.go      | 10 ++++-----
 cpc/cpc_sketch_test.go | 58 +++++++++++++++++++++++++++++++-------------------
 cpc/utils.go           | 46 +++++++++++++++++++++------------------
 3 files changed, 66 insertions(+), 48 deletions(-)

diff --git a/cpc/cpc_sketch.go b/cpc/cpc_sketch.go
index 4d1c157..0f631cd 100644
--- a/cpc/cpc_sketch.go
+++ b/cpc/cpc_sketch.go
@@ -199,10 +199,10 @@ func hash(bs []byte, seed uint64) (uint64, uint64) {
        return murmur3.SeedSum128(seed, seed, bs)
 }
 
-func (c *CpcSketch) getFormat() cpcFormat {
+func (c *CpcSketch) getFormat() CpcFormat {
        ordinal := 0
-       f := c.getFlavor()
-       if f == flavor_hybrid || f == flavor_sparse {
+       f := c.GetFlavor()
+       if f == CpcFlavorHybrid || f == CpcFlavorSparse {
                ordinal = 2
                if !c.mergeFlag {
                        ordinal |= 1
@@ -219,10 +219,10 @@ func (c *CpcSketch) getFormat() cpcFormat {
                        ordinal |= 1
                }
        }
-       return cpcFormat(ordinal)
+       return CpcFormat(ordinal)
 }
 
-func (c *CpcSketch) getFlavor() cpcFlavor {
+func (c *CpcSketch) GetFlavor() CpcFlavor {
        return determineFlavor(c.lgK, c.numCoupons)
 }
 
diff --git a/cpc/cpc_sketch_test.go b/cpc/cpc_sketch_test.go
index fef126e..cb0661f 100644
--- a/cpc/cpc_sketch_test.go
+++ b/cpc/cpc_sketch_test.go
@@ -25,7 +25,7 @@ import (
 func TestCPCCheckUpdatesEstimate(t *testing.T) {
        sk, err := NewCpcSketch(10, 0)
        assert.NoError(t, err)
-       assert.Equal(t, sk.getFormat(), format_empty_hip)
+       assert.Equal(t, sk.getFormat(), CpcFormatEmptyHip)
        err = sk.UpdateUint64(1)
        assert.NoError(t, err)
        err = sk.UpdateFloat64(2.0)
@@ -50,33 +50,47 @@ func TestCPCCheckUpdatesEstimate(t *testing.T) {
        assert.True(t, lb >= 0)
        assert.True(t, lb <= est)
        assert.True(t, est <= ub)
+       assert.Equal(t, sk.GetFlavor(), CpcFlavorSparse)
+       assert.Equal(t, sk.getFormat(), CpcFormatSparceHybridHip)
+}
+
+func TestCPCCheckEstimatesWithMerge(t *testing.T) {
+       lgk := 4
+       sk1, err := NewCpcSketch(lgk, CpcDefaultUpdateSeed)
+       assert.NoError(t, err)
+       sk2, err := NewCpcSketch(lgk, CpcDefaultUpdateSeed)
+       assert.NoError(t, err)
+       n := 1 << lgk
+       for i := 0; i < n; i++ {
+               err = sk1.UpdateUint64(uint64(i))
+               assert.NoError(t, err)
+               err = sk2.UpdateUint64(uint64(i + n))
+               assert.NoError(t, err)
+       }
 }
 
 /*
   @Test
-  public void checkUpdatesEstimate() {
-    final CpcSketch sk = new CpcSketch(10, 0);
-    println(sk.toString(true));
-    assertEquals(sk.getFormat(), Format.EMPTY_HIP);
-    sk.update(1L);
-    sk.update(2.0);
-    sk.update("3");
-    byte[] bytes = new byte[] { 4, 4 };
-    sk.update(bytes);
-    sk.update(ByteBuffer.wrap(bytes));  // same as previous
-    sk.update(ByteBuffer.wrap(bytes, 0, 1));
-    sk.update(new char[] { 5 });
-    sk.update(new int[] { 6 });
-    sk.update(new long[] { 7 });
-    final double est = sk.getEstimate();
-    final double lb = sk.getLowerBound(2);
-    final double ub = sk.getUpperBound(2);
+  public void checkEstimatesWithMerge() {
+    final int lgK = 4;
+    final CpcSketch sk1 = new CpcSketch(lgK);
+    final CpcSketch sk2 = new CpcSketch(lgK);
+    final int n = 1 << lgK;
+    for (int i = 0; i < n; i++ ) {
+      sk1.update(i);
+      sk2.update(i + n);
+    }
+    final CpcUnion union = new CpcUnion(lgK);
+    union.update(sk1);
+    union.update(sk2);
+    final CpcSketch result = union.getResult();
+    final double est = result.getEstimate();
+    final double lb = result.getLowerBound(2);
+    final double ub = result.getUpperBound(2);
     assertTrue(lb >= 0);
     assertTrue(lb <= est);
     assertTrue(est <= ub);
-    assertEquals(sk.getFlavor(), Flavor.SPARSE);
-    assertEquals(sk.getFormat(), Format.SPARSE_HYBRID_HIP);
-    println(sk.toString());
-    println(sk.toString(true));
+    assertTrue(result.validate());
+    println(result.toString(true));
   }
 */
diff --git a/cpc/utils.go b/cpc/utils.go
index 6660d60..5897888 100644
--- a/cpc/utils.go
+++ b/cpc/utils.go
@@ -19,26 +19,30 @@ package cpc
 
 import "fmt"
 
-type cpcFormat int
-type cpcFlavor int
+type CpcFormat int
+type CpcFlavor int
 
 const (
-       format_empty_merged               cpcFormat = 0
-       format_empty_hip                  cpcFormat = 1
-       format_sparse_hybrid_merged       cpcFormat = 2
-       format_sparce_hybrid_hip          cpcFormat = 3
-       format_pinned_sliding_merged_nosv cpcFormat = 4
-       format_pinned_sliding_hip_nosv    cpcFormat = 5
-       format_pinned_sliding_merged      cpcFormat = 6
-       format_pinned_sliding_hip         cpcFormat = 7
+       CpcformatEmptyMerged             CpcFormat = 0
+       CpcFormatEmptyHip                CpcFormat = 1
+       CpcFormatSparseHybridMerged      CpcFormat = 2
+       CpcFormatSparceHybridHip         CpcFormat = 3
+       CpcFormatPinnedSlidingMergedNosv CpcFormat = 4
+       CpcFormatPinnedSlidingHipNosv    CpcFormat = 5
+       CpcFormatPinnedSlidingMerged     CpcFormat = 6
+       CpcFormatPinnedSlidingHip        CpcFormat = 7
 )
 
 const (
-       flavor_empty   cpcFlavor = 0 //    0  == C <    1
-       flavor_sparse  cpcFlavor = 1 //    1  <= C <   3K/32
-       flavor_hybrid  cpcFlavor = 2 // 3K/32 <= C <   K/2
-       flavor_pinned  cpcFlavor = 3 //   K/2 <= C < 27K/8  [NB: 27/8 = 3 + 3/8]
-       flavor_sliding cpcFlavor = 4 // 27K/8 <= C
+       CpcFlavorEmpty   CpcFlavor = 0 //    0  == C <    1
+       CpcFlavorSparse  CpcFlavor = 1 //    1  <= C <   3K/32
+       CpcFlavorHybrid  CpcFlavor = 2 // 3K/32 <= C <   K/2
+       CpcFlavorPinned  CpcFlavor = 3 //   K/2 <= C < 27K/8  [NB: 27/8 = 3 + 
3/8]
+       CpcFlavorSliding CpcFlavor = 4 // 27K/8 <= C
+)
+
+const (
+       CpcDefaultUpdateSeed = 9001
 )
 
 func checkLgK(lgK int) error {
@@ -55,23 +59,23 @@ func checkLgSizeInts(lgSizeInts int) error {
        return nil
 }
 
-func determineFlavor(lgK int, numCoupons uint64) cpcFlavor {
+func determineFlavor(lgK int, numCoupons uint64) CpcFlavor {
        c := numCoupons
        k := uint64(1) << lgK
        c2 := c << 1
        c8 := c << 3
        c32 := c << 5
        if c == 0 {
-               return flavor_empty //    0  == C <    1
+               return CpcFlavorEmpty //    0  == C <    1
        }
        if c32 < (3 * k) {
-               return flavor_sparse //    1  <= C <   3K/32
+               return CpcFlavorSparse //    1  <= C <   3K/32
        }
        if c2 < k {
-               return flavor_hybrid // 3K/32 <= C <   K/2
+               return CpcFlavorHybrid // 3K/32 <= C <   K/2
        }
        if c8 < (27 * k) {
-               return flavor_pinned //   K/2 <= C < 27K/8
+               return CpcFlavorPinned //   K/2 <= C < 27K/8
        }
-       return flavor_sliding // 27K/8 <= C
+       return CpcFlavorSliding // 27K/8 <= C
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to