vortex_array/arrays/primitive/
native_value.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::cmp::Ordering;
5
6use vortex_dtype::NativePType;
7use vortex_dtype::half;
8
9/// NativeValue serves as a wrapper type to allow us to implement Hash, Eq and other traits on all primitive types.
10///
11/// Rust does not define Hash/Eq for any of the float types due to the presence of
12/// NaN and +/- 0. We don't care about storing multiple NaNs or zeros in our dictionaries,
13/// so we define simple bit-wise Hash/Eq for the Value-wrapped versions of these types.
14#[repr(transparent)]
15#[derive(Copy, Clone, Debug)]
16pub struct NativeValue<T>(pub T);
17
18impl<T: NativePType> PartialEq<NativeValue<T>> for NativeValue<T> {
19    fn eq(&self, other: &NativeValue<T>) -> bool {
20        self.0.is_eq(other.0)
21    }
22}
23
24impl<T: NativePType> Eq for NativeValue<T> {}
25
26macro_rules! prim_value {
27    ($typ:ty) => {
28        impl core::hash::Hash for NativeValue<$typ> {
29            fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
30                self.0.hash(state);
31            }
32        }
33    };
34}
35
36impl<T: NativePType> PartialOrd<NativeValue<T>> for NativeValue<T> {
37    fn partial_cmp(&self, other: &NativeValue<T>) -> Option<Ordering> {
38        Some(self.0.total_compare(other.0))
39    }
40}
41
42macro_rules! float_value {
43    ($typ:ty) => {
44        impl core::hash::Hash for NativeValue<$typ> {
45            fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
46                self.0.to_bits().hash(state);
47            }
48        }
49    };
50}
51
52prim_value!(u8);
53prim_value!(u16);
54prim_value!(u32);
55prim_value!(u64);
56prim_value!(i8);
57prim_value!(i16);
58prim_value!(i32);
59prim_value!(i64);
60float_value!(half::f16);
61float_value!(f32);
62float_value!(f64);