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