use search_core::Cost;
use crate::pareto::{Front, TreeFront};
#[derive(Debug, Clone)]
pub(crate) enum TruncatedFront<C, const N: usize> {
Two(Front<C, 2>),
Zeroed(Front<C, N>),
}
impl<C: Cost, const N: usize> TruncatedFront<C, N> {
pub(crate) const fn new() -> Self {
if N == 3 {
Self::Two(Front::new())
} else {
Self::Zeroed(Front::new())
}
}
pub(crate) fn weakly_dominates(&self, point: &[C; N]) -> bool {
match self {
Self::Two(front) => front.weakly_dominates(&[point[1], point[2]]),
Self::Zeroed(front) => front.weakly_dominates(&zero_first(*point)),
}
}
pub(crate) fn insert(&mut self, point: [C; N]) -> bool {
match self {
Self::Two(front) => front.insert([point[1], point[2]]),
Self::Zeroed(front) => front.insert(zero_first(point)),
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct TruncatedTree<C, const N: usize>(TreeFront<C, N>);
impl<C: Cost, const N: usize> TruncatedTree<C, N> {
pub(crate) const fn new() -> Self {
Self(TreeFront::new())
}
pub(crate) fn weakly_dominates(&self, point: &[C; N]) -> bool {
self.0.weakly_dominates(&zero_first(*point))
}
pub(crate) fn insert(&mut self, point: [C; N]) -> bool {
self.0.insert(zero_first(point))
}
}
fn zero_first<C: Cost, const N: usize>(mut point: [C; N]) -> [C; N] {
if let Some(first) = point.first_mut() {
*first = C::ZERO;
}
point
}
#[cfg(test)]
mod tests {
use super::TruncatedFront;
#[test]
fn ignores_first_objective() {
let mut two = TruncatedFront::<u8, 2>::new();
assert!(two.insert([9, 3]));
assert!(two.weakly_dominates(&[0, 3]));
assert!(!two.weakly_dominates(&[0, 2]));
let mut three = TruncatedFront::<u8, 3>::new();
assert!(three.insert([9, 3, 3]));
assert!(three.weakly_dominates(&[0, 3, 4]));
assert!(!three.weakly_dominates(&[0, 4, 2]));
let mut four = TruncatedFront::<u8, 4>::new();
assert!(four.insert([9, 3, 3, 3]));
assert!(four.weakly_dominates(&[0, 3, 4, 3]));
assert!(!four.weakly_dominates(&[9, 3, 3, 2]));
}
}