From 98ea4d76d1899fca759b46e85da65607e0663aef Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Fri, 16 Oct 2020 21:44:13 -0600 Subject: [PATCH 1/2] add day 301 to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b45b9db..01ad874 100644 --- a/README.md +++ b/README.md @@ -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) From feb390c65e3236a1c68fbbe4e50d8383a9c4f76c Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Fri, 16 Oct 2020 22:26:57 -0600 Subject: [PATCH 2/2] day 301: bloom filter delegating to algds/bloom --- day301/problem.go | 24 ++++++++++++++++ day301/problem_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 day301/problem.go create mode 100644 day301/problem_test.go diff --git a/day301/problem.go b/day301/problem.go new file mode 100644 index 0000000..46c29de --- /dev/null +++ b/day301/problem.go @@ -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...)} +} diff --git a/day301/problem_test.go b/day301/problem_test.go new file mode 100644 index 0000000..5a32264 --- /dev/null +++ b/day301/problem_test.go @@ -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") + } +}