-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Showing
10 changed files
with
276 additions
and
21 deletions.
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
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
7 changes: 1 addition & 6 deletions
7
polars/polars-core/src/series/arithmetic.rs → ...rs-core/src/series/arithmetic/borrowed.rs
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,11 @@ | ||
mod borrowed; | ||
mod owned; | ||
|
||
use crate::prelude::*; | ||
use crate::utils::{get_supertype, get_time_units}; | ||
use num::{Num, NumCast}; | ||
use std::borrow::Cow; | ||
use std::fmt::Debug; | ||
use std::ops::{self, Add, Mul, Sub}; | ||
|
||
pub use borrowed::*; |
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,82 @@ | ||
use super::*; | ||
#[cfg(feature = "performant")] | ||
use crate::utils::align_chunks_binary_owned_series; | ||
|
||
#[cfg(feature = "performant")] | ||
pub fn coerce_lhs_rhs_owned(lhs: Series, rhs: Series) -> Result<(Series, Series)> { | ||
let dtype = get_supertype(lhs.dtype(), rhs.dtype())?; | ||
let left = if lhs.dtype() == &dtype { | ||
lhs | ||
} else { | ||
lhs.cast(&dtype)? | ||
}; | ||
let right = if rhs.dtype() == &dtype { | ||
rhs | ||
} else { | ||
rhs.cast(&dtype)? | ||
}; | ||
Ok((left, right)) | ||
} | ||
|
||
#[cfg(feature = "performant")] | ||
fn apply_operation_mut<T, F>(mut lhs: Series, mut rhs: Series, op: F) -> Series | ||
where | ||
T: PolarsNumericType, | ||
F: Fn(ChunkedArray<T>, ChunkedArray<T>) -> ChunkedArray<T> + Copy, | ||
ChunkedArray<T>: IntoSeries, | ||
{ | ||
let lhs_ca: &mut ChunkedArray<T> = lhs._get_inner_mut().as_mut(); | ||
let rhs_ca: &mut ChunkedArray<T> = rhs._get_inner_mut().as_mut(); | ||
|
||
let lhs = std::mem::take(lhs_ca); | ||
let rhs = std::mem::take(rhs_ca); | ||
|
||
op(lhs, rhs).into_series() | ||
} | ||
|
||
macro_rules! impl_operation { | ||
($operation:ident, $method:ident, $function:expr) => { | ||
impl $operation for Series { | ||
type Output = Series; | ||
|
||
fn $method(self, rhs: Self) -> Self::Output { | ||
#[cfg(feature = "performant")] | ||
{ | ||
// only physical numeric values take the mutable path | ||
if !self.is_logical() && self.is_numeric_physical() { | ||
let (lhs, rhs) = coerce_lhs_rhs_owned(self, rhs).unwrap(); | ||
let (lhs, rhs) = align_chunks_binary_owned_series(lhs, rhs); | ||
use DataType::*; | ||
match lhs.dtype() { | ||
#[cfg(feature = "dtype-i8")] | ||
Int8 => apply_operation_mut::<Int8Type, _>(lhs, rhs, $function), | ||
#[cfg(feature = "dtype-i16")] | ||
Int16 => apply_operation_mut::<Int16Type, _>(lhs, rhs, $function), | ||
Int32 => apply_operation_mut::<Int32Type, _>(lhs, rhs, $function), | ||
Int64 => apply_operation_mut::<Int64Type, _>(lhs, rhs, $function), | ||
#[cfg(feature = "dtype-u8")] | ||
UInt8 => apply_operation_mut::<UInt8Type, _>(lhs, rhs, $function), | ||
#[cfg(feature = "dtype-u16")] | ||
UInt16 => apply_operation_mut::<UInt16Type, _>(lhs, rhs, $function), | ||
UInt32 => apply_operation_mut::<UInt32Type, _>(lhs, rhs, $function), | ||
UInt64 => apply_operation_mut::<UInt64Type, _>(lhs, rhs, $function), | ||
Float32 => apply_operation_mut::<Float32Type, _>(lhs, rhs, $function), | ||
Float64 => apply_operation_mut::<Float64Type, _>(lhs, rhs, $function), | ||
_ => unreachable!(), | ||
} | ||
} else { | ||
(&self).$method(&rhs) | ||
} | ||
} | ||
#[cfg(not(feature = "performant"))] | ||
{ | ||
(&self).$method(&rhs) | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_operation!(Add, add, |a, b| a.add(b)); | ||
impl_operation!(Sub, sub, |a, b| a.sub(b)); | ||
impl_operation!(Mul, mul, |a, b| a.mul(b)); |
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
Oops, something went wrong.