mirror of
https://github.com/tursom/GoCollections.git
synced 2024-12-28 09:30:20 +08:00
Compare commits
No commits in common. "c74913135182c9383c59ed3481a298f798995153" and "9e06ed4b5e83731a77964ecb9378aa17b7f7ab81" have entirely different histories.
c749131351
...
9e06ed4b5e
@ -1,14 +1,10 @@
|
||||
package bloom
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"math"
|
||||
"unsafe"
|
||||
|
||||
"github.com/spaolacci/murmur3"
|
||||
|
||||
"github.com/tursom/GoCollections/exceptions"
|
||||
"github.com/tursom/GoCollections/lang"
|
||||
)
|
||||
|
||||
@ -74,7 +70,7 @@ func (b *Bloom) M() uint {
|
||||
func (b *Bloom) Contains(data []byte) bool {
|
||||
for i := 0; i < int(b.k); i++ {
|
||||
hashCode := uint(HashFunc(data, uint32(i)))
|
||||
if !b.m.GetBit(hashCode % b.m.BitLength()) {
|
||||
if !b.m.GetBit(hashCode & b.m.BitLength()) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -89,29 +85,3 @@ func (b *Bloom) Add(data []byte) {
|
||||
b.m.SetBit(hashCode%b.m.BitLength(), true)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bloom) Marshal(writer io.Writer) {
|
||||
if err := binary.Write(writer, binary.BigEndian, uint32(b.k)); err != nil {
|
||||
panic(exceptions.Package(err))
|
||||
}
|
||||
if err := binary.Write(writer, binary.BigEndian, uint32(b.c)); err != nil {
|
||||
panic(exceptions.Package(err))
|
||||
}
|
||||
|
||||
if _, err := writer.Write(b.m.Bytes()); err != nil {
|
||||
panic(exceptions.Package(err))
|
||||
}
|
||||
}
|
||||
|
||||
func Unmarshal(data []byte) *Bloom {
|
||||
k := binary.BigEndian.Uint32(data)
|
||||
c := binary.BigEndian.Uint32(data[4:])
|
||||
|
||||
m := data[8:]
|
||||
|
||||
return &Bloom{
|
||||
m: *(*lang.UInt8Array)(unsafe.Pointer(&m)),
|
||||
k: uint(k),
|
||||
c: uint(c),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user