Skip to content

Commit

Permalink
Add is_subset methods for BitList and BitVector (#8)
Browse files Browse the repository at this point in the history
* Add is_subset methods

* Simplify List::is_subset and add tests

* Handles 0 in the highest byte

* Use set difference to find subset

* fmt
  • Loading branch information
pawanjay176 authored May 26, 2023
1 parent 910be25 commit 3f06d58
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ impl<N: Unsigned + Clone> Bitfield<Variable<N>> {
}
result
}

/// Returns `true` if `self` is a subset of `other` and `false` otherwise.
pub fn is_subset(&self, other: &Self) -> bool {
self.difference(other).is_zero()
}
}

impl<N: Unsigned + Clone> Bitfield<Fixed<N>> {
Expand Down Expand Up @@ -304,6 +309,11 @@ impl<N: Unsigned + Clone> Bitfield<Fixed<N>> {
}
result
}

/// Returns `true` if `self` is a subset of `other` and `false` otherwise.
pub fn is_subset(&self, other: &Self) -> bool {
self.difference(other).is_zero()
}
}

impl<N: Unsigned + Clone> Default for Bitfield<Fixed<N>> {
Expand Down Expand Up @@ -765,6 +775,30 @@ mod bitvector {
assert_eq!(b.intersection(&a), c);
}

#[test]
fn subset() {
let a = BitVector16::from_raw_bytes(smallvec![0b1000, 0b0001], 16).unwrap();
let b = BitVector16::from_raw_bytes(smallvec![0b1100, 0b0001], 16).unwrap();
let c = BitVector16::from_raw_bytes(smallvec![0b1100, 0b1001], 16).unwrap();

assert_eq!(a.len(), 16);
assert_eq!(b.len(), 16);
assert_eq!(c.len(), 16);

// a vector is always a subset of itself
assert!(a.is_subset(&a));
assert!(b.is_subset(&b));
assert!(c.is_subset(&c));

assert!(a.is_subset(&b));
assert!(a.is_subset(&c));
assert!(b.is_subset(&c));

assert!(!b.is_subset(&a));
assert!(!c.is_subset(&a));
assert!(!c.is_subset(&b));
}

#[test]
fn union() {
let a = BitVector16::from_raw_bytes(smallvec![0b1100, 0b0001], 16).unwrap();
Expand Down Expand Up @@ -1209,6 +1243,46 @@ mod bitlist {
assert_eq!(c.intersection(&c), c);
}

#[test]
fn subset() {
let a = BitList1024::from_raw_bytes(smallvec![0b1000, 0b0001], 16).unwrap();
let b = BitList1024::from_raw_bytes(smallvec![0b1100, 0b0001], 16).unwrap();
let c = BitList1024::from_raw_bytes(smallvec![0b1100, 0b1001], 16).unwrap();

assert_eq!(a.len(), 16);
assert_eq!(b.len(), 16);
assert_eq!(c.len(), 16);

// a vector is always a subset of itself
assert!(a.is_subset(&a));
assert!(b.is_subset(&b));
assert!(c.is_subset(&c));

assert!(a.is_subset(&b));
assert!(a.is_subset(&c));
assert!(b.is_subset(&c));

assert!(!b.is_subset(&a));
assert!(!c.is_subset(&a));
assert!(!c.is_subset(&b));

let d = BitList1024::from_raw_bytes(smallvec![0b1100, 0b1001, 0b1010], 24).unwrap();
assert!(d.is_subset(&d));

assert!(a.is_subset(&d));
assert!(b.is_subset(&d));
assert!(c.is_subset(&d));

// A bigger length bitlist cannot be a subset of a smaller length bitlist
assert!(!d.is_subset(&a));
assert!(!d.is_subset(&b));
assert!(!d.is_subset(&c));

let e = BitList1024::from_raw_bytes(smallvec![0b1100, 0b1001, 0b0000], 24).unwrap();
assert!(e.is_subset(&c));
assert!(c.is_subset(&e));
}

#[test]
fn intersection_diff_length() {
let a = BitList1024::from_bytes(smallvec![0b0010_1110, 0b0010_1011]).unwrap();
Expand Down

0 comments on commit 3f06d58

Please sign in to comment.