diff --git a/src/array/binary/iterator.rs b/src/array/binary/iterator.rs index 8a96cc400e5..3dc085ab220 100644 --- a/src/array/binary/iterator.rs +++ b/src/array/binary/iterator.rs @@ -1,5 +1,5 @@ use crate::{ - array::{Array, Offset}, + array::Offset, bitmap::utils::{zip_validity, ZipValidity}, trusted_len::TrustedLen, }; diff --git a/src/array/binary/mod.rs b/src/array/binary/mod.rs index ed446561dde..0369a305588 100644 --- a/src/array/binary/mod.rs +++ b/src/array/binary/mod.rs @@ -161,6 +161,12 @@ impl BinaryArray { // accessors impl BinaryArray { + /// 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()` @@ -211,7 +217,7 @@ impl Array for BinaryArray { #[inline] fn len(&self) -> usize { - self.offsets.len() - 1 + self.len() } #[inline] diff --git a/src/array/boolean/mod.rs b/src/array/boolean/mod.rs index 06935af24ee..79b1e959f10 100644 --- a/src/array/boolean/mod.rs +++ b/src/array/boolean/mod.rs @@ -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()`. @@ -136,7 +142,7 @@ impl Array for BooleanArray { #[inline] fn len(&self) -> usize { - self.values.len() + self.len() } #[inline] diff --git a/src/array/dictionary/iterator.rs b/src/array/dictionary/iterator.rs index cc7bb4d5d31..21b1cc7bad4 100644 --- a/src/array/dictionary/iterator.rs +++ b/src/array/dictionary/iterator.rs @@ -1,4 +1,3 @@ -use crate::array::Array; use crate::bitmap::utils::{zip_validity, ZipValidity}; use crate::scalar::Scalar; use crate::trusted_len::TrustedLen; diff --git a/src/array/dictionary/mod.rs b/src/array/dictionary/mod.rs index de45eff8bee..6d186b6bce1 100644 --- a/src/array/dictionary/mod.rs +++ b/src/array/dictionary/mod.rs @@ -119,6 +119,15 @@ impl DictionaryArray { arr.values = Arc::from(arr.values.with_validity(validity)); arr } +} + +// accessors +impl DictionaryArray { + /// Returns the length of this array + #[inline] + pub fn len(&self) -> usize { + self.keys.len() + } /// The optional validity. Equivalent to `self.keys().validity()`. #[inline] @@ -169,7 +178,7 @@ impl Array for DictionaryArray { #[inline] fn len(&self) -> usize { - self.keys.len() + self.len() } #[inline] diff --git a/src/array/equal/boolean.rs b/src/array/equal/boolean.rs index 03455d75285..d9c6af9b027 100644 --- a/src/array/equal/boolean.rs +++ b/src/array/equal/boolean.rs @@ -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()) diff --git a/src/array/fixed_size_binary/mod.rs b/src/array/fixed_size_binary/mod.rs index c5a4a2c1b6d..c7bf5e30a03 100644 --- a/src/array/fixed_size_binary/mod.rs +++ b/src/array/fixed_size_binary/mod.rs @@ -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) -> 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> { @@ -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) -> 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 { @@ -149,7 +158,7 @@ impl Array for FixedSizeBinaryArray { #[inline] fn len(&self) -> usize { - self.values.len() / self.size as usize + self.len() } #[inline] diff --git a/src/array/fixed_size_list/mod.rs b/src/array/fixed_size_list/mod.rs index aa0f860397f..f8ff1036ab0 100644 --- a/src/array/fixed_size_list/mod.rs +++ b/src/array/fixed_size_list/mod.rs @@ -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) -> 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> { @@ -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) -> 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 { @@ -164,7 +173,7 @@ impl Array for FixedSizeListArray { #[inline] fn len(&self) -> usize { - self.values.len() / self.size as usize + self.len() } #[inline] diff --git a/src/array/list/mod.rs b/src/array/list/mod.rs index 8920f89e4c5..11e6fd7592b 100644 --- a/src/array/list/mod.rs +++ b/src/array/list/mod.rs @@ -123,6 +123,12 @@ impl ListArray { // Accessors impl ListArray { + /// 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 { @@ -211,7 +217,7 @@ impl Array for ListArray { #[inline] fn len(&self) -> usize { - self.offsets.len() - 1 + self.len() } #[inline] diff --git a/src/array/map/mod.rs b/src/array/map/mod.rs index 616bc969c12..9bd05e1f5a5 100644 --- a/src/array/map/mod.rs +++ b/src/array/map/mod.rs @@ -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 { @@ -161,7 +167,7 @@ impl Array for MapArray { #[inline] fn len(&self) -> usize { - self.offsets.len() - 1 + self.len() } #[inline] diff --git a/src/array/null.rs b/src/array/null.rs index cb194e3901e..a3308dbe0df 100644 --- a/src/array/null.rs +++ b/src/array/null.rs @@ -32,6 +32,11 @@ impl NullArray { length, } } + + #[inline] + fn len(&self) -> usize { + self.length + } } impl Array for NullArray { @@ -42,7 +47,7 @@ impl Array for NullArray { #[inline] fn len(&self) -> usize { - self.length + self.len() } #[inline] diff --git a/src/array/primitive/mod.rs b/src/array/primitive/mod.rs index 37ab0817998..a84e5e60e7e 100644 --- a/src/array/primitive/mod.rs +++ b/src/array/primitive/mod.rs @@ -120,6 +120,14 @@ impl PrimitiveArray { arr.validity = validity; arr } +} + +impl PrimitiveArray { + /// Returns the length of this array + #[inline] + pub fn len(&self) -> usize { + self.values.len() + } /// The optional validity. #[inline] diff --git a/src/array/struct_.rs b/src/array/struct_.rs index 6e767ae712f..10b5060b598 100644 --- a/src/array/struct_.rs +++ b/src/array/struct_.rs @@ -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> { @@ -184,7 +189,7 @@ impl Array for StructArray { #[inline] fn len(&self) -> usize { - self.values[0].len() + self.len() } #[inline] diff --git a/src/array/union/iterator.rs b/src/array/union/iterator.rs index 7a859561c14..e72d4735824 100644 --- a/src/array/union/iterator.rs +++ b/src/array/union/iterator.rs @@ -1,4 +1,4 @@ -use super::{Array, UnionArray}; +use super::UnionArray; use crate::{scalar::Scalar, trusted_len::TrustedLen}; #[derive(Debug, Clone)] diff --git a/src/array/union/mod.rs b/src/array/union/mod.rs index 77574b1706f..a3facb19ded 100644 --- a/src/array/union/mod.rs +++ b/src/array/union/mod.rs @@ -129,6 +129,48 @@ impl UnionArray { } } + /// Returns a slice of this [`UnionArray`]. + /// # Implementation + /// This operation is `O(F)` where `F` is the number of fields. + /// # Panic + /// This function panics iff `offset + length >= self.len()`. + #[inline] + pub fn slice(&self, offset: usize, length: usize) -> Self { + Self { + data_type: self.data_type.clone(), + fields: self.fields.clone(), + fields_hash: self.fields_hash.clone(), + types: self.types.clone().slice(offset, length), + offsets: self.offsets.clone(), + offset: self.offset + offset, + } + } + + /// Returns a slice of this [`UnionArray`]. + /// # Implementation + /// This operation is `O(F)` where `F` is the number of fields. + /// # Safety + /// The caller must ensure that `offset + length <= self.len()`. + #[inline] + pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { + Self { + data_type: self.data_type.clone(), + fields: self.fields.clone(), + fields_hash: self.fields_hash.clone(), + types: self.types.clone().slice_unchecked(offset, length), + offsets: self.offsets.clone(), + offset: self.offset + offset, + } + } +} + +impl UnionArray { + /// Returns the length of this array + #[inline] + pub fn len(&self) -> usize { + self.types.len() + } + /// The optional offsets. pub fn offsets(&self) -> &Option> { &self.offsets @@ -179,40 +221,6 @@ impl UnionArray { let index = self.field_slot(index); new_scalar(field.as_ref(), index) } - - /// Returns a slice of this [`UnionArray`]. - /// # Implementation - /// This operation is `O(F)` where `F` is the number of fields. - /// # Panic - /// This function panics iff `offset + length >= self.len()`. - #[inline] - pub fn slice(&self, offset: usize, length: usize) -> Self { - Self { - data_type: self.data_type.clone(), - fields: self.fields.clone(), - fields_hash: self.fields_hash.clone(), - types: self.types.clone().slice(offset, length), - offsets: self.offsets.clone(), - offset: self.offset + offset, - } - } - - /// Returns a slice of this [`UnionArray`]. - /// # Implementation - /// This operation is `O(F)` where `F` is the number of fields. - /// # Safety - /// The caller must ensure that `offset + length <= self.len()`. - #[inline] - pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { - Self { - data_type: self.data_type.clone(), - fields: self.fields.clone(), - fields_hash: self.fields_hash.clone(), - types: self.types.clone().slice_unchecked(offset, length), - offsets: self.offsets.clone(), - offset: self.offset + offset, - } - } } impl Array for UnionArray { @@ -221,7 +229,7 @@ impl Array for UnionArray { } fn len(&self) -> usize { - self.types.len() + self.len() } fn data_type(&self) -> &DataType { diff --git a/src/array/utf8/iterator.rs b/src/array/utf8/iterator.rs index fd9a01f14a5..3a6e6310417 100644 --- a/src/array/utf8/iterator.rs +++ b/src/array/utf8/iterator.rs @@ -1,8 +1,5 @@ use crate::bitmap::utils::{zip_validity, ZipValidity}; -use crate::{ - array::{Array, Offset}, - trusted_len::TrustedLen, -}; +use crate::{array::Offset, trusted_len::TrustedLen}; use super::Utf8Array; diff --git a/src/array/utf8/mod.rs b/src/array/utf8/mod.rs index ef895092dc9..4b0abc10039 100644 --- a/src/array/utf8/mod.rs +++ b/src/array/utf8/mod.rs @@ -176,6 +176,12 @@ impl Utf8Array { // Accessors impl Utf8Array { + /// Returns the length of this array + #[inline] + fn len(&self) -> usize { + self.offsets.len() - 1 + } + /// Returns the element at index `i` as &str /// # Safety /// This function is safe iff `i < self.len`. @@ -230,7 +236,7 @@ impl Array for Utf8Array { #[inline] fn len(&self) -> usize { - self.offsets.len() - 1 + self.len() } #[inline] diff --git a/src/compute/arity.rs b/src/compute/arity.rs index 733f85aceb1..362181c731e 100644 --- a/src/compute/arity.rs +++ b/src/compute/arity.rs @@ -3,7 +3,7 @@ use super::utils::combine_validities; use crate::compute::arithmetics::basic::check_same_len; use crate::{ - array::{Array, PrimitiveArray}, + array::PrimitiveArray, bitmap::{Bitmap, MutableBitmap}, buffer::Buffer, datatypes::DataType, diff --git a/src/compute/boolean_kleene.rs b/src/compute/boolean_kleene.rs index 4d65f449f22..350cf6b8bd0 100644 --- a/src/compute/boolean_kleene.rs +++ b/src/compute/boolean_kleene.rs @@ -2,7 +2,7 @@ use crate::datatypes::DataType; use crate::error::{ArrowError, Result}; use crate::{ - array::{Array, BooleanArray}, + array::BooleanArray, bitmap::{quaternary, ternary}, }; diff --git a/src/compute/sort/binary.rs b/src/compute/sort/binary.rs index 109f88b8564..766efc678b2 100644 --- a/src/compute/sort/binary.rs +++ b/src/compute/sort/binary.rs @@ -1,4 +1,4 @@ -use crate::array::{Array, BinaryArray, Offset, PrimitiveArray}; +use crate::array::{BinaryArray, Offset, PrimitiveArray}; use crate::types::Index; use super::common; diff --git a/src/compute/sort/boolean.rs b/src/compute/sort/boolean.rs index 79ad428576f..1bf2b6f4a16 100644 --- a/src/compute/sort/boolean.rs +++ b/src/compute/sort/boolean.rs @@ -1,5 +1,5 @@ use crate::{ - array::{Array, BooleanArray, PrimitiveArray}, + array::{BooleanArray, PrimitiveArray}, buffer::MutableBuffer, types::Index, }; diff --git a/src/compute/sort/primitive/indices.rs b/src/compute/sort/primitive/indices.rs index 8b229f1c8b6..a3ff6969b5d 100644 --- a/src/compute/sort/primitive/indices.rs +++ b/src/compute/sort/primitive/indices.rs @@ -1,5 +1,5 @@ use crate::{ - array::{Array, PrimitiveArray}, + array::PrimitiveArray, types::{Index, NativeType}, }; diff --git a/src/compute/take/generic_binary.rs b/src/compute/take/generic_binary.rs index 4e960bae54a..22a8a707199 100644 --- a/src/compute/take/generic_binary.rs +++ b/src/compute/take/generic_binary.rs @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. use crate::{ - array::{Array, GenericBinaryArray, Offset, PrimitiveArray}, + array::{GenericBinaryArray, Offset, PrimitiveArray}, bitmap::{Bitmap, MutableBitmap}, buffer::{Buffer, MutableBuffer}, }; diff --git a/src/compute/take/list.rs b/src/compute/take/list.rs index 0b219a2888e..6e877e55b88 100644 --- a/src/compute/take/list.rs +++ b/src/compute/take/list.rs @@ -17,7 +17,7 @@ use crate::array::{ growable::{Growable, GrowableList}, - Array, ListArray, Offset, PrimitiveArray, + ListArray, Offset, PrimitiveArray, }; use super::Index; @@ -67,7 +67,7 @@ pub fn take( mod tests { use super::*; use crate::{ - array::{MutableListArray, MutablePrimitiveArray, PrimitiveArray, TryExtend}, + array::{Array, MutableListArray, MutablePrimitiveArray, PrimitiveArray, TryExtend}, bitmap::Bitmap, buffer::Buffer, datatypes::DataType, diff --git a/src/lib.rs b/src/lib.rs index f015b6bd199..4cb9b06a164 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ #![doc = include_str!("doc/lib.md")] // So that we have more control over what is `unsafe` inside an `unsafe` block #![allow(unused_unsafe)] +// +#![allow(clippy::len_without_is_empty)] #![cfg_attr(docsrs, feature(doc_cfg))] #[macro_use]