Skip to content

Commit 6b82631

Browse files
authored
Add missing operations to TreeMap (#113)
Add all the operations from C++'s Roaring64Map. Additionally, fixes suggested by cargo clippy -- -W clippy::pedantic, mostly adding must_use to most functions where the function is only useful for its output.
1 parent 91e85fc commit 6b82631

File tree

15 files changed

+593
-102
lines changed

15 files changed

+593
-102
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[workspace]
22
members = ["croaring", "croaring-sys"]
3+
resolver = "2"

croaring-sys/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99
build.file("CRoaring/roaring.c");
1010

1111
if let Ok(target_arch) = env::var("ROARING_ARCH") {
12-
build.flag_if_supported(&format!("-march={}", target_arch));
12+
build.flag_if_supported(&format!("-march={target_arch}"));
1313
}
1414

1515
build.flag_if_supported("-Wno-unused-function");

croaring/src/bitmap/imp.rs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::Bitset;
22
use ffi::roaring_bitmap_t;
33
use std::convert::TryInto;
4-
use std::mem;
4+
use std::ffi::c_void;
55
use std::ops::{Bound, RangeBounds};
6+
use std::{mem, ptr};
67

78
use super::serialization::{Deserializer, Serializer};
89
use super::{Bitmap, Statistics};
@@ -25,7 +26,7 @@ impl Bitmap {
2526
&& ffi::ROARING_VERSION_MINOR == 3
2627
&& ffi::ROARING_VERSION_REVISION == 0
2728
);
28-
ffi::roaring_free(p as *mut _);
29+
ffi::roaring_free(p.cast::<c_void>());
2930
result
3031
}
3132

