pub trait Key: Clone + Ord + Send + Sync + std::fmt::Debug + 'static {
fn to_model_input(&self) -> f64;
fn to_exact_ordinal(&self) -> i128;
}
macro_rules! impl_key_unsigned {
($($t:ty),*) => {
$(
impl Key for $t {
#[inline]
fn to_model_input(&self) -> f64 {
*self as f64
}
#[inline]
fn to_exact_ordinal(&self) -> i128 {
*self as i128
}
}
)*
};
}
macro_rules! impl_key_signed {
($($t:ty),*) => {
$(
impl Key for $t {
#[inline]
fn to_model_input(&self) -> f64 {
*self as f64
}
#[inline]
fn to_exact_ordinal(&self) -> i128 {
*self as i128
}
}
)*
};
}
impl_key_unsigned!(u8, u16, u32, u64);
impl_key_signed!(i8, i16, i32, i64);
impl Key for u128 {
#[inline]
fn to_model_input(&self) -> f64 {
*self as f64
}
#[inline]
#[allow(clippy::cast_possible_wrap)]
fn to_exact_ordinal(&self) -> i128 {
(*self as i128) ^ i128::MIN
}
}
impl Key for i128 {
#[inline]
fn to_model_input(&self) -> f64 {
*self as f64
}
#[inline]
fn to_exact_ordinal(&self) -> i128 {
*self
}
}
#[inline]
pub fn bytes_to_model_input(bytes: &[u8]) -> f64 {
let mut buf = [0u8; 8];
let len = bytes.len().min(8);
buf[..len].copy_from_slice(&bytes[..len]);
u64::from_be_bytes(buf) as f64
}
#[inline]
#[allow(clippy::cast_possible_wrap)]
pub fn bytes_to_exact_ordinal(bytes: &[u8]) -> i128 {
let mut buf = [0u8; 16];
let len = bytes.len().min(16);
buf[..len].copy_from_slice(&bytes[..len]);
(u128::from_be_bytes(buf) as i128) ^ i128::MIN
}
impl<const N: usize> Key for [u8; N] {
#[inline]
fn to_model_input(&self) -> f64 {
bytes_to_model_input(self)
}
#[inline]
fn to_exact_ordinal(&self) -> i128 {
bytes_to_exact_ordinal(self)
}
}
impl Key for String {
#[inline]
fn to_model_input(&self) -> f64 {
bytes_to_model_input(self.as_bytes())
}
#[inline]
fn to_exact_ordinal(&self) -> i128 {
bytes_to_exact_ordinal(self.as_bytes())
}
}
impl Key for Vec<u8> {
#[inline]
fn to_model_input(&self) -> f64 {
bytes_to_model_input(self)
}
#[inline]
fn to_exact_ordinal(&self) -> i128 {
bytes_to_exact_ordinal(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn u64_monotonic() {
let keys: Vec<u64> = vec![0, 1, 100, 1000, u64::MAX / 2];
for pair in keys.windows(2) {
assert!(
pair[0].to_model_input() < pair[1].to_model_input(),
"monotonicity violated: {} >= {}",
pair[0].to_model_input(),
pair[1].to_model_input()
);
}
}
#[test]
fn i64_monotonic() {
let keys: Vec<i64> = vec![i64::MIN, -1000, -1, 0, 1, 1000, i64::MAX / 2];
for pair in keys.windows(2) {
assert!(
pair[0].to_model_input() < pair[1].to_model_input(),
"monotonicity violated for i64: {} >= {}",
pair[0].to_model_input(),
pair[1].to_model_input()
);
}
}
#[test]
fn u32_finite() {
for &k in &[0u32, 1, u32::MAX] {
let v = k.to_model_input();
assert!(v.is_finite(), "{k} produced non-finite model input {v}");
}
}
#[test]
fn key_is_send_sync() {
fn assert_send_sync<T: Key>() {}
assert_send_sync::<u64>();
assert_send_sync::<i32>();
}
#[test]
fn exact_ordinal_injective_near_precision_boundary() {
let base: u64 = 1 << 53;
#[allow(clippy::float_cmp)]
{
assert_eq!(base as f64, (base + 1) as f64, "precondition: same f64");
}
let o1 = base.to_exact_ordinal();
let o2 = (base + 1).to_exact_ordinal();
assert_ne!(o1, o2, "to_exact_ordinal must be injective");
assert!(o1 < o2, "to_exact_ordinal must be monotonic");
}
#[test]
fn exact_ordinal_injective_nanosecond_timestamps() {
let base: u64 = 1_700_000_000_000_000_000;
for i in 0..256u64 {
let o1 = (base + i).to_exact_ordinal();
let o2 = (base + i + 1).to_exact_ordinal();
assert!(o1 < o2, "monotonicity violated at offset {i}");
}
}
#[test]
fn u128_exact_ordinal_preserves_order() {
let vals: Vec<u128> = vec![0, 1, u128::MAX / 2, u128::MAX / 2 + 1, u128::MAX];
for pair in vals.windows(2) {
assert!(
pair[0].to_exact_ordinal() < pair[1].to_exact_ordinal(),
"u128 order violated: {} vs {}",
pair[0],
pair[1]
);
}
}
#[test]
fn byte_array_key_is_send_sync() {
fn assert_key<T: Key>() {}
assert_key::<[u8; 4]>();
assert_key::<[u8; 8]>();
assert_key::<[u8; 16]>();
assert_key::<[u8; 32]>();
}
#[test]
fn byte4_model_input_monotonic() {
let keys: Vec<[u8; 4]> = vec![[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0]];
for pair in keys.windows(2) {
assert!(
pair[0].to_model_input() <= pair[1].to_model_input(),
"monotonicity violated for [u8; 4]: {:?} > {:?}",
pair[0],
pair[1]
);
}
}
#[test]
fn byte8_model_input_monotonic() {
let keys: Vec<[u8; 8]> = vec![
[0; 8],
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[255; 8],
];
for pair in keys.windows(2) {
assert!(
pair[0].to_model_input() <= pair[1].to_model_input(),
"monotonicity violated for [u8; 8]: {:?} > {:?}",
pair[0],
pair[1]
);
}
}
#[test]
fn byte16_exact_ordinal_injective() {
let keys: Vec<[u8; 16]> = vec![
[0; 16],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[255; 16],
];
for pair in keys.windows(2) {
assert!(
pair[0].to_exact_ordinal() < pair[1].to_exact_ordinal(),
"exact_ordinal not injective for [u8; 16]: {:?} >= {:?}",
pair[0],
pair[1]
);
}
}
#[test]
fn byte32_model_input_finite() {
let keys: Vec<[u8; 32]> = vec![[0; 32], [128; 32], [255; 32]];
for k in &keys {
let v = k.to_model_input();
assert!(v.is_finite(), "{k:?} produced non-finite model input {v}");
}
}
#[test]
fn byte4_exact_ordinal_monotonic() {
let keys: Vec<[u8; 4]> = vec![[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0]];
for pair in keys.windows(2) {
assert!(
pair[0].to_exact_ordinal() < pair[1].to_exact_ordinal(),
"exact_ordinal not monotonic for [u8; 4]: {:?} >= {:?}",
pair[0],
pair[1]
);
}
}
#[test]
fn byte32_exact_ordinal_prefix_only() {
let mut a = [0u8; 32];
let mut b = [0u8; 32];
a[20] = 1;
b[20] = 2;
assert_eq!(a.to_exact_ordinal(), b.to_exact_ordinal());
assert!(a < b);
}
#[test]
fn string_key_is_send_sync() {
fn assert_key<T: Key>() {}
assert_key::<String>();
}
#[test]
fn string_model_input_monotonic() {
let keys: Vec<String> = vec![
String::new(),
"a".into(),
"b".into(),
"hello".into(),
"world".into(),
"zzzzzzzz".into(),
];
for pair in keys.windows(2) {
assert!(
pair[0].to_model_input() <= pair[1].to_model_input(),
"monotonicity violated for String: {:?} > {:?}",
pair[0],
pair[1]
);
}
}
#[test]
fn string_model_input_finite() {
let keys = vec![String::new(), "hello".into(), "\u{ffff}".into()];
for k in &keys {
let v = k.to_model_input();
assert!(v.is_finite(), "{k:?} produced non-finite model input {v}");
}
}
#[test]
fn string_exact_ordinal_monotonic() {
let keys: Vec<String> = vec![
String::new(),
"a".into(),
"aa".into(),
"ab".into(),
"b".into(),
];
for pair in keys.windows(2) {
assert!(
pair[0].to_exact_ordinal() <= pair[1].to_exact_ordinal(),
"exact_ordinal not monotonic for String: {:?} >= {:?}",
pair[0],
pair[1]
);
}
}
#[test]
fn string_shared_prefix_same_ordinal() {
let a = "1234567890abcdefXXX".to_string();
let b = "1234567890abcdefYYY".to_string();
assert_eq!(a.to_exact_ordinal(), b.to_exact_ordinal());
assert!(a < b);
}
#[test]
fn string_different_in_first_16_bytes() {
let a = "1234567890abcdeX".to_string();
let b = "1234567890abcdeY".to_string();
assert!(a.to_exact_ordinal() < b.to_exact_ordinal());
}
#[test]
fn string_matches_byte_array() {
let s = "hello".to_string();
let arr: [u8; 5] = *b"hello";
#[allow(clippy::float_cmp)]
{
assert_eq!(s.to_model_input(), arr.to_model_input());
}
assert_eq!(s.to_exact_ordinal(), arr.to_exact_ordinal());
}
#[test]
fn vec_u8_key_is_send_sync() {
fn assert_key<T: Key>() {}
assert_key::<Vec<u8>>();
}
#[test]
fn vec_u8_model_input_monotonic() {
let keys: Vec<Vec<u8>> = vec![vec![], vec![0], vec![0, 1], vec![1], vec![1, 0], vec![255]];
for pair in keys.windows(2) {
assert!(
pair[0].to_model_input() <= pair[1].to_model_input(),
"monotonicity violated for Vec<u8>: {:?} > {:?}",
pair[0],
pair[1]
);
}
}
#[test]
fn vec_u8_exact_ordinal_monotonic() {
let keys: Vec<Vec<u8>> = vec![
vec![],
vec![0],
vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
vec![1],
];
for pair in keys.windows(2) {
assert!(
pair[0].to_exact_ordinal() <= pair[1].to_exact_ordinal(),
"exact_ordinal not monotonic for Vec<u8>: {:?} >= {:?}",
pair[0],
pair[1]
);
}
}
}