diff options
Diffstat (limited to 'crates/vfs/src/path_interner.rs')
-rw-r--r-- | crates/vfs/src/path_interner.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/crates/vfs/src/path_interner.rs b/crates/vfs/src/path_interner.rs new file mode 100644 index 000000000..4f70d61e8 --- /dev/null +++ b/crates/vfs/src/path_interner.rs | |||
@@ -0,0 +1,31 @@ | |||
1 | //! Maps paths to compact integer ids. We don't care about clearings paths which | ||
2 | //! no longer exist -- the assumption is total size of paths we ever look at is | ||
3 | //! not too big. | ||
4 | use rustc_hash::FxHashMap; | ||
5 | |||
6 | use crate::{FileId, VfsPath}; | ||
7 | |||
8 | #[derive(Default)] | ||
9 | pub(crate) struct PathInterner { | ||
10 | map: FxHashMap<VfsPath, FileId>, | ||
11 | vec: Vec<VfsPath>, | ||
12 | } | ||
13 | |||
14 | impl PathInterner { | ||
15 | pub(crate) fn get(&self, path: &VfsPath) -> Option<FileId> { | ||
16 | self.map.get(path).copied() | ||
17 | } | ||
18 | pub(crate) fn intern(&mut self, path: VfsPath) -> FileId { | ||
19 | if let Some(id) = self.get(&path) { | ||
20 | return id; | ||
21 | } | ||
22 | let id = FileId(self.vec.len() as u32); | ||
23 | self.map.insert(path.clone(), id); | ||
24 | self.vec.push(path); | ||
25 | id | ||
26 | } | ||
27 | |||
28 | pub(crate) fn lookup(&self, id: FileId) -> &VfsPath { | ||
29 | &self.vec[id.0 as usize] | ||
30 | } | ||
31 | } | ||