croaring/bitmap/lazy.rs
1use crate::Bitmap;
2
3/// A handle to lazily perform multiple bitwise operations on a bitmap
4pub struct LazyBitmap<'a> {
5 bitmap: &'a mut Bitmap,
6}
7
8impl<'a> LazyBitmap<'a> {
9 /// Modifies the bitmap this lazy bitmap is associated with to be the union of the two bitmaps.
10 ///
11 /// # Arguments
12 /// * `other` - The other bitmap to union with.
13 /// * `force_bitsets` - Whether to force conversions to bitsets when modifying containers
14 #[inline]
15 #[doc(alias = "roaring_bitmap_lazy_or_inplace")]
16 pub fn or_inplace(&mut self, other: &Bitmap, force_bitsets: bool) -> &mut Self {
17 unsafe {
18 // Because we have a mutable borrow of the bitmap, `other` cannot be == our bitmap,
19 // so this is always safe
20 ffi::roaring_bitmap_lazy_or_inplace(
21 &mut self.bitmap.bitmap,
22 &other.bitmap,
23 force_bitsets,
24 );
25 }
26 self
27 }
28
29 /// Modifies the bitmap this lazy bitmap is associated with to be the xor of the two bitmaps.
30 #[inline]
31 #[doc(alias = "roaring_bitmap_lazy_xor_inplace")]
32 pub fn xor_inplace(&mut self, other: &Bitmap) -> &mut Self {
33 unsafe {
34 // Because we have a mutable borrow of the bitmap, `other` cannot be == our bitmap,
35 // so this is always safe
36 ffi::roaring_bitmap_lazy_xor_inplace(&mut self.bitmap.bitmap, &other.bitmap);
37 }
38 self
39 }
40}
41
42impl<'a> core::ops::BitOrAssign<&Bitmap> for LazyBitmap<'a> {
43 #[inline]
44 fn bitor_assign(&mut self, other: &Bitmap) {
45 self.or_inplace(other, false);
46 }
47}
48
49impl<'a> core::ops::BitXorAssign<&Bitmap> for LazyBitmap<'a> {
50 #[inline]
51 fn bitxor_assign(&mut self, other: &Bitmap) {
52 self.xor_inplace(other);
53 }
54}
55
56impl Bitmap {
57 /// Perform multiple bitwise operations on a bitmap.
58 ///
59 /// The passed closure will be passed a handle which can be used to perform bitwise operations on the bitmap lazily.
60 ///
61 /// The result will be equivalent to doing the same operations on this bitmap directly, but because of reduced
62 /// bookkeeping in between operations, it should be faster
63 ///
64 /// # Examples
65 ///
66 /// ```
67 /// use croaring::Bitmap;
68 ///
69 /// // Perform a series of bitwise operations on a bitmap:
70 /// let mut bitmap = Bitmap::of(&[99]);
71 /// let bitmaps_to_or = [Bitmap::of(&[1, 2, 5, 10]), Bitmap::of(&[1, 30, 100])];
72 /// let bitmaps_to_xor = [Bitmap::of(&[5]), Bitmap::of(&[1, 1000, 1001])];
73 ///
74 /// bitmap.lazy_batch(|lazy| {
75 /// for b in &bitmaps_to_or {
76 /// *lazy |= b;
77 /// }
78 /// for b in &bitmaps_to_xor {
79 /// *lazy ^= b;
80 /// }
81 /// });
82 /// let mut bitmap2 = Bitmap::of(&[99]);
83 /// for b in &bitmaps_to_or {
84 /// bitmap2 |= b;
85 /// }
86 /// for b in &bitmaps_to_xor {
87 /// bitmap2 ^= b;
88 /// }
89 /// assert_eq!(bitmap, bitmap2);
90 /// assert_eq!(bitmap.iter().collect::<Vec<_>>(), [2, 10, 30, 99, 100, 1000, 1001]);
91 /// ```
92 ///
93 /// The result the passed closure is returned from `lazy_batch`
94 ///
95 /// ```
96 /// use croaring::Bitmap;
97 ///
98 /// let mut bitmap = Bitmap::new();
99 /// let bitmaps_to_or = [Bitmap::of(&[1, 2, 5, 10]), Bitmap::of(&[1, 30, 100])];
100 /// let total_added = bitmap.lazy_batch(|lazy| {
101 /// let mut total = 0;
102 /// for b in &bitmaps_to_or {
103 /// lazy.or_inplace(b, true);
104 /// total += b.cardinality();
105 /// }
106 /// total
107 /// });
108 /// assert_eq!(total_added, 7);
109 #[doc(alias = "roaring_bitmap_repair_after_lazy")]
110 pub fn lazy_batch<F, O>(&mut self, f: F) -> O
111 where
112 F: FnOnce(&mut LazyBitmap<'_>) -> O,
113 {
114 let mut lazy_bitmap = LazyBitmap { bitmap: self };
115 let result = f(&mut lazy_bitmap);
116 unsafe {
117 ffi::roaring_bitmap_repair_after_lazy(&mut self.bitmap);
118 }
119 result
120 }
121}