use crate::core::util::error::lucene_error::LuceneError;
use crate::core::util::error::lucene_error::Result;
use crate::core::util::numeric_utils::NumericUtils;
pub struct XYEncodingUtils;
impl XYEncodingUtils {
pub const MIN_VAL_INCL: f64 = -f32::MAX as f64;
pub const MAX_VAL_INCL: f64 = f32::MAX as f64;
pub fn check_val(x: f32) -> Result<f32> {
if !x.is_finite() {
return Err(LuceneError::illegal_argument(format!(
"invalid value {x}; must be between {} and {}",
Self::MIN_VAL_INCL,
Self::MAX_VAL_INCL
)));
}
Ok(x)
}
pub fn encode(x: f32) -> Result<i32> {
Ok(NumericUtils::float_to_sortable_int(Self::check_val(x)?))
}
pub fn decode(encoded: i32) -> f32 {
let result = NumericUtils::sortable_int_to_float(encoded);
debug_assert!(result >= Self::MIN_VAL_INCL as f32 && result <= Self::MAX_VAL_INCL as f32);
result
}
pub fn decode_bytes(src: &[u8], offset: usize) -> f32 {
Self::decode(NumericUtils::sortable_bytes_to_int(src, offset))
}
pub fn float_array_to_double_array(f: &[f32]) -> Vec<f64> {
f.iter().map(|&x| x as f64).collect()
}
}