Skip to content

Commit

Permalink
Add compare to ArrowNativeTypeOp (apache#3070)
Browse files Browse the repository at this point in the history
* Add total_cmp to ArrowNativeTypeOp

* Format
  • Loading branch information
tustvold authored Nov 10, 2022
1 parent 5fb3033 commit e44cb5b
Showing 1 changed file with 36 additions and 58 deletions.
94 changes: 36 additions & 58 deletions arrow-array/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use arrow_buffer::{i256, ArrowNativeType};
use arrow_schema::ArrowError;
use half::f16;
use num::complex::ComplexFloat;
use std::cmp::Ordering;

/// Trait for [`ArrowNativeType`] that adds checked and unchecked arithmetic operations,
/// and totally ordered comparison operations
Expand Down Expand Up @@ -74,17 +75,34 @@ pub trait ArrowNativeTypeOp: ArrowNativeType {

fn is_zero(self) -> bool;

fn is_eq(self, rhs: Self) -> bool;

fn is_ne(self, rhs: Self) -> bool;

fn is_lt(self, rhs: Self) -> bool;
fn compare(self, rhs: Self) -> Ordering;

fn is_le(self, rhs: Self) -> bool;

fn is_gt(self, rhs: Self) -> bool;
fn is_eq(self, rhs: Self) -> bool;

fn is_ge(self, rhs: Self) -> bool;
#[inline]
fn is_ne(self, rhs: Self) -> bool {
!self.is_eq(rhs)
}

#[inline]
fn is_lt(self, rhs: Self) -> bool {
self.compare(rhs).is_lt()
}

#[inline]
fn is_le(self, rhs: Self) -> bool {
self.compare(rhs).is_le()
}

#[inline]
fn is_gt(self, rhs: Self) -> bool {
self.compare(rhs).is_gt()
}

#[inline]
fn is_ge(self, rhs: Self) -> bool {
self.compare(rhs).is_ge()
}
}

macro_rules! native_type_op {
Expand Down Expand Up @@ -209,33 +227,13 @@ macro_rules! native_type_op {
}

#[inline]
fn is_eq(self, rhs: Self) -> bool {
self == rhs
}

#[inline]
fn is_ne(self, rhs: Self) -> bool {
self != rhs
fn compare(self, rhs: Self) -> Ordering {
self.cmp(&rhs)
}

#[inline]
fn is_lt(self, rhs: Self) -> bool {
self < rhs
}

#[inline]
fn is_le(self, rhs: Self) -> bool {
self <= rhs
}

#[inline]
fn is_gt(self, rhs: Self) -> bool {
self > rhs
}

#[inline]
fn is_ge(self, rhs: Self) -> bool {
self >= rhs
fn is_eq(self, rhs: Self) -> bool {
self == rhs
}
}
};
Expand Down Expand Up @@ -341,38 +339,18 @@ macro_rules! native_type_float_op {
self == $zero
}

#[inline]
fn compare(self, rhs: Self) -> Ordering {
<$t>::total_cmp(&self, &rhs)
}

#[inline]
fn is_eq(self, rhs: Self) -> bool {
// Equivalent to `self.total_cmp(&rhs).is_eq()`
// but LLVM isn't able to realise this is bitwise equality
// https://rust.godbolt.org/z/347nWGxoW
self.to_bits() == rhs.to_bits()
}

#[inline]
fn is_ne(self, rhs: Self) -> bool {
!self.is_eq(rhs)
}

#[inline]
fn is_lt(self, rhs: Self) -> bool {
self.total_cmp(&rhs).is_lt()
}

#[inline]
fn is_le(self, rhs: Self) -> bool {
self.total_cmp(&rhs).is_le()
}

#[inline]
fn is_gt(self, rhs: Self) -> bool {
self.total_cmp(&rhs).is_gt()
}

#[inline]
fn is_ge(self, rhs: Self) -> bool {
self.total_cmp(&rhs).is_ge()
}
}
};
}
Expand Down

0 comments on commit e44cb5b

Please sign in to comment.