fixedbitset icon indicating copy to clipboard operation
fixedbitset copied to clipboard

Implement custom behavior for PartialEq, Eq, PartialOrd, Ord and Hash

Open sitegui opened this issue 5 years ago • 2 comments
trafficstars

Hi,

Following up on https://github.com/petgraph/fixedbitset/issues/44, here is my take on implementing PartialEq, Eq, PartialOrd, Ord and Hash. This PR goes beyond the scope of the issue, so all those related traits would be in sync.

This is a behavior change that, in practice, makes FixedBitSet behave like a BTreeSet<usize>, whose elements are the items returned by FixedBitSet::ones(). The change can be breaking for some pieces of code that depended on the old behavior, even if it was undocumented and arguably surprising.

For example, using FixedBitSet in map will behave differently:

let mut map = HashMap::new();
map.insert(FixedBitSet::with_capacity(0), 17);
map.insert(FixedBitSet::with_capacity(10), 17);
dbg!(map.len()); // before = 2, now = 1

Fix #44

Please tell me what you think, Best regards,

sitegui avatar May 30 '20 17:05 sitegui

This results in a somewhat awkward condition that FixedBitSets with different sizes would compare equal:

    /// Return the length of the `FixedBitSet` in bits.
    #[inline]
    pub fn len(&self) -> usize { self.length }

We could also update the len function to be the number of set bits, instead of just the number of bits.

jrraymond avatar Aug 02 '20 16:08 jrraymond