use relmath::{
UnaryRelation,
annotated::{AnnotatedRelation, Semiring},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct SupportCount(u8);
impl Semiring for SupportCount {
fn zero() -> Self {
Self(0)
}
fn one() -> Self {
Self(1)
}
fn add(&self, rhs: &Self) -> Self {
Self(self.0.saturating_add(rhs.0))
}
fn mul(&self, rhs: &Self) -> Self {
Self(self.0.saturating_mul(rhs.0))
}
}
fn main() {
let confirmations = AnnotatedRelation::from_facts([
(("Alice", "Math"), SupportCount(1)),
(("Alice", "Math"), SupportCount(1)),
(("Bob", "Physics"), SupportCount(1)),
(("Cara", "Logic"), SupportCount::zero()),
]);
let exact_completed = confirmations.to_binary_relation();
let alice = UnaryRelation::singleton("Alice");
assert_eq!(
confirmations.annotation_of(&("Alice", "Math")),
Some(&SupportCount(2))
);
assert_eq!(exact_completed.image(&alice).to_vec(), vec!["Math"]);
assert!(!confirmations.contains_fact(&("Cara", "Logic")));
assert_eq!(
confirmations
.iter()
.map(|(fact, count)| (*fact, count.0))
.collect::<Vec<_>>(),
vec![(("Alice", "Math"), 2), (("Bob", "Physics"), 1)]
);
println!(
"stored support counts: {:?}",
confirmations
.iter()
.map(|(fact, count)| (*fact, count.0))
.collect::<Vec<_>>()
);
}