croaring/bitmap/
iter.rs

1use super::Bitmap;
2use core::marker::PhantomData;
3use core::mem::MaybeUninit;
4
5/// A cursor over the values of a bitmap
6///
7/// A Cursor is like an iterator, except that it can freely seek back-and-forth.
8///
9/// A cursor points at a single value in the bitmap, or at a "ghost" position,
10/// either one before the beginning of the bitmap, or one after the end of the bitmap.
11#[derive(Debug, Clone)]
12pub struct BitmapCursor<'a> {
13    raw: ffi::roaring_uint32_iterator_t,
14    _bitmap: PhantomData<&'a Bitmap>,
15}
16
17unsafe impl Send for BitmapCursor<'_> {}
18
19unsafe impl Sync for BitmapCursor<'_> {}
20
21impl<'a> BitmapCursor<'a> {
22    #[inline]
23    fn from_raw(raw: ffi::roaring_uint32_iterator_t) -> Self {
24        BitmapCursor {
25            raw,
26            _bitmap: PhantomData,
27        }
28    }
29
30    fn at_first(bitmap: &'a Bitmap) -> Self {
31        let mut raw = MaybeUninit::<ffi::roaring_uint32_iterator_s>::uninit();
32        unsafe { ffi::roaring_iterator_init(&bitmap.bitmap, raw.as_mut_ptr()) };
33        Self::from_raw(unsafe { raw.assume_init() })
34    }
35
36    fn at_last(bitmap: &'a Bitmap) -> Self {
37        let mut raw = MaybeUninit::<ffi::roaring_uint32_iterator_s>::uninit();
38        unsafe { ffi::roaring_iterator_init_last(&bitmap.bitmap, raw.as_mut_ptr()) };
39        Self::from_raw(unsafe { raw.assume_init() })
40    }
41
42    /// Returns true if the cursor is pointing at a value in the bitmap.
43    ///
44    /// If this returns false, then the cursor is pointing at a "ghost" position,
45    ///
46    /// # Examples
47    ///
48    /// ```
49    /// use croaring::Bitmap;
50    /// let mut bitmap = Bitmap::new();
51    /// assert!(!bitmap.cursor().has_value());
52    ///
53    /// bitmap.add(1);
54    /// let mut cursor = bitmap.cursor();
55    /// assert!(cursor.has_value());
56    /// assert_eq!(cursor.current(), Some(1));
57    /// cursor.move_next();
58    /// assert!(!cursor.has_value());
59    /// ```
60    #[inline]
61    #[must_use]
62    pub fn has_value(&self) -> bool {
63        self.raw.has_value
64    }
65
66    /// Returns the value at the cursor, if any.
67    ///
68    /// If the cursor is not pointing at a value, then this returns `None`.
69    ///
70    /// # Examples
71    ///
72    /// ```
73    /// use croaring::Bitmap;
74    /// let mut bitmap = Bitmap::new();
75    /// bitmap.add(1);
76    /// let mut cursor = bitmap.cursor();
77    /// assert_eq!(cursor.current(), Some(1));
78    /// cursor.move_next();
79    /// assert_eq!(cursor.current(), None);
80    /// ```
81    #[inline]
82    #[must_use]
83    pub fn current(&self) -> Option<u32> {
84        if self.has_value() {
85            Some(self.raw.current_value)
86        } else {
87            None
88        }
89    }
90
91    /// Moves the cursor to the next value in the bitmap
92    ///
93    /// If the cursor is already past the end of the bitmap, then this does nothing.
94    ///
95    /// If the cursor is at the ghost position before the beginning of the bitmap,
96    /// then this moves the cursor to the first value in the bitmap.
97    ///
98    /// # Examples
99    ///
100    /// ```
101    /// use croaring::Bitmap;
102    /// let mut bitmap = Bitmap::of(&[1, 2, 3]);
103    /// let mut cursor = bitmap.cursor();
104    /// assert_eq!(cursor.current(), Some(1));
105    /// cursor.move_prev();
106    /// assert_eq!(cursor.current(), None);
107    /// cursor.move_next();
108    /// assert_eq!(cursor.current(), Some(1));
109    /// cursor.move_next();
110    /// assert_eq!(cursor.current(), Some(2));
111    /// cursor.move_next();
112    /// assert_eq!(cursor.current(), Some(3));
113    /// cursor.move_next();
114    /// assert_eq!(cursor.current(), None);
115    /// ```
116    #[inline]
117    pub fn move_next(&mut self) {
118        unsafe { ffi::roaring_uint32_iterator_advance(&mut self.raw) };
119    }
120
121    /// Moves the cursor to the next value in the bitmap, and returns the value (if any)
122    ///
123    /// This is equivalent to calling [`Self::move_next`] followed by [`Self::current`].
124    ///
125    /// # Examples
126    ///
127    /// ```
128    /// use croaring::Bitmap;
129    /// let mut bitmap = Bitmap::of(&[1, 2, 3]);
130    /// let mut cursor = bitmap.cursor();
131    /// assert_eq!(cursor.current(), Some(1));
132    /// assert_eq!(cursor.next(), Some(2));
133    /// assert_eq!(cursor.next(), Some(3));
134    /// assert_eq!(cursor.next(), None);
135    /// ```
136    #[inline]
137    #[allow(clippy::should_implement_trait)]
138    pub fn next(&mut self) -> Option<u32> {
139        self.move_next();
140        self.current()
141    }
142
143    /// Moves the cursor to the previous value in the bitmap
144    ///
145    /// If the cursor is already before the beginning of the bitmap, then this does nothing.
146    ///
147    /// If the cursor is at the ghost position after the end of the bitmap,
148    /// then this moves the cursor to the last value in the bitmap.
149    ///
150    /// # Examples
151    ///
152    /// ```
153    /// use croaring::Bitmap;
154    /// let mut bitmap = Bitmap::of(&[1, 2, 3]);
155    /// let mut cursor = bitmap.cursor_to_last();
156    /// assert_eq!(cursor.current(), Some(3));
157    /// cursor.move_next();
158    /// assert_eq!(cursor.current(), None);
159    /// cursor.move_prev();
160    /// assert_eq!(cursor.current(), Some(3));
161    /// cursor.move_prev();
162    /// assert_eq!(cursor.current(), Some(2));
163    /// cursor.move_prev();
164    /// assert_eq!(cursor.current(), Some(1));
165    /// cursor.move_prev();
166    /// assert_eq!(cursor.current(), None);
167    /// ```
168    #[inline]
169    pub fn move_prev(&mut self) {
170        unsafe { ffi::roaring_uint32_iterator_previous(&mut self.raw) };
171    }
172
173    /// Moves the cursor to the previous value in the bitmap, and returns the value (if any)
174    ///
175    /// This is equivalent to calling [`Self::move_prev`] followed by [`Self::current`].
176    ///
177    /// # Examples
178    ///
179    /// ```
180    /// use croaring::Bitmap;
181    /// let mut bitmap = Bitmap::of(&[1, 2, 3]);
182    /// let mut cursor = bitmap.cursor_to_last();
183    /// assert_eq!(cursor.current(), Some(3));
184    /// assert_eq!(cursor.prev(), Some(2));
185    /// assert_eq!(cursor.prev(), Some(1));
186    /// assert_eq!(cursor.prev(), None);
187    /// ```
188    #[inline]
189    pub fn prev(&mut self) -> Option<u32> {
190        self.move_prev();
191        self.current()
192    }
193
194    /// Resets this cursor to the first value in the bitmap.
195    ///
196    /// The bitmap does not have to be the same bitmap that this cursor was created from:
197    /// this allows you to reuse a cursor for multiple bitmaps
198    ///
199    /// # Examples
200    ///
201    /// ```
202    /// use croaring::Bitmap;
203    /// let mut bitmap1 = Bitmap::of(&[1, 2, 3]);
204    /// let bitmap2 = Bitmap::of(&[4, 5, 6]);
205    /// let cursor = bitmap1.cursor();
206    /// assert_eq!(cursor.current(), Some(1));
207    /// let cursor = cursor.reset_to_first(&bitmap2);
208    /// assert_eq!(cursor.current(), Some(4));
209    /// // Cursor is no longer borrowing from bitmap1
210    /// bitmap1.add(100);
211    /// ```
212    #[must_use]
213    pub fn reset_to_first(self, bitmap: &Bitmap) -> BitmapCursor<'_> {
214        BitmapCursor::at_first(bitmap)
215    }
216
217    /// Resets this cursor to the last value in the bitmap.
218    ///
219    /// The bitmap does not have to be the same bitmap that this cursor was created from:
220    /// this allows you to reuse a cursor for multiple bitmaps
221    ///
222    /// # Examples
223    ///
224    /// ```
225    /// use croaring::Bitmap;
226    /// let mut bitmap1 = Bitmap::of(&[1, 2, 3]);
227    /// let bitmap2 = Bitmap::of(&[4, 5, 6]);
228    /// let cursor = bitmap1.cursor_to_last();
229    /// assert_eq!(cursor.current(), Some(3));
230    /// let cursor = cursor.reset_to_last(&bitmap2);
231    /// assert_eq!(cursor.current(), Some(6));
232    /// ```
233    #[must_use]
234    pub fn reset_to_last(self, bitmap: &Bitmap) -> BitmapCursor<'_> {
235        BitmapCursor::at_last(bitmap)
236    }
237
238    /// Attempt to read many values from the iterator into `dst`
239    ///
240    /// The current value _is_ included in the output.
241    ///
242    /// Returns the number of items read from the iterator, may be `< dst.len()` iff
243    /// the iterator is exhausted or `dst.len() > u32::MAX`.
244    ///
245    /// This can be much more efficient than repeated iteration.
246    ///
247    /// # Examples
248    ///
249    /// ```
250    /// use croaring::Bitmap;
251    ///
252    /// let mut bitmap = Bitmap::new();
253    /// bitmap.add_range(0..100);
254    /// bitmap.add(222);
255    /// bitmap.add(555);
256    /// bitmap.add(999);
257    ///
258    /// let mut buf = [0; 100];
259    /// let mut cursor = bitmap.cursor();
260    /// assert_eq!(cursor.read_many(&mut buf), 100);
261    /// // Get the first 100 items, from the original range added
262    /// for (i, item) in buf.iter().enumerate() {
263    ///     assert_eq!(*item, i as u32);
264    /// }
265    /// // Calls to next_many() can be interleaved with other cursor calls
266    /// assert_eq!(cursor.current(), Some(222));
267    /// assert_eq!(cursor.next(), Some(555));
268    /// assert_eq!(cursor.read_many(&mut buf), 2);
269    /// assert_eq!(buf[0], 555);
270    /// assert_eq!(buf[1], 999);
271    ///
272    /// assert_eq!(cursor.current(), None);
273    /// assert_eq!(cursor.read_many(&mut buf), 0);
274    /// ```
275    ///
276    /// ```
277    /// use croaring::Bitmap;
278    ///
279    /// fn print_by_chunks(bitmap: &Bitmap) {
280    ///     let mut buf = [0; 1024];
281    ///     let mut iter = bitmap.cursor();
282    ///     loop {
283    ///         let n = iter.read_many(&mut buf);
284    ///         if n == 0 {
285    ///             break;
286    ///         }
287    ///         println!("{:?}", &buf[..n]);
288    ///     }
289    /// }
290    ///
291    /// # print_by_chunks(&Bitmap::of(&[1, 2, 8, 20, 1000]));
292    /// ```
293    #[inline]
294    #[doc(alias = "roaring_uint32_iterator_read")]
295    #[must_use]
296    pub fn read_many(&mut self, dst: &mut [u32]) -> usize {
297        let count = u32::try_from(dst.len()).unwrap_or(u32::MAX);
298        let result =
299            unsafe { ffi::roaring_uint32_iterator_read(&mut self.raw, dst.as_mut_ptr(), count) };
300        debug_assert!(result <= count);
301        result as usize
302    }
303
304    /// Reset the iterator to the first value `>= val`
305    ///
306    /// This can move the iterator forwards or backwards.
307    ///
308    /// # Examples
309    /// ```
310    /// use croaring::Bitmap;
311    ///
312    /// let bitmap = Bitmap::of(&[0, 1, 100, 1000, u32::MAX]);
313    /// let mut cursor = bitmap.cursor();
314    /// cursor.reset_at_or_after(0);
315    /// assert_eq!(cursor.current(), Some(0));
316    /// cursor.reset_at_or_after(0);
317    /// assert_eq!(cursor.current(), Some(0));
318    ///
319    /// cursor.reset_at_or_after(101);
320    /// assert_eq!(cursor.current(), Some(1000));
321    /// assert_eq!(cursor.next(), Some(u32::MAX));
322    /// assert_eq!(cursor.next(), None);
323    /// cursor.reset_at_or_after(u32::MAX);
324    /// assert_eq!(cursor.current(), Some(u32::MAX));
325    /// assert_eq!(cursor.next(), None);
326    /// ```
327    #[inline]
328    #[doc(alias = "roaring_uint32_iterator_move_equalorlarger")]
329    pub fn reset_at_or_after(&mut self, val: u32) {
330        unsafe { ffi::roaring_uint32_iterator_move_equalorlarger(&mut self.raw, val) };
331    }
332
333    /// Advance the cursor by `n` positions
334    ///
335    /// Returns the number of positions actually advanced, which may be less than `n` if the
336    /// cursor reaches the end of the bitmap.
337    ///
338    /// This function is equivalent to calling [`move_next`](Self::move_next) up to `n` times,
339    /// but can be much more efficient.
340    ///
341    /// # Examples
342    /// ```
343    /// use croaring::Bitmap;
344    ///
345    /// let mut bitmap = Bitmap::of(&[1, 2, 3, 4, 5]);
346    /// let mut cursor = bitmap.cursor();
347    /// assert_eq!(cursor.current(), Some(1));
348    /// assert_eq!(cursor.skip(2), 2);
349    /// assert_eq!(cursor.current(), Some(3));
350    /// assert_eq!(cursor.skip(10), 3); // Skipped to one past the end
351    /// assert_eq!(cursor.current(), None);
352    /// ```
353    #[inline]
354    #[doc(alias = "roaring_uint32_iterator_skip")]
355    pub fn skip(&mut self, n: u32) -> u32 {
356        unsafe { ffi::roaring_uint32_iterator_skip(&mut self.raw, n) }
357    }
358
359    /// Move the cursor backwards by `n` positions
360    ///
361    /// Returns the number of positions actually moved backwards, which may be less than `n` if the
362    /// cursor reaches the beginning of the bitmap.
363    ///
364    /// This function is equivalent to calling [`move_prev`](Self::move_prev) up to `n` times,
365    /// but can be much more efficient.
366    ///
367    /// # Examples
368    /// ```
369    /// use croaring::Bitmap;
370    ///
371    /// let mut bitmap = Bitmap::of(&[1, 2, 3, 4, 5]);
372    /// let mut cursor = bitmap.cursor_to_last();
373    /// assert_eq!(cursor.current(), Some(5));
374    /// assert_eq!(cursor.skip_backward(2), 2);
375    /// assert_eq!(cursor.current(), Some(3));
376    /// assert_eq!(cursor.skip_backward(10), 3); // Skipped to one before the beginning
377    /// assert_eq!(cursor.current(), None);
378    /// ```
379    #[inline]
380    #[doc(alias = "roaring_uint32_iterator_skip_backward")]
381    pub fn skip_backward(&mut self, n: u32) -> u32 {
382        unsafe { ffi::roaring_uint32_iterator_skip_backward(&mut self.raw, n) }
383    }
384}
385
386/// Iterator over the values of a bitmap
387#[derive(Clone)]
388pub struct BitmapIterator<'a> {
389    cursor: BitmapCursor<'a>,
390}
391
392impl<'a> BitmapIterator<'a> {
393    fn new(bitmap: &'a Bitmap) -> Self {
394        Self {
395            cursor: BitmapCursor::at_first(bitmap),
396        }
397    }
398
399    #[inline]
400    fn current_value(&self) -> Option<u32> {
401        self.cursor.current()
402    }
403
404    #[inline]
405    fn advance(&mut self) {
406        self.cursor.move_next();
407    }
408
409    /// Attempt to read many values from the iterator into `dst`
410    ///
411    /// Returns the number of items read from the iterator, may be `< dst.len()` iff
412    /// the iterator is exhausted or `dst.len() > u32::MAX`.
413    ///
414    /// This can be much more efficient than repeated iteration.
415    ///
416    /// # Examples
417    ///
418    /// ```
419    /// use croaring::Bitmap;
420    ///
421    /// let mut bitmap: Bitmap = Bitmap::new();
422    /// bitmap.add_range(0..100);
423    /// bitmap.add(222);
424    /// bitmap.add(555);
425    ///
426    /// let mut buf = [0; 100];
427    /// let mut iter = bitmap.iter();
428    /// assert_eq!(iter.next_many(&mut buf), 100);
429    /// // Get the first 100 items, from the original range added
430    /// for (i, item) in buf.iter().enumerate() {
431    ///     assert_eq!(*item, i as u32);
432    /// }
433    /// // Calls to next_many() can be interleaved with calls to next()
434    /// assert_eq!(iter.next(), Some(222));
435    /// assert_eq!(iter.next_many(&mut buf), 1);
436    /// assert_eq!(buf[0], 555);
437    ///
438    /// assert_eq!(iter.next(), None);
439    /// assert_eq!(iter.next_many(&mut buf), 0);
440    /// ```
441    ///
442    /// ```
443    /// use croaring::Bitmap;
444    ///
445    /// fn print_by_chunks(bitmap: &Bitmap) {
446    ///     let mut buf = [0; 1024];
447    ///     let mut iter = bitmap.iter();
448    ///     loop {
449    ///         let n = iter.next_many(&mut buf);
450    ///         if n == 0 {
451    ///             break;
452    ///         }
453    ///         println!("{:?}", &buf[..n]);
454    ///     }
455    /// }
456    ///
457    /// # print_by_chunks(&Bitmap::of(&[1, 2, 8, 20, 1000]));
458    /// ```
459    #[inline]
460    #[doc(alias = "roaring_uint32_iterator_read")]
461    #[must_use]
462    pub fn next_many(&mut self, dst: &mut [u32]) -> usize {
463        self.cursor.read_many(dst)
464    }
465
466    /// Reset the iterator to the first value `>= val`
467    ///
468    /// This can move the iterator forwards or backwards.
469    ///
470    /// # Examples
471    /// ```
472    /// use croaring::Bitmap;
473    ///
474    /// let mut bitmap = Bitmap::of(&[0, 1, 100, 1000, u32::MAX]);
475    /// let mut iter = bitmap.iter();
476    /// iter.reset_at_or_after(0);
477    /// assert_eq!(iter.next(), Some(0));
478    /// iter.reset_at_or_after(0);
479    /// assert_eq!(iter.next(), Some(0));
480    ///
481    /// iter.reset_at_or_after(101);
482    /// assert_eq!(iter.next(), Some(1000));
483    /// assert_eq!(iter.next(), Some(u32::MAX));
484    /// assert_eq!(iter.next(), None);
485    /// iter.reset_at_or_after(u32::MAX);
486    /// assert_eq!(iter.next(), Some(u32::MAX));
487    /// assert_eq!(iter.next(), None);
488    /// ```
489    #[inline]
490    #[doc(alias = "roaring_uint32_iterator_move_equalorlarger")]
491    pub fn reset_at_or_after(&mut self, val: u32) {
492        self.cursor.reset_at_or_after(val);
493    }
494
495    /// Peek at the next value to be returned by the iterator (if any), without consuming it
496    ///
497    /// # Examples
498    ///
499    /// ```
500    /// use croaring::Bitmap;
501    /// let mut bitmap = Bitmap::of(&[1, 2, 3]);
502    /// let mut iter = bitmap.iter();
503    /// assert_eq!(iter.peek(), Some(1));
504    /// assert_eq!(iter.next(), Some(1));
505    /// ```
506    #[inline]
507    #[must_use]
508    pub fn peek(&self) -> Option<u32> {
509        self.cursor.current()
510    }
511}
512
513impl<'a> Iterator for BitmapIterator<'a> {
514    type Item = u32;
515
516    fn next(&mut self) -> Option<Self::Item> {
517        match self.current_value() {
518            Some(value) => {
519                self.advance();
520
521                Some(value)
522            }
523            None => None,
524        }
525    }
526
527    fn nth(&mut self, n: usize) -> Option<Self::Item> {
528        if n > 0 {
529            // `next` yeilds the current value from the cursor, so if we skip `n` values,
530            // the next call to `next` will return the `n`th value.
531            //
532            // Unlike most calls to `nth`, we want to skip `n` values, not `n-1`.
533            self.cursor.skip(u32::try_from(n).unwrap_or(u32::MAX));
534        }
535        self.next()
536    }
537}
538
539impl Bitmap {
540    /// Returns an iterator over each value stored in the bitmap.
541    /// Returned values are ordered in ascending order.
542    ///
543    /// # Examples
544    ///
545    /// ```
546    /// use croaring::Bitmap;
547    ///
548    /// let mut bitmap = Bitmap::new();
549    /// bitmap.add(4);
550    /// bitmap.add(3);
551    /// bitmap.add(2);
552    /// let mut iterator = bitmap.iter();
553    ///
554    /// assert_eq!(iterator.next(), Some(2));
555    /// assert_eq!(iterator.next(), Some(3));
556    /// assert_eq!(iterator.next(), Some(4));
557    /// assert_eq!(iterator.next(), None);
558    /// ```
559    #[inline]
560    #[doc(alias = "roaring_init_iterator")]
561    #[must_use]
562    pub fn iter(&self) -> BitmapIterator<'_> {
563        BitmapIterator::new(self)
564    }
565
566    /// Returns a cursor pointing at the first value in the bitmap.
567    ///
568    /// See [`BitmapCursor`] for more details.
569    #[inline]
570    #[must_use]
571    pub fn cursor(&self) -> BitmapCursor<'_> {
572        BitmapCursor::at_first(self)
573    }
574
575    /// Returns a cursor pointing at the last value in the bitmap.
576    ///
577    /// See [`BitmapCursor`] for more details.
578    #[inline]
579    #[must_use]
580    pub fn cursor_to_last(&self) -> BitmapCursor<'_> {
581        BitmapCursor::at_last(self)
582    }
583}
584
585/// Converts this iterator into a cursor
586///
587/// The cursor's current value will be the the item which would have been returned by the next call to `next()`
588/// or one past the end of the bitmap if the iterator is exhausted.
589///
590/// # Examples
591///
592/// ```
593/// use croaring::bitmap::{Bitmap, BitmapCursor};
594/// let mut bitmap = Bitmap::of(&[1, 2, 3]);
595/// let mut iter = bitmap.iter();
596/// assert_eq!(iter.peek(), Some(1));
597/// assert_eq!(iter.next(), Some(1));
598///
599/// assert_eq!(iter.peek(), Some(2));
600/// let mut cursor: BitmapCursor = iter.into();
601/// assert_eq!(cursor.current(), Some(2));
602/// ```
603impl<'a> From<BitmapIterator<'a>> for BitmapCursor<'a> {
604    fn from(iterator: BitmapIterator<'a>) -> Self {
605        iterator.cursor
606    }
607}
608
609/// Converts this cursor into an iterator
610///
611/// The next value returned by the iterator will be the current value of the cursor (if any).
612///
613/// # Examples
614///
615/// ```
616/// use croaring::bitmap::{Bitmap, BitmapIterator};
617///
618/// let mut bitmap = Bitmap::of(&[1, 2, 3]);
619/// let mut cursor = bitmap.cursor();
620/// assert_eq!(cursor.current(), Some(1));
621///
622/// let mut iter = BitmapIterator::from(cursor);
623/// assert_eq!(iter.next(), Some(1));
624/// ```
625impl<'a> From<BitmapCursor<'a>> for BitmapIterator<'a> {
626    fn from(cursor: BitmapCursor<'a>) -> Self {
627        BitmapIterator { cursor }
628    }
629}
630
631impl FromIterator<u32> for Bitmap {
632    /// Convenience method for creating bitmap from iterator.
633    ///
634    /// # Examples
635    ///
636    /// ```
637    /// use croaring::Bitmap;
638    ///
639    /// let bitmap: Bitmap = (1..3).collect();
640    ///
641    /// assert!(!bitmap.is_empty());
642    /// assert!(bitmap.contains(1));
643    /// assert!(bitmap.contains(2));
644    /// assert_eq!(bitmap.cardinality(), 2);
645    /// ```
646    fn from_iter<I: IntoIterator<Item = u32>>(iter: I) -> Self {
647        let mut bitmap = Bitmap::new();
648        bitmap.extend(iter);
649        bitmap
650    }
651}
652
653impl Extend<u32> for Bitmap {
654    fn extend<T: IntoIterator<Item = u32>>(&mut self, iter: T) {
655        let mut ctx = MaybeUninit::<ffi::roaring_bulk_context_t>::zeroed();
656        iter.into_iter().for_each(|item| unsafe {
657            ffi::roaring_bitmap_add_bulk(&mut self.bitmap, ctx.as_mut_ptr(), item);
658        });
659    }
660}
661
662#[cfg(test)]
663mod tests {
664    use super::*;
665
666    #[test]
667    fn iter_nth() {
668        let bitmap = Bitmap::of(&[1, 2, 3, 4, 5]);
669        let mut iter = bitmap.iter();
670        assert_eq!(iter.nth(0), Some(1));
671        assert_eq!(iter.nth(1), Some(3));
672        assert_eq!(iter.nth(10), None);
673    }
674}