[][src]Struct intaglio::bytes::SymbolTable

pub struct SymbolTable<S = RandomState> { /* fields omitted */ }

Byte string interner.

This symbol table is implemented by storing bytestrings with a fast path for &[u8] that are already 'static.

See module documentation for more.

Usage

let mut table = SymbolTable::new();
let sym = table.intern(&b"abc"[..])?;
assert_eq!(sym, table.intern(b"abc".to_vec())?);
assert!(table.contains(sym));
assert!(table.is_interned(b"abc"));

Implementations

impl SymbolTable<RandomState>[src]

#[must_use]pub fn new() -> Self[src]

Constructs a new, empty SymbolTable with default capacity.

Examples

let table = SymbolTable::new();
assert_eq!(0, table.len());
assert!(table.capacity() >= 4096);

#[must_use]pub fn with_capacity(capacity: usize) -> Self[src]

Constructs a new, empty SymbolTable with the specified capacity.

The symbol table will be able to hold at least capacity bytestrings without reallocating. If capacity is 0, the symbol table will not allocate.

Examples

let table = SymbolTable::with_capacity(10);
assert_eq!(0, table.len());
assert!(table.capacity() >= 10);

impl<S> SymbolTable<S>[src]

pub fn with_hasher(hash_builder: S) -> Self[src]

Constructs a new, empty SymbolTable with default capacity and the given hash builder.

Examples

let hash_builder = RandomState::new();
let table = SymbolTable::with_hasher(hash_builder);
assert_eq!(0, table.len());
assert!(table.capacity() >= 4096);

pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self[src]

Constructs a new, empty SymbolTable with the specified capacity and the given hash builder.

Examples

let hash_builder = RandomState::new();
let table = SymbolTable::with_capacity_and_hasher(10, hash_builder);
assert_eq!(0, table.len());
assert!(table.capacity() >= 10);

pub fn capacity(&self) -> usize[src]

Returns the number of bytestrings the table can hold without reallocating.

Examples

let table = SymbolTable::with_capacity(10);
assert!(table.capacity() >= 10);

pub fn len(&self) -> usize[src]

Returns the number of interned bytestrings in the table.

Examples

let mut table = SymbolTable::new();
assert_eq!(0, table.len());

table.intern(b"abc".to_vec())?;
// only uniquely interned bytestrings grow the symbol table.
table.intern(b"abc".to_vec())?;
table.intern(b"xyz".to_vec())?;
assert_eq!(2, table.len());

pub fn is_empty(&self) -> bool[src]

Returns true if the symbol table contains no interned bytestrings.

Examples

let mut table = SymbolTable::new();
assert!(table.is_empty());

table.intern(b"abc".to_vec())?;
assert!(!table.is_empty());

#[must_use]pub fn contains(&self, id: Symbol) -> bool[src]

Returns true if the symbol table contains the given symbol.

Examples

let mut table = SymbolTable::new();
assert!(!table.contains(Symbol::new(0)));

let sym = table.intern(b"abc".to_vec())?;
assert!(table.contains(Symbol::new(0)));
assert!(table.contains(sym));

#[must_use]pub fn get(&self, id: Symbol) -> Option<&[u8]>[src]

Returns a reference to the byte string associated with the given symbol.

If the given symbol does not exist in the underlying symbol table, None is returned.

The lifetime of the returned reference is bound to the symbol table.

Examples

let mut table = SymbolTable::new();
assert!(table.get(Symbol::new(0)).is_none());

let sym = table.intern(b"abc".to_vec())?;
assert_eq!(Some(&b"abc"[..]), table.get(Symbol::new(0)));
assert_eq!(Some(&b"abc"[..]), table.get(sym));

pub fn iter(&self) -> Iter<'_>

Notable traits for Iter<'a>

