-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #956 from vaskoz/day301
Day301
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package day301 | ||
|
||
import "github.com/algds/bloom" | ||
|
||
// BloomFilter encapsulates github.com/algds/bloom | ||
// to conform to method signatures. | ||
type BloomFilter struct { | ||
blf bloom.Filter | ||
} | ||
|
||
// Add a value to the set of values. | ||
func (bf *BloomFilter) Add(value interface{}) { | ||
bf.blf.Add(value) | ||
} | ||
|
||
// Check whether a value is in the set. | ||
func (bf *BloomFilter) Check(value interface{}) bool { | ||
return bf.blf.Contains(value) | ||
} | ||
|
||
// NewBloomFilter creates a new instance of BloomFilter. | ||
func NewBloomFilter(m uint, hashes []bloom.Hash) *BloomFilter { | ||
return &BloomFilter{blf: bloom.New(m, hashes...)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package day301 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/algds/bloom" | ||
) | ||
|
||
func TestBloomFilter(t *testing.T) { | ||
t.Parallel() | ||
|
||
f1 := func(d interface{}) uint { | ||
s := d.(string) | ||
|
||
return uint(s[0]) | ||
} | ||
f2 := func(d interface{}) uint { | ||
s := d.(string) | ||
|
||
return uint(s[1]) | ||
} | ||
|
||
blf := NewBloomFilter(100, []bloom.Hash{f1, f2}) | ||
|
||
blf.Add("foo") | ||
blf.Add("bar") | ||
|
||
if !blf.Check("foo") { | ||
t.Errorf("foo should be found") | ||
} | ||
|
||
if !blf.Check("bar") { | ||
t.Errorf("bar should be found") | ||
} | ||
|
||
if blf.Check("xyz") { | ||
t.Errorf("xyz should not be found") | ||
} | ||
} | ||
|
||
func BenchmarkBloomFilter(b *testing.B) { | ||
f1 := func(d interface{}) uint { | ||
s := d.(string) | ||
|
||
return uint(s[0]) | ||
} | ||
f2 := func(d interface{}) uint { | ||
s := d.(string) | ||
|
||
return uint(s[1]) | ||
} | ||
|
||
blf := NewBloomFilter(100, []bloom.Hash{f1, f2}) | ||
|
||
for i := 0; i < b.N; i++ { | ||
blf.Add("foo") | ||
blf.Add("bar") | ||
blf.Check("foo") | ||
blf.Check("bar") | ||
blf.Check("xyz") | ||
} | ||
} |