Skip to content

Commit

Permalink
Make BitArray.Count return an int
Browse files Browse the repository at this point in the history
  • Loading branch information
danielway-wk committed May 17, 2023
1 parent e309fb3 commit 9c88543
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions bitarray/bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ func (ba *bitArray) ClearBit(k uint64) error {
}

// Count returns the number of set bits in this array.
func (ba *bitArray) Count() uint64 {
func (ba *bitArray) Count() int {
count := 0
for _, block := range ba.blocks {
count += bits.OnesCount64(uint64(block))
}
return uint64(count)
return count
}

// Or will bitwise or two bit arrays and return a new bit array
Expand Down
10 changes: 5 additions & 5 deletions bitarray/bitarray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,24 @@ func TestIsEmpty(t *testing.T) {

func TestCount(t *testing.T) {
ba := newBitArray(500)
assert.Equal(t, uint64(0), ba.Count())
assert.Equal(t, 0, ba.Count())

require.NoError(t, ba.SetBit(0))
assert.Equal(t, uint64(1), ba.Count())
assert.Equal(t, 1, ba.Count())

require.NoError(t, ba.SetBit(40))
require.NoError(t, ba.SetBit(64))
require.NoError(t, ba.SetBit(100))
require.NoError(t, ba.SetBit(200))
require.NoError(t, ba.SetBit(469))
require.NoError(t, ba.SetBit(500))
assert.Equal(t, uint64(7), ba.Count())
assert.Equal(t, 7, ba.Count())

require.NoError(t, ba.ClearBit(200))
assert.Equal(t, uint64(6), ba.Count())
assert.Equal(t, 6, ba.Count())

ba.Reset()
assert.Equal(t, uint64(0), ba.Count())
assert.Equal(t, 0, ba.Count())
}

func TestClear(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion bitarray/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type BitArray interface {
// seen capacity of the sparse array.
Capacity() uint64
// Count returns the number of set bits in this array.
Count() uint64
Count() int
// Or will bitwise or the two bitarrays and return a new bitarray
// representing the result.
Or(other BitArray) BitArray
Expand Down
4 changes: 2 additions & 2 deletions bitarray/sparse_bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,12 @@ func (sba *sparseBitArray) Equals(other BitArray) bool {
}

// Count returns the number of set bits in this array.
func (sba *sparseBitArray) Count() uint64 {
func (sba *sparseBitArray) Count() int {
count := 0
for _, block := range sba.blocks {
count += bits.OnesCount64(uint64(block))
}
return uint64(count)
return count
}

// Or will perform a bitwise or operation with the provided bitarray and
Expand Down
10 changes: 5 additions & 5 deletions bitarray/sparse_bitarray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,24 @@ func BenchmarkGetSetCompressedBits(b *testing.B) {

func TestCompressedCount(t *testing.T) {
ba := newSparseBitArray()
assert.Equal(t, uint64(0), ba.Count())
assert.Equal(t, 0, ba.Count())

require.NoError(t, ba.SetBit(0))
assert.Equal(t, uint64(1), ba.Count())
assert.Equal(t, 1, ba.Count())

require.NoError(t, ba.SetBit(40))
require.NoError(t, ba.SetBit(64))
require.NoError(t, ba.SetBit(100))
require.NoError(t, ba.SetBit(200))
require.NoError(t, ba.SetBit(469))
require.NoError(t, ba.SetBit(500))
assert.Equal(t, uint64(7), ba.Count())
assert.Equal(t, 7, ba.Count())

require.NoError(t, ba.ClearBit(200))
assert.Equal(t, uint64(6), ba.Count())
assert.Equal(t, 6, ba.Count())

ba.Reset()
assert.Equal(t, uint64(0), ba.Count())
assert.Equal(t, 0, ba.Count())
}

func TestClearCompressedBit(t *testing.T) {
Expand Down

0 comments on commit 9c88543

Please sign in to comment.