pub struct PrimitiveArray<T>where
T: NativeType,{
data_type: DataType,
values: Buffer<T>,
validity: Option<Bitmap>,
}
Expand description
A PrimitiveArray
is Arrow’s semantically equivalent of an immutable Vec<Option<T>>
where
T is NativeType
(e.g. i32
). It implements Array
.
One way to think about a PrimitiveArray
is (DataType, Arc<Vec<T>>, Option<Arc<Vec<u8>>>)
where:
- the first item is the array’s logical type
- the second is the immutable values
- the third is the immutable validity (whether a value is null or not as a bitmap).
The size of this struct is O(1)
, as all data is stored behind an std::sync::Arc
.
§Example
use re_arrow2::array::PrimitiveArray;
use re_arrow2::bitmap::Bitmap;
use re_arrow2::buffer::Buffer;
let array = PrimitiveArray::from([Some(1i32), None, Some(10)]);
assert_eq!(array.value(0), 1);
assert_eq!(array.iter().collect::<Vec<_>>(), vec![Some(&1i32), None, Some(&10)]);
assert_eq!(array.values_iter().copied().collect::<Vec<_>>(), vec![1, 0, 10]);
// the underlying representation
assert_eq!(array.values(), &Buffer::from(vec![1i32, 0, 10]));
assert_eq!(array.validity(), Some(&Bitmap::from([true, false, true])));
Fields§
§data_type: DataType
§values: Buffer<T>
§validity: Option<Bitmap>
Implementations§
§impl<T> PrimitiveArray<T>where
T: NativeType,
impl<T> PrimitiveArray<T>where
T: NativeType,
pub fn try_new(
data_type: DataType,
values: Buffer<T>,
validity: Option<Bitmap>
) -> Result<PrimitiveArray<T>, Error>
pub fn try_new( data_type: DataType, values: Buffer<T>, validity: Option<Bitmap> ) -> Result<PrimitiveArray<T>, Error>
The canonical method to create a PrimitiveArray
out of its internal components.
§Implementation
This function is O(1)
.
§Errors
This function errors iff:
- The validity is not
None
and its length is different fromvalues
’s length - The
data_type
’sPhysicalType
is not equal to [PhysicalType::Primitive(T::PRIMITIVE)
]
pub fn to(self, data_type: DataType) -> PrimitiveArray<T>
pub fn to(self, data_type: DataType) -> PrimitiveArray<T>
Returns a new PrimitiveArray
with a different logical type.
This function is useful to assign a different DataType
to the array.
Used to change the arrays’ logical type (see example).
§Example
use re_arrow2::array::Int32Array;
use re_arrow2::datatypes::DataType;
let array = Int32Array::from(&[Some(1), None, Some(2)]).to(DataType::Date32);
assert_eq!(
format!("{:?}", array),
"Date32[1970-01-02, None, 1970-01-03]"
);
§Panics
Panics iff the data_type
’s PhysicalType
is not equal to [PhysicalType::Primitive(T::PRIMITIVE)
]
pub fn from_vec(values: Vec<T>) -> PrimitiveArray<T>
pub fn from_vec(values: Vec<T>) -> PrimitiveArray<T>
Creates a (non-null) PrimitiveArray
from a vector of values.
This function is O(1)
.
§Examples
use re_arrow2::array::PrimitiveArray;
let array = PrimitiveArray::from_vec(vec![1, 2, 3]);
assert_eq!(format!("{:?}", array), "Int32[1, 2, 3]");
pub fn iter(&self) -> ZipValidity<&T, Iter<'_, T>, BitmapIter<'_>> ⓘ
pub fn iter(&self) -> ZipValidity<&T, Iter<'_, T>, BitmapIter<'_>> ⓘ
Returns an iterator over the values and validity, Option<&T>
.
pub fn values_iter(&self) -> Iter<'_, T>
pub fn values_iter(&self) -> Iter<'_, T>
Returns an iterator of the values, &T
, ignoring the arrays’ validity.
pub fn values(&self) -> &Buffer<T>
pub fn values(&self) -> &Buffer<T>
The values Buffer
.
Values on null slots are undetermined (they can be anything).
pub fn value(&self, i: usize) -> T
pub fn value(&self, i: usize) -> T
Returns the value at slot i
.
Equivalent to self.values()[i]
. The value of a null slot is undetermined (it can be anything).
§Panic
This function panics iff i >= self.len
.
pub unsafe fn value_unchecked(&self, i: usize) -> T
pub unsafe fn value_unchecked(&self, i: usize) -> T
Returns the value at index i
.
The value on null slots is undetermined (it can be anything).
§Safety
Caller must be sure that i < self.len()
pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize)
pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize)
Slices this PrimitiveArray
by an offset and length.
§Implementation
This operation is O(1)
.
§Safety
The caller must ensure that offset + length <= self.len()
.
pub fn sliced(self, offset: usize, length: usize) -> PrimitiveArray<T>
pub fn sliced(self, offset: usize, length: usize) -> PrimitiveArray<T>
pub unsafe fn sliced_unchecked(
self,
offset: usize,
length: usize
) -> PrimitiveArray<T>
pub unsafe fn sliced_unchecked( self, offset: usize, length: usize ) -> PrimitiveArray<T>
pub fn with_validity(self, validity: Option<Bitmap>) -> PrimitiveArray<T>
pub fn with_validity(self, validity: Option<Bitmap>) -> PrimitiveArray<T>
pub fn set_validity(&mut self, validity: Option<Bitmap>)
pub fn set_validity(&mut self, validity: Option<Bitmap>)
pub fn boxed(self) -> Box<dyn Array>
pub fn boxed(self) -> Box<dyn Array>
Boxes this array into a Box<dyn Array>
.
pub fn arced(self) -> Arc<dyn Array>
pub fn arced(self) -> Arc<dyn Array>
Arcs this array into a std::sync::Arc<dyn Array>
.
pub fn with_values(self, values: Buffer<T>) -> PrimitiveArray<T>
pub fn with_values(self, values: Buffer<T>) -> PrimitiveArray<T>
Returns this PrimitiveArray
with new values.
§Panics
This function panics iff values.len() != self.len()
.
pub fn set_values(&mut self, values: Buffer<T>)
pub fn set_values(&mut self, values: Buffer<T>)
Update the values of this PrimitiveArray
.
§Panics
This function panics iff values.len() != self.len()
.
pub fn apply_validity<F>(&mut self, f: F)
pub fn apply_validity<F>(&mut self, f: F)
pub fn get_mut_values(&mut self) -> Option<&mut [T]>
pub fn get_mut_values(&mut self) -> Option<&mut [T]>
Returns an option of a mutable reference to the values of this PrimitiveArray
.
pub fn into_inner(self) -> (DataType, Buffer<T>, Option<Bitmap>)
pub fn into_inner(self) -> (DataType, Buffer<T>, Option<Bitmap>)
Returns its internal representation
pub fn from_inner(
data_type: DataType,
values: Buffer<T>,
validity: Option<Bitmap>
) -> Result<PrimitiveArray<T>, Error>
pub fn from_inner( data_type: DataType, values: Buffer<T>, validity: Option<Bitmap> ) -> Result<PrimitiveArray<T>, Error>
Creates a [PrimitiveArray]
from its internal representation.
This is the inverted from [PrimitiveArray::into_inner]
pub unsafe fn from_inner_unchecked(
data_type: DataType,
values: Buffer<T>,
validity: Option<Bitmap>
) -> PrimitiveArray<T>
pub unsafe fn from_inner_unchecked( data_type: DataType, values: Buffer<T>, validity: Option<Bitmap> ) -> PrimitiveArray<T>
Creates a [PrimitiveArray]
from its internal representation.
This is the inverted from [PrimitiveArray::into_inner]
§Safety
Callers must ensure all invariants of this struct are upheld.
pub fn into_mut(self) -> Either<PrimitiveArray<T>, MutablePrimitiveArray<T>> ⓘ
pub fn into_mut(self) -> Either<PrimitiveArray<T>, MutablePrimitiveArray<T>> ⓘ
Try to convert this PrimitiveArray
to a MutablePrimitiveArray
via copy-on-write semantics.
A PrimitiveArray
is backed by a Buffer
and Bitmap
which are essentially Arc<Vec<_>>
.
This function returns a MutablePrimitiveArray
(via std::sync::Arc::get_mut
) iff both values
and validity have not been cloned / are unique references to their underlying vectors.
This function is primarily used to re-use memory regions.
pub fn new_empty(data_type: DataType) -> PrimitiveArray<T>
pub fn new_empty(data_type: DataType) -> PrimitiveArray<T>
Returns a new empty (zero-length) PrimitiveArray
.
pub fn new_null(data_type: DataType, length: usize) -> PrimitiveArray<T>
pub fn new_null(data_type: DataType, length: usize) -> PrimitiveArray<T>
Returns a new PrimitiveArray
where all slots are null / None
.
pub fn from_values<I>(iter: I) -> PrimitiveArray<T>where
I: IntoIterator<Item = T>,
pub fn from_values<I>(iter: I) -> PrimitiveArray<T>where
I: IntoIterator<Item = T>,
Creates a (non-null) PrimitiveArray
from an iterator of values.
§Implementation
This does not assume that the iterator has a known length.
pub fn from_slice<P>(slice: P) -> PrimitiveArray<T>
pub fn from_slice<P>(slice: P) -> PrimitiveArray<T>
Creates a (non-null) PrimitiveArray
from a slice of values.
§Implementation
This is essentially a memcopy and is thus O(N)
pub fn from_trusted_len_values_iter<I>(iter: I) -> PrimitiveArray<T>where
I: TrustedLen<Item = T>,
pub fn from_trusted_len_values_iter<I>(iter: I) -> PrimitiveArray<T>where
I: TrustedLen<Item = T>,
Creates a (non-null) PrimitiveArray
from a TrustedLen
of values.
§Implementation
This does not assume that the iterator has a known length.
pub unsafe fn from_trusted_len_values_iter_unchecked<I>(
iter: I
) -> PrimitiveArray<T>where
I: Iterator<Item = T>,
pub unsafe fn from_trusted_len_values_iter_unchecked<I>(
iter: I
) -> PrimitiveArray<T>where
I: Iterator<Item = T>,
Creates a new PrimitiveArray
from an iterator over values
§Safety
The iterator must be TrustedLen
.
I.e. that size_hint().1
correctly reports its length.
pub fn from_trusted_len_iter<I>(iter: I) -> PrimitiveArray<T>where
I: TrustedLen<Item = Option<T>>,
pub fn from_trusted_len_iter<I>(iter: I) -> PrimitiveArray<T>where
I: TrustedLen<Item = Option<T>>,
Creates a PrimitiveArray
from a TrustedLen
of optional values.
pub unsafe fn from_trusted_len_iter_unchecked<I>(iter: I) -> PrimitiveArray<T>
pub unsafe fn from_trusted_len_iter_unchecked<I>(iter: I) -> PrimitiveArray<T>
Creates a PrimitiveArray
from an iterator of optional values.
§Safety
The iterator must be TrustedLen
.
I.e. that size_hint().1
correctly reports its length.
pub fn new(
data_type: DataType,
values: Buffer<T>,
validity: Option<Bitmap>
) -> PrimitiveArray<T>
pub fn new( data_type: DataType, values: Buffer<T>, validity: Option<Bitmap> ) -> PrimitiveArray<T>
Alias for Self::try_new(..).unwrap()
.
§Panics
This function errors iff:
- The validity is not
None
and its length is different fromvalues
’s length - The
data_type
’sPhysicalType
is not equal toPhysicalType::Primitive
.
Trait Implementations§
§impl<T> Arrow2Arrow for PrimitiveArray<T>where
T: NativeType,
impl<T> Arrow2Arrow for PrimitiveArray<T>where
T: NativeType,
§impl<T> Array for PrimitiveArray<T>where
T: NativeType,
impl<T> Array for PrimitiveArray<T>where
T: NativeType,
§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Any
, which enables downcasting to concrete types.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Any
, which enables mutable downcasting to concrete types.§fn len(&self) -> usize
fn len(&self) -> usize
Array
. Every array has a length corresponding to the number of
elements (slots).§fn data_type(&self) -> &DataType
fn data_type(&self) -> &DataType
DataType
of the Array
. In combination with Array::as_any
, this can be
used to downcast trait objects (dyn Array
) to concrete arrays.§fn null_count(&self) -> usize
fn null_count(&self) -> usize
§unsafe fn is_null_unchecked(&self, i: usize) -> bool
unsafe fn is_null_unchecked(&self, i: usize) -> bool
i
is null. Read more§impl<T> Clone for PrimitiveArray<T>where
T: Clone + NativeType,
impl<T> Clone for PrimitiveArray<T>where
T: Clone + NativeType,
§fn clone(&self) -> PrimitiveArray<T>
fn clone(&self) -> PrimitiveArray<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more§impl<T> Debug for PrimitiveArray<T>where
T: NativeType,
impl<T> Debug for PrimitiveArray<T>where
T: NativeType,
§impl<T> Default for PrimitiveArray<T>where
T: NativeType,
impl<T> Default for PrimitiveArray<T>where
T: NativeType,
§fn default() -> PrimitiveArray<T>
fn default() -> PrimitiveArray<T>
§impl<'a, T> From<GrowablePrimitive<'a, T>> for PrimitiveArray<T>where
T: NativeType,
impl<'a, T> From<GrowablePrimitive<'a, T>> for PrimitiveArray<T>where
T: NativeType,
§fn from(val: GrowablePrimitive<'a, T>) -> PrimitiveArray<T>
fn from(val: GrowablePrimitive<'a, T>) -> PrimitiveArray<T>
§impl<T> From<MutablePrimitiveArray<T>> for PrimitiveArray<T>where
T: NativeType,
impl<T> From<MutablePrimitiveArray<T>> for PrimitiveArray<T>where
T: NativeType,
§fn from(other: MutablePrimitiveArray<T>) -> PrimitiveArray<T>
fn from(other: MutablePrimitiveArray<T>) -> PrimitiveArray<T>
§impl<T, P> From<P> for PrimitiveArray<T>
impl<T, P> From<P> for PrimitiveArray<T>
§fn from(slice: P) -> PrimitiveArray<T>
fn from(slice: P) -> PrimitiveArray<T>
§impl<T, Ptr> FromIterator<Ptr> for PrimitiveArray<T>
impl<T, Ptr> FromIterator<Ptr> for PrimitiveArray<T>
§fn from_iter<I>(iter: I) -> PrimitiveArray<T>where
I: IntoIterator<Item = Ptr>,
fn from_iter<I>(iter: I) -> PrimitiveArray<T>where
I: IntoIterator<Item = Ptr>,
§impl<'a, T> IntoIterator for &'a PrimitiveArray<T>where
T: NativeType,
impl<'a, T> IntoIterator for &'a PrimitiveArray<T>where
T: NativeType,
§type IntoIter = ZipValidity<&'a T, Iter<'a, T>, BitmapIter<'a>>
type IntoIter = ZipValidity<&'a T, Iter<'a, T>, BitmapIter<'a>>
§fn into_iter(self) -> <&'a PrimitiveArray<T> as IntoIterator>::IntoIter
fn into_iter(self) -> <&'a PrimitiveArray<T> as IntoIterator>::IntoIter
§impl<T> IntoIterator for PrimitiveArray<T>where
T: NativeType,
impl<T> IntoIterator for PrimitiveArray<T>where
T: NativeType,
§type IntoIter = ZipValidity<T, IntoIter<T>, IntoIter>
type IntoIter = ZipValidity<T, IntoIter<T>, IntoIter>
§fn into_iter(self) -> <PrimitiveArray<T> as IntoIterator>::IntoIter
fn into_iter(self) -> <PrimitiveArray<T> as IntoIterator>::IntoIter
§impl<T> PartialEq<&(dyn Array + 'static)> for PrimitiveArray<T>where
T: NativeType,
impl<T> PartialEq<&(dyn Array + 'static)> for PrimitiveArray<T>where
T: NativeType,
§impl<T> PartialEq<PrimitiveArray<T>> for &(dyn Array + 'static)where
T: NativeType,
impl<T> PartialEq<PrimitiveArray<T>> for &(dyn Array + 'static)where
T: NativeType,
§fn eq(&self, other: &PrimitiveArray<T>) -> bool
fn eq(&self, other: &PrimitiveArray<T>) -> bool
self
and other
values to be equal, and is used
by ==
.§impl<T> PartialEq for PrimitiveArray<T>where
T: NativeType,
impl<T> PartialEq for PrimitiveArray<T>where
T: NativeType,
§fn eq(&self, other: &PrimitiveArray<T>) -> bool
fn eq(&self, other: &PrimitiveArray<T>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<T> SizeBytes for PrimitiveArray<T>where
T: SizeBytes + NativeType,
impl<T> SizeBytes for PrimitiveArray<T>where
T: SizeBytes + NativeType,
source§fn heap_size_bytes(&self) -> u64
fn heap_size_bytes(&self) -> u64
self
on the heap, in bytes.source§fn total_size_bytes(&self) -> u64
fn total_size_bytes(&self) -> u64
self
in bytes, accounting for both stack and heap space.source§fn stack_size_bytes(&self) -> u64
fn stack_size_bytes(&self) -> u64
self
on the stack, in bytes. Read moreAuto Trait Implementations§
impl<T> Freeze for PrimitiveArray<T>
impl<T> RefUnwindSafe for PrimitiveArray<T>
impl<T> Send for PrimitiveArray<T>
impl<T> Sync for PrimitiveArray<T>
impl<T> Unpin for PrimitiveArray<T>
impl<T> UnwindSafe for PrimitiveArray<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request