Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SetAll method #151

Merged
merged 1 commit into from Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions bitset.go
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
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)
}
}