Skip to content

Commit

Permalink
bugfix: Treemap.andnot would not include items with no container on rhs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Emann committed Jan 26, 2024
1 parent b01b9ce commit 401d96e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
15 changes: 12 additions & 3 deletions croaring/src/treemap/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,9 +938,18 @@ impl Treemap {
let mut treemap = Treemap::new();

for (key, bitmap) in &self.map {
if let Some(other_bitmap) = other.map.get(key) {
treemap.map.insert(*key, bitmap.andnot(other_bitmap));
}
let sub_bitmap = match other.map.get(key) {
Some(other_bitmap) => {
let sub_bitmap = bitmap.andnot(other_bitmap);
if sub_bitmap.is_empty() {
continue;
}
sub_bitmap
}
// x.andnot(empty) == x
None => bitmap.clone(),
};
treemap.map.insert(*key, sub_bitmap);
}

treemap
Expand Down
2 changes: 1 addition & 1 deletion croaring/src/treemap/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ impl<'a, 'b> Sub<&'a Treemap> for &'b Treemap {
/// let mut treemap2 = Treemap::new();
///
/// treemap2.add(25);
/// treemap1.add(u64::MAX);
/// treemap2.add(u64::MAX);
///
/// let treemap3 = &treemap1 - &treemap2;
///
Expand Down
12 changes: 12 additions & 0 deletions croaring/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ fn test_treemap_deserialize_jvm() {
}
}

#[test]
fn test_treemap_max_andnot_empty() {
let single_max = Treemap::of(&[std::u64::MAX]);
let empty = Treemap::new();
let diff = single_max.andnot(&empty);
assert_eq!(diff, single_max);

let mut diff = single_max.clone();
diff.andnot_inplace(&empty);
assert_eq!(diff, single_max);
}

#[test]
fn treemap_run_optimized() {
let mut initial = Bitmap::new();
Expand Down

0 comments on commit 401d96e

Please sign in to comment.