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 std::path::{PathBuf, Path};
use im;
use libanalysis::{FileId};
#[derive(Debug, Default, Clone)]
pub struct PathMap {
next_id: u32,
path2id: im::HashMap<PathBuf, FileId>,
id2path: im::HashMap<FileId, PathBuf>,
}
impl PathMap {
pub fn new() -> PathMap {
Default::default()
}
pub fn get_or_insert(&mut self, path: PathBuf) -> FileId {
self.path2id.get(path.as_path())
.map(|&id| id)
.unwrap_or_else(|| {
let id = self.new_file_id();
self.insert(path, id);
id
})
}
pub fn get_id(&self, path: &Path) -> Option<FileId> {
self.path2id.get(path).map(|&id| id)
}
pub fn get_path(&self, id: FileId) -> &Path {
self.id2path.get(&id)
.unwrap()
.as_path()
}
fn insert(&mut self, path: PathBuf, id: FileId) {
self.path2id.insert(path.clone(), id);
self.id2path.insert(id, path.clone());
}
fn new_file_id(&mut self) -> FileId {
let id = FileId(self.next_id);
self.next_id += 1;
id
}
}
|