use core::borrow::Borrow;
use core::iter::FromIterator;
#[derive(Debug)]
pub struct Translator<T> {
widdershins: Box<[T]>,
}
impl<T: Ord> Translator<T> {
pub fn forward(&self, t: &impl Borrow<T>) -> Option<usize> {
self.widdershins.binary_search(t.borrow()).ok()
}
pub fn back(&self, a: usize) -> Option<&T> {
self.widdershins.get(a)
}
}
impl<T: Ord> FromIterator<T> for Translator<T> {
fn from_iter<I: IntoIterator<Item = T>>(src: I) -> Self {
let mut table: Vec<T> = src.into_iter().collect();
table.sort_unstable();
table.dedup();
Self {
widdershins: table.into_boxed_slice(),
}
}
}