Skip to content

Commit 2df74d7

Browse files
committed
feat: remove_run_compression, add_offset, Clone::clone_from
bindings for new functions from upstream
1 parent 09fced0 commit 2df74d7

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

croaring/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ criterion = { version = "0.8", features = ["html_reports"] }
2929
[dependencies]
3030
# Support for allocators that use allocator-api2
3131
allocator-api2 = { version = "0.4.0", optional = true, default-features = false, features = ["alloc"] }
32-
ffi = { package = "croaring-sys", path = "../croaring-sys", version = "4.4.0" }
32+
ffi = { package = "croaring-sys", path = "../croaring-sys", version = "4.6.1" }
3333

3434
[[bench]]
3535
name = "benches"

croaring/src/bitmap64/imp.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,28 @@ impl Bitmap64 {
527527
unsafe { ffi::roaring64_bitmap_run_optimize(self.raw.as_ptr()) }
528528
}
529529

530+
/// Remove run-length encoding even when it is more space efficient
531+
///
532+
/// Returns true if a change was applied
533+
///
534+
/// # Examples
535+
///
536+
/// ```
537+
/// use croaring::Bitmap64;
538+
///
539+
/// let mut bitmap: Bitmap64 = (100..1000).collect();
540+
///
541+
/// bitmap.run_optimize();
542+
///
543+
/// assert!(bitmap.remove_run_compression());
544+
/// assert!(!bitmap.remove_run_compression());
545+
/// ```
546+
#[inline]
547+
#[doc(alias = "roaring64_bitmap_remove_run_compression")]
548+
pub fn remove_run_compression(&mut self) -> bool {
549+
unsafe { ffi::roaring64_bitmap_remove_run_compression(self.raw.as_ptr()) }
550+
}
551+
530552
/// Returns true if the element is contained in the bitmap
531553
///
532554
/// # Examples
@@ -755,6 +777,41 @@ impl Bitmap64 {
755777
unsafe { ffi::roaring64_bitmap_flip_closed_inplace(self.raw.as_ptr(), start, end) };
756778
}
757779

780+
/// Returns a new bitmap with all values shifted by the given offset
781+
///
782+
/// Any values which would underflow or overflow `u64` are dropped.
783+
///
784+
/// # Examples
785+
/// ```
786+
/// use croaring::Bitmap64;
787+
///
788+
/// let bitmap1 = Bitmap64::of(&[0, 1, 1000, u64::MAX]);
789+
/// let shifted_down = bitmap1.add_offset(-1);
790+
/// assert_eq!(shifted_down.iter().collect::<Vec<_>>(), [0, 999, u64::MAX - 1]);
791+
/// let shifted_up = bitmap1.add_offset(1);
792+
/// assert_eq!(shifted_up.iter().collect::<Vec<_>>(), [1, 2, 1001]);
793+
/// let big_shifted = bitmap1.add_offset(i128::from(u64::MAX) + 1);
794+
/// assert_eq!(big_shifted.iter().collect::<Vec<_>>(), []);
795+
/// ```
796+
#[inline]
797+
#[doc(alias = "roaring64_bitmap_add_offset_signed")]
798+
#[must_use]
799+
pub fn add_offset(&self, offset: i128) -> Self {
800+
let positive = offset >= 0;
801+
let offset = offset.unsigned_abs();
802+
let Ok(offset) = u64::try_from(offset) else {
803+
// If the offset doesn't fit in 64 bits, we shifted everything out
804+
return Self::new();
805+
};
806+
unsafe {
807+
Self::take_heap(ffi::roaring64_bitmap_add_offset_signed(
808+
self.raw.as_ptr(),
809+
positive,
810+
offset,
811+
))
812+
}
813+
}
814+
758815
/// Returns a vector containing the values in the bitmap in sorted order
759816
///
760817
/// # Examples

croaring/src/bitmap64/ops.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ impl Clone for Bitmap64 {
7878
Self::take_heap(raw)
7979
}
8080
}
81+
82+
#[inline]
83+
#[doc(alias = "roaring64_bitmap_overwrite")]
84+
fn clone_from(&mut self, source: &Self) {
85+
unsafe {
86+
ffi::roaring64_bitmap_overwrite(self.raw.as_ptr(), source.raw.as_ptr());
87+
}
88+
}
8189
}
8290

8391
impl Drop for Bitmap64 {

0 commit comments

Comments
 (0)