Skip to content

Conversation

@hniksic
Copy link

@hniksic hniksic commented Jan 15, 2026

Fixes #101

This PR includes:

  • the new FixedBitset::ones_with_capacity() constructor
  • optimization to with_capacity_and_blocks() to avoid double initialization
  • minor warning fixes

Compared to the last version in #101, this implementation of with_capacity_and_blocks() makes sure to mask off unused bits in the last block to ensure == and Hash work correctly.

Use Vec::with_capacity() and pointer writes instead of allocating
zeroed memory and overwriting it. This enables efficient construction
of bitsets from iterators like repeat(!0) without the overhead of
first zeroing the allocation.

Mask off unused bits in the last block to ensure consistent
behavior with PartialEq and Hash.
…pacity

Avoid changing with_capacity_and_blocks behavior by having
ones_with_capacity generate correctly masked block values directly.
Clarify in docs that bits beyond capacity are stored as provided.
@hniksic
Copy link
Author

hniksic commented Jan 16, 2026

I've now noticed that this PR, as originally submitted, subtly changes the behavior of with_capacity_and_blocks(): it masks off bits in the last block that exceed the capacity. The original implementation stored those bits as provided by the caller. While one could argue this is a bug fix (the docs say "truncated to the capacity"), it's also a change to a public API that existing code might rely on.

To avoid this, I've added a commit that moves the masking logic into ones_with_capacity() itself, which generates the correctly masked last block value directly rather than relying on with_capacity_and_blocks() to clean it up. I've also updated docs of with_capacity_and_blocks() to clarify current behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Efficient creation of all-ones bitset

1 participant