feat(data_types): SequenceNumberSet intersection

Support computing the intersection of two SequenceNumberSet.
pull/24376/head
Dom Dwyer 2023-03-03 17:09:41 +01:00
parent 146494f619
commit ac1b37c0f0
No known key found for this signature in database
GPG Key ID: E4C40DBD9157879A
1 changed files with 26 additions and 0 deletions

View File

@ -97,6 +97,11 @@ impl FromIterator<SequenceNumber> for SequenceNumberSet {
}
}
/// Return the intersection of `self` and `other`.
pub fn intersect(a: &SequenceNumberSet, b: &SequenceNumberSet) -> SequenceNumberSet {
SequenceNumberSet(a.0.and(&b.0))
}
#[cfg(test)]
mod tests {
use super::*;
@ -187,4 +192,25 @@ mod tests {
a.add(SequenceNumber::new(24));
assert_eq!(a, b);
}
#[test]
fn test_intersect() {
let a = [0, i64::MAX, 40, 41, 42, 43, 44, 45]
.into_iter()
.map(SequenceNumber::new)
.collect::<SequenceNumberSet>();
let b = [1, 5, i64::MAX, 42]
.into_iter()
.map(SequenceNumber::new)
.collect::<SequenceNumberSet>();
let intersection = intersect(&a, &b);
let want = [i64::MAX, 42]
.into_iter()
.map(SequenceNumber::new)
.collect::<SequenceNumberSet>();
assert_eq!(intersection, want);
}
}