From c002322bde06a73c8cfa02cd1cbe33cf225da47b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 15 Jun 2020 13:29:07 +0200 Subject: New VFS API --- crates/vfs/src/path_interner.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 crates/vfs/src/path_interner.rs (limited to 'crates/vfs/src/path_interner.rs') 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 @@ +//! Maps paths to compact integer ids. We don't care about clearings paths which +//! no longer exist -- the assumption is total size of paths we ever look at is +//! not too big. +use rustc_hash::FxHashMap; + +use crate::{FileId, VfsPath}; + +#[derive(Default)] +pub(crate) struct PathInterner { + map: FxHashMap, + vec: Vec, +} + +impl PathInterner { + pub(crate) fn get(&self, path: &VfsPath) -> Option { + self.map.get(path).copied() + } + pub(crate) fn intern(&mut self, path: VfsPath) -> FileId { + if let Some(id) = self.get(&path) { + return id; + } + let id = FileId(self.vec.len() as u32); + self.map.insert(path.clone(), id); + self.vec.push(path); + id + } + + pub(crate) fn lookup(&self, id: FileId) -> &VfsPath { + &self.vec[id.0 as usize] + } +} -- cgit v1.2.3