Struct flurry::HashSet

source ·
pub struct HashSet<T, S = DefaultHashBuilder> { /* private fields */ }
Expand description

A concurrent hash set implemented as a HashMap where the value is ().

§Examples

use flurry::HashSet;

// Initialize a new hash set.
let books = HashSet::new();
let guard = books.guard();

// Add some books
books.insert("Fight Club", &guard);
books.insert("Three Men In A Raft", &guard);
books.insert("The Book of Dust", &guard);
books.insert("The Dry", &guard);

// Check for a specific one.
if !books.contains(&"The Drunken Botanist", &guard) {
    println!("We don't have The Drunken Botanist.");
}

// Remove a book.
books.remove(&"Three Men In A Raft", &guard);

// Iterate over everything.
for book in books.iter(&guard) {
    println!("{}", book);
}

Implementations§

source§

impl<T> HashSet<T, DefaultHashBuilder>

source

pub fn new() -> Self

Creates an empty HashSet.

The hash set is initially created with a capacity of 0, so it will not allocate until it is first inserted into.

§Examples
use flurry::HashSet;
let set: HashSet<i32> = HashSet::new();
source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty HashSet with the specified capacity.

The hash map will be able to hold at least capacity elements without reallocating. If capacity is 0, the hash map will not allocate.

§Examples
use flurry::HashSet;
let map: HashSet<&str, _> = HashSet::with_capacity(10);
§Notes

There is no guarantee that the HashSet will not resize if capacity elements are inserted. The set will resize based on key collision, so bad key distribution may cause a resize before capacity is reached. For more information see the resizing behavior of HashMap.

source§

impl<T, S> HashSet<T, S>

source

pub fn with_hasher(hash_builder: S) -> Self

Creates an empty set which will use hash_builder to hash values.

The created set has the default initial capacity.

Warning: hash_builder is normally randomly generated, and is designed to allow the set to be resistant to attacks that cause many collisions and very poor performance. Setting it manually using this function can expose a DoS attack vector.

§Examples
use flurry::{HashSet, DefaultHashBuilder};

let set = HashSet::with_hasher(DefaultHashBuilder::default());
let guard = set.guard();
set.insert(1, &guard);
source

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

Creates an empty set with the specified capacity, using hash_builder to hash the values.

The set will be sized to accommodate capacity elements with a low chance of reallocating (assuming uniformly distributed hashes). If capacity is 0, the call will not allocate, and is equivalent to HashSet::new.

Warning: hash_builder is normally randomly generated, and is designed to allow the set to be resistant to attacks that cause many collisions and very poor performance. Setting it manually using this function can expose a DoS attack vector.

§Examples
use flurry::HashSet;
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let set = HashSet::with_capacity_and_hasher(10, s);
let guard = set.guard();
set.insert(1, &guard);
source

pub fn guard(&self) -> Guard<'_>

Pin a Guard for use with this set.

Keep in mind that for as long as you hold onto this Guard, you are preventing the collection of garbage generated by the set.

source

pub fn len(&self) -> usize

Returns the number of elements in the set.

§Examples
use flurry::HashSet;

let set = HashSet::new();

let guard = set.guard();
set.insert(1, &guard);
set.insert(2, &guard);
assert_eq!(set.len(), 2);
source

pub fn is_empty(&self) -> bool

Returns true if the set is empty. Otherwise returns false.

§Examples
use flurry::HashSet;

let set = HashSet::new();
assert!(set.is_empty());
set.insert("a", &set.guard());
assert!(!set.is_empty());
source

pub fn iter<'g>(&'g self, guard: &'g Guard<'_>) -> Keys<'g, T, ()>

An iterator visiting all elements in arbitrary order.

The iterator element type is &'g T.

See HashMap::keys for details.

§Examples
use flurry::HashSet;

let set = HashSet::new();
let guard = set.guard();
set.insert(1, &guard);
set.insert(2, &guard);

for x in set.iter(&guard) {
    println!("{}", x);
}
source§

impl<T, S> HashSet<T, S>
where T: Hash + Ord, S: BuildHasher,

source

