mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
布隆过滤器
This commit is contained in:
51
bloom/bloom.go
Normal file
51
bloom/bloom.go
Normal 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
20
bloom/boom_test.go
Normal 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"))
|
||||||
|
}
|
Reference in New Issue
Block a user