mod fragment;
mod spread;
mod tribleset;
use std::convert::TryInto;
use crate::id::ExclusiveId;
use crate::id::Id;
use crate::value::Value;
use crate::value::ValueSchema;
pub use fragment::Fragment;
pub use spread::Spread;
pub use tribleset::TribleSet;
pub use tribleset::TribleSetFingerprint;
pub const TRIBLE_LEN: usize = 64;
pub const E_START: usize = 0;
pub const E_END: usize = 15;
pub const A_START: usize = 16;
pub const A_END: usize = 31;
pub const V_START: usize = 32;
pub const V_END: usize = 63;
pub type RawTrible = [u8; TRIBLE_LEN];
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[repr(transparent)]
pub struct Trible {
pub data: RawTrible,
}
impl Trible {
pub fn new<V: ValueSchema>(e: &ExclusiveId, a: &Id, v: &Value<V>) -> Trible {
let mut data = [0; TRIBLE_LEN];
data[E_START..=E_END].copy_from_slice(&e[..]);
data[A_START..=A_END].copy_from_slice(&a[..]);
data[V_START..=V_END].copy_from_slice(&v.raw[..]);
Self { data }
}
pub fn force<V: ValueSchema>(e: &Id, a: &Id, v: &Value<V>) -> Trible {
Trible::new(ExclusiveId::force_ref(e), a, v)
}
pub fn force_raw(data: RawTrible) -> Option<Trible> {
if data[E_START..=E_END].iter().all(|&x| x == 0)
|| data[A_START..=A_END].iter().all(|&x| x == 0)
{
return None;
}
Some(Self { data })
}
pub fn as_transmute_force_raw(data: &RawTrible) -> Option<&Self> {
if data[E_START..=E_END].iter().all(|&x| x == 0)
|| data[A_START..=A_END].iter().all(|&x| x == 0)
{
return None;
}
Some(unsafe { std::mem::transmute::<&RawTrible, &Self>(data) })
}
pub fn as_transmute_raw_unchecked(data: &RawTrible) -> &Self {
unsafe { std::mem::transmute::<&RawTrible, &Self>(data) }
}
pub fn e(&self) -> &Id {
Id::as_transmute_raw(self.data[E_START..=E_END].try_into().unwrap()).unwrap()
}
pub fn a(&self) -> &Id {
Id::as_transmute_raw(self.data[A_START..=A_END].try_into().unwrap()).unwrap()
}
pub fn v<V: ValueSchema>(&self) -> &Value<V> {
Value::as_transmute_raw(self.data[V_START..=V_END].try_into().unwrap())
}
}
crate::key_segmentation!(
TribleSegmentation, TRIBLE_LEN, [16, 16, 32]
);
crate::key_schema!(
EAVOrder, TribleSegmentation, TRIBLE_LEN, [0, 1, 2]
);
crate::key_schema!(
EVAOrder, TribleSegmentation, TRIBLE_LEN, [0, 2, 1]
);
crate::key_schema!(
AEVOrder, TribleSegmentation, TRIBLE_LEN, [1, 0, 2]
);
crate::key_schema!(
AVEOrder, TribleSegmentation, TRIBLE_LEN, [1, 2, 0]
);
crate::key_schema!(
VEAOrder, TribleSegmentation, TRIBLE_LEN, [2, 0, 1]
);
crate::key_schema!(
VAEOrder, TribleSegmentation, TRIBLE_LEN, [2, 1, 0]
);
#[cfg(test)]
mod tests {
use super::*;
use crate::patch::KeySchema;
#[rustfmt::skip]
#[test]
fn order_eav() {
let key_bytes = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
];
let tree_bytes = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
];
assert_eq!(EAVOrder::tree_ordered(&key_bytes), tree_bytes);
assert_eq!(EAVOrder::key_ordered(&tree_bytes), key_bytes);
}
#[rustfmt::skip]
#[test]
fn order_eva() {
let key_bytes = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
];
let tree_bytes = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
];
assert_eq!(EVAOrder::tree_ordered(&key_bytes), tree_bytes);
assert_eq!(EVAOrder::key_ordered(&tree_bytes), key_bytes);
}
#[rustfmt::skip]
#[test]
fn order_aev() {
let key_bytes = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
];
let tree_bytes = [
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
];
assert_eq!(AEVOrder::tree_ordered(&key_bytes), tree_bytes);
assert_eq!(AEVOrder::key_ordered(&tree_bytes), key_bytes);
}
#[rustfmt::skip]
#[test]
fn order_ave() {
let key_bytes = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
];
let tree_bytes = [
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
];
assert_eq!(AVEOrder::tree_ordered(&key_bytes), tree_bytes);
assert_eq!(AVEOrder::key_ordered(&tree_bytes), key_bytes);
}
#[rustfmt::skip]
#[test]
fn order_vea() {
let key_bytes = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
];
let tree_bytes = [
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
];
assert_eq!(VEAOrder::tree_ordered(&key_bytes), tree_bytes);
assert_eq!(VEAOrder::key_ordered(&tree_bytes), key_bytes);
}
#[rustfmt::skip]
#[test]
fn order_vae() {
let key_bytes = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
];
let tree_bytes = [
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
];
assert_eq!(VAEOrder::tree_ordered(&key_bytes), tree_bytes);
assert_eq!(VAEOrder::key_ordered(&tree_bytes), key_bytes);
}
}