11package bits
22
33import (
4- "encoding/binary"
54 "errors"
65 "fmt"
76 "math"
@@ -139,7 +138,7 @@ func (bA *BitArray) Or(o *BitArray) *BitArray {
139138 o .mtx .Lock ()
140139 c := bA .copyBits (tmmath .MaxInt (bA .Bits , o .Bits ))
141140 smaller := tmmath .MinInt (len (bA .Elems ), len (o .Elems ))
142- for i := 0 ; i < smaller ; i ++ {
141+ for i := range smaller {
143142 c .Elems [i ] |= o .Elems [i ]
144143 }
145144 bA .mtx .Unlock ()
@@ -207,7 +206,7 @@ func (bA *BitArray) Sub(o *BitArray) *BitArray {
207206 // If bA is longer, then skipping those iterations is equivalent
208207 // to right padding with 0's
209208 smaller := tmmath .MinInt (len (bA .Elems ), len (o .Elems ))
210- for i := 0 ; i < smaller ; i ++ {
209+ for i := range smaller {
211210 // &^ is and not in golang
212211 c .Elems [i ] &^= o .Elems [i ]
213212 }
@@ -216,42 +215,6 @@ func (bA *BitArray) Sub(o *BitArray) *BitArray {
216215 return c
217216}
218217
219- // IsEmpty returns true iff all bits in the bit array are 0
220- func (bA * BitArray ) IsEmpty () bool {
221- if bA == nil {
222- return true // should this be opposite?
223- }
224- bA .mtx .Lock ()
225- defer bA .mtx .Unlock ()
226- for _ , e := range bA .Elems {
227- if e > 0 {
228- return false
229- }
230- }
231- return true
232- }
233-
234- // IsFull returns true iff all bits in the bit array are 1.
235- func (bA * BitArray ) IsFull () bool {
236- if bA == nil {
237- return true
238- }
239- bA .mtx .Lock ()
240- defer bA .mtx .Unlock ()
241-
242- // Check all elements except the last
243- for _ , elem := range bA .Elems [:len (bA .Elems )- 1 ] {
244- if (^ elem ) != 0 {
245- return false
246- }
247- }
248-
249- // Check that the last element has (lastElemBits) 1's
250- lastElemBits := (bA .Bits + 63 )% 64 + 1
251- lastElem := bA .Elems [len (bA .Elems )- 1 ]
252- return (lastElem + 1 )& ((uint64 (1 )<< uint (lastElemBits ))- 1 ) == 0
253- }
254-
255218// PickRandom returns a random index for a set bit in the bit array.
256219// If there is no such value, it returns 0, false.
257220// It uses math/rand's global randomness Source to get this index.
@@ -278,6 +241,9 @@ func (bA *BitArray) PickRandom() (int, bool) {
278241}
279242
280243func (bA * BitArray ) getTrueIndices () []int {
244+ if bA .Size () == 0 {
245+ return nil
246+ }
281247 trueIndices := make ([]int , 0 , bA .Bits )
282248 curBit := 0
283249 numElems := len (bA .Elems )
@@ -288,7 +254,7 @@ func (bA *BitArray) getTrueIndices() []int {
288254 curBit += 64
289255 continue
290256 }
291- for j := 0 ; j < 64 ; j ++ {
257+ for j := range 64 {
292258 if (elem & (uint64 (1 ) << uint64 (j ))) > 0 {
293259 trueIndices = append (trueIndices , curBit )
294260 }
@@ -298,7 +264,7 @@ func (bA *BitArray) getTrueIndices() []int {
298264 // handle last element
299265 lastElem := bA .Elems [numElems - 1 ]
300266 numFinalBits := bA .Bits - curBit
301- for i := 0 ; i < numFinalBits ; i ++ {
267+ for i := range numFinalBits {
302268 if (lastElem & (uint64 (1 ) << uint64 (i ))) > 0 {
303269 trueIndices = append (trueIndices , curBit )
304270 }
@@ -354,21 +320,6 @@ func (bA *BitArray) stringIndented(indent string) string {
354320 return fmt .Sprintf ("BA{%v:%v}" , bA .Bits , strings .Join (lines , indent ))
355321}
356322
357- // Bytes returns the byte representation of the bits within the bitarray.
358- func (bA * BitArray ) Bytes () []byte {
359- bA .mtx .Lock ()
360- defer bA .mtx .Unlock ()
361-
362- numBytes := (bA .Bits + 7 ) / 8
363- bytes := make ([]byte , numBytes )
364- for i := 0 ; i < len (bA .Elems ); i ++ {
365- elemBytes := [8 ]byte {}
366- binary .LittleEndian .PutUint64 (elemBytes [:], bA .Elems [i ])
367- copy (bytes [i * 8 :], elemBytes [:])
368- }
369- return bytes
370- }
371-
372323// Update sets the bA's bits to be that of the other bit array.
373324// The copying begins from the begin of both bit arrays.
374325func (bA * BitArray ) Update (o * BitArray ) {
@@ -427,7 +378,7 @@ func (bA *BitArray) UnmarshalJSON(bz []byte) error {
427378 numBits := len (bits )
428379
429380 bA .reset (numBits )
430- for i := 0 ; i < numBits ; i ++ {
381+ for i := range numBits {
431382 if bits [i ] == 'x' {
432383 bA .SetIndex (i , true )
433384 }
0 commit comments