4
4
5
5
package gc
6
6
7
- import "fmt"
8
-
9
7
const (
10
8
WORDBITS = 32
11
9
WORDMASK = WORDBITS - 1
@@ -44,14 +42,7 @@ func (b *bulkBvec) next() bvec {
44
42
return out
45
43
}
46
44
47
- // difference
48
- func bvandnot (dst bvec , src1 bvec , src2 bvec ) {
49
- for i , x := range src1 .b {
50
- dst .b [i ] = x &^ src2 .b [i ]
51
- }
52
- }
53
-
54
- func bveq (bv1 bvec , bv2 bvec ) bool {
45
+ func (bv1 bvec ) Eq (bv2 bvec ) bool {
55
46
if bv1 .n != bv2 .n {
56
47
Fatalf ("bvequal: lengths %d and %d are not equal" , bv1 .n , bv2 .n )
57
48
}
@@ -63,22 +54,31 @@ func bveq(bv1 bvec, bv2 bvec) bool {
63
54
return true
64
55
}
65
56
66
- func bvcopy (dst bvec , src bvec ) {
57
+ func (dst bvec ) Copy ( src bvec ) {
67
58
for i , x := range src .b {
68
59
dst .b [i ] = x
69
60
}
70
61
}
71
62
72
- func bvget (bv bvec , i int32 ) int {
63
+ func (bv bvec ) Get ( i int32 ) bool {
73
64
if i < 0 || i >= bv .n {
74
65
Fatalf ("bvget: index %d is out of bounds with length %d\n " , i , bv .n )
75
66
}
76
- return int ((bv .b [i >> WORDSHIFT ] >> uint (i & WORDMASK )) & 1 )
67
+ mask := uint32 (1 << uint (i % WORDBITS ))
68
+ return bv .b [i >> WORDSHIFT ]& mask != 0
69
+ }
70
+
71
+ func (bv bvec ) Set (i int32 ) {
72
+ if i < 0 || i >= bv .n {
73
+ Fatalf ("bvset: index %d is out of bounds with length %d\n " , i , bv .n )
74
+ }
75
+ mask := uint32 (1 << uint (i % WORDBITS ))
76
+ bv .b [i / WORDBITS ] |= mask
77
77
}
78
78
79
79
// bvnext returns the smallest index >= i for which bvget(bv, i) == 1.
80
80
// If there is no such index, bvnext returns -1.
81
- func bvnext (bv bvec , i int32 ) int32 {
81
+ func (bv bvec ) Next ( i int32 ) int32 {
82
82
if i >= bv .n {
83
83
return - 1
84
84
}
@@ -107,7 +107,7 @@ func bvnext(bv bvec, i int32) int32 {
107
107
return i
108
108
}
109
109
110
- func bvisempty (bv bvec ) bool {
110
+ func (bv bvec ) IsEmpty ( ) bool {
111
111
for i := int32 (0 ); i < bv .n ; i += WORDBITS {
112
112
if bv .b [i >> WORDSHIFT ] != 0 {
113
113
return false
@@ -116,7 +116,7 @@ func bvisempty(bv bvec) bool {
116
116
return true
117
117
}
118
118
119
- func bvnot (bv bvec ) {
119
+ func (bv bvec ) Not ( ) {
120
120
i := int32 (0 )
121
121
w := int32 (0 )
122
122
for ; i < bv .n ; i , w = i + WORDBITS , w + 1 {
@@ -125,36 +125,41 @@ func bvnot(bv bvec) {
125
125
}
126
126
127
127
// union
128
- func bvor (dst bvec , src1 bvec , src2 bvec ) {
128
+ func (dst bvec ) Or ( src1 , src2 bvec ) {
129
129
for i , x := range src1 .b {
130
130
dst .b [i ] = x | src2 .b [i ]
131
131
}
132
132
}
133
133
134
134
// intersection
135
- func bvand (dst bvec , src1 bvec , src2 bvec ) {
135
+ func (dst bvec ) And ( src1 , src2 bvec ) {
136
136
for i , x := range src1 .b {
137
137
dst .b [i ] = x & src2 .b [i ]
138
138
}
139
139
}
140
140
141
- func bvprint ( bv bvec ) {
142
- fmt . Printf ( "#*" )
143
- for i := int32 ( 0 ); i < bv . n ; i ++ {
144
- fmt . Printf ( "%d" , bvget ( bv , i ))
141
+ // difference
142
+ func ( dst bvec ) AndNot ( src1 , src2 bvec ) {
143
+ for i , x := range src1 . b {
144
+ dst . b [ i ] = x &^ src2 . b [ i ]
145
145
}
146
146
}
147
147
148
- func bvresetall (bv bvec ) {
149
- for i := range bv .b {
150
- bv .b [i ] = 0
148
+ func (bv bvec ) String () string {
149
+ s := make ([]byte , 2 + bv .n )
150
+ copy (s , "#*" )
151
+ for i := int32 (0 ); i < bv .n ; i ++ {
152
+ ch := byte ('0' )
153
+ if bv .Get (i ) {
154
+ ch = '1'
155
+ }
156
+ s [2 + i ] = ch
151
157
}
158
+ return string (s )
152
159
}
153
160
154
- func bvset (bv bvec , i int32 ) {
155
- if i < 0 || i >= bv .n {
156
- Fatalf ( "bvset: index %d is out of bounds with length %d \n " , i , bv .n )
161
+ func (bv bvec ) Clear ( ) {
162
+ for i := range bv .b {
163
+ bv .b [ i ] = 0
157
164
}
158
- mask := uint32 (1 << uint (i % WORDBITS ))
159
- bv .b [i / WORDBITS ] |= mask
160
165
}
0 commit comments