use arrow::{array::ArrayData, bitmap::Bitmap};
use std::ops::BitAnd;
#[derive(Debug, Clone)]
pub struct BitmapSlice {
offset: usize,
len: usize,
bitmap: Bitmap,
}
impl BitmapSlice {
pub fn offset(&self) -> usize {
self.offset
}
pub fn len(&self) -> usize {
self.len
}
pub fn is_set(&self, i: usize) -> bool {
self.bitmap.is_set(self.offset + i)
}
pub fn from_null_bitmap(data: &ArrayData) -> Option<Self> {
match data.null_buffer() {
None => None,
Some(buf) => Some(Self {
offset: data.offset(),
len: data.len(),
bitmap: Bitmap::from(buf.clone()), }),
}
}
}
impl<'a, 'b> BitAnd<&'b BitmapSlice> for &'a BitmapSlice {
type Output = BitmapSlice;
fn bitand(self, rhs: &'b BitmapSlice) -> Self::Output {
assert_eq!(self.len, rhs.len, "BitmapSlices have different lengths");
assert_eq!(
self.offset, rhs.offset,
"Operations on BitmapSlices with different offsets are not supported"
);
BitmapSlice {
offset: self.offset,
len: self.len,
bitmap: (&self.bitmap & &rhs.bitmap).unwrap(),
}
}
}