@@ -41,6 +42,7 @@ impl Bitmap {
4142
/// assert!(bitmap.is_empty());
4243
/// ```
4344
#[inline]
45+
#[must_use]
4446
pub fn create() -> Self {
4547
Self::create_with_capacity(0)
4648
}
@@ -59,6 +61,7 @@ impl Bitmap {
5961
/// ```
6062
#[inline]
6163
#[doc(alias = "roaring_bitmap_init_with_capacity")]
64+
#[must_use]
6265
pub fn create_with_capacity(capacity: u32) -> Self {
6366
let mut bitmap = mem::MaybeUninit::uninit();
6467
let success =
@@ -291,6 +294,7 @@ impl Bitmap {
291294
/// ```
292295
#[inline]
293296
#[doc(alias = "roaring_bitmap_contains")]
297+
#[must_use]
294298
pub fn contains(&self, element: u32) -> bool {
295299
unsafe { ffi::roaring_bitmap_contains(&self.bitmap, element) }
296300
}
@@ -313,6 +317,7 @@ impl Bitmap {
313317
/// ```
314318
#[inline]
315319
#[doc(alias = "roaring_bitmap_add_offset")]
320+
#[must_use]
316321
pub fn add_offset(&self, offset: i64) -> Self {
317322
unsafe { Bitmap::take_heap(ffi::roaring_bitmap_add_offset(&self.bitmap, offset)) }
318323
}
@@ -358,6 +363,7 @@ impl Bitmap {
358363
/// ```
359364
#[inline]
360365
#[doc(alias = "roaring_bitmap_get_cardinality")]
366+
#[must_use]
361367
pub fn cardinality(&self) -> u64 {
362368
unsafe { ffi::roaring_bitmap_get_cardinality(&self.bitmap) }
363369
}
@@ -380,6 +386,7 @@ impl Bitmap {
380386
/// ```
381387
#[inline]
382388
#[doc(alias = "roaring_bitmap_and")]
389+
#[must_use]
383390
pub fn and(&self, other: &Self) -> Self {
384391
unsafe { Self::take_heap(ffi::roaring_bitmap_and(&self.bitmap, &other.bitmap)) }
385392
}
@@ -434,6 +441,7 @@ impl Bitmap {
434441
/// ```
435442
#[inline]
436443
#[doc(alias = "roaring_bitmap_or")]
444+
#[must_use]
437445
pub fn or(&self, other: &Self) -> Self {
438446
unsafe { Self::take_heap(ffi::roaring_bitmap_or(&self.bitmap, &other.bitmap)) }
439447
}
@@ -482,10 +490,11 @@ impl Bitmap {
482490
/// ```
483491
#[inline]
484492
#[doc(alias = "roaring_bitmap_or_many")]
493+
#[must_use]
485494
pub fn fast_or(bitmaps: &[&Bitmap]) -> Self {
486495
let mut bms: Vec<*const ffi::roaring_bitmap_s> = bitmaps
487496
.iter()
488-
.map(|item| &item.bitmap as *const _)
497+
.map(|item| ptr::addr_of!(item.bitmap))
489498
.collect();
490499

491500
unsafe { Self::take_heap(ffi::roaring_bitmap_or_many(bms.len(), bms.as_mut_ptr())) }
@@ -512,18 +521,16 @@ impl Bitmap {
512521
/// ```
513522
#[inline]
514523
#[doc(alias = "roaring_bitmap_or_many_heap")]
524+
#[must_use]
515525
pub fn fast_or_heap(bitmaps: &[&Bitmap]) -> Self {
516526
let mut bms: Vec<*const ffi::roaring_bitmap_s> = bitmaps
517527
.iter()
518-
.map(|item| &item.bitmap as *const _)
528+
.map(|item| ptr::addr_of!(item.bitmap))
519529
.collect();
520530

521-
unsafe {
522-
Self::take_heap(ffi::roaring_bitmap_or_many_heap(
523-
bms.len() as u32,
524-
bms.as_mut_ptr(),
525-
))
526-
}
531+
let count = u32::try_from(bms.len()).expect("can only or up to 2^32 bitmaps");
532+
533+
unsafe { Self::take_heap(ffi::roaring_bitmap_or_many_heap(count, bms.as_mut_ptr())) }
527534
}
528535

529536
/// Computes the symmetric difference (xor) between two bitmaps
@@ -546,6 +553,7 @@ impl Bitmap {
546553
/// ```
547554
#[inline]
548555
#[doc(alias = "roaring_bitmap_xor")]
556+
#[must_use]
549557
pub fn xor(&self, other: &Self) -> Self {
550558
unsafe { Self::take_heap(ffi::roaring_bitmap_xor(&self.bitmap, &other.bitmap)) }
551559
}
@@ -593,10 +601,11 @@ impl Bitmap {
593601
/// ```
594602
#[inline]
595603
#[doc(alias = "roaring_bitmap_xor_many")]
604+
#[must_use]
596605
pub fn fast_xor(bitmaps: &[&Bitmap]) -> Self {
597606
let mut bms: Vec<*const ffi::roaring_bitmap_s> = bitmaps
598607
.iter()
599-
.map(|item| &item.bitmap as *const _)
608+
.map(|item| ptr::addr_of!(item.bitmap))
600609
.collect();
601610

602611
unsafe { Self::take_heap(ffi::roaring_bitmap_xor_many(bms.len(), bms.as_mut_ptr())) }
@@ -621,6 +630,7 @@ impl Bitmap {
621630
/// ```
622631
#[inline]
623632
#[doc(alias = "roaring_bitmap_andnot")]
633+
#[must_use]
624634
pub fn andnot(&self, other: &Self) -> Self {
625635
unsafe { Self::take_heap(ffi::roaring_bitmap_andnot(&self.bitmap, &other.bitmap)) }
626636
}
@@ -679,6 +689,7 @@ impl Bitmap {
679689
/// ```
680690
#[inline]
681691
#[doc(alias = "roaring_bitmap_flip")]
692+
#[must_use]
682693
pub fn flip<R: RangeBounds<u32>>(&self, range: R) -> Self {
683694
let (start, end) = range_to_exclusive(range);
684695
unsafe { Self::take_heap(ffi::roaring_bitmap_flip(&self.bitmap, start, end)) }
@@ -723,6 +734,7 @@ impl Bitmap {
723734
/// ```
724735
#[inline]
725736
#[doc(alias = "roaring_bitmap_to_uint32_array")]
737+
#[must_use]
726738
pub fn to_vec(&self) -> Vec<u32> {
727739
let bitmap_size: usize = self.cardinality().try_into().unwrap();
728740

@@ -736,8 +748,9 @@ impl Bitmap {
736748

737749
/// Computes the serialized size in bytes of the Bitmap in format `S`.
738750
#[inline]
751+
#[must_use]
739752
pub fn get_serialized_size_in_bytes<S: Serializer>(&self) -> usize {
740-
S::get_serialized_size_in_bytes(&self)
753+
S::get_serialized_size_in_bytes(self)
741754
}
742755

743756
/// Serializes a bitmap to a slice of bytes in format `S`.
@@ -756,6 +769,7 @@ impl Bitmap {
756769
/// assert_eq!(original_bitmap, deserialized_bitmap);
757770
/// ```
758771
#[inline]
772+
#[must_use]
759773
pub fn serialize<S: Serializer>(&self) -> Vec<u8> {
760774
let mut dst = Vec::new();
761775
self.serialize_into::<S>(&mut dst);
@@ -809,6 +823,7 @@ impl Bitmap {
809823
/// assert!(deserialized_bitmap.is_none());
810824
/// ```
811825
#[inline]
826+
#[must_use]
812827
pub fn try_deserialize<D: Deserializer>(buffer: &[u8]) -> Option<Self> {
813828
D::try_deserialize(buffer)
814829
}
@@ -846,6 +861,7 @@ impl Bitmap {
846861
/// ```
847862
#[inline]
848863
#[doc(alias = "roaring_bitmap_of_ptr")]
864+
#[must_use]
849865
pub fn of(elements: &[u32]) -> Self {
850866
unsafe {
851867
Self::take_heap(ffi::roaring_bitmap_of_ptr(
@@ -1017,6 +1033,7 @@ impl Bitmap {
10171033
/// ```
10181034
#[inline]
10191035
#[doc(alias = "roaring_bitmap_is_empty")]
1036+
#[must_use]
10201037
pub fn is_empty(&self) -> bool {
10211038
unsafe { ffi::roaring_bitmap_is_empty(&self.bitmap) }
10221039
}
@@ -1039,6 +1056,7 @@ impl Bitmap {
10391056
/// ```
10401057
#[inline]
10411058
#[doc(alias = "roaring_bitmap_is_subset")]
1059+
#[must_use]
10421060
pub fn is_subset(&self, other: &Self) -> bool {
10431061
unsafe { ffi::roaring_bitmap_is_subset(&self.bitmap, &other.bitmap) }
10441062
}
@@ -1062,6 +1080,7 @@ impl Bitmap {
10621080
/// ```
10631081
#[inline]
10641082
#[doc(alias = "roaring_bitmap_is_strict_subset")]
1083+
#[must_use]
10651084
pub fn is_strict_subset(&self, other: &Self) -> bool {
10661085
unsafe { ffi::roaring_bitmap_is_strict_subset(&self.bitmap, &other.bitmap) }
10671086
}
@@ -1083,6 +1102,7 @@ impl Bitmap {
10831102
/// ```
10841103
#[inline]
10851104
#[doc(alias = "roaring_bitmap_intersect")]
1105+
#[must_use]
10861106
pub fn intersect(&self, other: &Self) -> bool {
10871107
unsafe { ffi::roaring_bitmap_intersect(&self.bitmap, &other.bitmap) }
10881108
}
@@ -1126,6 +1146,7 @@ impl Bitmap {
11261146
/// ```
11271147
#[inline]
11281148
#[doc(alias = "roaring_bitmap_jaccard_index")]
1149+
#[must_use]
11291150
pub fn jaccard_index(&self, other: &Self) -> f64 {
11301151
unsafe { ffi::roaring_bitmap_jaccard_index(&self.bitmap, &other.bitmap) }
11311152
}
@@ -1144,6 +1165,7 @@ impl Bitmap {
11441165
/// ```
11451166
#[inline]
11461167
#[doc(alias = "roaring_bitmap_and_cardinality")]
1168+
#[must_use]
11471169
pub fn and_cardinality(&self, other: &Self) -> u64 {
11481170
unsafe { ffi::roaring_bitmap_and_cardinality(&self.bitmap, &other.bitmap) }
11491171
}
@@ -1161,6 +1183,7 @@ impl Bitmap {
11611183
/// assert_eq!(bitmap1.or_cardinality(&bitmap2), 2);
11621184
#[inline]
11631185
#[doc(alias = "roaring_bitmap_or_cardinality")]
1186+
#[must_use]
11641187
pub fn or_cardinality(&self, other: &Self) -> u64 {
11651188
unsafe { ffi::roaring_bitmap_or_cardinality(&self.bitmap, &other.bitmap) }
11661189
}
@@ -1179,6 +1202,7 @@ impl Bitmap {
11791202
/// ```
11801203
#[inline]
11811204
#[doc(alias = "roaring_bitmap_andnot_cardinality")]
1205+
#[must_use]
11821206
pub fn andnot_cardinality(&self, other: &Self) -> u64 {
11831207
unsafe { ffi::roaring_bitmap_andnot_cardinality(&self.bitmap, &other.bitmap) }
11841208
}
@@ -1197,6 +1221,7 @@ impl Bitmap {
11971221
/// ```
11981222
#[inline]
11991223
#[doc(alias = "roaring_bitmap_xor_cardinality")]
1224+
#[must_use]
12001225
pub fn xor_cardinality(&self, other: &Self) -> u64 {
12011226
unsafe { ffi::roaring_bitmap_xor_cardinality(&self.bitmap, &other.bitmap) }
12021227
}
@@ -1222,6 +1247,7 @@ impl Bitmap {
12221247
/// ```
12231248
#[inline]
12241249
#[doc(alias = "roaring_bitmap_minimum")]
1250+
#[must_use]
12251251
pub fn minimum(&self) -> Option<u32> {
12261252
if self.is_empty() {
12271253
None
@@ -1251,6 +1277,7 @@ impl Bitmap {
12511277
/// ```
12521278
#[inline]
12531279
#[doc(alias = "roaring_bitmap_maximum")]
1280+
#[must_use]
12541281
pub fn maximum(&self) -> Option<u32> {
12551282
if self.is_empty() {
12561283
None
@@ -1279,6 +1306,7 @@ impl Bitmap {
12791306
/// ```
12801307
#[inline]
12811308
#[doc(alias = "roaring_bitmap_rank")]
1309+
#[must_use]
12821310
pub fn rank(&self, x: u32) -> u64 {
12831311
unsafe { ffi::roaring_bitmap_rank(&self.bitmap, x) }
12841312
}
@@ -1316,13 +1344,13 @@ impl Bitmap {
13161344
#[inline]
13171345
#[doc(alias = "index")]
13181346
#[doc(alias = "roaring_bitmap_get_index")]
1347+
#[must_use]
13191348
pub fn position(&self, x: u32) -> Option<u32> {
13201349
let index = unsafe { ffi::roaring_bitmap_get_index(&self.bitmap, x) };
13211350
if index == -1 {
13221351
None
13231352
} else {
1324-
debug_assert!((0..=u32::MAX as i64).contains(&index));
1325-
Some(index as u32)
1353+
Some(u32::try_from(index).unwrap())
13261354
}
13271355
}
13281356

@@ -1355,6 +1383,7 @@ impl Bitmap {
13551383
/// ```
13561384
#[inline]
13571385
#[doc(alias = "roaring_bitmap_select")]
1386+
#[must_use]
13581387
pub fn select(&self, position: u32) -> Option<u32> {
13591388
let mut element: u32 = 0;
13601389
let result = unsafe { ffi::roaring_bitmap_select(&self.bitmap, position, &mut element) };
@@ -1411,6 +1440,7 @@ impl Bitmap {
14111440
/// ```
14121441
#[inline]
14131442
#[doc(alias = "roaring_bitmap_statistics")]
1443+
#[must_use]
14141444
pub fn statistics(&self) -> Statistics {
14151445
let mut statistics: ffi::roaring_statistics_s = unsafe { ::std::mem::zeroed() };
14161446

@@ -1436,6 +1466,7 @@ impl Bitmap {
14361466
/// ```
14371467
#[inline]
14381468
#[doc(alias = "roaring_bitmap_to_bitset")]
1469+
#[must_use]
14391470
pub fn to_bitset(&self) -> Option<Bitset> {
14401471
let mut bitset = Bitset::new();
14411472
let success = unsafe { ffi::roaring_bitmap_to_bitset(&self.bitmap, bitset.as_raw_mut()) };

croaring/src/bitmap/iter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ impl Bitmap {
172172
/// ```
173173
#[inline]
174174
#[doc(alias = "roaring_init_iterator")]
175+
#[must_use]
175176
pub fn iter(&self) -> BitmapIterator {
176177
BitmapIterator::new(self)
177178
}

croaring/src/bitmap/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! Rust wrapper for CRoaring (a C/C++ implementation at https://github.com/RoaringBitmap/CRoaring)
1+
//! Rust wrapper for `CRoaring` (a C/C++ implementation at <https://github.com/RoaringBitmap/CRoaring>)
22
//!
3-
//! The original Java version can be found at https://github.com/RoaringBitmap/RoaringBitmap
3+
//! The original Java version can be found at <https://github.com/RoaringBitmap/RoaringBitmap>
44
//! # Example
55
//!
66
//! ```rust

croaring/src/bitmap/ops.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ impl PartialEq<Bitmap> for BitmapView<'_> {
4848
}
4949
}
5050

51+
impl Eq for Bitmap {}
52+
5153
impl Clone for Bitmap {
5254
/// Create a copy of a Bitmap
5355
/// # Examples

0 commit comments

Comments
 (0)