Preparation for sharing

- rustfmt
- clippy
- comments
- README
This commit is contained in:
2023-05-25 19:14:24 +01:00
parent e99ade92ba
commit bc2714aad8
144 changed files with 3734 additions and 3243 deletions

View File

@@ -1,18 +1,20 @@
use hashbrown::{HashMap, HashSet};
use std::hash::Hash;
use crate::interner::Token;
use hashbrown::{HashMap, HashSet};
use crate::interner::Sym;
#[derive(Clone, Debug, Default)]
pub struct AliasMap{
pub targets: HashMap<Token<Vec<Token<String>>>, Token<Vec<Token<String>>>>,
pub aliases: HashMap<Token<Vec<Token<String>>>, HashSet<Token<Vec<Token<String>>>>>,
pub struct AliasMap {
pub targets: HashMap<Sym, Sym>,
pub aliases: HashMap<Sym, HashSet<Sym>>,
}
impl AliasMap {
pub fn new() -> Self {Self::default()}
pub fn new() -> Self {
Self::default()
}
pub fn link(&mut self, alias: Token<Vec<Token<String>>>, target: Token<Vec<Token<String>>>) {
pub fn link(&mut self, alias: Sym, target: Sym) {
let prev = self.targets.insert(alias, target);
debug_assert!(prev.is_none(), "Alias already has a target");
multimap_entry(&mut self.aliases, &target).insert(alias);
@@ -21,9 +23,7 @@ impl AliasMap {
for alt in alts {
// Assert that this step has always been done in the past
debug_assert!(
self.aliases.get(&alt)
.map(HashSet::is_empty)
.unwrap_or(true),
self.aliases.get(&alt).map(HashSet::is_empty).unwrap_or(true),
"Alias set of alias not empty"
);
debug_assert!(
@@ -35,7 +35,7 @@ impl AliasMap {
}
}
pub fn resolve(&self, alias: Token<Vec<Token<String>>>) -> Option<Token<Vec<Token<String>>>> {
pub fn resolve(&self, alias: Sym) -> Option<Sym> {
self.targets.get(&alias).copied()
}
}
@@ -44,10 +44,11 @@ impl AliasMap {
/// map-to-set (aka. multimap)
fn multimap_entry<'a, K: Eq + Hash + Clone, V>(
map: &'a mut HashMap<K, HashSet<V>>,
key: &'_ K
key: &'_ K,
) -> &'a mut HashSet<V> {
map.raw_entry_mut()
map
.raw_entry_mut()
.from_key(key)
.or_insert_with(|| (key.clone(), HashSet::new()))
.1
}
}