add FillRatio for the percentage fill rate of the bloom

parent 27e9d278
......@@ -269,6 +269,27 @@ func JSONUnmarshal(dbData []byte) *Bloom {
return bf
}
func (bl *Bloom) FillRatio() float64 {
count := uint64(0)
for _, b := range bl.bitset {
count += uint64(popcount(b))
}
return float64(count) / float64(bl.size+1)
}
func popcount(x uint64) uint {
const (
m1 = 0x5555555555555555 //binary: 0101...
m2 = 0x3333333333333333 //binary: 00110011..
m4 = 0x0f0f0f0f0f0f0f0f //binary: 4 zeros, 4 ones ...
h01 = 0x0101010101010101 //the sum of 256 to the power of 0,1,2,3...
)
x -= (x >> 1) & m1 //put count of each 2 bits into those 2 bits
x = (x & m2) + ((x >> 2) & m2) //put count of each 4 bits into those 4 bits
x = (x + (x >> 4)) & m4 //put count of each 8 bits into those 8 bits
return uint((x * h01) >> 56)
}
// // alternative hashFn
// func (bl Bloom) fnv64a(b *[]byte) (l, h uint64) {
// h64 := fnv.New64a()
......
......@@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"log"
"math"
"os"
"testing"
)
......@@ -33,7 +34,7 @@ func TestMain(m *testing.M) {
fmt.Println("\n###############\nbbloom_test.go")
fmt.Print("Benchmarks relate to 2**16 OP. --> output/65536 op/ns\n###############\n\n")
m.Run()
os.Exit(m.Run())
}
......@@ -86,7 +87,17 @@ func TestM_JSON(t *testing.T) {
if cnt2 != shallBe {
t.Errorf("FAILED !AddIfNotHas = %v; want %v", cnt2, shallBe)
}
}
func TestFillRatio(t *testing.T) {
bf, err := New(float64(512), float64(7))
if err != nil {
t.Fatal(err)
}
bf.Add([]byte("test"))
r := bf.FillRatio()
if math.Abs(r-float64(7)/float64(512)) > 0.001 {
t.Error("ratio doesn't work")
}
}
func ExampleM_NewAddHasAddIfNotHas() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment