use std::fmt;
use indexmap::IndexSet;
#[derive(Clone, Default)]
pub struct VarRegistry {
names: IndexSet<String>,
}
impl VarRegistry {
#[inline]
pub fn new() -> Self {
Self { names: IndexSet::new() }
}
pub fn from_names<I, S>(names: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
let mut set = IndexSet::new();
for n in names {
set.insert(n.into());
}
Self { names: set }
}
pub fn insert(&mut self, name: impl Into<String>) -> usize {
let (idx, _is_new) = self.names.insert_full(name.into());
idx
}
#[inline]
pub fn index_of(&self, name: &str) -> Option<usize> {
self.names.get_index_of(name)
}
#[inline]
pub fn name(&self, idx: usize) -> Option<&str> {
self.names.get_index(idx).map(String::as_str)
}
#[inline]
pub fn len(&self) -> usize {
self.names.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.names.is_empty()
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item = &str> {
self.names.iter().map(String::as_str)
}
}
impl fmt::Debug for VarRegistry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VarRegistry")
.field("len", &self.names.len())
.field("names", &self.names.iter().collect::<Vec<_>>())
.finish()
}
}