2020-06-15 06:29:07 -05:00
|
|
|
//! 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.
|
2021-04-01 06:01:59 -05:00
|
|
|
use std::hash::BuildHasherDefault;
|
|
|
|
|
|
|
|
use indexmap::IndexSet;
|
|
|
|
use rustc_hash::FxHasher;
|
2020-06-15 06:29:07 -05:00
|
|
|
|
|
|
|
use crate::{FileId, VfsPath};
|
|
|
|
|
2021-01-12 10:41:45 -06:00
|
|
|
/// Structure to map between [`VfsPath`] and [`FileId`].
|
2020-06-15 06:29:07 -05:00
|
|
|
pub(crate) struct PathInterner {
|
2021-04-01 06:01:59 -05:00
|
|
|
map: IndexSet<VfsPath, BuildHasherDefault<FxHasher>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PathInterner {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { map: IndexSet::default() }
|
|
|
|
}
|
2020-06-15 06:29:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PathInterner {
|
2021-01-12 10:41:45 -06:00
|
|
|
/// Get the id corresponding to `path`.
|
|
|
|
///
|
|
|
|
/// If `path` does not exists in `self`, returns [`None`].
|
2020-06-15 06:29:07 -05:00
|
|
|
pub(crate) fn get(&self, path: &VfsPath) -> Option<FileId> {
|
2021-04-01 06:01:59 -05:00
|
|
|
self.map.get_index_of(path).map(|i| FileId(i as u32))
|
2020-06-15 06:29:07 -05:00
|
|
|
}
|
2021-01-12 10:41:45 -06:00
|
|
|
|
|
|
|
/// Insert `path` in `self`.
|
|
|
|
///
|
|
|
|
/// - If `path` already exists in `self`, returns its associated id;
|
|
|
|
/// - Else, returns a newly allocated id.
|
2020-06-15 06:29:07 -05:00
|
|
|
pub(crate) fn intern(&mut self, path: VfsPath) -> FileId {
|
2021-04-01 06:01:59 -05:00
|
|
|
let (id, _added) = self.map.insert_full(path);
|
|
|
|
assert!(id < u32::MAX as usize);
|
|
|
|
FileId(id as u32)
|
2020-06-15 06:29:07 -05:00
|
|
|
}
|
|
|
|
|
2021-01-12 10:41:45 -06:00
|
|
|
/// Returns the path corresponding to `id`.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if `id` does not exists in `self`.
|
2020-06-15 06:29:07 -05:00
|
|
|
pub(crate) fn lookup(&self, id: FileId) -> &VfsPath {
|
2021-04-01 06:01:59 -05:00
|
|
|
self.map.get_index(id.0 as usize).unwrap()
|
2020-06-15 06:29:07 -05:00
|
|
|
}
|
|
|
|
}
|