Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Added convenience accessor array.get #1416

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/array/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ impl<O: Offset> BinaryArray<O> {
self.values.get_unchecked(start..end)
}

/// Returns the element at index `i` or `None` if it is null
/// # Panics
/// iff `i >= self.len()`
#[inline]
pub fn get(&self, i: usize) -> Option<&[u8]> {
if !self.is_null(i) {
// soundness: Array::is_null panics if i >= self.len
unsafe { Some(self.value_unchecked(i)) }
} else {
None
}
}

/// Returns the [`DataType`] of this array.
#[inline]
pub fn data_type(&self) -> &DataType {
Expand Down
13 changes: 13 additions & 0 deletions src/array/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ impl BooleanArray {
self.values.get_bit_unchecked(i)
}

/// Returns the element at index `i` or `None` if it is null
/// # Panics
/// iff `i >= self.len()`
#[inline]
pub fn get(&self, i: usize) -> Option<bool> {
if !self.is_null(i) {
// soundness: Array::is_null panics if i >= self.len
unsafe { Some(self.value_unchecked(i)) }
} else {
None
}
}

/// Slices this [`BooleanArray`].
/// # Implementation
/// This operation is `O(1)` as it amounts to increase up to two ref counts.
Expand Down
13 changes: 13 additions & 0 deletions src/array/fixed_size_binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ impl FixedSizeBinaryArray {
.get_unchecked(i * self.size..(i + 1) * self.size)
}

/// Returns the element at index `i` or `None` if it is null
/// # Panics
/// iff `i >= self.len()`
#[inline]
pub fn get(&self, i: usize) -> Option<&[u8]> {
if !self.is_null(i) {
// soundness: Array::is_null panics if i >= self.len
unsafe { Some(self.value_unchecked(i)) }
} else {
None
}
}

/// Returns a new [`FixedSizeBinaryArray`] with a different logical type.
/// This is `O(1)`.
/// # Panics
Expand Down
13 changes: 13 additions & 0 deletions src/array/fixed_size_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ impl FixedSizeListArray {
pub unsafe fn value_unchecked(&self, i: usize) -> Box<dyn Array> {
self.values.sliced_unchecked(i * self.size, self.size)
}

/// Returns the element at index `i` or `None` if it is null
/// # Panics
/// iff `i >= self.len()`
#[inline]
pub fn get(&self, i: usize) -> Option<Box<dyn Array>> {
if !self.is_null(i) {
// soundness: Array::is_null panics if i >= self.len
unsafe { Some(self.value_unchecked(i)) }
} else {
None
}
}
}

impl FixedSizeListArray {
Expand Down
13 changes: 13 additions & 0 deletions src/array/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ impl<T: NativeType> PrimitiveArray<T> {
*self.values.get_unchecked(i)
}

/// Returns the element at index `i` or `None` if it is null
/// # Panics
/// iff `i >= self.len()`
#[inline]
pub fn get(&self, i: usize) -> Option<T> {
if !self.is_null(i) {
// soundness: Array::is_null panics if i >= self.len
unsafe { Some(self.value_unchecked(i)) }
} else {
None
}
}

/// Slices this [`PrimitiveArray`] by an offset and length.
/// # Implementation
/// This operation is `O(1)`.
Expand Down
13 changes: 13 additions & 0 deletions src/array/utf8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ impl<O: Offset> Utf8Array<O> {
std::str::from_utf8_unchecked(slice)
}

/// Returns the element at index `i` or `None` if it is null
/// # Panics
/// iff `i >= self.len()`
#[inline]
pub fn get(&self, i: usize) -> Option<&str> {
if !self.is_null(i) {
// soundness: Array::is_null panics if i >= self.len
unsafe { Some(self.value_unchecked(i)) }
} else {
None
}
}

/// Returns the [`DataType`] of this array.
#[inline]
pub fn data_type(&self) -> &DataType {
Expand Down