Skip to content

Commit

Permalink
Simplified code via DRY (jorgecarleitao#1398)
Browse files Browse the repository at this point in the history
* Simplified code

* Simplified code
  • Loading branch information
jorgecarleitao authored and ritchie46 committed Mar 29, 2023
1 parent 155a792 commit 0605625
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 661 deletions.
63 changes: 4 additions & 59 deletions src/array/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,35 +202,8 @@ impl<O: Offset> BinaryArray<O> {
}

impl_sliced!();

/// Boxes self into a [`Box<dyn Array>`].
pub fn boxed(self) -> Box<dyn Array> {
Box::new(self)
}

/// Boxes self into a [`std::sync::Arc<dyn Array>`].
pub fn arced(self) -> std::sync::Arc<dyn Array> {
std::sync::Arc::new(self)
}

/// Returns this [`BinaryArray`] with a new validity.
/// # Panic
/// Panics iff `validity.len() != self.len()`.
#[must_use]
pub fn with_validity(mut self, validity: Option<Bitmap>) -> Self {
self.set_validity(validity);
self
}

/// Sets the validity of this [`BinaryArray`].
/// # Panics
/// This function panics iff `values.len() != self.len()`.
pub fn set_validity(&mut self, validity: Option<Bitmap>) {
if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) {
panic!("validity must be equal to the array's length")
}
self.validity = validity;
}
impl_mut_validity!();
impl_into_array!();

/// Try to convert this `BinaryArray` to a `MutableBinaryArray`
pub fn into_mut(mut self) -> Either<Self, MutableBinaryArray<O>> {
Expand Down Expand Up @@ -409,44 +382,16 @@ impl<O: Offset> BinaryArray<O> {
}

impl<O: Offset> Array for BinaryArray<O> {
#[inline]
fn as_any(&self) -> &dyn std::any::Any {
self
}

#[inline]
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}

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

#[inline]
fn data_type(&self) -> &DataType {
&self.data_type
}
impl_common_array!();

fn validity(&self) -> Option<&Bitmap> {
self.validity.as_ref()
}

fn slice(&mut self, offset: usize, length: usize) {
self.slice(offset, length)
}

unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) {
self.slice_unchecked(offset, length)
}

#[inline]
fn with_validity(&self, validity: Option<Bitmap>) -> Box<dyn Array> {
Box::new(self.clone().with_validity(validity))
}
fn to_boxed(&self) -> Box<dyn Array> {
Box::new(self.clone())
}
}

unsafe impl<O: Offset> GenericBinaryArray<O> for BinaryArray<O> {
Expand Down
64 changes: 3 additions & 61 deletions src/array/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,8 @@ impl BooleanArray {
}

impl_sliced!();

/// Returns this [`BooleanArray`] with a new validity.
/// # Panic
/// This function panics iff `validity.len() != self.len()`.
#[must_use]
pub fn with_validity(mut self, validity: Option<Bitmap>) -> Self {
self.set_validity(validity);
self
}

/// Sets the validity of this [`BooleanArray`].
/// # Panics
/// This function panics iff `values.len() != self.len()`.
pub fn set_validity(&mut self, validity: Option<Bitmap>) {
if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) {
panic!("validity must be equal to the array's length")
}
self.validity = validity;
}
impl_mut_validity!();
impl_into_array!();

/// Returns a clone of this [`BooleanArray`] with new values.
/// # Panics
Expand Down Expand Up @@ -346,16 +329,6 @@ impl BooleanArray {
Ok(MutableBooleanArray::try_from_trusted_len_iter(iterator)?.into())
}

/// Boxes self into a [`Box<dyn Array>`].
pub fn boxed(self) -> Box<dyn Array> {
Box::new(self)
}

/// Boxes self into a [`std::sync::Arc<dyn Array>`].
pub fn arced(self) -> std::sync::Arc<dyn Array> {
std::sync::Arc::new(self)
}

/// Returns its internal representation
#[must_use]
pub fn into_inner(self) -> (DataType, Bitmap, Option<Bitmap>) {
Expand All @@ -369,45 +342,14 @@ impl BooleanArray {
}

impl Array for BooleanArray {
#[inline]
fn as_any(&self) -> &dyn std::any::Any {
self
}

#[inline]
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}

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

#[inline]
fn data_type(&self) -> &DataType {
&self.data_type
}
impl_common_array!();

#[inline]
fn validity(&self) -> Option<&Bitmap> {
self.validity.as_ref()
}

#[inline]
fn slice(&mut self, offset: usize, length: usize) {
self.slice(offset, length)
}

#[inline]
unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) {
self.slice_unchecked(offset, length)
}

