Skip to main content

roaring/bitmap/
inherent.rs

1use core::cmp::Ordering;
2use core::mem::size_of;
3use core::ops::{RangeBounds, RangeInclusive};
4
5use crate::bitmap::store::BITMAP_LENGTH;
6use crate::{IntegerTooSmall, RoaringBitmap};
7
8use super::container::Container;
9use super::util;
10
11#[cfg(not(feature = "std"))]
12use alloc::vec::Vec;
13
14impl RoaringBitmap {
15    /// Creates an empty `RoaringBitmap`.
16    ///
17    /// # Examples
18    ///
19    /// ```rust
20    /// use roaring::RoaringBitmap;
21    /// let rb = RoaringBitmap::new();
22    /// ```
23    pub fn new() -> RoaringBitmap {
24        RoaringBitmap { containers: Vec::new() }
25    }
26
27    /// Creates a full `RoaringBitmap`.
28    ///
29    /// # Examples
30    ///
31    /// ```rust
32    /// use roaring::RoaringBitmap;
33    /// let rb = RoaringBitmap::full();
34    /// ```
35    pub fn full() -> RoaringBitmap {
36        RoaringBitmap { containers: (0..=u16::MAX).map(Container::full).collect() }
37    }
38
39    /// Creates a `RoaringBitmap` from a byte slice, interpreting the bytes as a bitmap with a specified offset.
40    ///
41    /// # Arguments
42    ///
43    /// - `offset: u32` - The starting position in the bitmap where the byte slice will be applied, specified in bits.
44    ///                   This means that if `offset` is `n`, the first byte in the slice will correspond to the `n`th bit(0-indexed) in the bitmap.
45    /// - `bytes: &[u8]` - The byte slice containing the bitmap data. The bytes are interpreted in "Least-Significant-First" bit order.
46    ///
47    /// # Interpretation of `bytes`
48    ///
49    /// The `bytes` slice is interpreted in "Least-Significant-First" bit order. Each byte is read from least significant bit (LSB) to most significant bit (MSB).
50    /// For example, the byte `0b00000101` represents the bits `1, 0, 1, 0, 0, 0, 0, 0` in that order (see Examples section).
51    ///
52    ///
53    /// # Panics
54    ///
55    /// This function will panic if `bytes.len() + offset` is greater than 2^32.
56    ///
57    ///
58    /// # Examples
59    ///
60    /// ```rust
61    /// use roaring::RoaringBitmap;
62    ///
63    /// let bytes = [0b00000101, 0b00000010, 0b00000000, 0b10000000];
64    /// //             ^^^^^^^^    ^^^^^^^^    ^^^^^^^^    ^^^^^^^^
65    /// //             76543210          98
66    /// let rb = RoaringBitmap::from_lsb0_bytes(0, &bytes);
67    /// assert!(rb.contains(0));
68    /// assert!(!rb.contains(1));
69    /// assert!(rb.contains(2));
70    /// assert!(rb.contains(9));
71    /// assert!(rb.contains(31));
72    ///
73    /// let rb = RoaringBitmap::from_lsb0_bytes(8, &bytes);
74    /// assert!(rb.contains(8));
75    /// assert!(!rb.contains(9));
76    /// assert!(rb.contains(10));
77    /// assert!(rb.contains(17));
78    /// assert!(rb.contains(39));
79    ///
80    /// let rb = RoaringBitmap::from_lsb0_bytes(3, &bytes);
81    /// assert!(rb.contains(3));
82    /// assert!(!rb.contains(4));
83    /// assert!(rb.contains(5));
84    /// assert!(rb.contains(12));
85    /// assert!(rb.contains(34));
86    /// ```
87    pub fn from_lsb0_bytes(offset: u32, mut bytes: &[u8]) -> RoaringBitmap {
88        fn shift_bytes(bytes: &[u8], amount: usize) -> Vec<u8> {
89            let mut result = Vec::with_capacity(bytes.len() + 1);
90            let mut carry = 0u8;
91
92            for &byte in bytes {
93                let shifted = (byte << amount) | carry;
94                carry = byte >> (8 - amount);
95                result.push(shifted);
96            }
97
98            if carry != 0 {
99                result.push(carry);
100            }
101
102            result
103        }
104        if !offset.is_multiple_of(8) {
105            let shift = offset as usize % 8;
106            let shifted_bytes = shift_bytes(bytes, shift);
107            return RoaringBitmap::from_lsb0_bytes(offset - shift as u32, &shifted_bytes);
108        }
109
110        if bytes.is_empty() {
111            return RoaringBitmap::new();
112        }
113
114        // Using inclusive range avoids overflow: the max exclusive value is 2^32 (u32::MAX + 1).
115        let end_bit_inc = u32::try_from(bytes.len())
116            .ok()
117            .and_then(|len_bytes| len_bytes.checked_mul(8))
118            // `bytes` is non-empty, so len_bits is > 0
119            .and_then(|len_bits| offset.checked_add(len_bits - 1))
120            .expect("offset + bytes.len() must be <= 2^32");
121
122        // offsets are in bytes
123        let (mut start_container, start_offset) =
124            (offset as usize >> 16, (offset as usize % 0x1_0000) / 8);
125        let (end_container_inc, end_offset) =
126            (end_bit_inc as usize >> 16, (end_bit_inc as usize % 0x1_0000 + 1) / 8);
127
128        let n_containers_needed = end_container_inc + 1 - start_container;
129        let mut containers = Vec::with_capacity(n_containers_needed);
130
131        // Handle a partial first container
132        if start_offset != 0 {
133            let end_byte = if end_container_inc == start_container {
134                end_offset
135            } else {
136                BITMAP_LENGTH * size_of::<u64>()
137            };
138
139            let (src, rest) = bytes.split_at(end_byte - start_offset);
140            bytes = rest;
141
142            if let Some(container) =
143                Container::from_lsb0_bytes(start_container as u16, src, start_offset)
144            {
145                containers.push(container);
146            }
147
148            start_container += 1;
149        }
150
151        // Handle all full containers
152        for full_container_key in start_container..end_container_inc {
153            let (src, rest) = bytes.split_at(BITMAP_LENGTH * size_of::<u64>());
154            bytes = rest;
155
156            if let Some(container) = Container::from_lsb0_bytes(full_container_key as u16, src, 0) {
157                containers.push(container);
158            }
159        }
160
161        // Handle a last container
162        if !bytes.is_empty() {
163            if let Some(container) = Container::from_lsb0_bytes(end_container_inc as u16, bytes, 0)
164            {
165                containers.push(container);
166            }
167        }
168
169        RoaringBitmap { containers }
170    }
171
172    /// Adds a value to the set.
173    ///
174    /// Returns whether the value was absent from the set.
175    ///
176    /// # Examples
177    ///
178    /// ```rust
179    /// use roaring::RoaringBitmap;
180    ///
181    /// let mut rb = RoaringBitmap::new();
182    /// assert_eq!(rb.insert(3), true);
183    /// assert_eq!(rb.insert(3), false);
184    /// assert_eq!(rb.contains(3), true);
185    /// ```
186    #[inline]
187    pub fn insert(&mut self, value: u32) -> bool {
188        let (key, index) = util::split(value);
189        let container = match self.containers.binary_search_by_key(&key, |c| c.key) {
190            Ok(loc) => &mut self.containers[loc],
191            Err(loc) => {
192                self.containers.insert(loc, Container::new(key));
193                &mut self.containers[loc]
194            }
195        };
196        container.insert(index)
197    }
198
199    /// Searches for the specific container by the given key.
200    /// Creates a new container if it doesn't exist.
201    ///
202    /// Return the index of the target container.
203    #[inline]
204    pub(crate) fn find_container_by_key(&mut self, key: u16) -> usize {
205        match self.containers.binary_search_by_key(&key, |c| c.key) {
206            Ok(loc) => loc,
207            Err(loc) => {
208                self.containers.insert(loc, Container::new(key));
209                loc
210            }
211        }
212    }
213
214    /// Searches and then modifies a specific container with `M` by the given key.
215    /// Creates a new container using `B` if it doesn't exist.
216    ///
217    /// Returns `R` based on `M` or `B`.
218    #[inline]
219    pub(crate) fn mod_or_build_container_by_key<
220        R,
221        M: FnMut(&mut Container) -> R,
222        B: FnMut(u16) -> (Container, R),
223    >(
224        &mut self,
225        key: u16,
226        mut modifier: M,
227        mut builder: B,
228    ) -> R {
229        match self.containers.binary_search_by_key(&key, |c| c.key) {
230            Ok(loc) => modifier(&mut self.containers[loc]),
231            Err(loc) => {
232                let build_value = builder(key);
233                self.containers.insert(loc, build_value.0);
234                build_value.1
235            }
236        }
237    }
238
239    /// Inserts a range of values.
240    /// Returns the number of inserted values.
241    ///
242    /// # Examples
243    ///
244    /// ```rust
245    /// use roaring::RoaringBitmap;
246    ///
247    /// let mut rb = RoaringBitmap::new();
248    /// rb.insert_range(2..4);
249    /// assert!(rb.contains(2));
250    /// assert!(rb.contains(3));
251    /// assert!(!rb.contains(4));
252    /// ```
253    #[inline]
254    pub fn insert_range<R>(&mut self, range: R) -> u64
255    where
256        R: RangeBounds<u32>,
257    {
258        let (start, end) = match util::convert_range_to_inclusive(range) {
259            Ok(range) => (*range.start(), *range.end()),
260            Err(_) => return 0,
261        };
262
263        let (start_container_key, start_index) = util::split(start);
264        let (end_container_key, end_index) = util::split(end);
265        let modify_container_range =
266            |bitmap: &mut Self, container_key: u16, range: RangeInclusive<u16>| {
267                bitmap.mod_or_build_container_by_key(
268                    container_key,
269                    |container| container.insert_range(range.clone()),
270                    |key| (Container::new_with_range(key, range.clone()), range.len() as u64),
271                )
272            };
273
274        // If the end range value is in the same container, just call into
275        // the one container.
276        if start_container_key == end_container_key {
277            return modify_container_range(self, start_container_key, start_index..=end_index);
278        }
279
280        // For the first container, insert start_index..=u16::MAX, with
281        // subsequent containers inserting 0..MAX.
282        //
283        // The last container (end_container_key) is handled explicitly outside
284        // the loop.
285        let mut low = start_index;
286        let mut inserted = 0;
287
288        for i in start_container_key..end_container_key {
289            inserted += modify_container_range(self, i, low..=u16::MAX);
290
291            // After the first container, always fill the containers.
292            low = 0;
293        }
294
295        // Handle the last container
296        inserted += modify_container_range(self, end_container_key, 0..=end_index);
297
298        inserted
299    }
300
301    /// Pushes `value` in the bitmap only if it is greater than the current maximum value.
302    ///
303    /// Returns whether the value was inserted.
304    ///
305    /// # Examples
306    ///
307    /// ```rust
308    /// use roaring::RoaringBitmap;
309    ///
310    /// let mut rb = RoaringBitmap::new();
311    /// assert!(rb.push(1));
312    /// assert!(rb.push(3));
313    /// assert_eq!(rb.push(3), false);
314    /// assert!(rb.push(5));
315    ///
316    /// assert_eq!(rb.iter().collect::<Vec<u32>>(), vec![1, 3, 5]);
317    /// ```
318    #[inline]
319    #[deprecated(since = "0.11.0", note = "use `try_push` instead")]
320    pub fn push(&mut self, value: u32) -> bool {
321        self.try_push(value).is_ok()
322    }
323
324    /// Pushes `value` in the bitmap only if it is greater than the current maximum value.
325    ///
326    /// Returns an error if the value is not greater than the current maximum value.
327    ///
328    /// # Examples
329    ///
330    /// ```rust
331    /// use roaring::{RoaringBitmap, IntegerTooSmall};
332    ///
333    /// let mut rb = RoaringBitmap::new();
334    /// assert!(rb.try_push(1).is_ok());
335    /// assert!(rb.try_push(3).is_ok());
336    /// assert_eq!(rb.try_push(3), Err(IntegerTooSmall));
337    /// assert!(rb.try_push(5).is_ok());
338    ///
339    /// assert_eq!(rb.iter().collect::<Vec<u32>>(), vec![1, 3, 5]);
340    /// ```
341    #[inline]
342    pub fn try_push(&mut self, value: u32) -> Result<(), IntegerTooSmall> {
343        let (key, index) = util::split(value);
344
345        match self.containers.last_mut() {
346            Some(container) if container.key == key => {
347                if container.push(index) {
348                    Ok(())
349                } else {
350                    Err(IntegerTooSmall)
351                }
352            }
353            Some(container) if container.key > key => Err(IntegerTooSmall),
354            _otherwise => {
355                let mut container = Container::new(key);
356                container.push(index);
357                self.containers.push(container);
358                Ok(())
359            }
360        }
361    }
362
363    /// Pushes `value` at the end of the bitmap.
364    /// It is up to the caller to have validated index > self.max()
365    ///
366    /// # Panics
367    ///
368    /// If debug_assertions enabled and index is > self.max()
369    #[inline]
370    pub(crate) fn push_unchecked(&mut self, value: u32) {
371        let (key, index) = util::split(value);
372
373        match self.containers.last_mut() {
374            Some(container) if container.key == key => container.push_unchecked(index),
375            Some(container) if cfg!(debug_assertions) && container.key > key => {
376                panic!("last container key > key of value")
377            }
378            _otherwise => {
379                let mut container = Container::new(key);
380                container.push_unchecked(index);
381                self.containers.push(container);
382            }
383        }
384    }
385
386    /// Removes a value from the set. Returns `true` if the value was present in the set.
387    ///
388    /// # Examples
389    ///
390    /// ```rust
391    /// use roaring::RoaringBitmap;
392    ///
393    /// let mut rb = RoaringBitmap::new();
394    /// rb.insert(3);
395    /// assert_eq!(rb.remove(3), true);
396    /// assert_eq!(rb.remove(3), false);
397    /// assert_eq!(rb.contains(3), false);
398    /// ```
399    #[inline]
400    pub fn remove(&mut self, value: u32) -> bool {
401        let (key, index) = util::split(value);
402        match self.containers.binary_search_by_key(&key, |c| c.key) {
403            Ok(loc) if self.containers[loc].remove(index) => {
404                if self.containers[loc].is_empty() {
405                    self.containers.remove(loc);
406                }
407                true
408            }
409            _ => false,
410        }
411    }
412
413    /// Removes a range of values.
414    /// Returns the number of removed values.
415    ///
416    /// # Examples
417    ///
418    /// ```rust
419    /// use roaring::RoaringBitmap;
420    ///
421    /// let mut rb = RoaringBitmap::new();
422    /// rb.insert(2);
423    /// rb.insert(3);
424    /// assert_eq!(rb.remove_range(2..4), 2);
425    /// ```
426    #[inline]
427    pub fn remove_range<R>(&mut self, range: R) -> u64
428    where
429        R: RangeBounds<u32>,
430    {
431        let (start, end) = match util::convert_range_to_inclusive(range) {
432            Ok(range) => (*range.start(), *range.end()),
433            Err(_) => return 0,
434        };
435
436        let (start_container_key, start_index) = util::split(start);
437        let (end_container_key, end_index) = util::split(end);
438
439        let mut index = 0;
440        let mut removed = 0;
441        while index < self.containers.len() {
442            let key = self.containers[index].key;
443            if key >= start_container_key && key <= end_container_key {
444                let a = if key == start_container_key { start_index } else { 0 };
445                let b = if key == end_container_key { end_index } else { u16::MAX };
446                removed += self.containers[index].remove_range(a..=b);
447                if self.containers[index].is_empty() {
448                    self.containers.remove(index);
449                    continue;
450                }
451            }
452            index += 1;
453        }
454        removed
455    }
456
457    /// Returns `true` if this set contains the specified integer.
458    ///
459    /// # Examples
460    ///
461    /// ```rust
462    /// use roaring::RoaringBitmap;
463    ///
464    /// let mut rb = RoaringBitmap::new();
465    /// rb.insert(1);
466    /// assert_eq!(rb.contains(0), false);
467    /// assert_eq!(rb.contains(1), true);
468    /// assert_eq!(rb.contains(100), false);
469    /// ```
470    #[inline]
471    pub fn contains(&self, value: u32) -> bool {
472        let (key, index) = util::split(value);
473        match self.containers.binary_search_by_key(&key, |c| c.key) {
474            Ok(loc) => self.containers[loc].contains(index),
475            Err(_) => false,
476        }
477    }
478
479    /// Returns `true` if all values in the range are present in this set.
480    ///
481    /// # Examples
482    ///
483    /// ```
484    /// use roaring::RoaringBitmap;
485    ///
486    /// let mut rb = RoaringBitmap::new();
487    /// // An empty range is always contained
488    /// assert!(rb.contains_range(7..7));
489    ///
490    /// rb.insert_range(1..0xFFF);
491    /// assert!(rb.contains_range(1..0xFFF));
492    /// assert!(rb.contains_range(2..0xFFF));
493    /// // 0 is not contained
494    /// assert!(!rb.contains_range(0..2));
495    /// // 0xFFF is not contained
496    /// assert!(!rb.contains_range(1..=0xFFF));
497    /// ```
498    #[inline]
499    pub fn contains_range<R>(&self, range: R) -> bool
500    where
501        R: RangeBounds<u32>,
502    {
503        let (start, end) = match util::convert_range_to_inclusive(range) {
504            Ok(range) => (*range.start(), *range.end()),
505            // Empty/Invalid ranges are always contained
506            Err(_) => return true,
507        };
508        let (start_high, start_low) = util::split(start);
509        let (end_high, end_low) = util::split(end);
510        debug_assert!(start_high <= end_high);
511
512        let containers =
513            match self.containers.binary_search_by_key(&start_high, |container| container.key) {
514                Ok(i) => &self.containers[i..],
515                Err(_) => return false,
516            };
517
518        if start_high == end_high {
519            return containers[0].contains_range(start_low..=end_low);
520        }
521
522        let high_span = usize::from(end_high - start_high);
523        // If this contains everything in the range, there should be a container for every item in the span
524        // and the container that many items away should be the high key
525        let containers = match containers.get(high_span) {
526            Some(c) if c.key == end_high => &containers[..=high_span],
527            _ => return false,
528        };
529
530        match containers {
531            [first, rest @ .., last] => {
532                first.contains_range(start_low..=u16::MAX)
533                    && rest.iter().all(|container| container.is_full())
534                    && last.contains_range(0..=end_low)
535            }
536            _ => unreachable!("already validated containers has at least 2 items"),
537        }
538    }
539
540    /// Returns the number of elements in this set which are in the passed range.
541    ///
542    /// # Examples
543    ///
544    /// ```
545    /// use roaring::RoaringBitmap;
546    ///
547    /// let mut rb = RoaringBitmap::new();
548    /// rb.insert_range(0x10000..0x40000);
549    /// rb.insert(0x50001);
550    /// rb.insert(0x50005);
551    /// rb.insert(u32::MAX);
552    ///
553    /// assert_eq!(rb.range_cardinality(0..0x10000), 0);
554    /// assert_eq!(rb.range_cardinality(0x10000..0x40000), 0x30000);
555    /// assert_eq!(rb.range_cardinality(0x50000..0x60000), 2);
556    /// assert_eq!(rb.range_cardinality(0x10000..0x10000), 0);
557    /// assert_eq!(rb.range_cardinality(0x50000..=u32::MAX), 3);
558    /// ```
559    #[inline]
560    pub fn range_cardinality<R>(&self, range: R) -> u64
561    where
562        R: RangeBounds<u32>,
563    {
564        let (start, end) = match util::convert_range_to_inclusive(range) {
565            Ok(range) => (*range.start(), *range.end()),
566            // Empty/invalid ranges have 0 bits set in them
567            Err(_) => return 0,
568        };
569
570        let (start_key, start_low) = util::split(start);
571        let (end_key, end_low) = util::split(end);
572
573        let mut cardinality = 0;
574
575        let i = match self.containers.binary_search_by_key(&start_key, |c| c.key) {
576            Ok(i) => {
577                let container = &self.containers[i];
578                if start_key == end_key {
579                    cardinality += container.rank(end_low)
580                } else {
581                    cardinality += container.len();
582                }
583                if start_low != 0 {
584                    cardinality -= container.rank(start_low - 1);
585                }
586                i + 1
587            }
588            Err(i) => i,
589        };
590        for container in &self.containers[i..] {
591            match container.key.cmp(&end_key) {
592                Ordering::Less => cardinality += container.len(),
593                Ordering::Equal => {
594                    cardinality += container.rank(end_low);
595                    break;
596                }
597                Ordering::Greater => {
598                    break;
599                }
600            }
601        }
602
603        cardinality
604    }
605
606    /// Clears all integers in this set.
607    ///
608    /// # Examples
609    ///
610    /// ```rust
611    /// use roaring::RoaringBitmap;
612    ///
613    /// let mut rb = RoaringBitmap::new();
614    /// rb.insert(1);
615    /// assert_eq!(rb.contains(1), true);
616    /// rb.clear();
617    /// assert_eq!(rb.contains(1), false);
618    /// ```
619    #[inline]
620    pub fn clear(&mut self) {
621        self.containers.clear();
622    }
623
624    /// Returns `true` if there are no integers in this set.
625    ///
626    /// # Examples
627    ///
628    /// ```rust
629    /// use roaring::RoaringBitmap;
630    ///
631    /// let mut rb = RoaringBitmap::new();
632    /// assert_eq!(rb.is_empty(), true);
633    ///
634    /// rb.insert(3);
635    /// assert_eq!(rb.is_empty(), false);
636    /// ```
637    #[inline]
638    pub fn is_empty(&self) -> bool {
639        self.containers.is_empty()
640    }
641
642    /// Returns `true` if there are every possible integers in this set.
643    ///
644    /// # Examples
645    ///
646    /// ```rust
647    /// use roaring::RoaringBitmap;
648    ///
649    /// let mut rb = RoaringBitmap::full();
650    /// assert!(!rb.is_empty());
651    /// assert!(rb.is_full());
652    /// ```
653    #[inline]
654    pub fn is_full(&self) -> bool {
655        self.containers.len() == (u16::MAX as usize + 1)
656            && self.containers.iter().all(Container::is_full)
657    }
658
659    /// Returns the number of distinct integers added to the set.
660    ///
661    /// # Examples
662    ///
663    /// ```rust
664    /// use roaring::RoaringBitmap;
665    ///
666    /// let mut rb = RoaringBitmap::new();
667    /// assert_eq!(rb.len(), 0);
668    ///
669    /// rb.insert(3);
670    /// assert_eq!(rb.len(), 1);
671    ///
672    /// rb.insert(3);
673    /// rb.insert(4);
674    /// assert_eq!(rb.len(), 2);
675    /// ```
676    #[inline]
677    pub fn len(&self) -> u64 {
678        self.containers.iter().map(|container| container.len()).sum()
679    }
680
681    /// Returns the minimum value in the set (if the set is non-empty).
682    ///
683    /// # Examples
684    ///
685    /// ```rust
686    /// use roaring::RoaringBitmap;
687    ///
688    /// let mut rb = RoaringBitmap::new();
689    /// assert_eq!(rb.min(), None);
690    ///
691    /// rb.insert(3);
692    /// rb.insert(4);
693    /// assert_eq!(rb.min(), Some(3));
694    /// ```
695    #[inline]
696    pub fn min(&self) -> Option<u32> {
697        self.containers.first().and_then(|tail| tail.min().map(|min| util::join(tail.key, min)))
698    }
699
700    /// Returns the maximum value in the set (if the set is non-empty).
701    ///
702    /// # Examples
703    ///
704    /// ```rust
705    /// use roaring::RoaringBitmap;
706    ///
707    /// let mut rb = RoaringBitmap::new();
708    /// assert_eq!(rb.max(), None);
709    ///
710    /// rb.insert(3);
711    /// rb.insert(4);
712    /// assert_eq!(rb.max(), Some(4));
713    /// ```
714    #[inline]
715    pub fn max(&self) -> Option<u32> {
716        self.containers.last().and_then(|tail| tail.max().map(|max| util::join(tail.key, max)))
717    }
718
719    /// Returns the number of integers that are <= value. rank(u32::MAX) == len()
720    ///
721    /// # Examples
722    ///
723    /// ```rust
724    /// use roaring::RoaringBitmap;
725    ///
726    /// let mut rb = RoaringBitmap::new();
727    /// assert_eq!(rb.rank(0), 0);
728    ///
729    /// rb.insert(3);
730    /// rb.insert(4);
731    /// assert_eq!(rb.rank(3), 1);
732    /// assert_eq!(rb.rank(10), 2)
733    /// ```
734    #[inline]
735    pub fn rank(&self, value: u32) -> u64 {
736        // if len becomes cached for RoaringBitmap: return len if len > value
737
738        let (key, index) = util::split(value);
739
740        match self.containers.binary_search_by_key(&key, |c| c.key) {
741            Ok(i) => {
742                // For optimal locality of reference:
743                //  * container[i] should be a cache hit after binary search, rank it first
744                //  * sum in reverse to avoid cache misses near i
745                unsafe { self.containers.get_unchecked(i) }.rank(index)
746                    + self.containers[..i].iter().rev().map(|c| c.len()).sum::<u64>()
747            }
748            Err(i) => self.containers[..i].iter().map(|c| c.len()).sum(),
749        }
750    }
751
752    /// Returns the `n`th integer in the set or `None` if `n >= len()`
753    ///
754    /// # Examples
755    ///
756    /// ```rust
757    /// use roaring::RoaringBitmap;
758    ///
759    /// let mut rb = RoaringBitmap::new();
760    /// assert_eq!(rb.select(0), None);
761    ///
762    /// rb.append(vec![0, 10, 100]);
763    ///
764    /// assert_eq!(rb.select(0), Some(0));
765    /// assert_eq!(rb.select(1), Some(10));
766    /// assert_eq!(rb.select(2), Some(100));
767    /// assert_eq!(rb.select(3), None);
768    /// ```
769    #[inline]
770    pub fn select(&self, n: u32) -> Option<u32> {
771        let mut n = n as u64;
772
773        for container in &self.containers {
774            let len = container.len();
775            if len > n {
776                return container
777                    .store
778                    .select(n as u16)
779                    .map(|index| util::join(container.key, index));
780            }
781            n -= len;
782        }
783
784        None
785    }
786
787    /// Removes the `n` smallests values from this bitmap.
788    ///
789    /// # Examples
790    ///
791    /// ```rust
792    /// use roaring::RoaringBitmap;
793    ///
794    /// let mut rb = RoaringBitmap::from_iter([1, 5, 7, 9]);
795    /// rb.remove_smallest(2);
796    /// assert_eq!(rb, RoaringBitmap::from_iter([7, 9]));
797    ///
798    /// let mut rb = RoaringBitmap::from_iter([1, 3, 7, 9]);
799    /// rb.remove_smallest(2);
800    /// assert_eq!(rb, RoaringBitmap::from_iter([7, 9]));
801    #[inline]
802    pub fn remove_smallest(&mut self, mut n: u64) {
803        // remove containers up to the front of the target
804        let position = self.containers.iter().position(|container| {
805            let container_len = container.len();
806            if container_len <= n {
807                n -= container_len;
808                false
809            } else {
810                true
811            }
812        });
813        let position = position.unwrap_or(self.containers.len());
814        if position > 0 {
815            self.containers.drain(..position);
816        }
817        // remove data in containers if there are still targets for deletion
818        if n > 0 && !self.containers.is_empty() {
819            // container immediately before should have been deleted, so the target is 0 index
820            self.containers[0].remove_smallest(n);
821        }
822    }
823
824    /// Removes the `n` biggests values from this bitmap.
825    ///
826    /// # Examples
827    ///
828    /// ```rust
829    /// use roaring::RoaringBitmap;
830    ///
831    /// let mut rb = RoaringBitmap::from_iter([1, 5, 7, 9]);
832    /// rb.remove_biggest(2);
833    /// assert_eq!(rb, RoaringBitmap::from_iter([1, 5]));
834    /// rb.remove_biggest(1);
835    /// assert_eq!(rb, RoaringBitmap::from_iter([1]));
836    #[inline]
837    pub fn remove_biggest(&mut self, mut n: u64) {
838        // remove containers up to the back of the target
839        let position = self.containers.iter().rposition(|container| {
840            let container_len = container.len();
841            if container_len <= n {
842                n -= container_len;
843                false
844            } else {
845                true
846            }
847        });
848        // It is checked at the beginning of the function, so it is usually never an Err
849        if let Some(position) = position {
850            self.containers.drain(position + 1..);
851            if n > 0 && !self.containers.is_empty() {
852                self.containers[position].remove_biggest(n);
853            }
854        } else {
855            self.containers.clear();
856        }
857    }
858
859    /// Optimizes the container storage for this bitmap.
860    /// Returns true if the container storage was modified, false if not.
861    ///
862    /// # Examples
863    ///
864    /// ```
865    /// use roaring::RoaringBitmap;
866    ///
867    /// let mut rb = RoaringBitmap::from_iter(1000..100000);
868    /// rb.optimize();
869    /// ```
870    pub fn optimize(&mut self) -> bool {
871        let mut changed = false;
872        for container in &mut self.containers {
873            changed |= container.optimize()
874        }
875        changed
876    }
877
878    /// Removes run-length encoding even when it is more space efficient.
879    ///
880    /// Returns true if the container storage was modified, false if not.
881    ///
882    /// # Examples
883    ///
884    /// ```
885    /// use roaring::RoaringBitmap;
886    ///
887    /// let mut rb = RoaringBitmap::from_iter(0..=10000);
888    /// rb.optimize();
889    /// assert!(rb.remove_run_compression());
890    /// ```
891    pub fn remove_run_compression(&mut self) -> bool {
892        let mut changed = false;
893        for container in &mut self.containers {
894            changed |= container.remove_run_compression()
895        }
896        changed
897    }
898
899    /// Ensure the bitmap is internally valid
900    ///
901    /// This is useful for development, but is not needed for normal use:
902    /// bitmaps should _always_ be internally valid.
903    ///
904    /// # Errors
905    ///
906    /// Returns an error if the bitmap is not valid, with a description of the problem.
907    #[doc(hidden)]
908    pub fn internal_validate(&self) -> Result<(), &'static str> {
909        for window in self.containers.windows(2) {
910            let [first, second] = window else { unreachable!() };
911            if second.key <= first.key {
912                return Err("keys are not strictly increasing");
913            }
914        }
915        for container in &self.containers {
916            container.store.internal_validate()?;
917        }
918        Ok(())
919    }
920}
921
922impl Default for RoaringBitmap {
923    fn default() -> RoaringBitmap {
924        RoaringBitmap::new()
925    }
926}
927
928impl Clone for RoaringBitmap {
929    fn clone(&self) -> Self {
930        RoaringBitmap { containers: self.containers.clone() }
931    }
932
933    fn clone_from(&mut self, other: &Self) {
934        self.containers.clone_from(&other.containers);
935    }
936}
937
938#[cfg(test)]
939mod tests {
940    use proptest::collection::vec;
941    use proptest::prelude::*;
942
943    use super::*;
944
945    proptest! {
946        #[test]
947        fn insert_range(
948            lo in 0u32..=65535, hi in 65536u32..=131071,
949            checks in vec(0u32..=262143, 1000)
950        ){
951            let r = lo..hi;
952            let mut b = RoaringBitmap::new();
953            let inserted = b.insert_range(r.clone());
954            if r.end > r.start {
955                assert_eq!(inserted, r.end as u64 - r.start as u64);
956            } else {
957                assert_eq!(inserted, 0);
958            }
959
960            // Assert all values in the range are present
961            for i in r.clone() {
962                assert!(b.contains(i), "does not contain {i}");
963            }
964
965            // Run the check values looking for any false positives
966            for i in checks {
967                let bitmap_has = b.contains(i);
968                let range_has = r.contains(&i);
969                assert_eq!(
970                    bitmap_has, range_has,
971                    "value {i} in bitmap={bitmap_has} and range={range_has}"
972                );
973            }
974        }
975    }
976
977    #[test]
978    fn test_insert_remove_range_same_container() {
979        let mut b = RoaringBitmap::new();
980        let inserted = b.insert_range(1..5);
981        assert_eq!(inserted, 4);
982
983        for i in 1..5 {
984            assert!(b.contains(i));
985        }
986
987        let removed = b.remove_range(2..10);
988        assert_eq!(removed, 3);
989        assert!(b.contains(1));
990        for i in 2..5 {
991            assert!(!b.contains(i));
992        }
993    }
994
995    #[test]
996    fn test_insert_remove_range_pre_populated() {
997        let mut b = RoaringBitmap::new();
998        let inserted = b.insert_range(1..20_000);
999        assert_eq!(inserted, 19_999);
1000
1001        let removed = b.remove_range(10_000..21_000);
1002        assert_eq!(removed, 10_000);
1003
1004        let inserted = b.insert_range(1..20_000);
1005        assert_eq!(inserted, 10_000);
1006    }
1007
1008    #[test]
1009    fn test_insert_max_u32() {
1010        let mut b = RoaringBitmap::new();
1011        let inserted = b.insert(u32::MAX);
1012        // We are allowed to add u32::MAX
1013        assert!(inserted);
1014    }
1015
1016    #[test]
1017    fn test_insert_remove_across_container() {
1018        let mut b = RoaringBitmap::new();
1019        let inserted = b.insert_range(u16::MAX as u32..=u16::MAX as u32 + 1);
1020        assert_eq!(inserted, 2);
1021
1022        assert_eq!(b.containers.len(), 2);
1023
1024        let removed = b.remove_range(u16::MAX as u32 + 1..=u16::MAX as u32 + 1);
1025        assert_eq!(removed, 1);
1026
1027        assert_eq!(b.containers.len(), 1);
1028    }
1029
1030    #[test]
1031    fn test_insert_remove_single_element() {
1032        let mut b = RoaringBitmap::new();
1033        let inserted = b.insert_range(u16::MAX as u32 + 1..=u16::MAX as u32 + 1);
1034        assert_eq!(inserted, 1);
1035
1036        assert_eq!(b.containers[0].len(), 1);
1037        assert_eq!(b.containers.len(), 1);
1038
1039        let removed = b.remove_range(u16::MAX as u32 + 1..=u16::MAX as u32 + 1);
1040        assert_eq!(removed, 1);
1041
1042        assert_eq!(b.containers.len(), 0);
1043    }
1044
1045    #[test]
1046    fn test_insert_remove_range_multi_container() {
1047        let mut bitmap = RoaringBitmap::new();
1048        assert_eq!(bitmap.insert_range(0..((1_u32 << 16) + 1)), (1_u64 << 16) + 1);
1049        assert_eq!(bitmap.containers.len(), 2);
1050        assert_eq!(bitmap.containers[0].key, 0);
1051        assert_eq!(bitmap.containers[1].key, 1);
1052        assert_eq!(bitmap.insert_range(0..((1_u32 << 16) + 1)), 0);
1053
1054        assert!(bitmap.insert((1_u32 << 16) * 4));
1055        assert_eq!(bitmap.containers.len(), 3);
1056        assert_eq!(bitmap.containers[2].key, 4);
1057
1058        assert_eq!(bitmap.remove_range(((1_u32 << 16) * 3)..=((1_u32 << 16) * 4)), 1);
1059        assert_eq!(bitmap.containers.len(), 2);
1060    }
1061
1062    #[test]
1063    fn insert_range_single() {
1064        let mut bitmap = RoaringBitmap::new();
1065        assert_eq!(bitmap.insert_range((1_u32 << 16)..(2_u32 << 16)), 1_u64 << 16);
1066        assert_eq!(bitmap.containers.len(), 1);
1067        assert_eq!(bitmap.containers[0].key, 1);
1068    }
1069
1070    #[test]
1071    fn remove_smallest_for_vec() {
1072        let mut bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]);
1073        bitmap.remove_smallest(3);
1074        assert_eq!(bitmap.len(), 3);
1075        assert_eq!(bitmap, RoaringBitmap::from_iter([7, 9, 11]));
1076
1077        bitmap = RoaringBitmap::from_iter([1, 2, 5, 7, 9, 11]);
1078        bitmap.remove_smallest(3);
1079        assert_eq!(bitmap.len(), 3);
1080        assert_eq!(bitmap, RoaringBitmap::from_iter([7, 9, 11]));
1081
1082        bitmap = RoaringBitmap::from_iter([1, 3]);
1083        bitmap.remove_smallest(2);
1084        assert_eq!(bitmap.len(), 0);
1085
1086        bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]);
1087        bitmap.remove_smallest(0);
1088        assert_eq!(bitmap.len(), 6);
1089        assert_eq!(bitmap, RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]));
1090
1091        bitmap = RoaringBitmap::new();
1092        bitmap.insert_range(0..(1_u32 << 16) + 5);
1093        bitmap.remove_smallest(65537);
1094        assert_eq!(bitmap.len(), 4);
1095        assert_eq!(bitmap, RoaringBitmap::from_iter([65537, 65538, 65539, 65540]));
1096
1097        bitmap = RoaringBitmap::from_iter([1, 2, 5, 7, 9, 11]);
1098        bitmap.remove_smallest(7);
1099        assert_eq!(bitmap, RoaringBitmap::default());
1100    }
1101
1102    #[test]
1103    fn remove_smallest_for_bit() {
1104        let mut bitmap = RoaringBitmap::new();
1105        bitmap.insert_range(0..4098);
1106        bitmap.remove_smallest(4095);
1107        assert_eq!(bitmap.len(), 3);
1108        // removed bit to vec
1109        assert_eq!(bitmap, RoaringBitmap::from_iter([4095, 4096, 4097]));
1110
1111        bitmap = RoaringBitmap::new();
1112        bitmap.insert_range(0..6000);
1113        bitmap.remove_smallest(999);
1114        assert_eq!(bitmap.len(), 5001);
1115
1116        bitmap = RoaringBitmap::new();
1117        bitmap.insert_range(0..8000);
1118        bitmap.remove_smallest(10);
1119        assert_eq!(bitmap.len(), 7990);
1120
1121        bitmap = RoaringBitmap::new();
1122        bitmap.insert_range(0..200000);
1123        bitmap.remove_smallest(2000);
1124        assert_eq!(bitmap.len(), 198000);
1125        assert_eq!(bitmap, RoaringBitmap::from_iter(2000..200000));
1126
1127        bitmap = RoaringBitmap::new();
1128        bitmap.insert_range(0..2);
1129        bitmap.insert_range(4..7);
1130        bitmap.insert_range(1000..6000);
1131        bitmap.remove_smallest(30);
1132        assert_eq!(bitmap.len(), 4975);
1133
1134        bitmap = RoaringBitmap::new();
1135        bitmap.insert_range(0..65535);
1136        bitmap.remove_smallest(0);
1137        assert_eq!(bitmap.len(), 65535);
1138    }
1139
1140    #[test]
1141    fn remove_biggest_for_bit() {
1142        let mut bitmap = RoaringBitmap::new();
1143        bitmap.insert_range(0..5000);
1144        bitmap.remove_biggest(1000);
1145        assert_eq!(bitmap.len(), 4000);
1146
1147        bitmap = RoaringBitmap::new();
1148        bitmap.insert_range(0..6000);
1149        bitmap.remove_biggest(1000);
1150        assert_eq!(bitmap.len(), 5000);
1151
1152        bitmap = RoaringBitmap::new();
1153        bitmap.insert_range(0..200000);
1154        bitmap.remove_biggest(196000);
1155        assert_eq!(bitmap.len(), 4000);
1156
1157        bitmap = RoaringBitmap::new();
1158        bitmap.insert_range(0..200000);
1159        bitmap.remove_biggest(2000);
1160        assert_eq!(bitmap.len(), 198000);
1161        assert_eq!(bitmap, RoaringBitmap::from_iter(0..198000));
1162
1163        bitmap = RoaringBitmap::new();
1164        bitmap.insert_range(0..65535);
1165        bitmap.remove_biggest(0);
1166        assert_eq!(bitmap.len(), 65535);
1167    }
1168
1169    #[test]
1170    fn remove_biggest_for_vec() {
1171        let mut bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]);
1172        bitmap.remove_biggest(2);
1173        assert_eq!(bitmap, RoaringBitmap::from_iter([1, 2, 3, 7]));
1174
1175        bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]);
1176        bitmap.remove_biggest(6);
1177        assert_eq!(bitmap.len(), 0);
1178
1179        bitmap = RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]);
1180        bitmap.remove_biggest(0);
1181        assert_eq!(bitmap.len(), 6);
1182        assert_eq!(bitmap, RoaringBitmap::from_iter([1, 2, 3, 7, 9, 11]));
1183
1184        bitmap = RoaringBitmap::new();
1185        bitmap.insert_range(0..(1_u32 << 16) + 5);
1186        bitmap.remove_biggest(65537);
1187        assert_eq!(bitmap.len(), 4);
1188        assert_eq!(bitmap, RoaringBitmap::from_iter([0, 1, 2, 3]));
1189
1190        let mut bitmap = RoaringBitmap::from_iter([1, 2, 3]);
1191        bitmap.remove_biggest(4);
1192        assert_eq!(bitmap, RoaringBitmap::default());
1193    }
1194}