diff --git a/bitset.go b/bitset.go index 8fb9e9f..58d3983 100644 --- a/bitset.go +++ b/bitset.go @@ -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) diff --git a/bitset_test.go b/bitset_test.go index 3dae32e..eec6721 100644 --- a/bitset_test.go +++ b/bitset_test.go @@ -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) + } +}