2022-07-20 11:36:10 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use tt::SmolStr;
|
|
|
|
|
|
|
|
// Identifier for an interned symbol.
|
|
|
|
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
|
|
|
|
pub struct Symbol(u32);
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct IdentInterner {
|
|
|
|
idents: HashMap<SmolStr, u32>,
|
|
|
|
ident_data: Vec<SmolStr>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IdentInterner {
|
|
|
|
fn intern(&mut self, data: &str) -> Symbol {
|
|
|
|
if let Some(index) = self.idents.get(data) {
|
2022-07-20 11:43:59 -05:00
|
|
|
return Symbol(*index);
|
2022-07-20 11:36:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let index = self.idents.len() as u32;
|
2022-07-20 11:43:59 -05:00
|
|
|
let data = SmolStr::from(data);
|
2022-07-20 11:36:10 -05:00
|
|
|
self.ident_data.push(data.clone());
|
2022-07-20 11:43:59 -05:00
|
|
|
self.idents.insert(data, index);
|
|
|
|
Symbol(index)
|
2022-07-20 11:36:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get(&self, index: u32) -> &SmolStr {
|
|
|
|
&self.ident_data[index as usize]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
fn get_mut(&mut self, index: u32) -> &mut SmolStr {
|
|
|
|
self.ident_data.get_mut(index as usize).expect("Should be consistent")
|
|
|
|
}
|
|
|
|
}
|