IsFull returns true iff all bits in the bit array are 1.
()
| 219 | |
| 220 | // IsFull returns true iff all bits in the bit array are 1. |
| 221 | func (bA *BitArray) IsFull() bool { |
| 222 | if bA == nil { |
| 223 | return true |
| 224 | } |
| 225 | bA.mtx.Lock() |
| 226 | defer bA.mtx.Unlock() |
| 227 | |
| 228 | // Check all elements except the last |
| 229 | for _, elem := range bA.Elems[:len(bA.Elems)-1] { |
| 230 | if (^elem) != 0 { |
| 231 | return false |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Check that the last element has (lastElemBits) 1's |
| 236 | lastElemBits := (bA.Bits+63)%64 + 1 |
| 237 | lastElem := bA.Elems[len(bA.Elems)-1] |
| 238 | return (lastElem+1)&((uint64(1)<<uint(lastElemBits))-1) == 0 |
| 239 | } |
| 240 | |
| 241 | // PickRandom returns a random index for a set bit in the bit array. |
| 242 | // If there is no such value, it returns 0, false. |