1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-17 16:12:42 +08:00

布隆过滤器

This commit is contained in:
2022-03-27 10:17:21 +08:00
commit 0c3a00ba36
2 changed files with 71 additions and 0 deletions

51
bloom/bloom.go Normal file
View File

@ -0,0 +1,51 @@
package bloom
import "github.com/bits-and-blooms/bitset"
const DEFAULT_SIZE = 2 << 24
var seeds = []uint{7, 11, 13, 31, 37, 61}
type simplehash struct {
cap uint
seed uint
}
type BloomFilter struct {
set *bitset.BitSet
funcs [6]simplehash
}
func NewBloomFilter() *BloomFilter {
bf := new(BloomFilter)
for i := 0; i < len(bf.funcs); i++ {
bf.funcs[i] = simplehash{DEFAULT_SIZE, seeds[i]}
}
bf.set = bitset.New(DEFAULT_SIZE)
return bf
}
func (bf *BloomFilter) Add(value string) {
for _, f := range bf.funcs {
bf.set.Set(f.hash(value))
}
}
func (bf *BloomFilter) Contains(value string) bool {
if value == "" {
return false
}
ret := true
for _, f := range bf.funcs {
ret = ret && bf.set.Test(f.hash(value))
}
return ret
}
func (s simplehash) hash(value string) uint {
var result uint = 0
for i := 0; i < len(value); i++ {
result = result*s.seed + uint(value[i])
}
return (s.cap - 1) & result
}

20
bloom/boom_test.go Normal file
View File

@ -0,0 +1,20 @@
package bloom_test
import (
"fmt"
"strconv"
"testing"
"github.com/charlienet/go-mixed/bloom"
)
func TestBloom(t *testing.T) {
b := bloom.NewBloomFilter()
for i := 0; i < 1000000; i++ {
b.Add(strconv.Itoa(i))
}
fmt.Println(b.Contains(strconv.Itoa(9999)))
fmt.Println(b.Contains("ss"))
}