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

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 91 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, I
use std::cmp::{Ord, Ordering};
use std::iter::{Chain, FromIterator};
pub use range::IndexRange;
use std::hash::{Hash, Hasher};

const BITS: usize = 32;
type Block = u32;
Expand All @@ -50,7 +51,7 @@ fn div_rem(x: usize, d: usize) -> (usize, usize)
///
/// The bit set has a fixed capacity in terms of enabling bits (and the
/// capacity can grow using the `grow` method).
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[derive(Debug, Default)]
pub struct FixedBitSet {
data: Vec<Block>,
/// length in bits
Expand Down Expand Up @@ -770,6 +771,43 @@ impl <'a> BitXorAssign for FixedBitSet
}
}

/// Two `FixedBitSet`s are equal if and only if they have the exact same set of "one" elements
impl PartialEq for FixedBitSet
{
#[inline]
fn eq(&self, other: &Self) -> bool {
self.ones().eq(other.ones())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While conceptually this algorithm for comparison might be good, I'm afraid that it is slow. Comparison should be based on comparing the blocks, I would guess.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense!

I've rewrote the code to extract the trimmed slice of blocks instead of using the iterator of ones.

}
}

impl Eq for FixedBitSet {}

impl PartialOrd for FixedBitSet
{
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for FixedBitSet
{
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.ones().cmp(other.ones())
}
}

impl Hash for FixedBitSet
{
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
for elt in self.ones() {
elt.hash(state);
}
}
}

#[test]
fn it_works() {
const N: usize = 50;
Expand Down Expand Up @@ -1556,3 +1594,55 @@ fn display_trait() {
assert_eq!(format!("{}", fb), "00101000");
assert_eq!(format!("{:#}", fb), "0b00101000");
}

#[test]
fn comparison_and_hash() {
fn with_values(values: &[usize], capacity: usize) -> FixedBitSet {
let mut r = FixedBitSet::with_capacity(capacity);
for &v in values {
r.put(v);
}
r
}

#[derive(Default)]
struct MockHasher {
bytes: Vec<u8>
}
impl Hasher for MockHasher {
fn finish(&self) -> u64 {
unimplemented!()
}
fn write(&mut self, bytes: &[u8]) {
self.bytes.extend_from_slice(bytes);
}
}

fn hash_of<T: Hash>(t: &T) -> Vec<u8> {
let mut s = MockHasher::default();
t.hash(&mut s);
s.bytes
}

let single_7_17 = with_values(&[7, 17], 32);
let single_7_10 = with_values(&[7, 10], 32);
let single_7_20 = with_values(&[7, 20], 32);
let double_7_17 = with_values(&[7, 17], 64);
let double_7_17_37 = with_values(&[7, 17, 37], 64);

// Equality
assert_eq!(single_7_17, double_7_17);
assert_ne!(single_7_17, single_7_10);
assert_ne!(double_7_17, double_7_17_37);

// Comparison
assert_eq!(single_7_17.cmp(&double_7_17), Ordering::Equal);
assert_eq!(single_7_17.cmp(&single_7_10), Ordering::Greater);
assert_eq!(single_7_17.cmp(&single_7_20), Ordering::Less);
assert_eq!(single_7_17.cmp(&double_7_17_37), Ordering::Less);

// Hash
assert_eq!(hash_of(&single_7_17), hash_of(&double_7_17));
assert_ne!(hash_of(&single_7_17), hash_of(&single_7_10));
assert_ne!(hash_of(&double_7_17), hash_of(&double_7_17_37));
}