swc_ecma_transforms_base/rename/analyzer/
reverse_map.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use rustc_hash::FxHashMap;
use swc_atoms::Atom;
use swc_ecma_ast::Id;

#[derive(Debug, Default)]
pub(crate) struct ReverseMap<'a> {
    prev: Option<&'a ReverseMap<'a>>,

    inner: FxHashMap<Atom, Vec<Id>>,
}

impl ReverseMap<'_> {
    pub fn push_entry(&mut self, key: Atom, id: Id) {
        self.inner.entry(key).or_default().push(id);
    }

    fn iter(&self) -> Iter {
        Iter { cur: Some(self) }
    }

    pub fn get<'a>(&'a self, key: &'a Atom) -> impl Iterator<Item = &'a Id> + 'a {
        self.iter()
            .filter_map(|v| v.inner.get(key))
            .flat_map(|v| v.iter())
    }

    pub fn next(&self) -> ReverseMap {
        ReverseMap {
            prev: Some(self),
            ..Default::default()
        }
    }
}

pub(crate) struct Iter<'a> {
    cur: Option<&'a ReverseMap<'a>>,
}

impl<'a> Iterator for Iter<'a> {
    type Item = &'a ReverseMap<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let cur = self.cur.take()?;
        self.cur = cur.prev;
        Some(cur)
    }
}