croaring_mw/bitmap/
iter.rs1use std::iter::{FromIterator, IntoIterator};
2use std::marker::PhantomData;
3
4use super::{ffi, Bitmap};
5
6pub struct BitmapIterator<'a> {
7 iterator: *mut ffi::roaring_uint32_iterator_s,
8 phantom: PhantomData<&'a ()>,
9}
10
11impl<'a> BitmapIterator<'a> {
12 fn new(bitmap: &Bitmap) -> Self {
13 BitmapIterator {
14 iterator: unsafe { ffi::roaring_create_iterator(bitmap.bitmap) },
15 phantom: PhantomData,
16 }
17 }
18
19 #[inline]
20 fn current_value(&self) -> Option<u32> {
21 unsafe {
22 if self.has_value() {
23 Some((*self.iterator).current_value)
24 } else {
25 None
26 }
27 }
28 }
29
30 #[inline]
31 fn has_value(&self) -> bool {
32 unsafe { (*self.iterator).has_value }
33 }
34
35 #[inline]
36 fn advance(&mut self) -> bool {
37 unsafe { ffi::roaring_advance_uint32_iterator(self.iterator) }
38 }
39}
40
41impl<'a> Iterator for BitmapIterator<'a> {
42 type Item = u32;
43
44 fn next(&mut self) -> Option<Self::Item> {
45 match self.current_value() {
46 Some(value) => {
47 self.advance();
48
49 Some(value)
50 }
51 None => None,
52 }
53 }
54}
55
56impl<'a> Drop for BitmapIterator<'a> {
57 fn drop(&mut self) {
58 unsafe { ffi::roaring_free_uint32_iterator(self.iterator) }
59 }
60}
61
62impl Bitmap {
63 pub fn iter(&self) -> BitmapIterator {
83 BitmapIterator::new(self)
84 }
85}
86
87impl FromIterator<u32> for Bitmap {
88 fn from_iter<I: IntoIterator<Item = u32>>(iter: I) -> Self {
103 Bitmap::of(&Vec::from_iter(iter))
104 }
105}