extern crate fnv;
use fnv::FnvHashMap;
pub trait GrayVersion {
fn get_gray(&self) -> &Option<usize>;
fn set_gray(&mut self, g: Option<usize>);
fn get_id(&self) -> usize;
}
pub trait Gray {}
pub struct GrayTab<T: Gray> {
last: usize,
tab: FnvHashMap<usize, T>,
}
impl<T: Gray> GrayTab<T> {
pub fn new(first: T) -> Self {
let mut map = FnvHashMap::default();
map.insert(0, first);
GrayTab { last: 0, tab: map }
}
pub fn get(&self, version: &usize) -> Option<&T> {
self.tab.get(version)
}
pub fn add(&mut self, gray: T) -> usize {
self.last += 1;
self.tab.insert(self.last, gray);
self.last
}
pub fn remove(&mut self, version: &usize) -> Option<T> {
match self.last {
0 => {
println!("The only gray cannot be removed");
return None;
}
_ => (),
};
match version == &self.last {
true => self.version_back(),
false => (),
}
self.tab.remove(version)
}
pub fn get_last(&self) -> &T {
self.tab.get(&self.last).unwrap()
}
fn version_back(&mut self) {
self.last = self.last - 1;
match self.tab.get(&self.last) {
Some(_) => (),
None => self.version_back(),
}
}
}