arrow2/bitmap/utils/
iterator.rs1use crate::trusted_len::TrustedLen;
2
3use super::get_bit_unchecked;
4
5#[derive(Debug, Clone)]
8pub struct BitmapIter<'a> {
9 bytes: &'a [u8],
10 index: usize,
11 end: usize,
12}
13
14impl<'a> BitmapIter<'a> {
15 pub fn new(slice: &'a [u8], offset: usize, len: usize) -> Self {
17 let bytes = &slice[offset / 8..];
23 let index = offset % 8;
25 let end = len + index;
27 assert!(end <= bytes.len() * 8);
29 Self { bytes, index, end }
33 }
34}
35
36impl<'a> Iterator for BitmapIter<'a> {
37 type Item = bool;
38
39 #[inline]
40 fn next(&mut self) -> Option<Self::Item> {
41 if self.index == self.end {
42 return None;
43 }
44 let old = self.index;
45 self.index += 1;
46 Some(unsafe { get_bit_unchecked(self.bytes, old) })
48 }
49
50 #[inline]
51 fn size_hint(&self) -> (usize, Option<usize>) {
52 let exact = self.end - self.index;
53 (exact, Some(exact))
54 }
55
56 #[inline]
57 fn nth(&mut self, n: usize) -> Option<Self::Item> {
58 let new_index = self.index + n;
59 if new_index > self.end {
60 self.index = self.end;
61 None
62 } else {
63 self.index = new_index;
64 self.next()
65 }
66 }
67}
68
69impl<'a> DoubleEndedIterator for BitmapIter<'a> {
70 #[inline]
71 fn next_back(&mut self) -> Option<bool> {
72 if self.index == self.end {
73 None
74 } else {
75 self.end -= 1;
76 Some(unsafe { get_bit_unchecked(self.bytes, self.end) })
78 }
79 }
80}
81
82unsafe impl TrustedLen for BitmapIter<'_> {}
83impl ExactSizeIterator for BitmapIter<'_> {}