-
Notifications
You must be signed in to change notification settings - Fork 8
/
gob.go
73 lines (64 loc) · 1.21 KB
/
gob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package gohll
import (
"bytes"
"encoding/gob"
)
type serializable struct {
P uint8
M1 uint
M2 uint
Alpha float64
Format byte
TempSet tempSet
SparseList sparseList
Registers []uint8
}
// MarshalBinary implements encoding.BinaryMarshaler.
// Does not serialize hasher!
func (h *HLL) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
ts := h.tempSet
if ts == nil {
ts = &tempSet{}
}
sl := h.sparseList
if sl == nil {
sl = &sparseList{}
}
err := gob.NewEncoder(&buf).Encode(
serializable{
P: h.P,
M1: h.m1,
M2: h.m2,
Alpha: h.alpha,
Format: h.format,
TempSet: *ts,
SparseList: *sl,
Registers: h.registers,
})
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
// Preserves the hasher.
func (h *HLL) UnmarshalBinary(data []byte) error {
var s serializable
err := gob.NewDecoder(bytes.NewReader(data)).Decode(&s)
if err != nil {
return err
}
h.P = s.P
h.m1 = s.M1
h.m2 = s.M2
h.alpha = s.Alpha
h.format = s.Format
h.tempSet = &s.TempSet
h.sparseList = &s.SparseList
h.registers = s.Registers
if h.Hasher == nil {
h.Hasher = MMH3Hash
}
return nil
}