use std::{iter::FromIterator, sync::Arc};
use crate::{
array::{Array, MutableArray, TryExtend, TryExtendFromSelf, TryPush},
bitmap::{
utils::{BitmapIter, ZipValidity},
Bitmap, MutableBitmap,
},
datatypes::DataType,
error::{Error, Result},
offset::{Offset, Offsets},
trusted_len::TrustedLen,
};
use super::{BinaryArray, MutableBinaryValuesArray, MutableBinaryValuesIter};
use crate::array::physical_binary::*;
#[derive(Debug, Clone)]
pub struct MutableBinaryArray<O: Offset> {
values: MutableBinaryValuesArray<O>,
validity: Option<MutableBitmap>,
}
impl<O: Offset> From<MutableBinaryArray<O>> for BinaryArray<O> {
fn from(other: MutableBinaryArray<O>) -> Self {
let validity = other.validity.and_then(|x| {
let validity: Option<Bitmap> = x.into();
validity
});
let array: BinaryArray<O> = other.values.into();
array.with_validity(validity)
}
}
impl<O: Offset> Default for MutableBinaryArray<O> {
fn default() -> Self {
Self::new()
}
}
impl<O: Offset> MutableBinaryArray<O> {
pub fn new() -> Self {
Self::with_capacity(0)
}
pub fn try_new(
data_type: DataType,
offsets: Offsets<O>,
values: Vec<u8>,
validity: Option<MutableBitmap>,
) -> Result<Self> {
let values = MutableBinaryValuesArray::try_new(data_type, offsets, values)?;
if validity
.as_ref()
.map_or(false, |validity| validity.len() != values.len())
{
return Err(Error::oos(
"validity's length must be equal to the number of values",
));
}
Ok(Self { values, validity })
}
pub fn from<T: AsRef<[u8]>, P: AsRef<[Option<T>]>>(slice: P) -> Self {
Self::from_trusted_len_iter(slice.as_ref().iter().map(|x| x.as_ref()))
}
fn default_data_type() -> DataType {
BinaryArray::<O>::default_data_type()
}
pub fn with_capacity(capacity: usize) -> Self {
Self::with_capacities(capacity, 0)
}
pub fn with_capacities(capacity: usize, values: usize) -> Self {
Self {
values: MutableBinaryValuesArray::with_capacities(capacity, values),
validity: None,
}
}
pub fn reserve(&mut self, additional: usize, additional_values: usize) {
self.values.reserve(additional, additional_values);
if let Some(x) = self.validity.as_mut() {
x.reserve(additional)
}
}
pub fn push<T: AsRef<[u8]>>(&mut self, value: Option<T>) {
self.try_push(value).unwrap()
}
pub fn pop(&mut self) -> Option<Vec<u8>> {
let value = self.values.pop()?;
self.validity
.as_mut()
.map(|x| x.pop()?.then(|| ()))
.unwrap_or_else(|| Some(()))
.map(|_| value)
}
fn try_from_iter<P: AsRef<[u8]>, I: IntoIterator<Item = Option<P>>>(iter: I) -> Result<Self> {
let iterator = iter.into_iter();
let (lower, _) = iterator.size_hint();
let mut primitive = Self::with_capacity(lower);
for item in iterator {
primitive.try_push(item.as_ref())?
}
Ok(primitive)
}
fn init_validity(&mut self) {
let mut validity = MutableBitmap::with_capacity(self.values.capacity());
validity.extend_constant(self.len(), true);
validity.set(self.len() - 1, false);
self.validity = Some(validity);
}
pub fn into_arc(self) -> Arc<dyn Array> {
let a: BinaryArray<O> = self.into();
Arc::new(a)
}
pub fn shrink_to_fit(&mut self) {
self.values.shrink_to_fit();
if let Some(validity) = &mut self.validity {
validity.shrink_to_fit()
}
}
impl_mutable_array_mut_validity!();
}
impl<O: Offset> MutableBinaryArray<O> {
pub fn values(&self) -> &Vec<u8> {
self.values.values()
}
pub fn offsets(&self) -> &Offsets<O> {
self.values.offsets()
}
pub fn iter(&self) -> ZipValidity<&[u8], MutableBinaryValuesIter<O>, BitmapIter> {
ZipValidity::new(self.values_iter(), self.validity.as_ref().map(|x| x.iter()))
}
pub fn values_iter(&self) -> MutableBinaryValuesIter<O> {
self.values.iter()
}
}
impl<O: Offset> MutableArray for MutableBinaryArray<O> {
fn len(&self) -> usize {
self.values.len()
}
fn validity(&self) -> Option<&MutableBitmap> {
self.validity.as_ref()
}
fn as_box(&mut self) -> Box<dyn Array> {
let array: BinaryArray<O> = std::mem::take(self).into();
array.boxed()
}
fn as_arc(&mut self) -> Arc<dyn Array> {
let array: BinaryArray<O> = std::mem::take(self).into();
array.arced()
}
fn data_type(&self) -> &DataType {
self.values.data_type()
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
self
}
#[inline]
fn push_null(&mut self) {
self.push::<&[u8]>(None)
}
fn reserve(&mut self, additional: usize) {
self.reserve(additional, 0)
}
fn shrink_to_fit(&mut self) {
self.shrink_to_fit()
}
}
impl<O: Offset, P: AsRef<[u8]>> FromIterator<Option<P>> for MutableBinaryArray<O> {
fn from_iter<I: IntoIterator<Item = Option<P>>>(iter: I) -> Self {
Self::try_from_iter(iter).unwrap()
}
}
impl<O: Offset> MutableBinaryArray<O> {
#[inline]
pub unsafe fn from_trusted_len_iter_unchecked<I, P>(iterator: I) -> Self
where
P: AsRef<[u8]>,
I: Iterator<Item = Option<P>>,
{
let (validity, offsets, values) = trusted_len_unzip(iterator);
Self::try_new(Self::default_data_type(), offsets, values, validity).unwrap()
}
#[inline]
pub fn from_trusted_len_iter<I, P>(iterator: I) -> Self
where
P: AsRef<[u8]>,
I: TrustedLen<Item = Option<P>>,
{
unsafe { Self::from_trusted_len_iter_unchecked(iterator) }
}
#[inline]
pub unsafe fn from_trusted_len_values_iter_unchecked<T: AsRef<[u8]>, I: Iterator<Item = T>>(
iterator: I,
) -> Self {
let (offsets, values) = trusted_len_values_iter(iterator);
Self::try_new(Self::default_data_type(), offsets, values, None).unwrap()
}
#[inline]
pub fn from_trusted_len_values_iter<T: AsRef<[u8]>, I: TrustedLen<Item = T>>(
iterator: I,
) -> Self {
unsafe { Self::from_trusted_len_values_iter_unchecked(iterator) }
}
#[inline]
pub unsafe fn try_from_trusted_len_iter_unchecked<E, I, P>(
iterator: I,
) -> std::result::Result<Self, E>
where
P: AsRef<[u8]>,
I: IntoIterator<Item = std::result::Result<Option<P>, E>>,
{
let iterator = iterator.into_iter();
let (mut validity, offsets, values) = try_trusted_len_unzip(iterator)?;
if validity.as_mut().unwrap().unset_bits() == 0 {
validity = None;
}
Ok(Self::try_new(Self::default_data_type(), offsets, values, validity).unwrap())
}
#[inline]
pub fn try_from_trusted_len_iter<E, I, P>(iterator: I) -> std::result::Result<Self, E>
where
P: AsRef<[u8]>,
I: TrustedLen<Item = std::result::Result<Option<P>, E>>,
{
unsafe { Self::try_from_trusted_len_iter_unchecked(iterator) }
}
#[inline]
pub fn extend_trusted_len_values<I, P>(&mut self, iterator: I)
where
P: AsRef<[u8]>,
I: TrustedLen<Item = P>,
{
unsafe { self.extend_trusted_len_values_unchecked(iterator) }
}
#[inline]
pub fn extend_values<I, P>(&mut self, iterator: I)
where
P: AsRef<[u8]>,
I: Iterator<Item = P>,
{
let length = self.values.len();
self.values.extend(iterator);
let additional = self.values.len() - length;
if let Some(validity) = self.validity.as_mut() {
validity.extend_constant(additional, true);
}
}
#[inline]
pub unsafe fn extend_trusted_len_values_unchecked<I, P>(&mut self, iterator: I)
where
P: AsRef<[u8]>,
I: Iterator<Item = P>,
{
let length = self.values.len();
self.values.extend_trusted_len_unchecked(iterator);
let additional = self.values.len() - length;
if let Some(validity) = self.validity.as_mut() {
validity.extend_constant(additional, true);
}
}
#[inline]
pub fn extend_trusted_len<I, P>(&mut self, iterator: I)
where
P: AsRef<[u8]>,
I: TrustedLen<Item = Option<P>>,
{
unsafe { self.extend_trusted_len_unchecked(iterator) }
}
#[inline]
pub unsafe fn extend_trusted_len_unchecked<I, P>(&mut self, iterator: I)
where
P: AsRef<[u8]>,
I: Iterator<Item = Option<P>>,
{
if self.validity.is_none() {
let mut validity = MutableBitmap::new();
validity.extend_constant(self.len(), true);
self.validity = Some(validity);
}
self.values
.extend_from_trusted_len_iter(self.validity.as_mut().unwrap(), iterator);
}
pub fn from_iter_values<T: AsRef<[u8]>, I: Iterator<Item = T>>(iterator: I) -> Self {
let (offsets, values) = values_iter(iterator);
Self::try_new(Self::default_data_type(), offsets, values, None).unwrap()
}
pub fn extend_fallible<T, I, E>(&mut self, iter: I) -> std::result::Result<(), E>
where
E: std::error::Error,
I: IntoIterator<Item = std::result::Result<Option<T>, E>>,
T: AsRef<[u8]>,
{
let mut iter = iter.into_iter();
self.reserve(iter.size_hint().0, 0);
iter.try_for_each(|x| {
self.push(x?);
Ok(())
})
}
}
impl<O: Offset, T: AsRef<[u8]>> Extend<Option<T>> for MutableBinaryArray<O> {
fn extend<I: IntoIterator<Item = Option<T>>>(&mut self, iter: I) {
self.try_extend(iter).unwrap();
}
}
impl<O: Offset, T: AsRef<[u8]>> TryExtend<Option<T>> for MutableBinaryArray<O> {
fn try_extend<I: IntoIterator<Item = Option<T>>>(&mut self, iter: I) -> Result<()> {
let mut iter = iter.into_iter();
self.reserve(iter.size_hint().0, 0);
iter.try_for_each(|x| self.try_push(x))
}
}
impl<O: Offset, T: AsRef<[u8]>> TryPush<Option<T>> for MutableBinaryArray<O> {
fn try_push(&mut self, value: Option<T>) -> Result<()> {
match value {
Some(value) => {
self.values.try_push(value.as_ref())?;
match &mut self.validity {
Some(validity) => validity.push(true),
None => {}
}
}
None => {
self.values.push("");
match &mut self.validity {
Some(validity) => validity.push(false),
None => self.init_validity(),
}
}
}
Ok(())
}
}
impl<O: Offset> PartialEq for MutableBinaryArray<O> {
fn eq(&self, other: &Self) -> bool {
self.iter().eq(other.iter())
}
}
impl<O: Offset> TryExtendFromSelf for MutableBinaryArray<O> {
fn try_extend_from_self(&mut self, other: &Self) -> Result<()> {
extend_validity(self.len(), &mut self.validity, &other.validity);
self.values.try_extend_from_self(&other.values)
}
}