From 11168c464cd962af3336a2cc68295496066edd6c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 03:25:20 +0300 Subject: move db basics to ra_db This should allow to move hir to a separate crate --- crates/ra_db/src/loc2id.rs | 100 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 crates/ra_db/src/loc2id.rs (limited to 'crates/ra_db/src/loc2id.rs') diff --git a/crates/ra_db/src/loc2id.rs b/crates/ra_db/src/loc2id.rs new file mode 100644 index 000000000..69ba43d0f --- /dev/null +++ b/crates/ra_db/src/loc2id.rs @@ -0,0 +1,100 @@ +use parking_lot::Mutex; + +use std::hash::Hash; + +use rustc_hash::FxHashMap; + +/// 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 +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + loc2id: FxHashMap, + id2loc: FxHashMap, +} + +impl Default for Loc2IdMap +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + fn default() -> Self { + Loc2IdMap { + loc2id: FxHashMap::default(), + id2loc: FxHashMap::default(), + } + } +} + +impl Loc2IdMap +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + pub fn loc2id(&mut self, loc: &LOC) -> ID { + match self.loc2id.get(loc) { + Some(id) => return id.clone(), + None => (), + } + let id = self.loc2id.len(); + assert!(id < u32::max_value() as usize); + let id = ID::from_u32(id as u32); + self.loc2id.insert(loc.clone(), id.clone()); + self.id2loc.insert(id.clone(), loc.clone()); + id + } + + pub fn id2loc(&self, id: ID) -> LOC { + self.id2loc[&id].clone() + } +} + +pub trait NumericId: Clone + Eq + Hash { + fn from_u32(id: u32) -> Self; + fn to_u32(self) -> u32; +} + +#[derive(Debug)] +pub struct LocationIntener +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + map: Mutex>, +} + +impl Default for LocationIntener +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + fn default() -> Self { + LocationIntener { + map: Default::default(), + } + } +} + +impl LocationIntener +where + ID: NumericId, + LOC: Clone + Eq + Hash, +{ + pub fn loc2id(&self, loc: &LOC) -> ID { + self.map.lock().loc2id(loc) + } + pub fn id2loc(&self, id: ID) -> LOC { + self.map.lock().id2loc(id) + } +} -- cgit v1.2.3