-
Notifications
You must be signed in to change notification settings - Fork 108
/
bitmap.go
208 lines (166 loc) · 4.48 KB
/
bitmap.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package field
import (
"fmt"
"strings"
)
var _ Field = (*Bitmap)(nil)
// Bitmap is a 1-indexed big endian bitmap field.
type Bitmap struct {
spec *Spec
data []byte
bitmapLenght int
}
const defaultBitmapLength = 8
const firstBitOn = 0b10000000 // big endian
func NewBitmap(spec *Spec) *Bitmap {
length := spec.Length
if length == 0 {
length = defaultBitmapLength
}
return &Bitmap{
spec: spec,
data: make([]byte, length),
bitmapLenght: length,
}
}
func (f *Bitmap) Spec() *Spec {
return f.spec
}
func (f *Bitmap) SetSpec(spec *Spec) {
f.spec = spec
}
func (f *Bitmap) SetBytes(b []byte) error {
f.data = b
return nil
}
func (f *Bitmap) Bytes() ([]byte, error) {
if f == nil {
return nil, nil
}
return f.data, nil
}
func (f *Bitmap) String() (string, error) {
if f == nil {
return "", nil
}
var bits []string
for _, byte_ := range f.data {
bits = append(bits, fmt.Sprintf("%08b", byte_))
}
return strings.Join(bits, " "), nil
}
func (f *Bitmap) Pack() ([]byte, error) {
packed, err := f.spec.Enc.Encode(f.data)
if err != nil {
return nil, fmt.Errorf("failed to encode content: %w", err)
}
return packed, nil
}
// Unpack sets the bitmap data. It returns the number of bytes read from the
// data. Usually it's 8 for binary, 16 for hex - for a single bitmap.
// If DisableAutoExpand is not set (default), it will read all bitmaps until
// the first bit of the read bitmap is not set.
// If DisableAutoExpand is set, it will only read the first bitmap regardless
// of the first bit being set.
func (f *Bitmap) Unpack(data []byte) (int, error) {
minLen, _, err := f.spec.Pref.DecodeLength(f.bitmapLenght, data)
if err != nil {
return 0, fmt.Errorf("failed to decode length: %w", err)
}
f.data = make([]byte, 0)
read := 0
var i int
// read until we have no more bitmaps
// or only read one bitmap if DisableAutoExpand is set
for {
i++
decoded, readDecoded, err := f.spec.Enc.Decode(data[read:], minLen)
if err != nil {
return 0, fmt.Errorf("failed to decode content for %d bitmap: %w", i, err)
}
read += readDecoded
f.data = append(f.data, decoded...)
// if it's a fixed bitmap or first bit of the decoded bitmap is not set, exit loop
if f.spec.DisableAutoExpand || decoded[0]&firstBitOn == 0 {
break
}
}
return read, nil
}
func (f *Bitmap) Unmarshal(v interface{}) error {
if v == nil {
return nil
}
bmap, ok := v.(*Bitmap)
if !ok {
return fmt.Errorf("data does not match required *Bitmap type")
}
bmap.data = f.data
return nil
}
func (f *Bitmap) SetData(data interface{}) error {
if data == nil {
return nil
}
bmap, ok := data.(*Bitmap)
if !ok {
return fmt.Errorf("data does not match required *Bitmap type")
}
f.data = bmap.data
return nil
}
func (f *Bitmap) Marshal(data interface{}) error {
return f.SetData(data)
}
// Reset resets the bitmap to its initial state because of how message works,
// Message need a way to initialize bitmap. That's why we set parameters to
// their default values here like we do in constructor.
func (f *Bitmap) Reset() {
length := f.spec.Length
if length == 0 {
length = defaultBitmapLength
}
f.bitmapLenght = length
// this actually resets the bitmap
f.data = make([]byte, f.bitmapLenght)
}
// For auto expand mode if we expand bitmap we should set bit that shows the presence of the next bitmap
func (f *Bitmap) Set(n int) {
if n <= 0 {
return
}
// do we have to expand bitmap?
if n > len(f.data)*8 {
if f.spec.DisableAutoExpand {
return
}
// calculate how many bitmaps we need to store n-th bit
bitmapIndex := (n - 1) / (f.bitmapLenght * 8)
newBitmapsCount := (bitmapIndex + 1)
// set first bit of the first byte of the last bitmap in
// current data to 1 to show the presence of the next bitmap
f.data[len(f.data)-f.bitmapLenght] |= firstBitOn
// add new empty bitmaps and for every new bitmap except the
// last one, set bit that shows the presence of the next bitmap
for i := newBitmapsCount - len(f.data)/f.bitmapLenght; i > 0; i-- {
newBitmap := make([]byte, f.bitmapLenght)
// set first bit of the first byte of the new bitmap to 1
// but only if it is not the last bitmap
if i > 1 {
newBitmap[0] = firstBitOn
}
f.data = append(f.data, newBitmap...)
}
}
// set bit
f.data[(n-1)/8] |= 1 << (uint(7-(n-1)) % 8)
}
func (f *Bitmap) IsSet(n int) bool {
if n <= 0 || n > len(f.data)*8 {
return false
}
return f.data[(n-1)/8]&(1<<(uint(7-(n-1))%8)) != 0
}
func (f *Bitmap) Len() int {
return len(f.data) * 8
}