vortex_compute/comparison/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Comparison operations for Vortex vectors.
5
6use vortex_dtype::half::f16;
7
8mod bool;
9mod collection;
10mod pvector;
11
12/// Trait for comparison operations.
13pub trait Compare<Op, Rhs = Self> {
14    /// The result type after performing the operation.
15    type Output;
16
17    /// Perform the comparison operation.
18    fn compare(self, rhs: Rhs) -> Self::Output;
19}
20
21/// Trait for comparison operators.
22pub trait ComparisonOperator<T> {
23    /// Apply the operator to the two operands.
24    fn apply(a: &T, b: &T) -> bool;
25}
26
27/// A marker type for equality comparison operations.
28pub struct Equal;
29/// A marker type for inequality comparison operations.
30pub struct NotEqual;
31/// A marker type for less-than comparison operations.
32pub struct LessThan;
33/// A marker type for less-than-or-equal comparison operations.
34pub struct LessThanOrEqual;
35/// A marker type for greater-than comparison operations.
36pub struct GreaterThan;
37/// A marker type for greater-than-or-equal comparison operations.
38pub struct GreaterThanOrEqual;
39
40/// Marker trait for comparable items.
41pub trait ComparableItem {
42    /// Check if two items are equal.
43    fn is_equal(lhs: &Self, rhs: &Self) -> bool;
44
45    /// Check if the `lhs` item is less than the `rhs` item.
46    fn is_less_than(lhs: &Self, rhs: &Self) -> bool;
47}
48
49impl<T: ComparableItem> ComparisonOperator<T> for Equal {
50    fn apply(a: &T, b: &T) -> bool {
51        T::is_equal(a, b)
52    }
53}
54
55impl<T: ComparableItem> ComparisonOperator<T> for NotEqual {
56    fn apply(a: &T, b: &T) -> bool {
57        !T::is_equal(a, b)
58    }
59}
60
61impl<T: ComparableItem> ComparisonOperator<T> for LessThan {
62    fn apply(a: &T, b: &T) -> bool {
63        T::is_less_than(a, b)
64    }
65}
66
67impl<T: ComparableItem> ComparisonOperator<T> for GreaterThanOrEqual {
68    fn apply(a: &T, b: &T) -> bool {
69        !T::is_less_than(a, b)
70    }
71}
72
73impl<T: ComparableItem> ComparisonOperator<T> for GreaterThan {
74    fn apply(a: &T, b: &T) -> bool {
75        T::is_less_than(b, a)
76    }
77}
78
79impl<T: ComparableItem> ComparisonOperator<T> for LessThanOrEqual {
80    fn apply(a: &T, b: &T) -> bool {
81        !T::is_less_than(b, a)
82    }
83}
84
85macro_rules! impl_integer {
86    ($T:ty) => {
87        impl ComparableItem for $T {
88            #[inline(always)]
89            fn is_equal(lhs: &Self, rhs: &Self) -> bool {
90                lhs == rhs
91            }
92
93            #[inline(always)]
94            fn is_less_than(lhs: &Self, rhs: &Self) -> bool {
95                lhs < rhs
96            }
97        }
98    };
99}
100
101impl_integer!(i8);
102impl_integer!(i16);
103impl_integer!(i32);
104impl_integer!(i64);
105impl_integer!(i128);
106impl_integer!(u8);
107impl_integer!(u16);
108impl_integer!(u32);
109impl_integer!(u64);
110impl_integer!(u128);
111
112macro_rules! impl_float {
113    ($T:ty) => {
114        impl ComparableItem for $T {
115            #[inline(always)]
116            fn is_equal(lhs: &Self, rhs: &Self) -> bool {
117                lhs.to_bits().eq(&rhs.to_bits())
118            }
119
120            #[inline(always)]
121            fn is_less_than(lhs: &Self, rhs: &Self) -> bool {
122                lhs.total_cmp(rhs).is_lt()
123            }
124        }
125    };
126}
127
128impl_float!(f16);
129impl_float!(f32);
130impl_float!(f64);