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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
#![cfg_attr(all(feature = "bench", test), feature(test))] #![doc(html_root_url = "https://docs.rs/crate/arc-string-interner/0.1.0")] #![deny(missing_docs)] //! 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. //! //! ### Example: Interning & Symbols //! //! ``` //! use arc_string_interner::StringInterner; //! //! let mut interner = StringInterner::default(); //! let sym0 = interner.get_or_intern("Elephant"); //! let sym1 = interner.get_or_intern("Tiger"); //! let sym2 = interner.get_or_intern("Horse"); //! let sym3 = interner.get_or_intern("Tiger"); //! assert_ne!(sym0, sym1); //! assert_ne!(sym0, sym2); //! assert_ne!(sym1, sym2); //! assert_eq!(sym1, sym3); // same! //! ``` //! //! ### Example: Creation by `FromIterator` //! //! ``` //! # use arc_string_interner::DefaultStringInterner; //! let interner = vec!["Elephant", "Tiger", "Horse", "Tiger"] //! .into_iter() //! .collect::<DefaultStringInterner>(); //! ``` //! //! ### Example: Look-up //! //! ``` //! # use arc_string_interner::StringInterner; //! let mut interner = StringInterner::default(); //! let sym = interner.get_or_intern("Banana"); //! assert_eq!(interner.resolve(sym).map(ToString::to_string), Some("Banana".to_owned())); //! ``` //! //! ### Example: Iteration //! //! ``` //! # use arc_string_interner::DefaultStringInterner; //! let interner = vec!["Earth", "Water", "Fire", "Air"] //! .into_iter() //! .collect::<DefaultStringInterner>(); //! for (sym, str) in interner { //! // iteration code here! //! } //! ``` #[cfg(all(feature = "bench", test))] extern crate test; #[cfg(test)] mod tests; #[cfg(all(feature = "bench", test))] mod benches; #[cfg(feature = "serde_support")] mod serde_impl; use std::iter::FromIterator; use std::{ collections::{hash_map::RandomState, HashMap}, hash::{BuildHasher, Hash, Hasher}, iter, marker, num::NonZeroU32, slice, sync::Arc, u32, vec, }; /// Types implementing this trait are able to act as symbols for string interners. /// /// Symbols are returned by `StringInterner::get_or_intern` and allow look-ups of the /// original string contents with `StringInterner::resolve`. /// /// # Note /// /// Optimal symbols allow for efficient comparisons and have a small memory footprint. pub trait Symbol: Copy + Ord + Eq { /// Creates a symbol from a `usize`. /// /// # Note /// /// Implementations panic if the operation cannot succeed. fn from_usize(val: usize) -> Self; /// Returns the `usize` representation of `self`. fn to_usize(self) -> usize; } /// Symbol type used by the `DefaultStringInterner`. /// /// # Note /// /// This special symbol type has a memory footprint of 32 bits /// and allows for certain space optimizations such as using it within an option: `Option<Sym>` #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Sym(NonZeroU32); impl Symbol for Sym { /// Creates a `Sym` from the given `usize`. /// /// # Panics /// /// If the given `usize` is greater than `u32::MAX - 1`. fn from_usize(val: usize) -> Self { assert!( val < u32::MAX as usize, "Symbol value {} is too large and not supported by `string_interner::Sym` type", val ); Sym(NonZeroU32::new((val + 1) as u32).unwrap_or_else(|| { unreachable!("Should never fail because `val + 1` is nonzero and `<= u32::MAX`") })) } fn to_usize(self) -> usize { (self.0.get() as usize) - 1 } } impl Symbol for usize { fn from_usize(val: usize) -> Self { val } fn to_usize(self) -> usize { self } } /// Internal reference to `str` used only within the `StringInterner` itself /// to encapsulate the unsafe behaviour of interior references. #[derive(Debug, Copy, Clone, Eq)] struct InternalStrRef(*const str); impl InternalStrRef { /// Creates an InternalStrRef from a str. /// /// This just wraps the str internally. fn from_str(val: &str) -> Self { InternalStrRef(val as *const str) } /// Reinterprets this InternalStrRef as a str. /// /// This is "safe" as long as this InternalStrRef only /// refers to strs that outlive this instance or /// the instance that owns this InternalStrRef. /// This should hold true for `StringInterner`. /// /// Does not allocate memory! fn as_str(&self) -> &str { unsafe { &*self.0 } } } impl<T> From<T> for InternalStrRef where T: AsRef<str>, { fn from(val: T) -> Self { InternalStrRef::from_str(val.as_ref()) } } impl Hash for InternalStrRef { fn hash<H: Hasher>(&self, state: &mut H) { self.as_str().hash(state) } } impl PartialEq for InternalStrRef { fn eq(&self, other: &InternalStrRef) -> bool { self.as_str() == other.as_str() } } /// `StringInterner` that uses `Sym` as its underlying symbol type. pub type DefaultStringInterner = StringInterner<Sym>; /// 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. #[derive(Debug, Eq)] pub struct StringInterner<S, H = RandomState> where S: Symbol, H: BuildHasher, { map: HashMap<InternalStrRef, S, H>, values: Vec<Arc<str>>, } impl<S, H> PartialEq for StringInterner<S, H> where S: Symbol, H: BuildHasher, { fn eq(&self, rhs: &Self) -> bool { self.len() == rhs.len() && self.values == rhs.values } } impl Default for StringInterner<Sym, RandomState> { #[inline] fn default() -> Self { StringInterner::new() } } // Should be manually cloned. // See <https://github.com/Robbepop/string-interner/issues/9>. impl<S, H> Clone for StringInterner<S, H> where S: Symbol, H: Clone + BuildHasher, { fn clone(&self) -> Self { let values = self.values.clone(); let mut map = HashMap::with_capacity_and_hasher(values.len(), self.map.hasher().clone()); // Recreate `InternalStrRef` from the newly cloned `Box<str>`s. // Use `extend()` to avoid `H: Default` trait bound required by `FromIterator for HashMap`. map.extend( values .iter() .enumerate() .map(|(i, s)| (InternalStrRef::from_str(s), S::from_usize(i))), ); Self { values, map } } } // About `Send` and `Sync` impls for `StringInterner` // -------------------------------------------------- // // tl;dr: Automation of Send+Sync impl was prevented by `InternalStrRef` // being an unsafe abstraction and thus prevented Send+Sync default derivation. // // These implementations are safe due to the following reasons: // - `InternalStrRef` cannot be used outside `StringInterner`. // - Strings stored in `StringInterner` are not mutable. // - Iterator invalidation while growing the underlying `Vec<Box<str>>` is prevented by // using an additional indirection to store strings. unsafe impl<S, H> Send for StringInterner<S, H> where S: Symbol + Send, H: BuildHasher, { } unsafe impl<S, H> Sync for StringInterner<S, H> where S: Symbol + Sync, H: BuildHasher, { } impl<S> StringInterner<S> where S: Symbol, { /// Creates a new empty `StringInterner`. #[inline] pub fn new() -> StringInterner<S, RandomState> { StringInterner { map: HashMap::new(), values: Vec::new(), } } /// Creates a new `StringInterner` with the given initial capacity. #[inline] pub fn with_capacity(cap: usize) -> Self { StringInterner { map: HashMap::with_capacity(cap), values: Vec::with_capacity(cap), } } /// Returns the number of elements the `StringInterner` can hold without reallocating. #[inline] pub fn capacity(&self) -> usize { std::cmp::min(self.map.capacity(), self.values.capacity()) } /// Reserves capacity for at least `additional` more elements to be interned into `self`. /// /// The collection may reserve more space to avoid frequent allocations. /// After calling `reserve`, capacity will be greater than or equal to `self.len() + additional`. /// Does nothing if capacity is already sufficient. #[inline] pub fn reserve(&mut self, additional: usize) { self.map.reserve(additional); self.values.reserve(additional); } } impl<S, H> StringInterner<S, H> where S: Symbol, H: BuildHasher, { /// Creates a new empty `StringInterner` with the given hasher. #[inline] pub fn with_hasher(hash_builder: H) -> StringInterner<S, H> { StringInterner { map: HashMap::with_hasher(hash_builder), values: Vec::new(), } } /// Creates a new empty `StringInterner` with the given initial capacity and the given hasher. #[inline] pub fn with_capacity_and_hasher(cap: usize, hash_builder: H) -> StringInterner<S, H> { StringInterner { map: HashMap::with_hasher(hash_builder), values: Vec::with_capacity(cap), } } /// Interns the given value. /// /// Returns a symbol to access it within this interner. /// /// This either copies the contents of the string (e.g. for str) /// or moves them into this interner (e.g. for String). #[inline] pub fn get_or_intern<T>(&mut self, val: T) -> S where T: Into<String> + AsRef<str>, { match self.map.get(&val.as_ref().into()) { Some(&sym) => sym, None => self.intern(val), } } /// Interns the given value and ignores collissions. /// /// Returns a symbol to access it within this interner. fn intern<T>(&mut self, new_val: T) -> S where T: Into<String> + AsRef<str>, { let new_id: S = self.make_symbol(); let new_boxed_val: Arc<str> = new_val.into().into(); let new_ref: InternalStrRef = new_boxed_val.as_ref().into(); self.values.push(new_boxed_val); self.map.insert(new_ref, new_id); new_id } /// Creates a new symbol for the current state of the interner. fn make_symbol(&self) -> S { S::from_usize(self.len()) } /// Returns the string slice associated with the given symbol if available, /// otherwise returns `None`. #[inline] pub fn resolve(&self, symbol: S) -> Option<&Arc<str>> { self.values.get(symbol.to_usize()) } /// Returns the string associated with the given symbol. /// /// # Note /// /// This does not check whether the given symbol has an associated string /// for the given string interner instance. /// /// # Safety /// /// This will result in undefined behaviour if the given symbol /// had no associated string for this interner instance. #[inline] pub unsafe fn resolve_unchecked(&self, symbol: S) -> &Arc<str> { self.values.get_unchecked(symbol.to_usize()) } /// Returns the symbol associated with the given string for this interner /// if existent, otherwise returns `None`. #[inline] pub fn get<T>(&self, val: T) -> Option<S> where T: AsRef<str>, { self.map.get(&val.as_ref().into()).cloned() } /// Returns the number of uniquely interned strings within this interner. #[inline] pub fn len(&self) -> usize { self.values.len() } /// Returns true if the string interner holds no elements. #[inline] pub fn is_empty(&self) -> bool { self.len() == 0 } /// Returns an iterator over the interned strings. #[inline] pub fn iter(&self) -> Iter<S> { Iter::new(self) } /// Returns an iterator over all intern indices and their associated strings. #[inline] pub fn iter_values(&self) -> Values<S> { Values::new(self) } /// Shrinks the capacity of the interner as much as possible. pub fn shrink_to_fit(&mut self) { self.map.shrink_to_fit(); self.values.shrink_to_fit(); } } impl<T, S> FromIterator<T> for StringInterner<S> where S: Symbol, T: Into<String> + AsRef<str>, { fn from_iter<I>(iter: I) -> Self where I: IntoIterator<Item = T>, { let iter = iter.into_iter(); let mut interner = StringInterner::with_capacity(iter.size_hint().0); interner.extend(iter); interner } } impl<T, S> std::iter::Extend<T> for StringInterner<S> where S: Symbol, T: Into<String> + AsRef<str>, { fn extend<I>(&mut self, iter: I) where I: IntoIterator<Item = T>, { for s in iter { self.get_or_intern(s); } } } /// Iterator over the pairs of associated symbols and interned strings for a `StringInterner`. pub struct Iter<'a, S> { iter: iter::Enumerate<slice::Iter<'a, Arc<str>>>, mark: marker::PhantomData<S>, } impl<'a, S> Iter<'a, S> where S: Symbol + 'a, { /// Creates a new iterator for the given StringIterator over pairs of /// symbols and their associated interned string. #[inline] fn new<H>(interner: &'a StringInterner<S, H>) -> Self where H: BuildHasher, { Iter { iter: interner.values.iter().enumerate(), mark: marker::PhantomData, } } } impl<'a, S> Iterator for Iter<'a, S> where S: Symbol + 'a, { type Item = (S, &'a Arc<str>); #[inline] fn next(&mut self) -> Option<Self::Item> { self.iter .next() .map(|(num, boxed_str)| (S::from_usize(num), boxed_str)) } #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } } /// Iterator over the interned strings of a `StringInterner`. pub struct Values<'a, S> where S: Symbol + 'a, { iter: slice::Iter<'a, Arc<str>>, mark: marker::PhantomData<S>, } impl<'a, S> Values<'a, S> where S: Symbol + 'a, { /// Creates a new iterator for the given StringIterator over its interned strings. #[inline] fn new<H>(interner: &'a StringInterner<S, H>) -> Self where H: BuildHasher, { Values { iter: interner.values.iter(), mark: marker::PhantomData, } } } impl<'a, S> Iterator for Values<'a, S> where S: Symbol + 'a, { type Item = &'a Arc<str>; #[inline] fn next(&mut self) -> Option<Self::Item> { self.iter.next() } #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } } impl<S, H> iter::IntoIterator for StringInterner<S, H> where S: Symbol, H: BuildHasher, { type Item = (S, Arc<str>); type IntoIter = IntoIter<S>; fn into_iter(self) -> Self::IntoIter { IntoIter { iter: self.values.into_iter().enumerate(), mark: marker::PhantomData, } } } /// Iterator over the pairs of associated symbol and strings. /// /// Consumes the `StringInterner` upon usage. pub struct IntoIter<S> where S: Symbol, { iter: iter::Enumerate<vec::IntoIter<Arc<str>>>, mark: marker::PhantomData<S>, } impl<S> Iterator for IntoIter<S> where S: Symbol, { type Item = (S, Arc<str>); fn next(&mut self) -> Option<Self::Item> { self.iter .next() .map(|(num, boxed_str)| (S::from_usize(num), boxed_str)) } #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } }