tendermint/binary/bit_array.go

172 lines
3.5 KiB
Go
Raw Normal View History

2014-12-29 18:09:06 -08:00
package binary
2014-09-14 15:37:32 -07:00
import (
"fmt"
2014-09-14 15:37:32 -07:00
"math/rand"
2014-10-15 20:15:38 -07:00
"strings"
2014-12-29 18:09:06 -08:00
. "github.com/tendermint/tendermint/common"
2014-09-14 15:37:32 -07:00
)
// Not goroutine safe
type BitArray struct {
2014-12-17 01:37:13 -08:00
Bits uint // NOTE: persisted via reflect, must be exported
Elems []uint64 // NOTE: persisted via reflect, must be exported
}
2014-09-14 15:37:32 -07:00
func NewBitArray(bits uint) BitArray {
return BitArray{bits, make([]uint64, (bits+63)/64)}
2014-09-14 15:37:32 -07:00
}
func (bA BitArray) Size() uint {
2014-12-17 01:37:13 -08:00
return bA.Bits
}
func (bA BitArray) IsZero() bool {
2014-12-17 01:37:13 -08:00
return bA.Bits == 0
}
2014-12-17 01:37:13 -08:00
// NOTE: behavior is undefined if i >= bA.Bits
2014-09-14 15:37:32 -07:00
func (bA BitArray) GetIndex(i uint) bool {
2014-12-17 01:37:13 -08:00
if i >= bA.Bits {
2014-10-25 14:27:53 -07:00
return false
}
2014-12-17 01:37:13 -08:00
return bA.Elems[i/64]&uint64(1<<(i%64)) > 0
2014-09-14 15:37:32 -07:00
}
2014-12-17 01:37:13 -08:00
// NOTE: behavior is undefined if i >= bA.Bits
2014-10-25 14:27:53 -07:00
func (bA BitArray) SetIndex(i uint, v bool) bool {
2014-12-17 01:37:13 -08:00
if i >= bA.Bits {
2014-10-25 14:27:53 -07:00
return false
}
2014-09-14 15:37:32 -07:00
if v {
2014-12-17 01:37:13 -08:00
bA.Elems[i/64] |= uint64(1 << (i % 64))
2014-09-14 15:37:32 -07:00
} else {
2014-12-17 01:37:13 -08:00
bA.Elems[i/64] &= ^uint64(1 << (i % 64))
2014-09-14 15:37:32 -07:00
}
2014-10-25 14:27:53 -07:00
return true
2014-09-14 15:37:32 -07:00
}
func (bA BitArray) Copy() BitArray {
2014-12-17 01:37:13 -08:00
c := make([]uint64, len(bA.Elems))
copy(c, bA.Elems)
return BitArray{bA.Bits, c}
}
func (bA BitArray) copyBits(bits uint) BitArray {
c := make([]uint64, (bits+63)/64)
2014-12-17 01:37:13 -08:00
copy(c, bA.Elems)
return BitArray{bits, c}
2014-09-14 15:37:32 -07:00
}
// Returns a BitArray of larger bits size.
2014-09-14 15:37:32 -07:00
func (bA BitArray) Or(o BitArray) BitArray {
2014-12-17 01:37:13 -08:00
c := bA.copyBits(MaxUint(bA.Bits, o.Bits))
for i := 0; i < len(c.Elems); i++ {
c.Elems[i] |= o.Elems[i]
2014-09-14 15:37:32 -07:00
}
return c
}
// Returns a BitArray of smaller bit size.
2014-09-14 15:37:32 -07:00
func (bA BitArray) And(o BitArray) BitArray {
2014-12-17 01:37:13 -08:00
c := bA.copyBits(MinUint(bA.Bits, o.Bits))
for i := 0; i < len(c.Elems); i++ {
c.Elems[i] &= o.Elems[i]
2014-09-14 15:37:32 -07:00
}
return c
}
func (bA BitArray) Not() BitArray {
c := bA.Copy()
2014-12-17 01:37:13 -08:00
for i := 0; i < len(c.Elems); i++ {
c.Elems[i] = ^c.Elems[i]
2014-09-14 15:37:32 -07:00
}
return c
}
func (bA BitArray) Sub(o BitArray) BitArray {
2014-12-17 01:37:13 -08:00
if bA.Bits > o.Bits {
2014-10-25 14:27:53 -07:00
c := bA.Copy()
2014-12-17 01:37:13 -08:00
for i := 0; i < len(o.Elems)-1; i++ {
c.Elems[i] &= ^c.Elems[i]
2014-10-25 14:27:53 -07:00
}
2014-12-17 01:37:13 -08:00
i := uint(len(o.Elems) - 1)
2014-10-25 14:27:53 -07:00
if i >= 0 {
2014-12-17 01:37:13 -08:00
for idx := i * 64; idx < o.Bits; idx++ {
2014-10-25 14:27:53 -07:00
c.SetIndex(idx, c.GetIndex(idx) && !o.GetIndex(idx))
}
}
return c
} else {
return bA.And(o.Not())
}
2014-09-14 15:37:32 -07:00
}
func (bA BitArray) PickRandom() (uint, bool) {
2014-12-17 01:37:13 -08:00
length := len(bA.Elems)
2014-10-25 14:27:53 -07:00
if length == 0 {
return 0, false
}
randElemStart := rand.Intn(length)
for i := 0; i < length; i++ {
elemIdx := ((i + randElemStart) % length)
if elemIdx < length-1 {
2014-12-17 01:37:13 -08:00
if bA.Elems[elemIdx] > 0 {
randBitStart := rand.Intn(64)
for j := 0; j < 64; j++ {
bitIdx := ((j + randBitStart) % 64)
2014-12-17 01:37:13 -08:00
if (bA.Elems[elemIdx] & (1 << uint(bitIdx))) > 0 {
return 64*uint(elemIdx) + uint(bitIdx), true
}
2014-09-14 15:37:32 -07:00
}
panic("should not happen")
2014-09-14 15:37:32 -07:00
}
} else {
// Special case for last elem, to ignore straggler bits
2014-12-17 01:37:13 -08:00
elemBits := int(bA.Bits) % 64
if elemBits == 0 {
elemBits = 64
}
randBitStart := rand.Intn(elemBits)
for j := 0; j < elemBits; j++ {
bitIdx := ((j + randBitStart) % elemBits)
2014-12-17 01:37:13 -08:00
if (bA.Elems[elemIdx] & (1 << uint(bitIdx))) > 0 {
return 64*uint(elemIdx) + uint(bitIdx), true
2014-09-14 15:37:32 -07:00
}
}
}
}
return 0, false
2014-09-14 15:37:32 -07:00
}
2014-10-15 20:15:38 -07:00
func (bA BitArray) String() string {
2014-12-23 01:35:54 -08:00
return bA.StringIndented("")
2014-10-15 20:15:38 -07:00
}
2014-12-23 01:35:54 -08:00
func (bA BitArray) StringIndented(indent string) string {
2014-10-15 20:15:38 -07:00
lines := []string{}
bits := ""
2014-12-17 01:37:13 -08:00
for i := uint(0); i < bA.Bits; i++ {
if bA.GetIndex(i) {
2014-10-15 20:15:38 -07:00
bits += "X"
} else {
bits += "_"
}
if i%100 == 99 {
lines = append(lines, bits)
bits = ""
}
if i%10 == 9 {
bits += " "
}
if i%50 == 49 {
bits += " "
}
}
if len(bits) > 0 {
lines = append(lines, bits)
}
2014-12-17 01:37:13 -08:00
return fmt.Sprintf("BA{%v:%v}", bA.Bits, strings.Join(lines, indent))
2014-10-15 20:15:38 -07:00
}