pub fn contains<Q>(&self, value: &Q, guard: &Guard<'_>) -> bool
where T: Borrow<Q>, Q: ?Sized + Hash + Ord,

Returns true if the given value is an element of this set.

The value may be any borrowed form of the set’s value type, but Hash and Ord on the borrowed form must match those for the value type.

§Examples
use flurry::HashSet;

let set = HashSet::new();
let guard = set.guard();
set.insert(2, &guard);

assert!(set.contains(&2, &guard));
assert!(!set.contains(&1, &guard));
source

pub fn get<'g, Q>(&'g self, value: &Q, guard: &'g Guard<'_>) -> Option<&'g T>
where T: Borrow<Q>, Q: ?Sized + Hash + Ord,

Returns a reference to the element in the set, if any, that is equal to the given value.

The value may be any borrowed form of the set’s value type, but Hash and Ord on the borrowed form must match those for the value type.

§Examples
use flurry::HashSet;

let set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
let guard = set.guard();
assert_eq!(set.get(&2, &guard), Some(&2));
assert_eq!(set.get(&4, &guard), None);
source

pub fn is_disjoint( &self, other: &HashSet<T, S>, our_guard: &Guard<'_>, their_guard: &Guard<'_> ) -> bool

Returns true if self has no elements in common with other.

This is equivalent to checking for an empty intersection.

§Examples
use std::iter::FromIterator;
use flurry::HashSet;

let a = HashSet::from_iter(&[1, 2, 3]);
let b = HashSet::new();

assert!(a.pin().is_disjoint(&b.pin()));
b.pin().insert(4);
assert!(a.pin().is_disjoint(&b.pin()));
b.pin().insert(1);
assert!(!a.pin().is_disjoint(&b.pin()));
source

pub fn is_subset( &self, other: &HashSet<T, S>, our_guard: &Guard<'_>, their_guard: &Guard<'_> ) -> bool

Returns true if the set is a subset of another, i.e., other contains at least all the values in self.

§Examples
use std::iter::FromIterator;
use flurry::HashSet;

let sup = HashSet::from_iter(&[1, 2, 3]);
let set = HashSet::new();

assert!(set.pin().is_subset(&sup.pin()));
set.pin().insert(2);
assert!(set.pin().is_subset(&sup.pin()));
set.pin().insert(4);
assert!(!set.pin().is_subset(&sup.pin()));
source

pub fn is_superset( &self, other: &HashSet<T, S>, our_guard: &Guard<'_>, their_guard: &Guard<'_> ) -> bool

Returns true if the set is a superset of another, i.e., self contains at least all the values in other.

§Examples
use std::iter::FromIterator;
use flurry::HashSet;

let sub = HashSet::from_iter(&[1, 2]);
let set = HashSet::new();

assert!(!set.pin().is_superset(&sub.pin()));

set.pin().insert(0);
set.pin().insert(1);
assert!(!set.pin().is_superset(&sub.pin()));

set.pin().insert(2);
assert!(set.pin().is_superset(&sub.pin()));
source§

impl<T, S> HashSet<T, S>
where T: Sync + Send + Clone + Hash + Ord, S: BuildHasher,

source

