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

Added len to every array #599

Merged
merged 1 commit into from
Nov 12, 2021
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
2 changes: 1 addition & 1 deletion src/array/binary/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
array::{Array, Offset},
array::Offset,
bitmap::utils::{zip_validity, ZipValidity},
trusted_len::TrustedLen,
};
Expand Down
8 changes: 7 additions & 1 deletion src/array/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ impl<O: Offset> BinaryArray<O> {

// accessors
impl<O: Offset> BinaryArray<O> {
/// Returns the length of this array
#[inline]
pub fn len(&self) -> usize {
self.offsets.len() - 1
}

/// Returns the element at index `i`
/// # Panics
/// iff `i >= self.len()`
Expand Down Expand Up @@ -211,7 +217,7 @@ impl<O: Offset> Array for BinaryArray<O> {

#[inline]
fn len(&self) -> usize {
self.offsets.len() - 1
self.len()
}

#[inline]
Expand Down
8 changes: 7 additions & 1 deletion src/array/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ impl BooleanArray {

// accessors
impl BooleanArray {
/// Returns the length of this array
#[inline]
pub fn len(&self) -> usize {
self.values.len()
}

/// Returns the value at index `i`
/// # Panic
/// This function panics iff `i >= self.len()`.
Expand Down Expand Up @@ -136,7 +142,7 @@ impl Array for BooleanArray {

#[inline]
fn len(&self) -> usize {
self.values.len()
self.len()
}

#[inline]
Expand Down
1 change: 0 additions & 1 deletion src/array/dictionary/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::array::Array;
use crate::bitmap::utils::{zip_validity, ZipValidity};
use crate::scalar::Scalar;
use crate::trusted_len::TrustedLen;
Expand Down
11 changes: 10 additions & 1 deletion src/array/dictionary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ impl<K: DictionaryKey> DictionaryArray<K> {
arr.values = Arc::from(arr.values.with_validity(validity));
arr
}
}

// accessors
impl<K: DictionaryKey> DictionaryArray<K> {
/// Returns the length of this array
#[inline]
pub fn len(&self) -> usize {
self.keys.len()
}

/// The optional validity. Equivalent to `self.keys().validity()`.
#[inline]
Expand Down Expand Up @@ -169,7 +178,7 @@ impl<K: DictionaryKey> Array for DictionaryArray<K> {

#[inline]
fn len(&self) -> usize {
self.keys.len()
self.len()
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/array/equal/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::array::{Array, BooleanArray};
use crate::array::BooleanArray;

pub(super) fn equal(lhs: &BooleanArray, rhs: &BooleanArray) -> bool {
lhs.len() == rhs.len() && lhs.iter().eq(rhs.iter())
Expand Down
35 changes: 22 additions & 13 deletions src/array/fixed_size_binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ impl FixedSizeBinaryArray {
}
}

/// Sets the validity bitmap on this [`FixedSizeBinaryArray`].
/// # Panic
/// This function panics iff `validity.len() != self.len()`.
pub fn with_validity(&self, validity: Option<Bitmap>) -> Self {
if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) {
panic!("validity should be as least as large as the array")
}
let mut arr = self.clone();
arr.validity = validity;
arr
}
}

// accessors
impl FixedSizeBinaryArray {
/// Returns the length of this array
#[inline]
pub fn len(&self) -> usize {
self.values.len() / self.size as usize
}

/// The optional validity.
#[inline]
pub fn validity(&self) -> Option<&Bitmap> {
Expand Down Expand Up @@ -118,18 +139,6 @@ impl FixedSizeBinaryArray {
pub fn size(&self) -> usize {
self.size
}

/// Sets the validity bitmap on this [`FixedSizeBinaryArray`].
/// # Panic
/// This function panics iff `validity.len() != self.len()`.
pub fn with_validity(&self, validity: Option<Bitmap>) -> Self {
if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) {
panic!("validity should be as least as large as the array")
}
let mut arr = self.clone();
arr.validity = validity;
arr
}
}

impl FixedSizeBinaryArray {
Expand All @@ -149,7 +158,7 @@ impl Array for FixedSizeBinaryArray {

#[inline]
fn len(&self) -> usize {
self.values.len() / self.size as usize
self.len()
}

#[inline]
Expand Down
35 changes: 22 additions & 13 deletions src/array/fixed_size_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,27 @@ impl FixedSizeListArray {
}
}

/// Sets the validity bitmap on this [`FixedSizeListArray`].
/// # Panic
/// This function panics iff `validity.len() != self.len()`.
pub fn with_validity(&self, validity: Option<Bitmap>) -> Self {
if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) {
panic!("validity should be as least as large as the array")
}
let mut arr = self.clone();
arr.validity = validity;
arr
}
}

// accessors
impl FixedSizeListArray {
/// Returns the length of this array
#[inline]
pub fn len(&self) -> usize {
self.values.len() / self.size as usize
}

/// The optional validity.
#[inline]
pub fn validity(&self) -> Option<&Bitmap> {
Expand Down Expand Up @@ -127,18 +148,6 @@ impl FixedSizeListArray {
self.values
.slice_unchecked(i * self.size as usize, self.size as usize)
}

/// Sets the validity bitmap on this [`FixedSizeListArray`].
/// # Panic
/// This function panics iff `validity.len() != self.len()`.
pub fn with_validity(&self, validity: Option<Bitmap>) -> Self {
if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) {
panic!("validity should be as least as large as the array")
}
let mut arr = self.clone();
arr.validity = validity;
arr
}
}

impl FixedSizeListArray {
Expand All @@ -164,7 +173,7 @@ impl Array for FixedSizeListArray {

#[inline]
fn len(&self) -> usize {
self.values.len() / self.size as usize
self.len()
}

#[inline]
Expand Down
8 changes: 7 additions & 1 deletion src/array/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ impl<O: Offset> ListArray<O> {

// Accessors
impl<O: Offset> ListArray<O> {
/// Returns the length of this array
#[inline]
pub fn len(&self) -> usize {
self.offsets.len() - 1
}

/// Returns the element at index `i`
#[inline]
pub fn value(&self, i: usize) -> Box<dyn Array> {
Expand Down Expand Up @@ -211,7 +217,7 @@ impl<O: Offset> Array for ListArray<O> {

#[inline]
fn len(&self) -> usize {
self.offsets.len() - 1
self.len()
}

#[inline]
Expand Down
8 changes: 7 additions & 1 deletion src/array/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ impl MapArray {

// Accessors
impl MapArray {
/// Returns the length of this array
#[inline]
pub fn len(&self) -> usize {
self.offsets.len() - 1
}

/// returns the offsets
#[inline]
pub fn offsets(&self) -> &Buffer<i32> {
Expand Down Expand Up @@ -161,7 +167,7 @@ impl Array for MapArray {

#[inline]
fn len(&self) -> usize {
self.offsets.len() - 1
self.len()
}

#[inline]
Expand Down
7 changes: 6 additions & 1 deletion src/array/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ impl NullArray {
length,
}
}

#[inline]
fn len(&self) -> usize {
self.length
}
}

impl Array for NullArray {
Expand All @@ -42,7 +47,7 @@ impl Array for NullArray {

#[inline]
fn len(&self) -> usize {
self.length
self.len()
}

#[inline]
Expand Down
8 changes: 8 additions & 0 deletions src/array/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ impl<T: NativeType> PrimitiveArray<T> {
arr.validity = validity;
arr
}
}

impl<T: NativeType> PrimitiveArray<T> {
/// Returns the length of this array
#[inline]
pub fn len(&self) -> usize {
self.values.len()
}

/// The optional validity.
#[inline]
Expand Down
7 changes: 6 additions & 1 deletion src/array/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ impl StructArray {

// Accessors
impl StructArray {
#[inline]
fn len(&self) -> usize {
self.values[0].len()
}

/// The optional validity.
#[inline]
pub fn validity(&self) -> Option<&Bitmap> {
Expand Down Expand Up @@ -184,7 +189,7 @@ impl Array for StructArray {

#[inline]
fn len(&self) -> usize {
self.values[0].len()
self.len()
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/array/union/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Array, UnionArray};
use super::UnionArray;
use crate::{scalar::Scalar, trusted_len::TrustedLen};

#[derive(Debug, Clone)]
Expand Down
Loading