use crate::tree::OpaqueElement;
use fxhash::FxHashMap;
#[derive(Default)]
pub struct NthIndexCache {
nth: NthIndexCacheInner,
nth_last: NthIndexCacheInner,
nth_of_type: NthIndexCacheInner,
nth_last_of_type: NthIndexCacheInner,
}
impl NthIndexCache {
pub fn get(&mut self, is_of_type: bool, is_from_end: bool) -> &mut NthIndexCacheInner {
match (is_of_type, is_from_end) {
(false, false) => &mut self.nth,
(false, true) => &mut self.nth_last,
(true, false) => &mut self.nth_of_type,
(true, true) => &mut self.nth_last_of_type,
}
}
}
#[derive(Default)]
pub struct NthIndexCacheInner(FxHashMap<OpaqueElement, i32>);
impl NthIndexCacheInner {
pub fn lookup(&mut self, el: OpaqueElement) -> Option<i32> {
self.0.get(&el).copied()
}
pub fn insert(&mut self, element: OpaqueElement, index: i32) {
self.0.insert(element, index);
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}