From 21feac6f44011b75d61645179afb18f5e55c28a3 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Thu, 4 Nov 2021 07:06:46 +0000 Subject: [PATCH] Simplified trait in compute. --- src/compute/arithmetics/basic/add.rs | 24 ++++++++++++------------ src/compute/arithmetics/basic/div.rs | 12 ++++++------ src/compute/arithmetics/basic/mul.rs | 26 +++++++++++++------------- src/compute/arithmetics/basic/rem.rs | 16 ++++++++-------- src/compute/arithmetics/basic/sub.rs | 26 +++++++++++++------------- src/compute/arithmetics/mod.rs | 25 ++++++++++++------------- 6 files changed, 64 insertions(+), 65 deletions(-) diff --git a/src/compute/arithmetics/basic/add.rs b/src/compute/arithmetics/basic/add.rs index 73bf1420a4a..d71abfaec39 100644 --- a/src/compute/arithmetics/basic/add.rs +++ b/src/compute/arithmetics/basic/add.rs @@ -10,7 +10,7 @@ use crate::{ bitmap::Bitmap, compute::{ arithmetics::{ - ArrayAdd, ArrayCheckedAdd, ArrayOverflowingAdd, ArraySaturatingAdd, NotI128, + ArrayAdd, ArrayCheckedAdd, ArrayOverflowingAdd, ArraySaturatingAdd, NativeArithmetics, }, arity::{ binary, binary_checked, binary_with_bitmap, unary, unary_checked, unary_with_bitmap, @@ -87,7 +87,7 @@ where /// ``` pub fn checked_add(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> where - T: NativeType + CheckedAdd + Zero, + T: NativeType + CheckedAdd, { check_same_type(lhs, rhs)?; @@ -158,7 +158,7 @@ where // Implementation of ArrayAdd trait for PrimitiveArrays impl ArrayAdd> for PrimitiveArray where - T: NativeType + Add + NotI128, + T: NativeArithmetics + Add, { type Output = Self; @@ -169,7 +169,7 @@ where impl ArrayWrappingAdd> for PrimitiveArray where - T: NativeType + WrappingAdd + NotI128, + T: NativeArithmetics + WrappingAdd, { type Output = Self; @@ -181,7 +181,7 @@ where // Implementation of ArrayCheckedAdd trait for PrimitiveArrays impl ArrayCheckedAdd> for PrimitiveArray where - T: NativeType + CheckedAdd + Zero + NotI128, + T: NativeArithmetics + CheckedAdd, { type Output = Self; @@ -193,7 +193,7 @@ where // Implementation of ArraySaturatingAdd trait for PrimitiveArrays impl ArraySaturatingAdd> for PrimitiveArray where - T: NativeType + SaturatingAdd + NotI128, + T: NativeArithmetics + SaturatingAdd, { type Output = Self; @@ -205,7 +205,7 @@ where // Implementation of ArraySaturatingAdd trait for PrimitiveArrays impl ArrayOverflowingAdd> for PrimitiveArray where - T: NativeType + OverflowingAdd + NotI128, + T: NativeArithmetics + OverflowingAdd, { type Output = Self; @@ -271,7 +271,7 @@ where /// ``` pub fn checked_add_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + CheckedAdd + Zero, + T: NativeType + CheckedAdd, { let rhs = *rhs; let op = move |a: T| a.checked_add(&rhs); @@ -331,7 +331,7 @@ where // Implementation of ArrayAdd trait for PrimitiveArrays with a scalar impl ArrayAdd for PrimitiveArray where - T: NativeType + Add + NotI128, + T: NativeArithmetics + Add, { type Output = Self; @@ -343,7 +343,7 @@ where // Implementation of ArrayCheckedAdd trait for PrimitiveArrays with a scalar impl ArrayCheckedAdd for PrimitiveArray where - T: NativeType + CheckedAdd + Zero + NotI128, + T: NativeArithmetics + CheckedAdd + Zero, { type Output = Self; @@ -355,7 +355,7 @@ where // Implementation of ArraySaturatingAdd trait for PrimitiveArrays with a scalar impl ArraySaturatingAdd for PrimitiveArray where - T: NativeType + SaturatingAdd + NotI128, + T: NativeArithmetics + SaturatingAdd, { type Output = Self; @@ -367,7 +367,7 @@ where // Implementation of ArraySaturatingAdd trait for PrimitiveArrays with a scalar impl ArrayOverflowingAdd for PrimitiveArray where - T: NativeType + OverflowingAdd + NotI128, + T: NativeArithmetics + OverflowingAdd, { type Output = Self; diff --git a/src/compute/arithmetics/basic/div.rs b/src/compute/arithmetics/basic/div.rs index 4b891d4d707..ca2f474baea 100644 --- a/src/compute/arithmetics/basic/div.rs +++ b/src/compute/arithmetics/basic/div.rs @@ -8,7 +8,7 @@ use crate::datatypes::DataType; use crate::{ array::{Array, PrimitiveArray}, compute::{ - arithmetics::{ArrayCheckedDiv, ArrayDiv, NotI128}, + arithmetics::{ArrayCheckedDiv, ArrayDiv, NativeArithmetics}, arity::{binary, binary_checked, unary, unary_checked}, }, error::Result, @@ -68,7 +68,7 @@ where /// ``` pub fn checked_div(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> where - T: NativeType + CheckedDiv + Zero, + T: NativeArithmetics + CheckedDiv, { check_same_type(lhs, rhs)?; @@ -80,7 +80,7 @@ where // Implementation of ArrayDiv trait for PrimitiveArrays impl ArrayDiv> for PrimitiveArray where - T: NativeType + Div + NotI128, + T: NativeArithmetics + Div, { type Output = Self; @@ -92,7 +92,7 @@ where // Implementation of ArrayCheckedDiv trait for PrimitiveArrays impl ArrayCheckedDiv> for PrimitiveArray where - T: NativeType + CheckedDiv + Zero + NotI128, + T: NativeArithmetics + CheckedDiv, { type Output = Self; @@ -210,7 +210,7 @@ where // Implementation of ArrayDiv trait for PrimitiveArrays with a scalar impl ArrayDiv for PrimitiveArray where - T: NativeType + Div + NotI128 + NumCast, + T: NativeType + Div + NativeArithmetics + NumCast, { type Output = Self; @@ -222,7 +222,7 @@ where // Implementation of ArrayCheckedDiv trait for PrimitiveArrays with a scalar impl ArrayCheckedDiv for PrimitiveArray where - T: NativeType + CheckedDiv + Zero + NotI128, + T: NativeType + CheckedDiv + Zero + NativeArithmetics, { type Output = Self; diff --git a/src/compute/arithmetics/basic/mul.rs b/src/compute/arithmetics/basic/mul.rs index 101442947d3..1128ffad875 100644 --- a/src/compute/arithmetics/basic/mul.rs +++ b/src/compute/arithmetics/basic/mul.rs @@ -1,7 +1,7 @@ //! Definition of basic mul operations with primitive arrays use std::ops::Mul; -use num_traits::{ops::overflowing::OverflowingMul, CheckedMul, SaturatingMul, WrappingMul, Zero}; +use num_traits::{ops::overflowing::OverflowingMul, CheckedMul, SaturatingMul, WrappingMul}; use crate::compute::arithmetics::basic::check_same_type; use crate::compute::arithmetics::ArrayWrappingMul; @@ -10,7 +10,7 @@ use crate::{ bitmap::Bitmap, compute::{ arithmetics::{ - ArrayCheckedMul, ArrayMul, ArrayOverflowingMul, ArraySaturatingMul, NotI128, + ArrayCheckedMul, ArrayMul, ArrayOverflowingMul, ArraySaturatingMul, NativeArithmetics, }, arity::{ binary, binary_checked, binary_with_bitmap, unary, unary_checked, unary_with_bitmap, @@ -88,7 +88,7 @@ where /// ``` pub fn checked_mul(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> where - T: NativeType + CheckedMul + Zero, + T: NativeType + CheckedMul, { check_same_type(lhs, rhs)?; @@ -159,7 +159,7 @@ where // Implementation of ArrayMul trait for PrimitiveArrays impl ArrayMul> for PrimitiveArray where - T: NativeType + Mul + NotI128, + T: NativeArithmetics + Mul, { type Output = Self; @@ -170,7 +170,7 @@ where impl ArrayWrappingMul> for PrimitiveArray where - T: NativeType + WrappingMul + NotI128, + T: NativeArithmetics + WrappingMul, { type Output = Self; @@ -182,7 +182,7 @@ where // Implementation of ArrayCheckedMul trait for PrimitiveArrays impl ArrayCheckedMul> for PrimitiveArray where - T: NativeType + CheckedMul + Zero + NotI128, + T: NativeArithmetics + CheckedMul, { type Output = Self; @@ -194,7 +194,7 @@ where // Implementation of ArraySaturatingMul trait for PrimitiveArrays impl ArraySaturatingMul> for PrimitiveArray where - T: NativeType + SaturatingMul + NotI128, + T: NativeArithmetics + SaturatingMul, { type Output = Self; @@ -206,7 +206,7 @@ where // Implementation of ArraySaturatingMul trait for PrimitiveArrays impl ArrayOverflowingMul> for PrimitiveArray where - T: NativeType + OverflowingMul + NotI128, + T: NativeArithmetics + OverflowingMul, { type Output = Self; @@ -271,7 +271,7 @@ where /// ``` pub fn checked_mul_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + CheckedMul + Zero, + T: NativeType + CheckedMul, { let rhs = *rhs; let op = move |a: T| a.checked_mul(&rhs); @@ -331,7 +331,7 @@ where // Implementation of ArrayMul trait for PrimitiveArrays with a scalar impl ArrayMul for PrimitiveArray where - T: NativeType + Mul + NotI128, + T: NativeType + Mul + NativeArithmetics, { type Output = Self; @@ -343,7 +343,7 @@ where // Implementation of ArrayCheckedMul trait for PrimitiveArrays with a scalar impl ArrayCheckedMul for PrimitiveArray where - T: NativeType + CheckedMul + Zero + NotI128, + T: NativeArithmetics + CheckedMul, { type Output = Self; @@ -355,7 +355,7 @@ where // Implementation of ArraySaturatingMul trait for PrimitiveArrays with a scalar impl ArraySaturatingMul for PrimitiveArray where - T: NativeType + SaturatingMul + NotI128, + T: NativeArithmetics + SaturatingMul, { type Output = Self; @@ -367,7 +367,7 @@ where // Implementation of ArraySaturatingMul trait for PrimitiveArrays with a scalar impl ArrayOverflowingMul for PrimitiveArray where - T: NativeType + OverflowingMul + NotI128, + T: NativeArithmetics + OverflowingMul, { type Output = Self; diff --git a/src/compute/arithmetics/basic/rem.rs b/src/compute/arithmetics/basic/rem.rs index 006fc5ab800..8e43082def2 100644 --- a/src/compute/arithmetics/basic/rem.rs +++ b/src/compute/arithmetics/basic/rem.rs @@ -1,13 +1,13 @@ use std::ops::Rem; -use num_traits::{CheckedRem, NumCast, Zero}; +use num_traits::{CheckedRem, NumCast}; use crate::compute::arithmetics::basic::check_same_type; use crate::datatypes::DataType; use crate::{ array::{Array, PrimitiveArray}, compute::{ - arithmetics::{ArrayCheckedRem, ArrayRem, NotI128}, + arithmetics::{ArrayCheckedRem, ArrayRem, NativeArithmetics}, arity::{binary, binary_checked, unary, unary_checked}, }, error::Result, @@ -57,7 +57,7 @@ where /// ``` pub fn checked_rem(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> where - T: NativeType + CheckedRem + Zero, + T: NativeType + CheckedRem, { check_same_type(lhs, rhs)?; @@ -68,7 +68,7 @@ where impl ArrayRem> for PrimitiveArray where - T: NativeType + Rem + NotI128, + T: NativeArithmetics + Rem, { type Output = Self; @@ -79,7 +79,7 @@ where impl ArrayCheckedRem> for PrimitiveArray where - T: NativeType + CheckedRem + Zero + NotI128, + T: NativeArithmetics + CheckedRem, { type Output = Self; @@ -187,7 +187,7 @@ where /// ``` pub fn checked_rem_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + CheckedRem + Zero, + T: NativeType + CheckedRem, { let rhs = *rhs; let op = move |a: T| a.checked_rem(&rhs); @@ -197,7 +197,7 @@ where impl ArrayRem for PrimitiveArray where - T: NativeType + Rem + NotI128 + NumCast, + T: NativeArithmetics + Rem + NumCast, { type Output = Self; @@ -208,7 +208,7 @@ where impl ArrayCheckedRem for PrimitiveArray where - T: NativeType + CheckedRem + Zero + NotI128, + T: NativeArithmetics + CheckedRem, { type Output = Self; diff --git a/src/compute/arithmetics/basic/sub.rs b/src/compute/arithmetics/basic/sub.rs index eaefed35f1b..66bff91a50d 100644 --- a/src/compute/arithmetics/basic/sub.rs +++ b/src/compute/arithmetics/basic/sub.rs @@ -1,7 +1,7 @@ //! Definition of basic sub operations with primitive arrays use std::ops::Sub; -use num_traits::{ops::overflowing::OverflowingSub, CheckedSub, SaturatingSub, WrappingSub, Zero}; +use num_traits::{ops::overflowing::OverflowingSub, CheckedSub, SaturatingSub, WrappingSub}; use crate::compute::arithmetics::basic::check_same_type; use crate::compute::arithmetics::ArrayWrappingSub; @@ -10,7 +10,7 @@ use crate::{ bitmap::Bitmap, compute::{ arithmetics::{ - ArrayCheckedSub, ArrayOverflowingSub, ArraySaturatingSub, ArraySub, NotI128, + ArrayCheckedSub, ArrayOverflowingSub, ArraySaturatingSub, ArraySub, NativeArithmetics, }, arity::{ binary, binary_checked, binary_with_bitmap, unary, unary_checked, unary_with_bitmap, @@ -87,7 +87,7 @@ where /// ``` pub fn checked_sub(lhs: &PrimitiveArray, rhs: &PrimitiveArray) -> Result> where - T: NativeType + CheckedSub + Zero, + T: NativeType + CheckedSub, { check_same_type(lhs, rhs)?; @@ -158,7 +158,7 @@ where // Implementation of ArraySub trait for PrimitiveArrays impl ArraySub> for PrimitiveArray where - T: NativeType + Sub + NotI128, + T: NativeArithmetics + Sub, { type Output = Self; @@ -169,7 +169,7 @@ where impl ArrayWrappingSub> for PrimitiveArray where - T: NativeType + WrappingSub + NotI128, + T: NativeArithmetics + WrappingSub, { type Output = Self; @@ -181,7 +181,7 @@ where // Implementation of ArrayCheckedSub trait for PrimitiveArrays impl ArrayCheckedSub> for PrimitiveArray where - T: NativeType + CheckedSub + Zero + NotI128, + T: NativeArithmetics + CheckedSub, { type Output = Self; @@ -193,7 +193,7 @@ where // Implementation of ArraySaturatingSub trait for PrimitiveArrays impl ArraySaturatingSub> for PrimitiveArray where - T: NativeType + SaturatingSub + NotI128, + T: NativeArithmetics + SaturatingSub, { type Output = Self; @@ -205,7 +205,7 @@ where // Implementation of ArraySaturatingSub trait for PrimitiveArrays impl ArrayOverflowingSub> for PrimitiveArray where - T: NativeType + OverflowingSub, + T: NativeArithmetics + OverflowingSub, { type Output = Self; @@ -271,7 +271,7 @@ where /// ``` pub fn checked_sub_scalar(lhs: &PrimitiveArray, rhs: &T) -> PrimitiveArray where - T: NativeType + CheckedSub + Zero, + T: NativeType + CheckedSub, { let rhs = *rhs; let op = move |a: T| a.checked_sub(&rhs); @@ -331,7 +331,7 @@ where // Implementation of ArraySub trait for PrimitiveArrays with a scalar impl ArraySub for PrimitiveArray where - T: NativeType + Sub + NotI128, + T: NativeArithmetics + Sub, { type Output = Self; @@ -343,7 +343,7 @@ where // Implementation of ArrayCheckedSub trait for PrimitiveArrays with a scalar impl ArrayCheckedSub for PrimitiveArray where - T: NativeType + CheckedSub + Zero + NotI128, + T: NativeArithmetics + CheckedSub, { type Output = Self; @@ -355,7 +355,7 @@ where // Implementation of ArraySaturatingSub trait for PrimitiveArrays with a scalar impl ArraySaturatingSub for PrimitiveArray where - T: NativeType + SaturatingSub + NotI128, + T: NativeArithmetics + SaturatingSub, { type Output = Self; @@ -367,7 +367,7 @@ where // Implementation of ArraySaturatingSub trait for PrimitiveArrays with a scalar impl ArrayOverflowingSub for PrimitiveArray where - T: NativeType + OverflowingSub + NotI128, + T: NativeArithmetics + OverflowingSub, { type Output = Self; diff --git a/src/compute/arithmetics/mod.rs b/src/compute/arithmetics/mod.rs index e024779ca76..168c24e1cdc 100644 --- a/src/compute/arithmetics/mod.rs +++ b/src/compute/arithmetics/mod.rs @@ -474,16 +474,15 @@ pub trait ArrayCheckedRem { fn checked_rem(&self, rhs: &Rhs) -> Result; } -// The decimal primitive array defines different arithmetic functions and -// it requires specialization -pub unsafe trait NotI128 {} -unsafe impl NotI128 for u8 {} -unsafe impl NotI128 for u16 {} -unsafe impl NotI128 for u32 {} -unsafe impl NotI128 for u64 {} -unsafe impl NotI128 for i8 {} -unsafe impl NotI128 for i16 {} -unsafe impl NotI128 for i32 {} -unsafe impl NotI128 for i64 {} -unsafe impl NotI128 for f32 {} -unsafe impl NotI128 for f64 {} +/// Trait describing types whose arithmetics is simple. +pub trait NativeArithmetics: NativeType {} +impl NativeArithmetics for u8 {} +impl NativeArithmetics for u16 {} +impl NativeArithmetics for u32 {} +impl NativeArithmetics for u64 {} +impl NativeArithmetics for i8 {} +impl NativeArithmetics for i16 {} +impl NativeArithmetics for i32 {} +impl NativeArithmetics for i64 {} +impl NativeArithmetics for f32 {} +impl NativeArithmetics for f64 {}