string_interner/interner.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
use crate::{backend::Backend, Symbol};
use core::{
fmt,
fmt::{Debug, Formatter},
hash::{BuildHasher, Hash, Hasher},
iter::FromIterator,
};
use hashbrown::{DefaultHashBuilder, HashMap};
/// Creates the `u64` hash value for the given value using the given hash builder.
fn make_hash<T>(builder: &impl BuildHasher, value: &T) -> u64
where
T: ?Sized + Hash,
{
let state = &mut builder.build_hasher();
value.hash(state);
state.finish()
}
/// Data structure to intern and resolve strings.
///
/// Caches strings efficiently, with minimal memory footprint and associates them with unique symbols.
/// These symbols allow constant time comparisons and look-ups to the underlying interned strings.
///
/// The following API covers the main functionality:
///
/// - [`StringInterner::get_or_intern`]: To intern a new string.
/// - This maps from `string` type to `symbol` type.
/// - [`StringInterner::resolve`]: To resolve your already interned strings.
/// - This maps from `symbol` type to `string` type.
pub struct StringInterner<B, H = DefaultHashBuilder>
where
B: Backend,
{
dedup: HashMap<<B as Backend>::Symbol, (), ()>,
hasher: H,
backend: B,
}
impl<B, H> Debug for StringInterner<B, H>
where
B: Backend + Debug,
<B as Backend>::Symbol: Symbol + Debug,
H: BuildHasher,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("StringInterner")
.field("dedup", &self.dedup)
.field("backend", &self.backend)
.finish()
}
}
#[cfg(feature = "backends")]
impl Default for StringInterner<crate::DefaultBackend> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
StringInterner::new()
}
}
impl<B, H> Clone for StringInterner<B, H>
where
B: Backend + Clone,
<B as Backend>::Symbol: Symbol,
H: BuildHasher + Clone,
{
fn clone(&self) -> Self {
Self {
dedup: self.dedup.clone(),
hasher: self.hasher.clone(),
backend: self.backend.clone(),
}
}
}
impl<B, H> PartialEq for StringInterner<B, H>
where
B: Backend + PartialEq,
<B as Backend>::Symbol: Symbol,
H: BuildHasher,
{
fn eq(&self, rhs: &Self) -> bool {
self.len() == rhs.len() && self.backend == rhs.backend
}
}
impl<B, H> Eq for StringInterner<B, H>
where
B: Backend + Eq,
<B as Backend>::Symbol: Symbol,
H: BuildHasher,
{
}
impl<B, H> StringInterner<B, H>
where
B: Backend,
<B as Backend>::Symbol: Symbol,
H: BuildHasher + Default,
{
/// Creates a new empty `StringInterner`.
#[cfg_attr(feature = "inline-more", inline)]
pub fn new() -> Self {
Self {
dedup: HashMap::default(),
hasher: Default::default(),
backend: B::default(),
}
}
/// Creates a new `StringInterner` with the given initial capacity.
#[cfg_attr(feature = "inline-more", inline)]
pub fn with_capacity(cap: usize) -> Self {
Self {
dedup: HashMap::with_capacity_and_hasher(cap, ()),
hasher: Default::default(),
backend: B::with_capacity(cap),
}
}
}
impl<B, H> StringInterner<B, H>
where
B: Backend,
<B as Backend>::Symbol: Symbol,
H: BuildHasher,
{
/// Creates a new empty `StringInterner` with the given hasher.
#[cfg_attr(feature = "inline-more", inline)]
pub fn with_hasher(hash_builder: H) -> Self {
StringInterner {
dedup: HashMap::default(),
hasher: hash_builder,
backend: B::default(),
}
}
/// Creates a new empty `StringInterner` with the given initial capacity and the given hasher.
#[cfg_attr(feature = "inline-more", inline)]
pub fn with_capacity_and_hasher(cap: usize, hash_builder: H) -> Self {
StringInterner {
dedup: HashMap::with_capacity_and_hasher(cap, ()),
hasher: hash_builder,
backend: B::with_capacity(cap),
}
}
/// Returns the number of strings interned by the interner.
#[cfg_attr(feature = "inline-more", inline)]
pub fn len(&self) -> usize {
self.dedup.len()
}
/// Returns `true` if the string interner has no interned strings.
#[cfg_attr(feature = "inline-more", inline)]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns the symbol for the given string if any.
///
/// Can be used to query if a string has already been interned without interning.
#[inline]
pub fn get<T>(&self, string: T) -> Option<<B as Backend>::Symbol>
where
T: AsRef<str>,
{
let string = string.as_ref();
let Self {
dedup,
hasher,
backend,
} = self;
let hash = make_hash(hasher, string);
dedup
.raw_entry()
.from_hash(hash, |symbol| {
// SAFETY: This is safe because we only operate on symbols that
// we receive from our backend making them valid.
string == unsafe { backend.resolve_unchecked(*symbol) }
})
.map(|(&symbol, &())| symbol)
}
/// Interns the given string.
///
/// This is used as backend by [`get_or_intern`][1] and [`get_or_intern_static`][2].
///
/// [1]: [`StringInterner::get_or_intern`]
/// [2]: [`StringInterner::get_or_intern_static`]
#[cfg_attr(feature = "inline-more", inline)]
fn get_or_intern_using<T>(
&mut self,
string: T,
intern_fn: fn(&mut B, T) -> <B as Backend>::Symbol,
) -> <B as Backend>::Symbol
where
T: Copy + Hash + AsRef<str> + for<'a> PartialEq<&'a str>,
{
let Self {
dedup,
hasher,
backend,
} = self;
let hash = make_hash(hasher, string.as_ref());
let entry = dedup.raw_entry_mut().from_hash(hash, |symbol| {
// SAFETY: This is safe because we only operate on symbols that
// we receive from our backend making them valid.
string == unsafe { backend.resolve_unchecked(*symbol) }
});
use hashbrown::hash_map::RawEntryMut;
let (&mut symbol, &mut ()) = match entry {
RawEntryMut::Occupied(occupied) => occupied.into_key_value(),
RawEntryMut::Vacant(vacant) => {
let symbol = intern_fn(backend, string);
vacant.insert_with_hasher(hash, symbol, (), |symbol| {
// SAFETY: This is safe because we only operate on symbols that
// we receive from our backend making them valid.
let string = unsafe { backend.resolve_unchecked(*symbol) };
make_hash(hasher, string)
})
}
};
symbol
}
/// Interns the given string.
///
/// Returns a symbol for resolution into the original string.
///
/// # Panics
///
/// If the interner already interns the maximum number of strings possible
/// by the chosen symbol type.
#[inline]
pub fn get_or_intern<T>(&mut self, string: T) -> <B as Backend>::Symbol
where
T: AsRef<str>,
{
self.get_or_intern_using(string.as_ref(), B::intern)
}
/// Interns the given `'static` string.
///
/// Returns a symbol for resolution into the original string.
///
/// # Note
///
/// This is more efficient than [`StringInterner::get_or_intern`] since it might
/// avoid some memory allocations if the backends supports this.
///
/// # Panics
///
/// If the interner already interns the maximum number of strings possible
/// by the chosen symbol type.
#[inline]
pub fn get_or_intern_static(&mut self, string: &'static str) -> <B as Backend>::Symbol {
self.get_or_intern_using(string, B::intern_static)
}
/// Shrink backend capacity to fit the interned strings exactly.
pub fn shrink_to_fit(&mut self) {
self.backend.shrink_to_fit()
}
/// Returns the string for the given `symbol`` if any.
#[inline]
pub fn resolve(&self, symbol: <B as Backend>::Symbol) -> Option<&str> {
self.backend.resolve(symbol)
}
/// Returns the string for the given `symbol` without performing any checks.
///
/// # Safety
///
/// It is the caller's responsibility to provide this method with `symbol`s
/// that are valid for the [`StringInterner`].
#[inline]
pub unsafe fn resolve_unchecked(&self, symbol: <B as Backend>::Symbol) -> &str {
unsafe { self.backend.resolve_unchecked(symbol) }
}
/// Returns an iterator that yields all interned strings and their symbols.
#[inline]
pub fn iter(&self) -> <B as Backend>::Iter<'_> {
self.backend.iter()
}
}
impl<B, H, T> FromIterator<T> for StringInterner<B, H>
where
B: Backend,
<B as Backend>::Symbol: Symbol,
H: BuildHasher + Default,
T: AsRef<str>,
{
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
let iter = iter.into_iter();
let (capacity, _) = iter.size_hint();
let mut interner = Self::with_capacity(capacity);
interner.extend(iter);
interner
}
}
impl<B, H, T> Extend<T> for StringInterner<B, H>
where
B: Backend,
<B as Backend>::Symbol: Symbol,
H: BuildHasher,
T: AsRef<str>,
{
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = T>,
{
for s in iter {
self.get_or_intern(s.as_ref());
}
}
}
impl<'a, B, H> IntoIterator for &'a StringInterner<B, H>
where
B: Backend,
<B as Backend>::Symbol: Symbol,
&'a B: IntoIterator<Item = (<B as Backend>::Symbol, &'a str)>,
H: BuildHasher,
{
type Item = (<B as Backend>::Symbol, &'a str);
type IntoIter = <&'a B as IntoIterator>::IntoIter;
#[cfg_attr(feature = "inline-more", inline)]
fn into_iter(self) -> Self::IntoIter {
self.backend.into_iter()
}
}