use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
pub type StringId = u32;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SymbolTable {
symbols: Vec<String>,
index: BTreeMap<String, StringId>,
}
impl SymbolTable {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn intern(&mut self, value: &str) -> StringId {
if let Some(id) = self.index.get(value) {
return *id;
}
let id = match u32::try_from(self.symbols.len()) {
Ok(id) => id,
Err(_) => return u32::MAX,
};
let owned = value.to_string();
self.symbols.push(owned.clone());
self.index.insert(owned, id);
id
}
#[must_use]
#[allow(clippy::as_conversions)]
pub fn resolve(&self, id: StringId) -> Option<&str> {
self.symbols.get(id as usize).map(String::as_str)
}
#[must_use]
pub fn len(&self) -> usize {
self.symbols.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.symbols.is_empty()
}
}