Skip to content

Commit

Permalink
DRY: centralize trait bounds (#1469)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Oct 1, 2021
1 parent d6d3d42 commit 7100f53
Show file tree
Hide file tree
Showing 32 changed files with 82 additions and 308 deletions.
100 changes: 1 addition & 99 deletions polars/polars-core/src/chunked_array/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::prelude::*;
use crate::utils::align_chunks_binary;
use arrow::array::PrimitiveArray;
use arrow::{array::ArrayRef, compute, compute::arithmetics::basic};
use num::{Num, NumCast, One, ToPrimitive, Zero};
use num::{Num, NumCast, ToPrimitive};
use std::borrow::Cow;
use std::ops::{Add, Div, Mul, Rem, Sub};
use std::sync::Arc;
Expand Down Expand Up @@ -60,11 +60,6 @@ fn arithmetic_helper<T, Kernel, F>(
) -> ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: Add<Output = T::Native>
+ Sub<Output = T::Native>
+ Mul<Output = T::Native>
+ Div<Output = T::Native>
+ num::Zero,
Kernel: Fn(
&PrimitiveArray<T::Native>,
&PrimitiveArray<T::Native>,
Expand Down Expand Up @@ -107,11 +102,6 @@ where
impl<T> Add for &ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: Add<Output = T::Native>
+ Sub<Output = T::Native>
+ Mul<Output = T::Native>
+ Div<Output = T::Native>
+ num::Zero,
{
type Output = ChunkedArray<T>;

Expand All @@ -123,13 +113,6 @@ where
impl<T> Div for &ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: Add<Output = T::Native>
+ Sub<Output = T::Native>
+ Mul<Output = T::Native>
+ Div<Output = T::Native>
+ Rem<Output = T::Native>
+ num::Zero
+ num::One,
{
type Output = ChunkedArray<T>;

Expand All @@ -141,12 +124,6 @@ where
impl<T> Mul for &ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: Add<Output = T::Native>
+ Sub<Output = T::Native>
+ Mul<Output = T::Native>
+ Div<Output = T::Native>
+ Rem<Output = T::Native>
+ num::Zero,
{
type Output = ChunkedArray<T>;

Expand All @@ -158,13 +135,6 @@ where
impl<T> Rem for &ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: Add<Output = T::Native>
+ Sub<Output = T::Native>
+ Mul<Output = T::Native>
+ Div<Output = T::Native>
+ Rem<Output = T::Native>
+ num::Zero
+ num::One,
{
type Output = ChunkedArray<T>;

Expand All @@ -176,12 +146,6 @@ where
impl<T> Sub for &ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: Add<Output = T::Native>
+ Sub<Output = T::Native>
+ Mul<Output = T::Native>
+ Div<Output = T::Native>
+ Rem<Output = T::Native>
+ num::Zero,
{
type Output = ChunkedArray<T>;

Expand All @@ -193,12 +157,6 @@ where
impl<T> Add for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: Add<Output = T::Native>
+ Sub<Output = T::Native>
+ Mul<Output = T::Native>
+ Div<Output = T::Native>
+ Rem<Output = T::Native>
+ num::Zero,
{
type Output = Self;

Expand All @@ -210,13 +168,6 @@ where
impl<T> Div for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: Add<Output = T::Native>
+ Sub<Output = T::Native>
+ Mul<Output = T::Native>
+ Div<Output = T::Native>
+ Rem<Output = T::Native>
+ num::Zero
+ num::One,
{
type Output = Self;

Expand All @@ -228,12 +179,6 @@ where
impl<T> Mul for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: Add<Output = T::Native>
+ Sub<Output = T::Native>
+ Mul<Output = T::Native>
+ Div<Output = T::Native>
+ Rem<Output = T::Native>
+ num::Zero,
{
type Output = Self;

Expand All @@ -245,12 +190,6 @@ where
impl<T> Sub for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: Add<Output = T::Native>
+ Sub<Output = T::Native>
+ Mul<Output = T::Native>
+ Div<Output = T::Native>
+ Rem<Output = T::Native>
+ num::Zero,
{
type Output = Self;

Expand All @@ -262,13 +201,6 @@ where
impl<T> Rem for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: Add<Output = T::Native>
+ Sub<Output = T::Native>
+ Mul<Output = T::Native>
+ Div<Output = T::Native>
+ Rem<Output = T::Native>
+ num::Zero
+ num::One,
{
type Output = ChunkedArray<T>;

Expand All @@ -282,9 +214,7 @@ where
impl<T, N> Add<N> for &ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NumCast,
N: Num + ToPrimitive,
T::Native: Add<Output = T::Native>,
{
type Output = ChunkedArray<T>;

Expand All @@ -297,9 +227,7 @@ where
impl<T, N> Sub<N> for &ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NumCast,
N: Num + ToPrimitive,
T::Native: Sub<Output = T::Native>,
{
type Output = ChunkedArray<T>;

Expand All @@ -312,12 +240,6 @@ where
impl<T, N> Div<N> for &ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NumCast
+ Div<Output = T::Native>
+ One
+ Zero
+ Rem<Output = T::Native>
+ Sub<Output = T::Native>,
N: Num + ToPrimitive,
{
type Output = ChunkedArray<T>;
Expand All @@ -331,9 +253,7 @@ where
impl<T, N> Mul<N> for &ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NumCast,
N: Num + ToPrimitive,
T::Native: Mul<Output = T::Native>,
{
type Output = ChunkedArray<T>;

Expand All @@ -346,10 +266,7 @@ where
impl<T, N> Rem<N> for &ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NumCast,
N: Num + ToPrimitive,
T::Native:
Div<Output = T::Native> + One + Zero + Rem<Output = T::Native> + Sub<Output = T::Native>,
{
type Output = ChunkedArray<T>;

Expand All @@ -362,9 +279,7 @@ where
impl<T, N> Add<N> for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NumCast,
N: Num + ToPrimitive,
T::Native: Add<Output = T::Native>,
{
type Output = ChunkedArray<T>;

Expand All @@ -376,9 +291,7 @@ where
impl<T, N> Sub<N> for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NumCast,
N: Num + ToPrimitive,
T::Native: Sub<Output = T::Native>,
{
type Output = ChunkedArray<T>;

Expand All @@ -390,12 +303,6 @@ where
impl<T, N> Div<N> for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NumCast
+ Div<Output = T::Native>
+ One
+ Zero
+ Sub<Output = T::Native>
+ Rem<Output = T::Native>,
N: Num + ToPrimitive,
{
type Output = ChunkedArray<T>;
Expand All @@ -408,9 +315,7 @@ where
impl<T, N> Mul<N> for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NumCast,
N: Num + ToPrimitive,
T::Native: Mul<Output = T::Native>,
{
type Output = ChunkedArray<T>;

Expand All @@ -422,10 +327,7 @@ where
impl<T, N> Rem<N> for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NumCast,
N: Num + ToPrimitive,
T::Native:
Div<Output = T::Native> + One + Zero + Rem<Output = T::Native> + Sub<Output = T::Native>,
{
type Output = ChunkedArray<T>;

Expand Down
10 changes: 4 additions & 6 deletions polars/polars-core/src/chunked_array/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,15 @@ impl BooleanChunkedBuilder {

pub struct PrimitiveChunkedBuilder<T>
where
T: PolarsPrimitiveType,
T::Native: Default,
T: PolarsNumericType,
{
array_builder: MutablePrimitiveArray<T::Native>,
field: Field,
}

impl<T> ChunkedBuilder<T::Native, T> for PrimitiveChunkedBuilder<T>
where
T: PolarsPrimitiveType,
T::Native: Default,
T: PolarsNumericType,
{
/// Appends a value of type `T` into the builder
#[inline]
Expand All @@ -101,7 +99,7 @@ where

impl<T> PrimitiveChunkedBuilder<T>
where
T: PolarsPrimitiveType,
T: PolarsNumericType,
{
pub fn new(name: &str, capacity: usize) -> Self {
let array_builder = MutablePrimitiveArray::<T::Native>::with_capacity(capacity)
Expand Down Expand Up @@ -220,7 +218,7 @@ pub trait NewChunkedArray<T, N> {

impl<T> NewChunkedArray<T, T::Native> for ChunkedArray<T>
where
T: PolarsPrimitiveType,
T: PolarsNumericType,
{
fn new_from_slice(name: &str, v: &[T::Native]) -> Self {
let arr = PrimitiveArray::<T::Native>::from_slice(v).to(T::get_dtype().to_arrow());
Expand Down
6 changes: 0 additions & 6 deletions polars/polars-core/src/chunked_array/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::chunked_array::categorical::CategoricalChunkedBuilder;
use crate::prelude::*;
use arrow::compute::cast;
use num::NumCast;

/// Casts a `PrimitiveArray` to a different physical type and logical type.
/// This operation is `O(N)`
Expand All @@ -12,8 +11,6 @@ pub(crate) fn cast_physical<S, T>(arr: &PrimitiveArray<S::Native>, datatype: &Da
where
S: PolarsNumericType,
T: PolarsNumericType,
T::Native: num::NumCast,
S::Native: num::NumCast,
{
let array =
arrow::compute::cast::primitive_to_primitive::<_, T::Native>(arr, &datatype.to_arrow());
Expand Down Expand Up @@ -42,8 +39,6 @@ fn cast_from_dtype<N, T>(chunked: &ChunkedArray<T>, dtype: DataType) -> Result<C
where
N: PolarsNumericType,
T: PolarsNumericType,
N::Native: NumCast,
T::Native: NumCast,
{
let chunks = chunked
.downcast_iter()
Expand Down Expand Up @@ -143,7 +138,6 @@ impl ChunkCast for CategoricalChunked {
impl<T> ChunkCast for ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: NumCast,
{
fn cast<N>(&self) -> Result<ChunkedArray<N>>
where
Expand Down

0 comments on commit 7100f53

Please sign in to comment.