2018-11-28 01:25:20 +01:00
|
|
|
use std::hash::Hash;
|
|
|
|
|
2019-01-08 13:53:32 +01:00
|
|
|
use parking_lot::Mutex;
|
2018-11-28 01:25:20 +01:00
|
|
|
use rustc_hash::FxHashMap;
|
2019-01-08 13:53:32 +01:00
|
|
|
use ra_arena::{Arena, ArenaId};
|
2018-11-28 01:25:20 +01:00
|
|
|
|
|
|
|
/// There are two principle ways to refer to things:
|
|
|
|
/// - by their locatinon (module in foo/bar/baz.rs at line 42)
|
|
|
|
/// - by their numeric id (module `ModuleId(42)`)
|
|
|
|
///
|
|
|
|
/// The first one is more powerful (you can actually find the thing in question
|
|
|
|
/// by id), but the second one is so much more compact.
|
|
|
|
///
|
|
|
|
/// `Loc2IdMap` allows us to have a cake an eat it as well: by maintaining a
|
|
|
|
/// bidirectional mapping between positional and numeric ids, we can use compact
|
|
|
|
/// representation wich still allows us to get the actual item
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Loc2IdMap<LOC, ID>
|
|
|
|
where
|
2019-01-08 13:53:32 +01:00
|
|
|
ID: ArenaId + Clone,
|
2018-11-28 01:25:20 +01:00
|
|
|
LOC: Clone + Eq + Hash,
|
|
|
|
{
|
2019-01-08 13:53:32 +01:00
|
|
|
id2loc: Arena<ID, LOC>,
|
2018-11-28 01:25:20 +01:00
|
|
|
loc2id: FxHashMap<LOC, ID>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<LOC, ID> Default for Loc2IdMap<LOC, ID>
|
|
|
|
where
|
2019-01-08 13:53:32 +01:00
|
|
|
ID: ArenaId + Clone,
|
2018-11-28 01:25:20 +01:00
|
|
|
LOC: Clone + Eq + Hash,
|
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
Loc2IdMap {
|
2019-01-08 13:53:32 +01:00
|
|
|
id2loc: Arena::default(),
|
2018-11-28 01:25:20 +01:00
|
|
|
loc2id: FxHashMap::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<LOC, ID> Loc2IdMap<LOC, ID>
|
|
|
|
where
|
2019-01-08 13:53:32 +01:00
|
|
|
ID: ArenaId + Clone,
|
2018-11-28 01:25:20 +01:00
|
|
|
LOC: Clone + Eq + Hash,
|
|
|
|
{
|
2018-12-21 09:48:52 +01:00
|
|
|
pub fn len(&self) -> usize {
|
2019-01-08 13:53:32 +01:00
|
|
|
self.id2loc.len()
|
2018-12-21 09:48:52 +01:00
|
|
|
}
|
|
|
|
|
2018-11-28 01:25:20 +01:00
|
|
|
pub fn loc2id(&mut self, loc: &LOC) -> ID {
|
|
|
|
match self.loc2id.get(loc) {
|
|
|
|
Some(id) => return id.clone(),
|
|
|
|
None => (),
|
|
|
|
}
|
2019-01-08 13:53:32 +01:00
|
|
|
let id = self.id2loc.alloc(loc.clone());
|
2018-11-28 01:25:20 +01:00
|
|
|
self.loc2id.insert(loc.clone(), id.clone());
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn id2loc(&self, id: ID) -> LOC {
|
2019-01-08 13:53:32 +01:00
|
|
|
self.id2loc[id].clone()
|
2018-11-28 01:25:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct LocationIntener<LOC, ID>
|
|
|
|
where
|
2019-01-08 13:53:32 +01:00
|
|
|
ID: ArenaId + Clone,
|
2018-11-28 01:25:20 +01:00
|
|
|
LOC: Clone + Eq + Hash,
|
|
|
|
{
|
|
|
|
map: Mutex<Loc2IdMap<LOC, ID>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<LOC, ID> Default for LocationIntener<LOC, ID>
|
|
|
|
where
|
2019-01-08 13:53:32 +01:00
|
|
|
ID: ArenaId + Clone,
|
2018-11-28 01:25:20 +01:00
|
|
|
LOC: Clone + Eq + Hash,
|
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
LocationIntener {
|
|
|
|
map: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<LOC, ID> LocationIntener<LOC, ID>
|
|
|
|
where
|
2019-01-08 13:53:32 +01:00
|
|
|
ID: ArenaId + Clone,
|
2018-11-28 01:25:20 +01:00
|
|
|
LOC: Clone + Eq + Hash,
|
|
|
|
{
|
2018-12-21 09:48:52 +01:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.map.lock().len()
|
|
|
|
}
|
2018-11-28 01:25:20 +01:00
|
|
|
pub fn loc2id(&self, loc: &LOC) -> ID {
|
|
|
|
self.map.lock().loc2id(loc)
|
|
|
|
}
|
|
|
|
pub fn id2loc(&self, id: ID) -> LOC {
|
|
|
|
self.map.lock().id2loc(id)
|
|
|
|
}
|
|
|
|
}
|