|
1 | 1 | package hyperloglog |
2 | 2 |
|
3 | | -import "encoding/binary" |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "slices" |
| 6 | +) |
4 | 7 |
|
5 | 8 | // Original author of this file is github.com/clarkduvall/hyperloglog |
6 | 9 | type iterable interface { |
@@ -52,32 +55,26 @@ func (v *compressedList) Clone() *compressedList { |
52 | 55 | return newV |
53 | 56 | } |
54 | 57 |
|
55 | | -func (v *compressedList) MarshalBinary() (data []byte, err error) { |
56 | | - // Marshal the variableLengthList |
57 | | - bdata, err := v.b.MarshalBinary() |
58 | | - if err != nil { |
59 | | - return nil, err |
60 | | - } |
61 | | - |
62 | | - // At least 4 bytes for the two fixed sized values plus the size of bdata. |
63 | | - data = make([]byte, 0, 4+4+len(bdata)) |
| 58 | +func (v *compressedList) AppendBinary(data []byte) ([]byte, error) { |
| 59 | + // At least 4 bytes for the two fixed sized values |
| 60 | + data = slices.Grow(data, 4+4) |
64 | 61 |
|
65 | 62 | // Marshal the count and last values. |
66 | | - data = append(data, []byte{ |
| 63 | + data = append(data, |
67 | 64 | // Number of items in the list. |
68 | | - byte(v.count >> 24), |
69 | | - byte(v.count >> 16), |
70 | | - byte(v.count >> 8), |
| 65 | + byte(v.count>>24), |
| 66 | + byte(v.count>>16), |
| 67 | + byte(v.count>>8), |
71 | 68 | byte(v.count), |
72 | 69 | // The last item in the list. |
73 | | - byte(v.last >> 24), |
74 | | - byte(v.last >> 16), |
75 | | - byte(v.last >> 8), |
| 70 | + byte(v.last>>24), |
| 71 | + byte(v.last>>16), |
| 72 | + byte(v.last>>8), |
76 | 73 | byte(v.last), |
77 | | - }...) |
| 74 | + ) |
78 | 75 |
|
79 | | - // Append the list |
80 | | - return append(data, bdata...), nil |
| 76 | + // Append the variableLengthList |
| 77 | + return v.b.AppendBinary(data) |
81 | 78 | } |
82 | 79 |
|
83 | 80 | func (v *compressedList) UnmarshalBinary(data []byte) error { |
@@ -130,20 +127,20 @@ func (v *compressedList) Iter() *iterator { |
130 | 127 |
|
131 | 128 | type variableLengthList []uint8 |
132 | 129 |
|
133 | | -func (v variableLengthList) MarshalBinary() (data []byte, err error) { |
| 130 | +func (v variableLengthList) AppendBinary(data []byte) ([]byte, error) { |
134 | 131 | // 4 bytes for the size of the list, and a byte for each element in the |
135 | 132 | // list. |
136 | | - data = make([]byte, 0, 4+v.Len()) |
| 133 | + data = slices.Grow(data, 4+v.Len()) |
137 | 134 |
|
138 | 135 | // Length of the list. We only need 32 bits because the size of the set |
139 | 136 | // couldn't exceed that on 32 bit architectures. |
140 | 137 | sz := v.Len() |
141 | | - data = append(data, []byte{ |
142 | | - byte(sz >> 24), |
143 | | - byte(sz >> 16), |
144 | | - byte(sz >> 8), |
| 138 | + data = append(data, |
| 139 | + byte(sz>>24), |
| 140 | + byte(sz>>16), |
| 141 | + byte(sz>>8), |
145 | 142 | byte(sz), |
146 | | - }...) |
| 143 | + ) |
147 | 144 |
|
148 | 145 | // Marshal each element in the list. |
149 | 146 | for i := 0; i < sz; i++ { |
|
0 commit comments