This commit is contained in:
Jae Kwon
2015-06-25 20:28:34 -07:00
parent a55950e1d0
commit 9965dd5de6
75 changed files with 556 additions and 522 deletions

View File

@ -9,12 +9,12 @@ import (
type BitArray struct {
mtx sync.Mutex
Bits uint `json:"bits"` // NOTE: persisted via reflect, must be exported
Bits int `json:"bits"` // NOTE: persisted via reflect, must be exported
Elems []uint64 `json:"elems"` // NOTE: persisted via reflect, must be exported
}
// There is no BitArray whose Size is 0. Use nil instead.
func NewBitArray(bits uint) *BitArray {
func NewBitArray(bits int) *BitArray {
if bits == 0 {
return nil
}
@ -24,7 +24,7 @@ func NewBitArray(bits uint) *BitArray {
}
}
func (bA *BitArray) Size() uint {
func (bA *BitArray) Size() int {
if bA == nil {
return 0
}
@ -32,7 +32,7 @@ func (bA *BitArray) Size() uint {
}
// NOTE: behavior is undefined if i >= bA.Bits
func (bA *BitArray) GetIndex(i uint) bool {
func (bA *BitArray) GetIndex(i int) bool {
if bA == nil {
return false
}
@ -41,15 +41,15 @@ func (bA *BitArray) GetIndex(i uint) bool {
return bA.getIndex(i)
}
func (bA *BitArray) getIndex(i uint) bool {
func (bA *BitArray) getIndex(i int) bool {
if i >= bA.Bits {
return false
}
return bA.Elems[i/64]&(uint64(1)<<(i%64)) > 0
return bA.Elems[i/64]&(uint64(1)<<uint(i%64)) > 0
}
// NOTE: behavior is undefined if i >= bA.Bits
func (bA *BitArray) SetIndex(i uint, v bool) bool {
func (bA *BitArray) SetIndex(i int, v bool) bool {
if bA == nil {
return false
}
@ -58,14 +58,14 @@ func (bA *BitArray) SetIndex(i uint, v bool) bool {
return bA.setIndex(i, v)
}
func (bA *BitArray) setIndex(i uint, v bool) bool {
func (bA *BitArray) setIndex(i int, v bool) bool {
if i >= bA.Bits {
return false
}
if v {
bA.Elems[i/64] |= (uint64(1) << (i % 64))
bA.Elems[i/64] |= (uint64(1) << uint(i%64))
} else {
bA.Elems[i/64] &= ^(uint64(1) << (i % 64))
bA.Elems[i/64] &= ^(uint64(1) << uint(i%64))
}
return true
}
@ -88,7 +88,7 @@ func (bA *BitArray) copy() *BitArray {
}
}
func (bA *BitArray) copyBits(bits uint) *BitArray {
func (bA *BitArray) copyBits(bits int) *BitArray {
c := make([]uint64, (bits+63)/64)
copy(c, bA.Elems)
return &BitArray{
@ -104,7 +104,7 @@ func (bA *BitArray) Or(o *BitArray) *BitArray {
}
bA.mtx.Lock()
defer bA.mtx.Unlock()
c := bA.copyBits(MaxUint(bA.Bits, o.Bits))
c := bA.copyBits(MaxInt(bA.Bits, o.Bits))
for i := 0; i < len(c.Elems); i++ {
c.Elems[i] |= o.Elems[i]
}
@ -122,7 +122,7 @@ func (bA *BitArray) And(o *BitArray) *BitArray {
}
func (bA *BitArray) and(o *BitArray) *BitArray {
c := bA.copyBits(MinUint(bA.Bits, o.Bits))
c := bA.copyBits(MinInt(bA.Bits, o.Bits))
for i := 0; i < len(c.Elems); i++ {
c.Elems[i] &= o.Elems[i]
}
@ -153,7 +153,7 @@ func (bA *BitArray) Sub(o *BitArray) *BitArray {
for i := 0; i < len(o.Elems)-1; i++ {
c.Elems[i] &= ^c.Elems[i]
}
i := uint(len(o.Elems) - 1)
i := len(o.Elems) - 1
if i >= 0 {
for idx := i * 64; idx < o.Bits; idx++ {
c.setIndex(idx, c.getIndex(idx) && !o.GetIndex(idx))
@ -182,10 +182,10 @@ func (bA *BitArray) IsFull() bool {
// Check that the last element has (lastElemBits) 1's
lastElemBits := (bA.Bits+63)%64 + 1
lastElem := bA.Elems[len(bA.Elems)-1]
return (lastElem+1)&((uint64(1)<<lastElemBits)-1) == 0
return (lastElem+1)&((uint64(1)<<uint(lastElemBits))-1) == 0
}
func (bA *BitArray) PickRandom() (uint, bool) {
func (bA *BitArray) PickRandom() (int, bool) {
if bA == nil {
return 0, false
}
@ -205,14 +205,14 @@ func (bA *BitArray) PickRandom() (uint, bool) {
for j := 0; j < 64; j++ {
bitIdx := ((j + randBitStart) % 64)
if (bA.Elems[elemIdx] & (uint64(1) << uint(bitIdx))) > 0 {
return 64*uint(elemIdx) + uint(bitIdx), true
return 64*elemIdx + bitIdx, true
}
}
panic("should not happen")
}
} else {
// Special case for last elem, to ignore straggler bits
elemBits := int(bA.Bits) % 64
elemBits := bA.Bits % 64
if elemBits == 0 {
elemBits = 64
}
@ -220,7 +220,7 @@ func (bA *BitArray) PickRandom() (uint, bool) {
for j := 0; j < elemBits; j++ {
bitIdx := ((j + randBitStart) % elemBits)
if (bA.Elems[elemIdx] & (uint64(1) << uint(bitIdx))) > 0 {
return 64*uint(elemIdx) + uint(bitIdx), true
return 64*elemIdx + bitIdx, true
}
}
}
@ -250,7 +250,7 @@ func (bA *BitArray) stringIndented(indent string) string {
lines := []string{}
bits := ""
for i := uint(0); i < bA.Bits; i++ {
for i := 0; i < bA.Bits; i++ {
if bA.getIndex(i) {
bits += "X"
} else {