use std::fmt::Debug;
use std::fmt::Formatter;
use std::ops::Deref;
use std::sync::Arc;
use crate::ArrayRef;
use crate::IntoArray;
use crate::dtype::DType;
use crate::stats::ArrayStats;
use crate::stats::StatsSetRef;
use crate::vtable::ArrayId;
use crate::vtable::VTable;
pub struct Array<V: VTable> {
vtable: V,
pub(crate) dtype: DType,
pub(crate) len: usize,
pub(crate) array: V::Array,
pub(crate) stats: ArrayStats,
}
#[allow(clippy::same_name_method)]
impl<V: VTable> Array<V> {
pub unsafe fn new_unchecked(
vtable: V,
dtype: DType,
len: usize,
array: V::Array,
stats: ArrayStats,
) -> Self {
Self {
vtable,
dtype,
len,
array,
stats,
}
}
pub fn typed_vtable(&self) -> &V {
&self.vtable
}
pub fn dtype(&self) -> &DType {
&self.dtype
}
pub fn len(&self) -> usize {
self.len
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn encoding_id(&self) -> ArrayId {
self.vtable.id()
}
pub fn statistics(&self) -> StatsSetRef<'_> {
self.stats.to_ref(self)
}
pub fn array_stats(&self) -> &ArrayStats {
&self.stats
}
pub fn inner(&self) -> &V::Array {
&self.array
}
pub fn into_inner(self) -> V::Array {
self.array
}
pub fn to_array_ref(&self) -> ArrayRef {
Arc::new(self.clone())
}
}
impl<V: VTable> Deref for Array<V> {
type Target = V::Array;
fn deref(&self) -> &V::Array {
&self.array
}
}
impl<V: VTable> Clone for Array<V> {
fn clone(&self) -> Self {
Self {
vtable: self.vtable.clone(),
dtype: self.dtype.clone(),
len: self.len,
array: self.array.clone(),
stats: self.stats.clone(),
}
}
}
impl<V: VTable> Debug for Array<V> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Array")
.field("encoding", &self.vtable.id())
.field("dtype", &self.dtype)
.field("len", &self.len)
.field("inner", &self.array)
.finish()
}
}
impl<V: VTable> IntoArray for Array<V> {
fn into_array(self) -> ArrayRef {
Arc::new(self)
}
}
impl<V: VTable> IntoArray for Arc<Array<V>> {
fn into_array(self) -> ArrayRef {
self
}
}
impl<V: VTable> From<Array<V>> for ArrayRef {
fn from(value: Array<V>) -> ArrayRef {
value.into_array()
}
}