Path: blob/main/crates/polars-arrow/src/array/indexable.rs
6939 views
#![allow(unsafe_op_in_unsafe_fn)]1use std::borrow::Borrow;23use crate::array::{4MutableArray, MutableBinaryArray, MutableBinaryValuesArray, MutableBinaryViewArray,5MutableBooleanArray, MutableFixedSizeBinaryArray, MutablePrimitiveArray, MutableUtf8Array,6MutableUtf8ValuesArray, ViewType,7};8use crate::offset::Offset;9use crate::types::NativeType;1011/// Trait for arrays that can be indexed directly to extract a value.12pub trait Indexable {13/// The type of the element at index `i`; may be a reference type or a value type.14type Value<'a>: Borrow<Self::Type>15where16Self: 'a;1718type Type: ?Sized;1920/// Returns the element at index `i`.21/// # Panic22/// May panic if `i >= self.len()`.23fn value_at(&self, index: usize) -> Self::Value<'_>;2425/// Returns the element at index `i`.26///27/// # Safety28/// Assumes that the `i < self.len`.29#[inline]30unsafe fn value_unchecked_at(&self, index: usize) -> Self::Value<'_> {31self.value_at(index)32}33}3435pub trait AsIndexed<M: Indexable> {36fn as_indexed(&self) -> &M::Type;37}3839impl Indexable for MutableBooleanArray {40type Value<'a> = bool;41type Type = bool;4243#[inline]44fn value_at(&self, i: usize) -> Self::Value<'_> {45self.values().get(i)46}47}4849impl AsIndexed<MutableBooleanArray> for bool {50#[inline]51fn as_indexed(&self) -> &bool {52self53}54}5556impl<O: Offset> Indexable for MutableBinaryArray<O> {57type Value<'a> = &'a [u8];58type Type = [u8];5960#[inline]61fn value_at(&self, i: usize) -> Self::Value<'_> {62// TODO: add .value() / .value_unchecked() to MutableBinaryArray?63assert!(i < self.len());64unsafe { self.value_unchecked_at(i) }65}6667#[inline]68unsafe fn value_unchecked_at(&self, i: usize) -> Self::Value<'_> {69// TODO: add .value() / .value_unchecked() to MutableBinaryArray?70// soundness: the invariant of the function71let (start, end) = self.offsets().start_end_unchecked(i);72// soundness: the invariant of the struct73self.values().get_unchecked(start..end)74}75}7677impl<O: Offset> AsIndexed<MutableBinaryArray<O>> for &[u8] {78#[inline]79fn as_indexed(&self) -> &[u8] {80self81}82}8384impl<O: Offset> Indexable for MutableBinaryValuesArray<O> {85type Value<'a> = &'a [u8];86type Type = [u8];8788#[inline]89fn value_at(&self, i: usize) -> Self::Value<'_> {90self.value(i)91}9293#[inline]94unsafe fn value_unchecked_at(&self, i: usize) -> Self::Value<'_> {95self.value_unchecked(i)96}97}9899impl<O: Offset> AsIndexed<MutableBinaryValuesArray<O>> for &[u8] {100#[inline]101fn as_indexed(&self) -> &[u8] {102self103}104}105106impl Indexable for MutableFixedSizeBinaryArray {107type Value<'a> = &'a [u8];108type Type = [u8];109110#[inline]111fn value_at(&self, i: usize) -> Self::Value<'_> {112self.value(i)113}114115#[inline]116unsafe fn value_unchecked_at(&self, i: usize) -> Self::Value<'_> {117// soundness: the invariant of the struct118self.value_unchecked(i)119}120}121122impl AsIndexed<MutableFixedSizeBinaryArray> for &[u8] {123#[inline]124fn as_indexed(&self) -> &[u8] {125self126}127}128129impl<T: ViewType + ?Sized> Indexable for MutableBinaryViewArray<T> {130type Value<'a> = &'a T;131type Type = T;132133fn value_at(&self, index: usize) -> Self::Value<'_> {134self.value(index)135}136137unsafe fn value_unchecked_at(&self, index: usize) -> Self::Value<'_> {138self.value_unchecked(index)139}140}141142impl<T: ViewType + ?Sized> AsIndexed<MutableBinaryViewArray<T>> for &T {143#[inline]144fn as_indexed(&self) -> &T {145self146}147}148149// TODO: should NativeType derive from Hash?150impl<T: NativeType> Indexable for MutablePrimitiveArray<T> {151type Value<'a> = T;152type Type = T;153154#[inline]155fn value_at(&self, i: usize) -> Self::Value<'_> {156assert!(i < self.len());157// TODO: add Length trait? (for both Array and MutableArray)158unsafe { self.value_unchecked_at(i) }159}160161#[inline]162unsafe fn value_unchecked_at(&self, i: usize) -> Self::Value<'_> {163*self.values().get_unchecked(i)164}165}166167impl<T: NativeType> AsIndexed<MutablePrimitiveArray<T>> for T {168#[inline]169fn as_indexed(&self) -> &T {170self171}172}173174impl<O: Offset> Indexable for MutableUtf8Array<O> {175type Value<'a> = &'a str;176type Type = str;177178#[inline]179fn value_at(&self, i: usize) -> Self::Value<'_> {180self.value(i)181}182183#[inline]184unsafe fn value_unchecked_at(&self, i: usize) -> Self::Value<'_> {185self.value_unchecked(i)186}187}188189impl<O: Offset, V: AsRef<str>> AsIndexed<MutableUtf8Array<O>> for V {190#[inline]191fn as_indexed(&self) -> &str {192self.as_ref()193}194}195196impl<O: Offset> Indexable for MutableUtf8ValuesArray<O> {197type Value<'a> = &'a str;198type Type = str;199200#[inline]201fn value_at(&self, i: usize) -> Self::Value<'_> {202self.value(i)203}204205#[inline]206unsafe fn value_unchecked_at(&self, i: usize) -> Self::Value<'_> {207self.value_unchecked(i)208}209}210211impl<O: Offset, V: AsRef<str>> AsIndexed<MutableUtf8ValuesArray<O>> for V {212#[inline]213fn as_indexed(&self) -> &str {214self.as_ref()215}216}217218219