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

Add BitList::is_disjoint #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ itertools = "0.10.0"
[dev-dependencies]
serde_json = "1.0.0"
tree_hash_derive = "0.5.0"
criterion = "0.5"

[[bench]]
name = "bitfield"
harness = false
30 changes: 30 additions & 0 deletions benches/bitfield.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use smallvec::smallvec;
use ssz_types::BitList;
use typenum::U2048;

type BitList2048 = BitList<U2048>;

fn is_disjoint(c: &mut Criterion) {
let x = BitList2048::from_raw_bytes(smallvec![0xff; 2048 / 8], 2048).unwrap();
let y = BitList2048::from_raw_bytes(smallvec![0x00; 2048 / 8], 2048).unwrap();

c.bench_with_input(
BenchmarkId::new("bitfield_is_disjoint", 2048),
&(x.clone(), y.clone()),
|b, &(ref x, ref y)| {
b.iter(|| assert!(x.is_disjoint(&y)));
},
);

c.bench_with_input(
BenchmarkId::new("bitfield_is_disjoint_by_intersection", 2048),
&(x.clone(), y.clone()),
|b, &(ref x, ref y)| {
b.iter(|| assert!(x.intersection(&y).is_zero()));
},
);
}

criterion_group!(benches, is_disjoint);
criterion_main!(benches);
54 changes: 53 additions & 1 deletion src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ impl<N: Unsigned + Clone> Bitfield<Variable<N>> {
pub fn is_subset(&self, other: &Self) -> bool {
self.difference(other).is_zero()
}

/// Returns `true` is `self` is disjoint from `other` and `false` otherwise.
///
/// This method is a faster alternative to `self.intersection(other).is_zero()` and does not
/// allocate!
pub fn is_disjoint(&self, other: &Self) -> bool {
self.bytes
.iter()
.zip(other.bytes.iter())
.all(|(self_byte, other_byte)| self_byte & other_byte == 0)
}
}

impl<N: Unsigned + Clone> Bitfield<Fixed<N>> {
Expand Down Expand Up @@ -391,7 +402,10 @@ impl<T: BitfieldBehaviour> Bitfield<T> {
/// - `bytes` is not the minimal required bytes to represent a bitfield of `bit_len` bits.
/// - `bit_len` is not a multiple of 8 and `bytes` contains set bits that are higher than, or
/// equal to `bit_len`.
fn from_raw_bytes(bytes: SmallVec<[u8; SMALLVEC_LEN]>, bit_len: usize) -> Result<Self, Error> {
pub fn from_raw_bytes(
bytes: SmallVec<[u8; SMALLVEC_LEN]>,
bit_len: usize,
) -> Result<Self, Error> {
if bit_len == 0 {
if bytes.len() == 1 && bytes[0] == 0 {
// A bitfield with `bit_len` 0 can only be represented by a single zero byte.
Expand Down Expand Up @@ -1243,6 +1257,44 @@ mod bitlist {
assert_eq!(c.intersection(&c), c);
}

#[test]
fn is_disjoint() {
let a = BitList1024::from_raw_bytes(smallvec![0b1000, 0b0001], 16).unwrap();
let b = BitList1024::from_raw_bytes(smallvec![0b0100, 0b0001], 16).unwrap();
let c = BitList1024::from_raw_bytes(smallvec![0b0111, 0b1110], 16).unwrap();
let d = BitList1024::from_raw_bytes(smallvec![0b0101, 0b0001, 0b0000], 24).unwrap();
let e = BitList1024::from_raw_bytes(smallvec![0b0101, 0b0000, 0b1011], 24).unwrap();

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

// a bitfield is never disjoint from itself
assert!(!a.is_disjoint(&a));
assert!(!b.is_disjoint(&b));
assert!(!c.is_disjoint(&c));
assert!(!d.is_disjoint(&d));
assert!(!e.is_disjoint(&e));

// same length, not disjoint
assert!(!a.is_disjoint(&b));
assert!(!b.is_disjoint(&a));

// same length, disjoint
assert!(a.is_disjoint(&c));
assert!(c.is_disjoint(&a));

// different length, not disjoint
assert!(!a.is_disjoint(&d));
assert!(!d.is_disjoint(&a));

// different length, disjoint
assert!(a.is_disjoint(&e));
assert!(e.is_disjoint(&a));
}

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