This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76d2a39
commit aa4eafa
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//! Defines generics suitable to perform operations to [`PrimitiveArray`] in-place. | ||
use super::utils::check_same_len; | ||
use crate::{array::PrimitiveArray, types::NativeType}; | ||
|
||
/// Applies an unary function to a [`PrimitiveArray`] in-place via cow semantics. | ||
/// | ||
/// # Implementation | ||
/// This is the fastest method to apply a binary operation and it is often vectorized (SIMD). | ||
/// # Panics | ||
/// This function panics iff | ||
/// * the arrays have a different length. | ||
/// * the function itself panics. | ||
#[inline] | ||
pub fn unary<I, F>(array: &mut PrimitiveArray<I>, op: F) | ||
where | ||
I: NativeType, | ||
F: Fn(I) -> I, | ||
{ | ||
array.apply_values(|values| values.iter_mut().for_each(|v| *v = op(*v))); | ||
} | ||
|
||
/// Applies a binary operations to two [`PrimitiveArray`], applying the operation | ||
/// in-place to the `lhs` via cow semantics. | ||
/// | ||
/// # Implementation | ||
/// This is the fastest way to perform a binary operation and it is often vectorized (SIMD). | ||
/// # Panics | ||
/// This function panics iff | ||
/// * the arrays have a different length. | ||
/// * the function itself panics. | ||
#[inline] | ||
pub fn binary<T, D, F>(lhs: &mut PrimitiveArray<T>, rhs: &PrimitiveArray<D>, op: F) | ||
where | ||
T: NativeType, | ||
D: NativeType, | ||
F: Fn(T, D) -> T, | ||
{ | ||
check_same_len(lhs, rhs).unwrap(); | ||
|
||
match rhs.validity() { | ||
None => {} | ||
Some(rhs) => { | ||
if lhs.validity().is_none() { | ||
*lhs = lhs.with_validity(Some(rhs.clone())) | ||
} else { | ||
lhs.apply_validity(|mut lhs| lhs &= rhs) | ||
} | ||
} | ||
} | ||
|
||
lhs.apply_values(|x| { | ||
x.iter_mut() | ||
.zip(rhs.values().iter()) | ||
.for_each(|(l, r)| *l = op(*l, *r)) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use arrow2::array::Int32Array; | ||
use arrow2::compute::arity_assign::{binary, unary}; | ||
|
||
#[test] | ||
fn test_unary_assign() { | ||
let mut a = Int32Array::from([Some(5), Some(6), None, Some(10)]); | ||
|
||
unary(&mut a, |x| x + 10); | ||
|
||
assert_eq!(a, Int32Array::from([Some(15), Some(16), None, Some(20)])) | ||
} | ||
|
||
#[test] | ||
fn test_binary_assign() { | ||
let mut a = Int32Array::from([Some(5), Some(6), None, Some(10)]); | ||
let b = Int32Array::from([Some(1), Some(2), Some(1), None]); | ||
|
||
binary(&mut a, &b, |x, y| x + y); | ||
|
||
assert_eq!(a, Int32Array::from([Some(6), Some(8), None, None])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,3 +46,5 @@ mod temporal; | |
mod utf8; | ||
#[cfg(feature = "compute_window")] | ||
mod window; | ||
|
||
mod arity_assign; |