fn with_validity(&self, validity: Option<Bitmap>) -> Box<dyn Array> {
Box::new(self.clone().with_validity(validity))
}
fn to_boxed(&self) -> Box<dyn Array> {
Box::new(self.clone())
}
}
45 changes: 4 additions & 41 deletions src/array/dictionary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ impl<K: DictionaryKey> DictionaryArray<K> {
self.keys.set_validity(validity);
}

impl_into_array!();

/// Returns the length of this array
#[inline]
pub fn len(&self) -> usize {
Expand Down Expand Up @@ -384,16 +386,6 @@ impl<K: DictionaryKey> DictionaryArray<K> {
new_scalar(self.values.as_ref(), index)
}

/// Boxes self into a [`Box<dyn Array>`].
pub fn boxed(self) -> Box<dyn Array> {
Box::new(self)
}

/// Boxes self into a [`std::sync::Arc<dyn Array>`].
pub fn arced(self) -> std::sync::Arc<dyn Array> {
std::sync::Arc::new(self)
}

pub(crate) fn try_get_child(data_type: &DataType) -> Result<&DataType, Error> {
Ok(match data_type.to_logical_type() {
DataType::Dictionary(_, values, _) => values.as_ref(),
Expand All @@ -407,43 +399,14 @@ impl<K: DictionaryKey> DictionaryArray<K> {
}

impl<K: DictionaryKey> Array for DictionaryArray<K> {
#[inline]
fn as_any(&self) -> &dyn std::any::Any {
self
}

#[inline]
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}

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

#[inline]
fn data_type(&self) -> &DataType {
&self.data_type
}
impl_common_array!();

fn validity(&self) -> Option<&Bitmap> {
self.keys.validity()
}

fn slice(&mut self, offset: usize, length: usize) {
self.slice(offset, length)
}

unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) {
self.slice_unchecked(offset, length)
}

#[inline]
fn with_validity(&self, validity: Option<Bitmap>) -> Box<dyn Array> {
Box::new(self.clone().with_validity(validity))
}

fn to_boxed(&self) -> Box<dyn Array> {
Box::new(self.clone())
}
}
64 changes: 4 additions & 60 deletions src/array/fixed_size_binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,6 @@ impl FixedSizeBinaryArray {
Some(Bitmap::new_zeroed(length)),
)
}

/// Boxes self into a [`Box<dyn Array>`].
pub fn boxed(self) -> Box<dyn Array> {
Box::new(self)
}

/// Boxes self into a [`std::sync::Arc<dyn Array>`].
pub fn arced(self) -> std::sync::Arc<dyn Array> {
std::sync::Arc::new(self)
}
}

// must use
Expand Down Expand Up @@ -125,25 +115,8 @@ impl FixedSizeBinaryArray {
}

impl_sliced!();

/// Returns this [`FixedSizeBinaryArray`] with a new validity.
/// # Panic
/// This function panics iff `validity.len() != self.len()`.
#[must_use]
pub fn with_validity(mut self, validity: Option<Bitmap>) -> Self {
self.set_validity(validity);
self
}

/// Sets the validity of this [`FixedSizeBinaryArray`].
/// # Panics
/// This function panics iff `validity.len() != self.len()`.
pub fn set_validity(&mut self, validity: Option<Bitmap>) {
if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) {
panic!("validity must be equal to the array's length")
}
self.validity = validity;
}
impl_mut_validity!();
impl_into_array!();
}

// accessors
Expand Down Expand Up @@ -234,45 +207,16 @@ impl FixedSizeBinaryArray {
}

impl Array for FixedSizeBinaryArray {
#[inline]
fn as_any(&self) -> &dyn std::any::Any {
self
}

#[inline]
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}

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

#[inline]
fn data_type(&self) -> &DataType {
&self.data_type
}
impl_common_array!();

fn validity(&self) -> Option<&Bitmap> {
self.validity.as_ref()
}

fn slice(&mut self, offset: usize, length: usize) {
self.slice(offset, length)
}

unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) {
self.slice_unchecked(offset, length)
}

#[inline]
fn with_validity(&self, validity: Option<Bitmap>) -> Box<dyn Array> {
Box::new(self.clone().with_validity(validity))
}

fn to_boxed(&self) -> Box<dyn Array> {
Box::new(self.clone())
}
}

impl FixedSizeBinaryArray {
Expand Down
Loading

0 comments on commit 0605625

Please sign in to comment.