Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Removed Result from arithmetics.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Nov 14, 2021
1 parent 7f973ee commit d0c0187
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 274 deletions.
58 changes: 21 additions & 37 deletions src/compute/arithmetics/basic/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ use std::ops::Add;

use num_traits::{ops::overflowing::OverflowingAdd, CheckedAdd, SaturatingAdd, WrappingAdd, Zero};

use crate::compute::arithmetics::basic::check_same_type;
use crate::compute::arithmetics::ArrayWrappingAdd;
use crate::{
array::{Array, PrimitiveArray},
bitmap::Bitmap,
compute::{
arithmetics::{
ArrayAdd, ArrayCheckedAdd, ArrayOverflowingAdd, ArraySaturatingAdd, NativeArithmetics,
basic::check_same_type, ArrayAdd, ArrayCheckedAdd, ArrayOverflowingAdd,
ArraySaturatingAdd, ArrayWrappingAdd, NativeArithmetics,
},
arity::{
binary, binary_checked, binary_with_bitmap, unary, unary_checked, unary_with_bitmap,
},
},
error::Result,
types::NativeType,
};

Expand All @@ -34,12 +32,10 @@ use crate::{
/// let expected = PrimitiveArray::from([None, None, None, Some(12)]);
/// assert_eq!(result, expected)
/// ```
pub fn add<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> Result<PrimitiveArray<T>>
pub fn add<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + Add<Output = T>,
{
check_same_type(lhs, rhs)?;

binary(lhs, rhs, lhs.data_type().clone(), |a, b| a + b)
}

Expand All @@ -57,15 +53,10 @@ where
/// let expected = PrimitiveArray::from([Some(-100i8), Some(-56i8), Some(100i8)]);
/// assert_eq!(result, expected);
/// ```
pub fn wrapping_add<T>(
lhs: &PrimitiveArray<T>,
rhs: &PrimitiveArray<T>,
) -> Result<PrimitiveArray<T>>
pub fn wrapping_add<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + WrappingAdd<Output = T>,
{
check_same_type(lhs, rhs)?;

let op = move |a: T, b: T| a.wrapping_add(&b);

binary(lhs, rhs, lhs.data_type().clone(), op)
Expand All @@ -85,11 +76,11 @@ where
/// let expected = PrimitiveArray::from([Some(100i8), None, Some(100i8)]);
/// assert_eq!(result, expected);
/// ```
pub fn checked_add<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> Result<PrimitiveArray<T>>
pub fn checked_add<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + CheckedAdd<Output = T>,
{
check_same_type(lhs, rhs)?;
check_same_type(lhs, rhs).unwrap();

let op = move |a: T, b: T| a.checked_add(&b);

Expand All @@ -111,15 +102,10 @@ where
/// let expected = PrimitiveArray::from([Some(127)]);
/// assert_eq!(result, expected);
/// ```
pub fn saturating_add<T>(
lhs: &PrimitiveArray<T>,
rhs: &PrimitiveArray<T>,
) -> Result<PrimitiveArray<T>>
pub fn saturating_add<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + SaturatingAdd<Output = T>,
{
check_same_type(lhs, rhs)?;

let op = move |a: T, b: T| a.saturating_add(&b);

binary(lhs, rhs, lhs.data_type().clone(), op)
Expand All @@ -144,12 +130,10 @@ where
pub fn overflowing_add<T>(
lhs: &PrimitiveArray<T>,
rhs: &PrimitiveArray<T>,
) -> Result<(PrimitiveArray<T>, Bitmap)>
) -> (PrimitiveArray<T>, Bitmap)
where
T: NativeType + OverflowingAdd<Output = T>,
{
check_same_type(lhs, rhs)?;

let op = move |a: T, b: T| a.overflowing_add(&b);

binary_with_bitmap(lhs, rhs, lhs.data_type().clone(), op)
Expand All @@ -160,7 +144,7 @@ impl<T> ArrayAdd<PrimitiveArray<T>> for PrimitiveArray<T>
where
T: NativeArithmetics + Add<Output = T>,
{
fn add(&self, rhs: &PrimitiveArray<T>) -> Result<Self> {
fn add(&self, rhs: &PrimitiveArray<T>) -> Self {
add(self, rhs)
}
}
Expand All @@ -169,7 +153,7 @@ impl<T> ArrayWrappingAdd<PrimitiveArray<T>> for PrimitiveArray<T>
where
T: NativeArithmetics + WrappingAdd<Output = T>,
{
fn wrapping_add(&self, rhs: &PrimitiveArray<T>) -> Result<Self> {
fn wrapping_add(&self, rhs: &PrimitiveArray<T>) -> Self {
wrapping_add(self, rhs)
}
}
Expand All @@ -179,7 +163,7 @@ impl<T> ArrayCheckedAdd<PrimitiveArray<T>> for PrimitiveArray<T>
where
T: NativeArithmetics + CheckedAdd<Output = T>,
{
fn checked_add(&self, rhs: &PrimitiveArray<T>) -> Result<Self> {
fn checked_add(&self, rhs: &PrimitiveArray<T>) -> Self {
checked_add(self, rhs)
}
}
Expand All @@ -189,7 +173,7 @@ impl<T> ArraySaturatingAdd<PrimitiveArray<T>> for PrimitiveArray<T>
where
T: NativeArithmetics + SaturatingAdd<Output = T>,
{
fn saturating_add(&self, rhs: &PrimitiveArray<T>) -> Result<Self> {
fn saturating_add(&self, rhs: &PrimitiveArray<T>) -> Self {
saturating_add(self, rhs)
}
}
Expand All @@ -199,7 +183,7 @@ impl<T> ArrayOverflowingAdd<PrimitiveArray<T>> for PrimitiveArray<T>
where
T: NativeArithmetics + OverflowingAdd<Output = T>,
{
fn overflowing_add(&self, rhs: &PrimitiveArray<T>) -> Result<(Self, Bitmap)> {
fn overflowing_add(&self, rhs: &PrimitiveArray<T>) -> (Self, Bitmap) {
overflowing_add(self, rhs)
}
}
Expand Down Expand Up @@ -323,8 +307,8 @@ impl<T> ArrayAdd<T> for PrimitiveArray<T>
where
T: NativeArithmetics + Add<Output = T>,
{
fn add(&self, rhs: &T) -> Result<Self> {
Ok(add_scalar(self, rhs))
fn add(&self, rhs: &T) -> Self {
add_scalar(self, rhs)
}
}

Expand All @@ -333,8 +317,8 @@ impl<T> ArrayCheckedAdd<T> for PrimitiveArray<T>
where
T: NativeArithmetics + CheckedAdd<Output = T> + Zero,
{
fn checked_add(&self, rhs: &T) -> Result<Self> {
Ok(checked_add_scalar(self, rhs))
fn checked_add(&self, rhs: &T) -> Self {
checked_add_scalar(self, rhs)
}
}

Expand All @@ -343,8 +327,8 @@ impl<T> ArraySaturatingAdd<T> for PrimitiveArray<T>
where
T: NativeArithmetics + SaturatingAdd<Output = T>,
{
fn saturating_add(&self, rhs: &T) -> Result<Self> {
Ok(saturating_add_scalar(self, rhs))
fn saturating_add(&self, rhs: &T) -> Self {
saturating_add_scalar(self, rhs)
}
}

Expand All @@ -353,7 +337,7 @@ impl<T> ArrayOverflowingAdd<T> for PrimitiveArray<T>
where
T: NativeArithmetics + OverflowingAdd<Output = T>,
{
fn overflowing_add(&self, rhs: &T) -> Result<(Self, Bitmap)> {
Ok(overflowing_add_scalar(self, rhs))
fn overflowing_add(&self, rhs: &T) -> (Self, Bitmap) {
overflowing_add_scalar(self, rhs)
}
}
31 changes: 13 additions & 18 deletions src/compute/arithmetics/basic/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use std::ops::Div;

use num_traits::{CheckedDiv, NumCast, Zero};

use crate::compute::arithmetics::basic::{check_same_len, check_same_type};
use crate::compute::arithmetics::basic::check_same_len;
use crate::datatypes::DataType;
use crate::{
array::{Array, PrimitiveArray},
compute::{
arithmetics::{ArrayCheckedDiv, ArrayDiv, NativeArithmetics},
arity::{binary, binary_checked, unary, unary_checked},
},
error::Result,
types::NativeType,
};
use strength_reduce::{
Expand All @@ -28,26 +27,24 @@ use strength_reduce::{
///
/// let a = Int32Array::from(&[Some(10), Some(1), Some(6)]);
/// let b = Int32Array::from(&[Some(5), None, Some(6)]);
/// let result = div(&a, &b).unwrap();
/// let result = div(&a, &b);
/// let expected = Int32Array::from(&[Some(2), None, Some(1)]);
/// assert_eq!(result, expected)
/// ```
pub fn div<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> Result<PrimitiveArray<T>>
pub fn div<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + Div<Output = T>,
{
check_same_type(lhs, rhs)?;

if rhs.null_count() == 0 {
binary(lhs, rhs, lhs.data_type().clone(), |a, b| a / b)
} else {
check_same_len(lhs, rhs)?;
check_same_len(lhs, rhs).unwrap();
let values = lhs.iter().zip(rhs.iter()).map(|(l, r)| match (l, r) {
(Some(l), Some(r)) => Some(*l / *r),
_ => None,
});

Ok(PrimitiveArray::from_trusted_len_iter(values).to(lhs.data_type().clone()))
PrimitiveArray::from_trusted_len_iter(values).to(lhs.data_type().clone())
}
}

Expand All @@ -62,16 +59,14 @@ where
///
/// let a = Int8Array::from(&[Some(-100i8), Some(10i8)]);
/// let b = Int8Array::from(&[Some(100i8), Some(0i8)]);
/// let result = checked_div(&a, &b).unwrap();
/// let result = checked_div(&a, &b);
/// let expected = Int8Array::from(&[Some(-1i8), None]);
/// assert_eq!(result, expected);
/// ```
pub fn checked_div<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> Result<PrimitiveArray<T>>
pub fn checked_div<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeArithmetics + CheckedDiv<Output = T>,
{
check_same_type(lhs, rhs)?;

let op = move |a: T, b: T| a.checked_div(&b);

binary_checked(lhs, rhs, lhs.data_type().clone(), op)
Expand All @@ -82,7 +77,7 @@ impl<T> ArrayDiv<PrimitiveArray<T>> for PrimitiveArray<T>
where
T: NativeArithmetics + Div<Output = T>,
{
fn div(&self, rhs: &PrimitiveArray<T>) -> Result<Self> {
fn div(&self, rhs: &PrimitiveArray<T>) -> Self {
div(self, rhs)
}
}
Expand All @@ -92,7 +87,7 @@ impl<T> ArrayCheckedDiv<PrimitiveArray<T>> for PrimitiveArray<T>
where
T: NativeArithmetics + CheckedDiv<Output = T>,
{
fn checked_div(&self, rhs: &PrimitiveArray<T>) -> Result<Self> {
fn checked_div(&self, rhs: &PrimitiveArray<T>) -> Self {
checked_div(self, rhs)
}
}
Expand Down Expand Up @@ -208,8 +203,8 @@ impl<T> ArrayDiv<T> for PrimitiveArray<T>
where
T: NativeType + Div<Output = T> + NativeArithmetics + NumCast,
{
fn div(&self, rhs: &T) -> Result<Self> {
Ok(div_scalar(self, rhs))
fn div(&self, rhs: &T) -> Self {
div_scalar(self, rhs)
}
}

Expand All @@ -218,7 +213,7 @@ impl<T> ArrayCheckedDiv<T> for PrimitiveArray<T>
where
T: NativeType + CheckedDiv<Output = T> + Zero + NativeArithmetics,
{
fn checked_div(&self, rhs: &T) -> Result<Self> {
Ok(checked_div_scalar(self, rhs))
fn checked_div(&self, rhs: &T) -> Self {
checked_div_scalar(self, rhs)
}
}
Loading

0 comments on commit d0c0187

Please sign in to comment.