use crate::{
bitmap::Bitmap,
datatypes::{DataType, PhysicalType},
};
use super::{display_fmt, Array};
mod ffi;
mod from;
mod iterator;
mod mutable;
pub use iterator::*;
pub use mutable::*;
#[derive(Debug, Clone)]
pub struct BooleanArray {
data_type: DataType,
values: Bitmap,
validity: Option<Bitmap>,
offset: usize,
}
impl BooleanArray {
pub fn new_empty(data_type: DataType) -> Self {
Self::from_data(data_type, Bitmap::new(), None)
}
pub fn new_null(data_type: DataType, length: usize) -> Self {
let bitmap = Bitmap::new_zeroed(length);
Self::from_data(data_type, bitmap.clone(), Some(bitmap))
}
pub fn from_data(data_type: DataType, values: Bitmap, validity: Option<Bitmap>) -> Self {
if let Some(ref validity) = validity {
assert_eq!(values.len(), validity.len());
}
if data_type.to_physical_type() != PhysicalType::Boolean {
panic!("BooleanArray can only be initialized with DataType::Boolean")
}
Self {
data_type,
values,
validity,
offset: 0,
}
}
#[inline]
pub fn slice(&self, offset: usize, length: usize) -> Self {
assert!(
offset + length <= self.len(),
"the offset of the new Buffer cannot exceed the existing length"
);
unsafe { self.slice_unchecked(offset, length) }
}
#[inline]
pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self {
let validity = self
.validity
.clone()
.map(|x| x.slice_unchecked(offset, length));
Self {
data_type: self.data_type.clone(),
values: self.values.clone().slice_unchecked(offset, length),
validity,
offset: self.offset + offset,
}
}
pub fn with_validity(&self, validity: Option<Bitmap>) -> Self {
if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) {
panic!("validity should be as least as large as the array")
}
let mut arr = self.clone();
arr.validity = validity;
arr
}
}
impl BooleanArray {
#[inline]
pub fn value(&self, i: usize) -> bool {
self.values.get_bit(i)
}
#[inline]
pub unsafe fn value_unchecked(&self, i: usize) -> bool {
self.values.get_bit_unchecked(i)
}
#[inline]
pub fn validity(&self) -> Option<&Bitmap> {
self.validity.as_ref()
}
#[inline]
pub fn values(&self) -> &Bitmap {
&self.values
}
}
impl Array for BooleanArray {
#[inline]
fn as_any(&self) -> &dyn std::any::Any {
self
}
#[inline]
fn len(&self) -> usize {
self.values.len()
}
#[inline]
fn data_type(&self) -> &DataType {
&self.data_type
}
#[inline]
fn validity(&self) -> Option<&Bitmap> {
self.validity.as_ref()
}
#[inline]
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
#[inline]
unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice_unchecked(offset, length))
}
fn with_validity(&self, validity: Option<Bitmap>) -> Box<dyn Array> {
Box::new(self.with_validity(validity))
}
}
impl std::fmt::Display for BooleanArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
display_fmt(self.iter(), "BooleanArray", f, false)
}
}