Skip to content

Commit

Permalink
Merge pull request #956 from vaskoz/day301
Browse files Browse the repository at this point in the history
Day301
  • Loading branch information
vaskoz committed Oct 17, 2020
2 parents f664981 + feb390c commit 02d9753
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ problems from
* [Day 298](https://github.com/vaskoz/dailycodingproblem-go/issues/609)
* [Day 299](https://github.com/vaskoz/dailycodingproblem-go/issues/611)
* [Day 300](https://github.com/vaskoz/dailycodingproblem-go/issues/612)
* [Day 301](https://github.com/vaskoz/dailycodingproblem-go/issues/613)
* [Day 302](https://github.com/vaskoz/dailycodingproblem-go/issues/615)
* [Day 303](https://github.com/vaskoz/dailycodingproblem-go/issues/616)
* [Day 304](https://github.com/vaskoz/dailycodingproblem-go/issues/618)
Expand Down
24 changes: 24 additions & 0 deletions day301/problem.go
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...)}
}
62 changes: 62 additions & 0 deletions day301/problem_test.go
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")
}
}

0 comments on commit 02d9753

Please sign in to comment.