-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
bit_matrix.go
392 lines (348 loc) · 8.66 KB
/
bit_matrix.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package gozxing
import (
"math/bits"
"strings"
errors "golang.org/x/xerrors"
)
type BitMatrix struct {
width int
height int
rowSize int
bits []uint32
}
func NewSquareBitMatrix(dimension int) (*BitMatrix, error) {
return NewBitMatrix(dimension, dimension)
}
func NewBitMatrix(width, height int) (*BitMatrix, error) {
if width < 1 || height < 1 {
return nil, errors.New("IllegalArgumentException: Both dimensions must be greater than 0")
}
rowSize := (width + 31) / 32
bits := make([]uint32, rowSize*height)
return &BitMatrix{width, height, rowSize, bits}, nil
}
func ParseBoolMapToBitMatrix(image [][]bool) (*BitMatrix, error) {
var width, height int
height = len(image)
if height > 0 {
width = len(image[0])
}
bits, e := NewBitMatrix(width, height)
if e != nil {
return nil, e
}
for i := 0; i < height; i++ {
imageI := image[i]
for j := 0; j < width; j++ {
if imageI[j] {
bits.Set(j, i)
}
}
}
return bits, nil
}
func ParseStringToBitMatrix(stringRepresentation, setString, unsetString string) (*BitMatrix, error) {
if stringRepresentation == "" {
return nil, errors.New("IllegalArgumentException")
}
bits := make([]bool, len(stringRepresentation))
bitsPos := 0
rowStartPos := 0
rowLength := -1
nRows := 0
pos := 0
for pos < len(stringRepresentation) {
if c := stringRepresentation[pos]; c == '\n' || c == '\r' {
if bitsPos > rowStartPos {
if rowLength == -1 {
rowLength = bitsPos - rowStartPos
} else if bitsPos-rowStartPos != rowLength {
return nil, errors.New("IllegalArgumentException: row length do not match")
}
rowStartPos = bitsPos
nRows++
}
pos++
} else if strings.HasPrefix(stringRepresentation[pos:], setString) {
pos += len(setString)
bits[bitsPos] = true
bitsPos++
} else if strings.HasPrefix(stringRepresentation[pos:], unsetString) {
pos += len(unsetString)
bits[bitsPos] = false
bitsPos++
} else {
return nil, errors.New(
"IllegalArgumentException: illegal character encountered: " + stringRepresentation[pos:])
}
}
if bitsPos > rowStartPos {
if rowLength == -1 {
rowLength = bitsPos - rowStartPos
} else if bitsPos-rowStartPos != rowLength {
return nil, errors.New("IllegalArgumentException: row length do not match")
}
nRows++
}
matrix, e := NewBitMatrix(rowLength, nRows)
if e != nil {
return nil, e
}
for i := 0; i < bitsPos; i++ {
if bits[i] {
matrix.Set(i%rowLength, i/rowLength)
}
}
return matrix, nil
}
func (b *BitMatrix) Get(x, y int) bool {
if x < 0 || x >= b.width || y < 0 || y >= b.height {
return false
}
offset := (y * b.rowSize) + (x / 32)
return ((b.bits[offset] >> uint(x%32)) & 1) != 0
}
func (b *BitMatrix) Set(x, y int) {
offset := (y * b.rowSize) + (x / 32)
b.bits[offset] |= 1 << uint(x%32)
}
func (b *BitMatrix) Unset(x, y int) {
offset := (y * b.rowSize) + (x / 32)
b.bits[offset] &= ^(1 << uint(x%32))
}
func (b *BitMatrix) Flip(x, y int) {
offset := (y * b.rowSize) + (x / 32)
b.bits[offset] ^= 1 << uint(x%32)
}
func (b *BitMatrix) FlipAll() {
max := len(b.bits)
for i := 0; i < max; i++ {
b.bits[i] = ^b.bits[i]
}
}
func (b *BitMatrix) Xor(mask *BitMatrix) error {
if b.width != mask.width || b.height != mask.height || b.rowSize != mask.rowSize {
return errors.New("IllegalArgumentException: input matrix dimensions do not match")
}
for y := 0; y < b.height; y++ {
bOffset := y * b.rowSize
mOffset := y * mask.rowSize
for x := 0; x < b.rowSize; x++ {
b.bits[bOffset+x] ^= mask.bits[mOffset+x]
}
}
return nil
}
func (b *BitMatrix) Clear() {
max := len(b.bits)
for i := 0; i < max; i++ {
b.bits[i] = 0
}
}
func (b *BitMatrix) SetRegion(left, top, width, height int) error {
if top < 0 || left < 0 {
return errors.New("IllegalArgumentException: Left and top must be nonnegative")
}
if height < 1 || width < 1 {
return errors.New("IllegalArgumentException: Height and width must be at least 1")
}
right := left + width
bottom := top + height
if bottom > b.height || right > b.width {
return errors.New("IllegalArgumentException: The region must fit inside the matrix")
}
for y := top; y < bottom; y++ {
offset := y * b.rowSize
for x := left; x < right; x++ {
b.bits[offset+(x/32)] |= 1 << uint(x%32)
}
}
return nil
}
func (b *BitMatrix) GetRow(y int, row *BitArray) *BitArray {
if row == nil || row.GetSize() < b.width {
row = NewBitArray(b.width)
} else {
row.Clear()
}
offset := y * b.rowSize
for x := 0; x < b.rowSize; x++ {
row.SetBulk(x*32, b.bits[offset+x])
}
return row
}
func (b *BitMatrix) SetRow(y int, row *BitArray) {
offset := y * b.rowSize
copy(b.bits[offset:offset+b.rowSize], row.bits)
}
func (b *BitMatrix) Rotate180() {
height := b.height
rowSize := b.rowSize
for i := 0; i < height/2; i++ {
topOffset := i * rowSize
bottomOffset := (height-i)*rowSize - 1
for j := 0; j < rowSize; j++ {
top := topOffset + j
bottom := bottomOffset - j
b.bits[top], b.bits[bottom] = b.bits[bottom], b.bits[top]
}
}
if height%2 != 0 {
offset := rowSize * (height - 1) / 2
for j := 0; j < rowSize/2; j++ {
left := offset + j
right := offset + rowSize - 1 - j
b.bits[left], b.bits[right] = b.bits[right], b.bits[left]
}
}
if shift := uint(b.width % 32); shift != 0 {
for i := 0; i < height; i++ {
offset := rowSize * i
b.bits[offset] = bits.Reverse32(b.bits[offset]) >> uint(32-shift)
for j := 1; j < rowSize; j++ {
curbits := bits.Reverse32(b.bits[offset+j])
b.bits[offset+j-1] |= curbits << shift
b.bits[offset+j] = curbits >> uint(32-shift)
}
}
}
}
func (b *BitMatrix) Rotate90() {
newWidth := b.height
newHeight := b.width
newRowSize := (newWidth + 31) / 32
newBits := make([]uint32, newRowSize*newHeight)
for y := 0; y < b.height; y++ {
for x := 0; x < b.width; x++ {
offset := y*b.rowSize + (x / 32)
if ((b.bits[offset] >> (x & 0x1f)) & 1) != 0 {
newOffset := (newHeight-1-x)*newRowSize + (y / 32)
newBits[newOffset] |= 1 << (y & 0x1f)
}
}
}
b.width = newWidth
b.height = newHeight
b.rowSize = newRowSize
b.bits = newBits
}
func (b *BitMatrix) GetEnclosingRectangle() []int {
left := b.width
top := b.height
right := -1
bottom := -1
for y := 0; y < b.height; y++ {
for x32 := 0; x32 < b.rowSize; x32++ {
theBits := b.bits[y*b.rowSize+x32]
if theBits != 0 {
if y < top {
top = y
}
if y > bottom {
bottom = y
}
if x32*32 < left {
bit := 0
for (theBits << uint(31-bit)) == 0 {
bit++
}
if (x32*32 + bit) < left {
left = x32*32 + bit
}
}
if x32*32+31 > right {
bit := 31
for (theBits >> uint(bit)) == 0 {
bit--
}
if (x32*32 + bit) > right {
right = x32*32 + bit
}
}
}
}
}
if right < left || bottom < top {
return nil
}
return []int{left, top, right - left + 1, bottom - top + 1}
}
func (b *BitMatrix) GetTopLeftOnBit() []int {
bitsOffset := 0
for bitsOffset < len(b.bits) && b.bits[bitsOffset] == 0 {
bitsOffset++
}
if bitsOffset == len(b.bits) {
return nil
}
y := bitsOffset / b.rowSize
x := (bitsOffset % b.rowSize) * 32
theBits := b.bits[bitsOffset]
bit := uint(0)
for (theBits << (31 - bit)) == 0 {
bit++
}
x += int(bit)
return []int{x, y}
}
func (b *BitMatrix) GetBottomRightOnBit() []int {
bitsOffset := len(b.bits) - 1
for bitsOffset >= 0 && b.bits[bitsOffset] == 0 {
bitsOffset--
}
if bitsOffset < 0 {
return nil
}
y := bitsOffset / b.rowSize
x := (bitsOffset % b.rowSize) * 32
theBits := b.bits[bitsOffset]
bit := uint(31)
for (theBits >> bit) == 0 {
bit--
}
x += int(bit)
return []int{x, y}
}
func (b *BitMatrix) GetWidth() int {
return b.width
}
func (b *BitMatrix) GetHeight() int {
return b.height
}
func (b *BitMatrix) GetRowSize() int {
return b.rowSize
}
// public boolean equals(Object o)
// public int hashCode()
func (b *BitMatrix) String() string {
return b.ToString("X ", " ")
}
func (b *BitMatrix) ToString(setString, unsetString string) string {
return b.ToStringWithLineSeparator(setString, unsetString, "\n")
}
func (b *BitMatrix) ToStringWithLineSeparator(setString, unsetString, lineSeparator string) string {
setBytes := []byte(setString)
unsetBytes := []byte(unsetString)
lineSepBytes := []byte(lineSeparator)
lineSize := len(lineSeparator)
if len(setString) > len(unsetString) {
lineSize += b.width * len(setString)
} else {
lineSize += b.width * len(unsetString)
}
result := make([]byte, 0, b.height*lineSize)
for y := 0; y < b.height; y++ {
for x := 0; x < b.width; x++ {
var s []byte
if b.Get(x, y) {
s = setBytes
} else {
s = unsetBytes
}
result = append(result, s...)
}
result = append(result, lineSepBytes...)
}
return string(result)
}
// public BitMatrix clone()