Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
bbloom
Commits
f1f60dc3
Unverified
Commit
f1f60dc3
authored
Sep 06, 2016
by
Jakub Sztandera
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add FillRatio for the percentage fill rate of the bloom
parent
27e9d278
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
2 deletions
+34
-2
bbloom.go
bbloom.go
+21
-0
bbloom_test.go
bbloom_test.go
+13
-2
No files found.
bbloom.go
View file @
f1f60dc3
...
...
@@ -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()
...
...
bbloom_test.go
View file @
f1f60dc3
...
...
@@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"log"
"math"
"os"
"testing"
)
...
...
@@ -33,7 +34,7 @@ func TestMain(m *testing.M) {
fmt
.
Println
(
"
\n
###############
\n
bbloom_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
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment