Skip to content

Commit 281c5d8

Browse files
committed
Cleanup
1 parent bc34adf commit 281c5d8

5 files changed

Lines changed: 30 additions & 28 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Golang Bloom filter implementation
44

55
![Neutral density filter](ND-filter.jpg)
66

7-
*Neutral density filter, image by [Robert Emperley][re], [CC BY-SA 2.0][ccbysa].*
7+
*Image by [Robert Emperley][re], [CC BY-SA 2.0][ccbysa].*
88

99
### Installation
1010

example_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Example_basics() {
2626
// Output: https://rascal.com seems to be shady.
2727
}
2828

29-
// Count the number of false positives.
29+
// Estimate the number of false positives.
3030
func Example_falsePositives() {
3131
// Create a Bloom filter with room for n elements
3232
// at a false-positives rate less than 1/p.
@@ -51,8 +51,8 @@ func Example_falsePositives() {
5151
}
5252

5353
// Compute the union of two filters.
54-
func ExampleFilter_Or() {
55-
// Create two Bloom filter with room for 1000 elements
54+
func ExampleFilter_Union() {
55+
// Create two Bloom filters, each with room for 1000 elements
5656
// at a false-positives rate less than 1/100.
5757
n, p := 1000, 100
5858
f1 := bloom.New(n, p)

filter.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
//
33
// Bloom filters
44
//
5-
// A Bloom filter is a space-efficient probabilistic data structure
6-
// used to test set membership. A member test returns either
5+
// A Bloom filter is a fast and space-efficient probabilistic data structure
6+
// used to test set membership.
7+
//
8+
// A membership test returns either
79
// ”likely member” or ”definitely not a member”. Only false positives
810
// can occur: an element that has been added to the filter
911
// will be identified as ”likely member”.
@@ -31,8 +33,8 @@
3133
//
3234
// This implementation is not intended for cryptographic use.
3335
// Each membership test makes a single call to a 128-bit MurmurHash3 function.
34-
// This saves on hashing without increasing the false-positives
35-
// probability as shown by Kirsch and Mitzenmacher.
36+
// This improves speed without increasing the false-positives rate
37+
// as shown by Kirsch and Mitzenmacher.
3638
//
3739
package bloom
3840

@@ -49,23 +51,24 @@ const (
4951
type Filter struct {
5052
data []uint64 // Bit array, the length is a power of 2.
5153
lookups int // Lookups per query
52-
count int64 // Estimated number of unique elements
54+
count int64 // Estimate number of elements
5355
}
5456

57+
// MurmurHash3 function.
5558
var murmur = new(digest)
5659

5760
// New creates an empty Bloom filter with room for n elements
5861
// at a false-positives rate less than 1/p.
5962
func New(n int, p int) *Filter {
60-
f := &Filter{}
6163
minWords := int(0.0325 * math.Log(float64(p)) * float64(n))
6264
words := 1
6365
for words < minWords {
6466
words *= 2
6567
}
66-
f.data = make([]uint64, words)
67-
f.lookups = int(1.4*math.Log(float64(p)) + 1)
68-
return f
68+
return &Filter{
69+
data: make([]uint64, words),
70+
lookups: int(1.4*math.Log(float64(p)) + 1),
71+
}
6972
}
7073

7174
// AddByte adds b to the filter and tells if b was already a likely member.
@@ -95,7 +98,7 @@ func (f *Filter) Add(s string) bool {
9598
return f.AddByte(b)
9699
}
97100

98-
// TestByte tells if b is a likely member of this filter.
101+
// TestByte tells if b is a likely member of the filter.
99102
func (f *Filter) TestByte(b []byte) bool {
100103
h1, h2 := murmur.hash(b)
101104
trunc := uint64(len(f.data))<<shift - 1
@@ -110,14 +113,14 @@ func (f *Filter) TestByte(b []byte) bool {
110113
return true
111114
}
112115

113-
// Test tells if s is a likely member of this filter.
116+
// Test tells if s is a likely member of the filter.
114117
func (f *Filter) Test(s string) bool {
115118
b := make([]byte, len(s))
116119
copy(b, s)
117120
return f.TestByte(b)
118121
}
119122

120-
// Count returns an estimate of the number of elements in this filter.
123+
// Count returns an estimate of the number of elements in the filter.
121124
func (f *Filter) Count() int64 {
122125
return f.count
123126
}

filter_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestFilter(t *testing.T) {
4848

4949
member = filter.Add(s3)
5050
if member {
51-
t.Errorf("Add(s1) = %v; want false\n", member)
51+
t.Errorf("Add(s3) = %v; want false\n", member)
5252
}
5353
count = filter.Count()
5454
if count != 2 {
@@ -72,15 +72,15 @@ func TestUnion(t *testing.T) {
7272
or := f1.Union(f2)
7373
member := or.Test(s1)
7474
if !member {
75-
t.Errorf("or.Test(s1) = %v; want true\n", member)
75+
t.Errorf("f1.Union(f2).Test(s1) = %v; want true\n", member)
7676
}
7777
member = or.Test(s2)
7878
if !member {
79-
t.Errorf("or.Test(s2) = %v; want true\n", member)
79+
t.Errorf("f1.Union(f2).Test(s2) = %v; want true\n", member)
8080
}
8181
member = or.Test(s3)
8282
if !member {
83-
t.Errorf("or.Test(s3) = %v; want true\n", member)
83+
t.Errorf("f1.Union(f2).Test(s3) = %v; want true\n", member)
8484
}
8585
}
8686
}
@@ -93,7 +93,7 @@ func BenchmarkAdd(b *testing.B) {
9393
filter := New(1<<30, 200)
9494
b.StartTimer()
9595
for i := 0; i < b.N; i++ {
96-
filter.Add(fox)
96+
_ = filter.Add(fox)
9797
}
9898
}
9999

@@ -103,7 +103,7 @@ func BenchmarkAddByte(b *testing.B) {
103103
b.StartTimer()
104104
bytes := []byte(fox)
105105
for i := 0; i < b.N; i++ {
106-
filter.AddByte(bytes)
106+
_ = filter.AddByte(bytes)
107107
}
108108
}
109109

@@ -112,7 +112,7 @@ func BenchmarkTest(b *testing.B) {
112112
filter := New(1<<30, 200)
113113
b.StartTimer()
114114
for i := 0; i < b.N; i++ {
115-
filter.Test(fox)
115+
_ = filter.Test(fox)
116116
}
117117
}
118118

@@ -122,7 +122,7 @@ func BenchmarkTestByte(b *testing.B) {
122122
b.StartTimer()
123123
bytes := []byte(fox)
124124
for i := 0; i < b.N; i++ {
125-
filter.TestByte(bytes)
125+
_ = filter.TestByte(bytes)
126126
}
127127
}
128128

hash_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import (
77
func TestHash(t *testing.T) {
88
d := new(digest)
99
var data = []struct {
10-
h1 uint64
11-
h2 uint64
12-
s string
10+
h1, h2 uint64
11+
s string
1312
}{
1413
{0x0000000000000000, 0x0000000000000000, ""},
1514
{0xcbd8a7b341bd9b02, 0x5b1e906a48ae1d19, "hello"},
@@ -23,7 +22,7 @@ func TestHash(t *testing.T) {
2322
t.Errorf("hash(%q).h1 = %d; want %d\n", x.s, h1, x.h1)
2423
}
2524
if h2 != x.h2 {
26-
t.Errorf("hash(%q).h1 = %d; want %d\n", x.s, h2, x.h2)
25+
t.Errorf("hash(%q).h2 = %d; want %d\n", x.s, h2, x.h2)
2726
}
2827
}
2928
}

0 commit comments

Comments
 (0)