@@ -13,6 +13,7 @@ mod serde;
13
13
pub use array:: * ;
14
14
pub use compress:: * ;
15
15
use vortex_buffer:: { Buffer , BufferMut } ;
16
+ use vortex_dtype:: NativePType ;
16
17
17
18
const SAMPLE_SIZE : usize = 32 ;
18
19
@@ -35,7 +36,7 @@ mod private {
35
36
impl Sealed for f64 { }
36
37
}
37
38
38
- pub trait ALPFloat : private:: Sealed + Float + Display + ' static {
39
+ pub trait ALPFloat : private:: Sealed + Float + Display + NativePType {
39
40
type ALPInt : PrimInt + Display + ToPrimitive + Copy ;
40
41
41
42
const FRACTIONAL_BITS : u8 ;
@@ -148,9 +149,9 @@ pub trait ALPFloat: private::Sealed + Float + Display + 'static {
148
149
149
150
#[ inline]
150
151
fn encode_single ( value : Self , exponents : Exponents ) -> Option < Self :: ALPInt > {
151
- let encoded = unsafe { Self :: encode_single_unchecked ( value, exponents) } ;
152
+ let encoded = Self :: encode_single_unchecked ( value, exponents) ;
152
153
let decoded = Self :: decode_single ( encoded, exponents) ;
153
- if decoded == value {
154
+ if decoded. is_eq ( value) {
154
155
return Some ( encoded) ;
155
156
}
156
157
None
@@ -185,11 +186,10 @@ pub trait ALPFloat: private::Sealed + Float + Display + 'static {
185
186
Self :: from_int ( encoded) * Self :: F10 [ exponents. f as usize ] * Self :: IF10 [ exponents. e as usize ]
186
187
}
187
188
188
- /// # Safety
189
- ///
190
- /// The returned value may not decode back to the original value.
189
+ /// Encode single float value. The returned value might decode to a different value than passed in.
190
+ /// Consider using [`Self::encode_single`] if you want the checked version of this function.
191
191
#[ inline( always) ]
192
- unsafe fn encode_single_unchecked ( value : Self , exponents : Exponents ) -> Self :: ALPInt {
192
+ fn encode_single_unchecked ( value : Self , exponents : Exponents ) -> Self :: ALPInt {
193
193
( value * Self :: F10 [ exponents. e as usize ] * Self :: IF10 [ exponents. f as usize ] )
194
194
. fast_round ( )
195
195
. as_int ( )
@@ -212,10 +212,10 @@ fn encode_chunk_unchecked<T: ALPFloat>(
212
212
213
213
// encode the chunk, counting the number of patches
214
214
let mut chunk_patch_count = 0 ;
215
- encoded_output. extend ( chunk. iter ( ) . map ( |v| {
216
- let encoded = unsafe { T :: encode_single_unchecked ( * v, exp) } ;
215
+ encoded_output. extend ( chunk. iter ( ) . map ( |& v| {
216
+ let encoded = T :: encode_single_unchecked ( v, exp) ;
217
217
let decoded = T :: decode_single ( encoded, exp) ;
218
- let neq = ( decoded != * v) as usize ;
218
+ let neq = !decoded . is_eq ( v) as usize ;
219
219
chunk_patch_count += neq;
220
220
encoded
221
221
} ) ) ;
@@ -237,7 +237,7 @@ fn encode_chunk_unchecked<T: ALPFloat>(
237
237
// write() is only safe to call more than once because the values are primitive (i.e., Drop is a no-op)
238
238
patch_indices_mut[ chunk_patch_index] . write ( i as u64 ) ;
239
239
patch_values_mut[ chunk_patch_index] . write ( chunk[ i - num_prev_encoded] ) ;
240
- chunk_patch_index += ( decoded != chunk[ i - num_prev_encoded] ) as usize ;
240
+ chunk_patch_index += !decoded . is_eq ( chunk[ i - num_prev_encoded] ) as usize ;
241
241
}
242
242
assert_eq ! ( chunk_patch_index, chunk_patch_count) ;
243
243
unsafe {
0 commit comments