lewp_selectors/
nth_index_cache.rs1use crate::tree::OpaqueElement;
6use fxhash::FxHashMap;
7
8#[derive(Default)]
14pub struct NthIndexCache {
15 nth: NthIndexCacheInner,
16 nth_last: NthIndexCacheInner,
17 nth_of_type: NthIndexCacheInner,
18 nth_last_of_type: NthIndexCacheInner,
19}
20
21impl NthIndexCache {
22 pub fn get(&mut self, is_of_type: bool, is_from_end: bool) -> &mut NthIndexCacheInner {
24 match (is_of_type, is_from_end) {
25 (false, false) => &mut self.nth,
26 (false, true) => &mut self.nth_last,
27 (true, false) => &mut self.nth_of_type,
28 (true, true) => &mut self.nth_last_of_type,
29 }
30 }
31}
32
33#[derive(Default)]
35pub struct NthIndexCacheInner(FxHashMap<OpaqueElement, i32>);
36
37impl NthIndexCacheInner {
38 pub fn lookup(&mut self, el: OpaqueElement) -> Option<i32> {
40 self.0.get(&el).copied()
41 }
42
43 pub fn insert(&mut self, element: OpaqueElement, index: i32) {
45 self.0.insert(element, index);
46 }
47
48 pub fn is_empty(&self) -> bool {
50 self.0.is_empty()
51 }
52}