[][src]Struct trashmap::TrashSet

pub struct TrashSet<K: ?Sized, S = RandomState> { /* fields omitted */ }

A hash set that can operate on known hash values (Trash) instead of actual keys

Sometimes you need to access the same element in the set multiple times and don't wish to re-hash each time. In such a case, you can use TrashMap, which will provide a Trash id that can be used to cheaply access map values as long as you keep it around.

An assumption made here is that there are no hash collisions in the u64 hash space for your hasher. If there are, this may result in unpredictable behavior.

use trashmap::TrashSet;

let mut map = TrashSet::new();
let id = map.insert("foo");
do_stuff(&mut map);
assert!(map.contains(id));
map.remove(id);

For example, this is useful if you're doing some kind of recursion-prevention scheme:

use trashmap::TrashSet;
struct State {
   seen: TrashSet<str>,
}

impl State {
    fn step_into(&mut self, entry: &str) {
        let (id, empty) = self.seen.insert_check(entry);
        if !empty {
            panic!("found recursive loop!");
        }
        let children = lookup_children(entry);
        for child in children {
           self.step_into(child);
        }
        self.seen.remove(id);
    }
}

Methods

impl<K: ?Sized, S> TrashSet<K, S> where
    K: Eq + Hash,
    S: BuildHasher
[src]

pub fn new() -> Self where
    S: Default
[src]

Construct a basic TrashSet

pub fn with_capacity_and_hasher(cap: usize, hasher: S) -> Self[src]

Construct a TrashSet with a custom hasher and/or capacity

pub fn insert<Q: ?Sized>(&mut self, key: &Q) -> Trash where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Insert a key, getting a Trash id to be used to access the entry later

pub fn insert_check<Q: ?Sized>(&mut self, key: &Q) -> (Trash, bool) where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Insert a key, getting a Trash id to be used to access the entry later, as well as a boolean indicating if the entry was empty (true if empty, false otherwise)

pub fn insert_id(&mut self, key: Trash) -> bool[src]

Insert an element based on its Trash id

Returns whether or not the entry was previously unset (true if unset, false otherwise)

pub fn contains(&self, key: Trash) -> bool[src]

Check if the Trash id has been inserted before

pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Check if the key has been inserted before

Also returns the Trash id for the key

pub fn remove(&mut self, key: Trash) -> bool[src]

Remove an entry based on its Trash id

pub fn remove_key<Q: ?Sized>(&mut self, key: &Q) -> bool where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Remove an entry given its key

pub fn trash<Q: ?Sized>(&self, k: &Q) -> Trash where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Get the Trash id for a given key

Trait Implementations

impl<K: ?Sized, S> Default for TrashSet<K, S> where
    K: Eq + Hash,
    S: BuildHasher + Default
[src]

Auto Trait Implementations

impl<K, S = RandomState> !Send for TrashSet<K, S>

impl<K, S = RandomState> !Sync for TrashSet<K, S>

Blanket Implementations

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.

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

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

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