use std::cmp::{PartialOrd};
use std::time::{Duration, SystemTime};
#[derive(Debug)]
pub struct SearchManager<N, B> {
t_start: SystemTime,
best: Option<N>,
best_val: Option<B>,
}
impl<N:Clone, B:PartialOrd+Copy> Default for SearchManager<N, B> {
fn default() -> Self {
SearchManager {
t_start: SystemTime::now(),
best: None,
best_val: None,
}
}
}
impl<N:Clone, B:PartialOrd+Copy> SearchManager<N, B> {
pub fn best(&self) -> &Option<N> { &self.best }
pub fn best_val(&self) -> &Option<B> { &self.best_val }
pub fn elapsed_time(&self) -> Duration { self.t_start.elapsed().unwrap() }
pub fn is_better(&self, e: B) -> bool {
match self.best_val {
Some(a) => e < a,
None => true,
}
}
pub fn update_best(&mut self, s: N, e: B) {
if self.is_better(e) {
self.best = Some(s);
self.best_val = Some(e);
}
}
pub fn give_best(&mut self, other: &mut Self) {
match self.best_val {
None => {},
Some(b) => {
other.update_best(self.best.as_ref().unwrap().clone(), b);
}
};
}
}