pub fn insert(&self, value: T, guard: &Guard<'_>) -> bool

Adds a value to the set.

If the set did not have this value present, true is returned.

If the set did have this value present, false is returned.

§Examples
use flurry::HashSet;

let set = HashSet::new();
let guard = set.guard();

assert_eq!(set.insert(2, &guard), true);
assert_eq!(set.insert(2, &guard), false);
assert!(set.contains(&2, &guard));
source

pub fn remove<Q>(&self, value: &Q, guard: &Guard<'_>) -> bool
where T: Borrow<Q>, Q: ?Sized + Hash + Ord,

Removes a value from the set.

If the set did not have this value present, false is returned.

If the set did have this value present, true is returned.

The value may be any borrowed form of the set’s value type, but Hash and Ord on the borrowed form must match those for the value type.

§Examples
use flurry::HashSet;

let set = HashSet::new();
let guard = set.guard();
set.insert(2, &guard);

assert_eq!(set.remove(&2, &guard), true);
assert!(!set.contains(&2, &guard));
assert_eq!(set.remove(&2, &guard), false);
source

pub fn take<'g, Q>(&'g self, value: &Q, guard: &'g Guard<'_>) -> Option<&'g T>
where T: Borrow<Q>, Q: ?Sized + Hash + Ord,

Removes and returns the value in the set, if any, that is equal to the given one.

The value may be any borrowed form of the set’s value type, but Hash and Ord on the borrowed form must match those for the value type.

§Examples
use flurry::HashSet;

let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
let guard = set.guard();
assert_eq!(set.take(&2, &guard), Some(&2));
assert_eq!(set.take(&2, &guard), None);
source

pub fn retain<F>(&self, f: F, guard: &Guard<'_>)
where F: FnMut(&T) -> bool,

Retains only the elements specified by the predicate.

In other words, remove all elements e such that f(&e) returns false.

§Examples
use flurry::HashSet;

let set = HashSet::new();

for i in 0..8 {
    set.pin().insert(i);
}
set.pin().retain(|&e| e % 2 == 0);
assert_eq!(set.pin().len(), 4);
source§

impl<T, S> HashSet<T, S>
where T: Clone + Ord,

source

pub fn clear(&self, guard: &Guard<'_>)

Clears the set, removing all elements.

§Examples
use flurry::HashSet;

let set = HashSet::new();

set.pin().insert("a");
set.pin().clear();
assert!(set.pin().is_empty());
source

pub fn reserve(&self, additional: usize, guard: &Guard<'_>)

Tries to reserve capacity for at least additional more elements to be inserted in the HashSet.

The collection may reserve more space to avoid frequent reallocations.

source§

impl<T, S> HashSet<T, S>

source

pub fn pin(&self) -> HashSetRef<'_, T, S>

Get a reference to this set with the current thread pinned.

Keep in mind that for as long as you hold onto this, you are preventing the collection of garbage generated by the set.

source

pub fn with_guard<'g>(&'g self, guard: &'g Guard<'_>) -> HashSetRef<'g, T, S>

Get a reference to this set with the given guard.

Trait Implementations§

source§

impl<T, S> Clone for HashSet<T, S>
where T: Sync + Send + Clone + Hash + Ord, S: BuildHasher + Clone,

source§

fn clone(&self) -> HashSet<T, S>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T, S> Debug for HashSet<T, S>
where T: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T, S> Default for HashSet<T, S>
where S: Default,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'a, T, S> Extend<&'a T> for &HashSet<T, S>
where T: Sync + Send + Copy + Hash + Ord, S: BuildHasher,

source§

fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<T, S> Extend<T> for &HashSet<T, S>
where T: Sync + Send + Clone + Hash + Ord, S: BuildHasher,

source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<'a, T, S> FromIterator<&'a T> for HashSet<T, S>
where T: Sync + Send + Copy + Hash + Ord, S: BuildHasher + Default,

source§

fn from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl<T, S> FromIterator<T> for HashSet<T, S>
where T: Sync + Send + Clone + Hash + Ord, S: BuildHasher + Default,

source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl<T, S> PartialEq<HashSet<T, S>> for HashSetRef<'_, T, S>
where T: Hash + Ord, S: BuildHasher,

source§

fn eq(&self, other: &HashSet<T, S>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T, S> PartialEq<HashSetRef<'_, T, S>> for HashSet<T, S>
where T: Hash + Ord, S: BuildHasher,

source§

fn eq(&self, other: &HashSetRef<'_, T, S>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T, S> PartialEq for HashSet<T, S>
where T: Ord + Hash, S: BuildHasher,

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T, S> Eq for HashSet<T, S>
where T: Ord + Hash, S: BuildHasher,

Auto Trait Implementations§

§

impl<T, S = DefaultHashBuilder> !Freeze for HashSet<T, S>

§

impl<T, S> RefUnwindSafe for HashSet<T, S>
where S: RefUnwindSafe,

§

impl<T, S> Send for HashSet<T, S>
where S: Send,

§

impl<T, S> Sync for HashSet<T, S>
where S: Sync,

§

impl<T, S> Unpin for HashSet<T, S>
where S: Unpin,

§

impl<T, S = DefaultHashBuilder> !UnwindSafe for HashSet<T, S>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.