impl<'a> Iterator for Iter<'a> type Item = (Symbol, &'a [u8]);
[src]

Returns an iterator over all Symbols and bytestrings in the SymbolTable.

Examples

let mut table = SymbolTable::new();
table.intern(b"abc".to_vec())?;
table.intern(b"xyz".to_vec())?;
table.intern(b"123".to_vec())?;
table.intern(b"789".to_vec())?;

let iter = table.iter();
let mut map = HashMap::new();
map.insert(Symbol::new(0), &b"abc"[..]);
map.insert(Symbol::new(1), &b"xyz"[..]);
map.insert(Symbol::new(2), &b"123"[..]);
map.insert(Symbol::new(3), &b"789"[..]);
assert_eq!(map, iter.collect::<HashMap<_, _>>());
let mut table = SymbolTable::new();
table.intern(b"abc".to_vec())?;
table.intern(b"xyz".to_vec())?;
table.intern(b"123".to_vec())?;
table.intern(b"789".to_vec())?;

let iter = table.iter();
assert_eq!(table.len(), iter.count());

pub fn all_symbols(&self) -> AllSymbols<'_>

Notable traits for AllSymbols<'a>

impl<'a> Iterator for AllSymbols<'a> type Item = Symbol;
[src]

Returns an iterator over all Symbols in the SymbolTable.

Examples

let mut table = SymbolTable::new();
table.intern(b"abc".to_vec())?;
table.intern(b"xyz".to_vec())?;
table.intern(b"123".to_vec())?;
table.intern(b"789".to_vec())?;

let mut all_symbols = table.all_symbols();
assert_eq!(Some(Symbol::new(0)), all_symbols.next());
assert_eq!(Some(Symbol::new(1)), all_symbols.nth_back(2));
assert_eq!(None, all_symbols.next());
let mut table = SymbolTable::new();
table.intern(b"abc".to_vec())?;
table.intern(b"xyz".to_vec())?;
table.intern(b"123".to_vec())?;
table.intern(b"789".to_vec())?;

let all_symbols = table.all_symbols();
assert_eq!(table.len(), all_symbols.count());

pub fn bytestrings(&self) -> Bytestrings<'_>

Notable traits for Bytestrings<'a>

impl<'a> Iterator for Bytestrings<'a> type Item = &'a [u8];
[src]

Returns an iterator over all bytestrings in the SymbolTable.

Examples

let mut table = SymbolTable::new();
table.intern(b"abc".to_vec())?;
table.intern(b"xyz".to_vec())?;
table.intern(b"123".to_vec())?;
table.intern(b"789".to_vec())?;

let mut bytestrings = table.bytestrings();
assert_eq!(Some(&b"abc"[..]), bytestrings.next());
assert_eq!(Some(&b"xyz"[..]), bytestrings.nth_back(2));
assert_eq!(None, bytestrings.next());
let mut table = SymbolTable::new();
table.intern(b"abc".to_vec())?;
table.intern(b"xyz".to_vec())?;
table.intern(b"123".to_vec())?;
table.intern(b"789".to_vec())?;

let  bytestrings = table.bytestrings();
assert_eq!(table.len(), bytestrings.count());

impl<S> SymbolTable<S> where
    S: BuildHasher
[src]

pub fn intern<T>(&mut self, contents: T) -> Result<Symbol, SymbolOverflowError> where
    T: Into<Cow<'static, [u8]>>, 
[src]

Intern a bytestring for the lifetime of the symbol table.

The returned Symbol allows retrieving of the underlying bytes. Equal bytestrings will be inserted into the symbol table exactly once.

This function only allocates if the underlying symbol table has no remaining capacity.

Errors

If the symbol table would grow larger than u32::MAX interned bytestrings, the Symbol counter would overflow and a SymbolOverflowError is returned.

Examples

let mut table = SymbolTable::new();
let sym = table.intern(b"abc".to_vec())?;
table.intern(b"xyz".to_vec())?;
table.intern(&b"123"[..])?;
table.intern(&b"789"[..])?;

assert_eq!(4, table.len());
assert_eq!(Some(&b"abc"[..]), table.get(sym));

#[must_use]pub fn check_interned(&self, contents: &[u8]) -> Option<Symbol>[src]

Returns the Symbol identifier for contents if it has been interned before, None otherwise.

This method does not modify the symbol table.

Examples

let mut table = SymbolTable::new();
assert!(!table.is_interned(b"abc"));
assert_eq!(None, table.check_interned(b"abc"));

table.intern(b"abc".to_vec())?;
assert!(table.is_interned(b"abc"));
assert_eq!(Some(Symbol::new(0)), table.check_interned(b"abc"));

#[must_use]pub fn is_interned(&self, contents: &[u8]) -> bool[src]

Returns true if the given byte string has been interned before.

This method does not modify the symbol table.

Examples

let mut table = SymbolTable::new();
assert!(!table.is_interned(b"abc"));
assert_eq!(None, table.check_interned(b"abc"));

table.intern(b"abc".to_vec())?;
assert!(table.is_interned(b"abc"));
assert_eq!(Some(Symbol::new(0)), table.check_interned(b"abc"));

pub fn reserve(&mut self, additional: usize)[src]

Reserves capacity for at least additional more elements to be inserted in the given SymbolTable. The collection may reserve more space to avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

Panics

Panics if the new capacity overflows usize.

Examples

let mut table = SymbolTable::with_capacity(1);
table.intern(b"abc".to_vec())?;
table.reserve(10);
assert!(table.capacity() >= 11);

pub fn shrink_to_fit(&mut self)[src]

Shrinks the capacity of the symbol table as much as possible.

It will drop down as close as possible to the length but the allocator may still inform the symbol table that there is space for a few more elements.

Examples

let mut table = SymbolTable::with_capacity(10);
table.intern(b"abc".to_vec());
table.intern(b"xyz".to_vec());
table.intern(b"123".to_vec());
table.shrink_to_fit();
assert!(table.capacity() >= 3);

Trait Implementations

impl<S: Debug> Debug for SymbolTable<S>[src]

impl<S: Default> Default for SymbolTable<S>[src]

impl<'a> IntoIterator for &'a SymbolTable[src]

type Item = (Symbol, &'a [u8])

The type of the elements being iterated over.

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?

Auto Trait Implementations

impl<S> RefUnwindSafe for SymbolTable<S> where
    S: RefUnwindSafe
[src]

impl<S> Send for SymbolTable<S> where
    S: Send
[src]

impl<S> Sync for SymbolTable<S> where
    S: Sync
[src]

impl<S> Unpin for SymbolTable<S> where
    S: Unpin
[src]

impl<S> UnwindSafe for SymbolTable<S> where
    S: UnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.