mirror of
https://github.com/tursom/GoCollections.git
synced 2025-03-13 17:00:18 +08:00
add bloom marshal function
This commit is contained in:
parent
418b9c4045
commit
c749131351
@ -1,10 +1,14 @@
|
||||
package bloom
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"math"
|
||||
"unsafe"
|
||||
|
||||
"github.com/spaolacci/murmur3"
|
||||
|
||||
"github.com/tursom/GoCollections/exceptions"
|
||||
"github.com/tursom/GoCollections/lang"
|
||||
)
|
||||
|
||||
@ -85,3 +89,29 @@ 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