Repository navigation
Mask trailing bits in with_capacity_and_blocks - #151
Open
amalsaidov wants to merge 1 commit into
Open
amalsaidov wants to merge 1 commit into
amalsaidov wants to merge 1 commit into
Conversation
with_capacity_and_blocks stored the provided blocks verbatim, including
any bits set beyond the requested capacity in the final block. Every
other operation upholds the invariant that bits at index >= length are
unset (contains/count_ones mask them off, and the existing tests assert
they are not part of the set), but PartialEq, Hash and Ord compare the
raw blocks. As a result two logically-equal bitsets could compare
unequal and hash differently, e.g.:
let mut a = FixedBitSet::with_capacity(10);
a.insert_range(..);
let b = FixedBitSet::with_capacity_and_blocks(10, repeat(!0));
assert_eq!(a, b); // failed: a != b
Clear the out-of-range bits in the final block so the constructed
bitset upholds the invariant.
Fixes petgraph#139
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
with_capacity_and_blocksstores the provided blocks verbatim, including any bits set beyond the requested capacity in the final block.Every other part of the crate upholds the invariant that bits at index
>= lengthare unset:contains/count_onesmask them off, and the existingwith_blocks_too_big*tests explicitly assert that such bits are not part of the set. ButPartialEq,HashandOrdcompare the raw blocks (as_simd_slice), so those stray bits leak through and two logically-equal bitsets can compare unequal and hash differently:This also means such a value hashes differently from its logical equal, so it misbehaves as a key in hash-based collections.
Fix
Clear the out-of-range bits in the final block at the end of
with_capacity_and_blocks, so the constructed bitset upholds the same invariant as every other constructor/operation. The mask touches only the last used block and leaves well-formed inputs (trailing bits already zero) unchanged — including the documented example and all existingwith_blocks_*tests.Test
Added
with_blocks_too_big_eq_and_hash, which asserts that a bitset built viainsert_rangeand one built viawith_capacity_and_blockswith the same length and in-range bits are equal, hash equally, order equally, and expose identical blocks. It fails before the change and passes after. Full suite (cargo test, incl. the quickcheck tests) passes.Fixes #139