diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-06-16 12:48:36 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-06-16 12:48:36 +0100 |
commit | 6b2175878da09feeb0eafc6287eb939e837e14dc (patch) | |
tree | c8a2de2f545cfa7337f298812d5e4f8fae6b01ae /crates/vfs/src/path_interner.rs | |
parent | 4342b717158eeb9aff20d1958229ffcbca17f1c7 (diff) | |
parent | c002322bde06a73c8cfa02cd1cbe33cf225da47b (diff) |
Merge #4891
4891: New VFS API r=matklad a=matklad
cc @flodiebold , @jonas-schievink
Co-authored-by: Aleksey Kladov <[email protected]>
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 | } | ||