-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
78 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,56 @@ | ||
package bloomfilter | ||
|
||
import ( | ||
"crypto/sha1" | ||
"strconv" | ||
) | ||
|
||
type BloomFilter struct { | ||
bitArray []bool | ||
size int | ||
numHashes int | ||
} | ||
|
||
func NewBloomFilter(size, numHashes int) *BloomFilter { | ||
return &BloomFilter{ | ||
bitArray: make([]bool, size), | ||
size: size, | ||
numHashes: numHashes, | ||
} | ||
} | ||
|
||
func (bf *BloomFilter) Add(value string) { | ||
for i := 0; i < bf.numHashes; i++ { | ||
hash := sha1.New() | ||
hash.Write([]byte(value + strconv.Itoa(i))) | ||
digest := hash.Sum(nil) | ||
|
||
index := int(digest[0])<<24 | int(digest[1])<<16 | int(digest[2])<<8 | int(digest[3]) | ||
index = index % bf.size | ||
if index < 0 { | ||
index += bf.size | ||
} | ||
|
||
bf.bitArray[index] = true | ||
} | ||
} | ||
|
||
func (bf *BloomFilter) Contains(value string) bool { | ||
for i := 0; i < bf.numHashes; i++ { | ||
hash := sha1.New() | ||
hash.Write([]byte(value + strconv.Itoa(i))) | ||
digest := hash.Sum(nil) | ||
|
||
index := int(digest[0])<<24 | int(digest[1])<<16 | int(digest[2])<<8 | int(digest[3]) | ||
index = index % bf.size | ||
if index < 0 { | ||
index += bf.size | ||
} | ||
|
||
if !bf.bitArray[index] { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
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,19 @@ | ||
package bloomfilter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/thenativeweb/codingcircle/bloomfilter" | ||
) | ||
|
||
func TestBloomFilter(t *testing.T) { | ||
bf := bloomfilter.NewBloomFilter(512, 5) | ||
|
||
bf.Add("www.example.com") | ||
bf.Add("dangerous.example.com") | ||
|
||
assert.True(t, bf.Contains("www.example.com")) | ||
assert.True(t, bf.Contains("dangerous.example.com")) | ||
assert.False(t, bf.Contains("www.thenativeweb.io")) | ||
} |
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,2 @@ | ||
// Package bloomfilter implements a bloom filter. | ||
package bloomfilter |