Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Allow creation of bitset with 0 size (#116)
Browse files Browse the repository at this point in the history
* allow creation of bitset with 0 size

Signed-off-by: Daniel Rammer <daniel@union.ai>

* removed unecessary math import

Signed-off-by: Daniel Rammer <daniel@union.ai>

* added unit test and fixed lint issue

Signed-off-by: Daniel Rammer <daniel@union.ai>
  • Loading branch information
hamersaw committed Feb 23, 2022
1 parent 9f812e9 commit d304944
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions bitarray/bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ func (s BitSet) DeepCopy() BitSet {

// Initializes a new BitSet of the specified size.
func NewBitSet(desiredCap uint) *BitSet {
// Create enough blocks to contain the number of intended bits.
a := make(BitSet, ((desiredCap-1)/blockSize)+1)
size := desiredCap / blockSize
if desiredCap%blockSize != 0 {
size++
}
a := make(BitSet, size)
return &a
}
8 changes: 8 additions & 0 deletions bitarray/bitset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func TestBitSet_Set(t *testing.T) {
}

func TestNewBitSet(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
b := new(BitSet)
assert.Equal(t, 0, b.BlockCount())

b = NewBitSet(0)
assert.Equal(t, 0, b.BlockCount())
})

t.Run("Block size", func(t *testing.T) {
b := NewBitSet(63)
assert.Equal(t, 2, b.BlockCount())
Expand Down

0 comments on commit d304944

Please sign in to comment.