crate::ix!();
pub trait CompareLeafGranularity {
fn compare_leaf_granularity(&self, other: &GrowerTreeConfiguration) -> CompareOutcome;
}
impl CompareLeafGranularity for GrowerTreeConfiguration {
fn compare_leaf_granularity(&self, other: &GrowerTreeConfiguration) -> CompareOutcome {
let g1 = self.leaf_granularity();
let g2 = other.leaf_granularity();
let diff = (g1 - g2).abs();
if diff < 0.01 {
CompareOutcome::Exact
} else if diff < 0.15 {
let score = 1.0 - diff * 3.0;
CompareOutcome::Partial(score.max(0.0))
} else {
CompareOutcome::Incompatible
}
}
}
#[cfg(test)]
mod compare_leaf_granularity_tests {
use super::*;
use traced_test::traced_test;
#[traced_test]
fn exact_match() {
let a = GrowerTreeConfiguration::base_config(5,2,2)
.to_builder().leaf_granularity(0.5).build().unwrap();
let b = a.clone();
assert_eq!(a.compare_leaf_granularity(&b), CompareOutcome::Exact);
}
#[traced_test]
fn partial_match() {
let a = GrowerTreeConfiguration::base_config(5,2,2)
.to_builder().leaf_granularity(0.3).build().unwrap();
let b = a.to_builder().leaf_granularity(0.45).build().unwrap();
match a.compare_leaf_granularity(&b) {
CompareOutcome::Partial(_) => {},
other => panic!("Expected partial, got {:?}", other),
}
}
#[traced_test]
fn incompatible() {
let a = GrowerTreeConfiguration::base_config(5,2,2)
.to_builder().leaf_granularity(0.1).build().unwrap();
let b = a.to_builder().leaf_granularity(0.8).build().unwrap();
assert_eq!(a.compare_leaf_granularity(&b), CompareOutcome::Incompatible);
}
}