aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db/src/loc2id.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_db/src/loc2id.rs')
-rw-r--r--crates/ra_db/src/loc2id.rs103
1 files changed, 0 insertions, 103 deletions
diff --git a/crates/ra_db/src/loc2id.rs b/crates/ra_db/src/loc2id.rs
deleted file mode 100644
index eae64a4eb..000000000
--- a/crates/ra_db/src/loc2id.rs
+++ /dev/null
@@ -1,103 +0,0 @@
1use std::{panic, hash::Hash};
2
3use parking_lot::Mutex;
4use rustc_hash::FxHashMap;
5use ra_arena::{Arena, ArenaId};
6
7/// There are two principle ways to refer to things:
8/// - by their location (module in foo/bar/baz.rs at line 42)
9/// - by their numeric id (module `ModuleId(42)`)
10///
11/// The first one is more powerful (you can actually find the thing in question
12/// by id), but the second one is so much more compact.
13///
14/// `Loc2IdMap` allows us to have a cake an eat it as well: by maintaining a
15/// bidirectional mapping between positional and numeric ids, we can use compact
16/// representation which still allows us to get the actual item.
17#[derive(Debug)]
18struct Loc2IdMap<LOC, ID>
19where
20 ID: ArenaId + Clone,
21 LOC: Clone + Eq + Hash,
22{
23 id2loc: Arena<ID, LOC>,
24 loc2id: FxHashMap<LOC, ID>,
25}
26
27impl<LOC, ID> Default for Loc2IdMap<LOC, ID>
28where
29 ID: ArenaId + Clone,
30 LOC: Clone + Eq + Hash,
31{
32 fn default() -> Self {
33 Loc2IdMap { id2loc: Arena::default(), loc2id: FxHashMap::default() }
34 }
35}
36
37impl<LOC, ID> Loc2IdMap<LOC, ID>
38where
39 ID: ArenaId + Clone,
40 LOC: Clone + Eq + Hash,
41{
42 pub fn len(&self) -> usize {
43 self.id2loc.len()
44 }
45
46 pub fn loc2id(&mut self, loc: &LOC) -> ID {
47 match self.loc2id.get(loc) {
48 Some(id) => return id.clone(),
49 None => (),
50 }
51 let id = self.id2loc.alloc(loc.clone());
52 self.loc2id.insert(loc.clone(), id.clone());
53 id
54 }
55
56 pub fn id2loc(&self, id: ID) -> LOC {
57 self.id2loc[id].clone()
58 }
59}
60
61#[derive(Debug)]
62pub struct LocationInterner<LOC, ID>
63where
64 ID: ArenaId + Clone,
65 LOC: Clone + Eq + Hash,
66{
67 map: Mutex<Loc2IdMap<LOC, ID>>,
68}
69
70impl<LOC, ID> panic::RefUnwindSafe for LocationInterner<LOC, ID>
71where
72 ID: ArenaId + Clone,
73 LOC: Clone + Eq + Hash,
74 ID: panic::RefUnwindSafe,
75 LOC: panic::RefUnwindSafe,
76{
77}
78
79impl<LOC, ID> Default for LocationInterner<LOC, ID>
80where
81 ID: ArenaId + Clone,
82 LOC: Clone + Eq + Hash,
83{
84 fn default() -> Self {
85 LocationInterner { map: Default::default() }
86 }
87}
88
89impl<LOC, ID> LocationInterner<LOC, ID>
90where
91 ID: ArenaId + Clone,
92 LOC: Clone + Eq + Hash,
93{
94 pub fn len(&self) -> usize {
95 self.map.lock().len()
96 }
97 pub fn loc2id(&self, loc: &LOC) -> ID {
98 self.map.lock().loc2id(loc)
99 }
100 pub fn id2loc(&self, id: ID) -> LOC {
101 self.map.lock().id2loc(id)
102 }
103}