use std::ops;
use super::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct StringId(usize);
impl StringId {
pub(crate) const MIN: StringId = StringId(usize::MIN);
pub(crate) const MAX: StringId = StringId(usize::MAX);
}
#[derive(Default)]
pub(crate) struct Strings(indexmap::IndexSet<Box<str>>);
impl Strings {
pub(crate) fn intern<T>(&mut self, string: T) -> StringId
where
T: AsRef<str> + Into<Box<str>>,
{
if let Some(idx) = self.0.get_index_of(string.as_ref()) {
return StringId(idx);
}
StringId(self.0.insert_full(string.into()).0)
}
pub(crate) fn resolve(&self, id: StringId) -> &str {
self.0.get_index(id.0).unwrap().as_ref()
}
pub(crate) fn lookup(&self, string: &str) -> Option<StringId> {
self.0.get_index_of(string).map(StringId)
}
}
impl ops::Index<StringId> for Subgraphs {
type Output = Box<str>;
fn index(&self, index: StringId) -> &Self::Output {
self.strings.0.get_index(index.0).unwrap()
}
}