use std::cmp::Ordering;
use compare::Compare;
use crate::{SubProblemRanking, SubProblem};
#[derive(Debug, Clone, Copy)]
pub struct CompareSubProblem<X:SubProblemRanking>(X);
impl <X:SubProblemRanking> CompareSubProblem<X> {
pub fn new(x: X) -> Self {
Self(x)
}
}
impl <X:SubProblemRanking> Compare<SubProblem<X::State>> for CompareSubProblem<X> {
fn compare(&self, l: &SubProblem<X::State>, r: &SubProblem<X::State>) -> Ordering {
self.0.compare(l, r)
}
}
#[cfg(test)]
mod test {
use std::{cmp::Ordering, ops::Deref, sync::Arc};
use compare::Compare;
use crate::{StateRanking, SubProblemRanking, SubProblem, CompareSubProblem};
struct CharRanking;
impl StateRanking for CharRanking {
type State = char;
fn compare(&self, a: &Self::State, b: &Self::State) -> Ordering {
a.cmp(b)
}
}
impl SubProblemRanking for CharRanking {
type State = char;
fn compare(&self, a: &SubProblem<char>, b: &SubProblem<char>) -> Ordering {
<Self as StateRanking>::compare(self, a.state.deref(), b.state.deref())
}
}
#[test]
fn when_a_is_less_than_b_comparesubproblem_returns_less() {
let cmp = CompareSubProblem::new(CharRanking);
assert_eq!(cmp.compare(
&SubProblem{state: Arc::new('a'), value: 0, ub: isize::MAX, path: vec![]},
&SubProblem{state: Arc::new('b'), value: 0, ub: isize::MAX, path: vec![]}),
Ordering::Less);
}
#[test]
fn when_a_is_greater_than_b_comparesubproblem_returns_greater() {
let cmp = CompareSubProblem::new(CharRanking);
assert_eq!(cmp.compare(
&SubProblem{state: Arc::new('b'), value: 0, ub: isize::MAX, path: vec![]},
&SubProblem{state: Arc::new('a'), value: 0, ub: isize::MAX, path: vec![]}),
Ordering::Greater);
}
#[test]
fn when_a_is_equal_to_b_comparesubproblem_returns_equal() {
let cmp = CompareSubProblem::new(CharRanking);
assert_eq!(cmp.compare(
&SubProblem{state: Arc::new('a'), value: 0, ub: isize::MAX, path: vec![]},
&SubProblem{state: Arc::new('a'), value: 0, ub: isize::MAX, path: vec![]}),
Ordering::Equal);
}
}