use std::collections::HashMap;
use std::hash::Hash;
pub struct Scope<K: Hash + Eq, V>(Vec<HashMap<K, V>>);
impl<K: Hash + Eq, V> Scope<K, V> {
#[inline]
pub fn new() -> Self {
Self(vec![HashMap::new()])
}
#[inline]
pub fn enter_scope(&mut self) {
self.0.push(HashMap::<K, V>::new());
}
#[inline]
pub fn leave_scope(&mut self) {
self.0.pop();
}
#[inline]
pub fn get(&self, item: &K) -> Option<&V> {
for stack_item in self.0.iter().rev() {
let cur_item = stack_item.get(item);
if cur_item.is_some() {
return cur_item;
}
}
None
}
#[inline]
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
self.0.last_mut().unwrap().insert(key, value)
}
pub fn depth(&self) -> usize {
self.0.len()
}
}