Skip to content

Commit

Permalink
Add SetAll method
Browse files Browse the repository at this point in the history
  • Loading branch information
nemars committed Dec 16, 2023
1 parent fcbcb64 commit 502896d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,18 @@ func (b *BitSet) ClearAll() *BitSet {
return b
}

// SetAll sets the entire BitSet
func (b *BitSet) SetAll() *BitSet {
if b != nil && b.set != nil {
for i := range b.set {
b.set[i] = allBits
}

b.cleanLastWord()
}
return b
}

// wordCount returns the number of words used in a bit set
func (b *BitSet) wordCount() int {
return wordsNeededUnbound(b.length)
Expand Down
16 changes: 16 additions & 0 deletions bitset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1910,3 +1910,19 @@ func TestReadFrom(t *testing.T) {
})
}
}

func TestSetAll(t *testing.T) {
test := func(name string, bs *BitSet, want uint) {
t.Run(name, func(t *testing.T) {
bs.SetAll()
if bs.Count() != want {
t.Errorf("expected %d bits to be set, got %d", want, bs.Count())
}
})
}

test("nil", nil, 0)
for _, length := range []uint{0, 1, 10, 63, 64, 65, 100, 640} {
test(fmt.Sprintf("length %d", length), New(length), length)
}
}

0 comments on commit 502896d

Please sign in to comment.