forked from celestiaorg/rsmt2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_matrix.go
98 lines (83 loc) · 1.87 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
package rsmt2d
import "fmt"
type bitMatrix struct {
mask []uint64
squareSize int
}
func newBitMatrix(squareSize int) bitMatrix {
bits := squareSize * squareSize
return bitMatrix{mask: make([]uint64, (bits+63)/64), squareSize: squareSize}
}
// idx = rowIndex*squareSize+colIdx
func (bm bitMatrix) SetFlat(idx int) {
bm.mask[idx/64] |= uint64(1) << uint(idx%64)
}
func (bm bitMatrix) Get(row, col int) bool {
assertValidIndices(row, col, bm.squareSize)
idx := row*bm.squareSize + col
return bm.mask[idx/64]&(uint64(1)<<uint(idx%64)) > 0
}
func (bm *bitMatrix) Set(row, col int) {
assertValidIndices(row, col, bm.squareSize)
idx := row*bm.squareSize + col
bm.mask[idx/64] |= uint64(1) << uint(idx%64)
}
func (bm bitMatrix) ColIsOne(c int) bool {
for r := 0; r < bm.squareSize; r++ {
if !bm.Get(r, c) {
return false
}
}
return true
}
func (bm bitMatrix) RowIsOne(r int) bool {
for c := 0; c < bm.squareSize; c++ {
if !bm.Get(r, c) {
return false
}
}
return true
}
func (bm bitMatrix) NumOnesInRow(r int) int {
var counter int
for c := 0; c < bm.squareSize; c++ {
if bm.Get(r, c) {
counter++
}
}
return counter
}
func (bm bitMatrix) NumOnesInCol(c int) int {
var counter int
for r := 0; r < bm.squareSize; r++ {
if bm.Get(r, c) {
counter++
}
}
return counter
}
func (bm bitMatrix) RowRangeIsOne(r, start, end int) bool {
for c := start; c <= end && c < bm.squareSize; c++ {
if !bm.Get(r, c) {
return false
}
}
return true
}
func (bm bitMatrix) ColRangeIsOne(c, start, end int) bool {
for r := start; r <= end && r < bm.squareSize; r++ {
if !bm.Get(r, c) {
return false
}
}
return true
}
func assertValidIndices(row, col, squareSize int) {
if row >= squareSize || col >= squareSize {
panic(fmt.Sprintf(
"want: row < squareSize && col < squareSize, got: %[1]v >= %[3]v || %[2]v >= %[3]v",
row,
col,
squareSize))
}
}