Struct HashSet

Source
pub struct HashSet<K, S = RandomState> { /* private fields */ }
Expand description

A concurrent hash set.

Most hash set operations require a Guard, which can be acquired through HashSet::guard or using the HashSet::pin API. See the crate-level documentation for details.

Implementations§

Source§

impl<K> HashSet<K>

Source

pub fn new() -> HashSet<K>

Creates an empty HashSet.

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

§Examples
use papaya::HashSet;
let map: HashSet<&str> = HashSet::new();
Source

pub fn with_capacity(capacity: usize) -> HashSet<K>

Creates an empty HashSet with the specified capacity.

The set should be able to hold at least capacity elements before resizing. However, the capacity is an estimate, and the set may prematurely resize due to poor hash distribution. If capacity is 0, the hash set will not allocate.

§Examples
use papaya::HashSet;
let set: HashSet<&str> = HashSet::with_capacity(10);
Source

pub fn builder() -> HashSetBuilder<K>

Returns a builder for a HashSet.

The builder can be used for more complex configuration, such as using a custom Collector, or ResizeMode.

Source§

impl<K, S> HashSet<K, S>

Source

pub fn with_hasher(hash_builder: S) -> HashSet<K, S>

Creates an empty HashSet which will use the given hash builder to hash keys.

Warning: hash_builder is normally randomly generated, and is designed to allow HashSets 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.

The hash_builder passed should implement the BuildHasher trait for the HashSet to be useful, see its documentation for details.

§Examples
use papaya::HashSet;
use std::hash::RandomState;

let s = RandomState::new();
let set = HashSet::with_hasher(s);
set.pin().insert(1);
Source

pub fn with_capacity_and_hasher( capacity: usize, hash_builder: S, ) -> HashSet<K, S>

Creates an empty HashSet with at least the specified capacity, using hash_builder to hash the keys.

The set should be able to hold at least capacity elements before resizing. However, the capacity is an estimate, and the set may prematurely resize due to poor hash distribution. If capacity is 0, the hash set will not allocate.

Warning: hash_builder is normally randomly generated, and is designed to allow HashSets 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.

The hasher passed should implement the BuildHasher trait for the HashSet to be useful, see its documentation for details.

§Examples
use papaya::HashSet;
use std::hash::RandomState;

let s = RandomState::new();
let set = HashSet::with_capacity_and_hasher(10, s);
set.pin().insert(1);
Source

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

Returns a pinned reference to the set.

The returned reference manages a guard internally, preventing garbage collection for as long as it is held. See the crate-level documentation for details.

Source

pub fn pin_owned(&self) -> HashSetRef<'_, K, S, OwnedGuard<'_>>

Returns a pinned reference to the set.

Unlike HashSet::pin, the returned reference implements Send and Sync, allowing it to be held across .await points in work-stealing schedulers. This is especially useful for iterators.

The returned reference manages a guard internally, preventing garbage collection for as long as it is held. See the crate-level documentation for details.

Source

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

Returns a guard for use with this set.

Note that holding on to a guard prevents garbage collection. See the crate-level documentation for details.

Source

pub fn owned_guard(&self) -> OwnedGuard<'_>

Returns an owned guard for use with this set.

Owned guards implement Send and Sync, allowing them to be held across .await points in work-stealing schedulers. This is especially useful for iterators.

Note that holding on to a guard prevents garbage collection. See the crate-level documentation for details.

Source§

impl<K, S> HashSet<K, S>
where K: Hash + Eq, S: BuildHasher,

Source

pub fn len(&self) -> usize

Returns the number of entries in the set.

§Examples
use papaya::HashSet;

let set = HashSet::new();

set.pin().insert(1);
set.pin().insert(2);
assert!(set.len() == 2);
Source

pub fn is_empty(&self) -> bool

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

§Examples
use papaya::HashSet;

let set = HashSet::new();
assert!(set.is_empty());
set.pin().insert("a");
assert!(!set.is_empty());
Source

pub fn contains<Q>(&self, key: &Q, guard: &impl Guard) -> bool
where Q: Equivalent<K> + Hash + ?Sized,

Returns true if the set contains a value for the specified key.

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

§Examples
use papaya::HashSet;

let set = HashSet::new();
set.pin().insert(1);
assert_eq!(set.pin().contains(&1), true);
assert_eq!(set.pin().contains(&2), false);
Source

pub fn get<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<&'g K>
where Q: Equivalent<K> + Hash + ?Sized,

Returns a reference to the value corresponding to the key.

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

§Examples
use papaya::HashSet;

let set = HashSet::new();
set.pin().insert(1);
assert_eq!(set.pin().get(&1), Some(&1));
assert_eq!(set.pin().get(&2), None);
Source

pub fn insert(&self, key: K, guard: &impl Guard) -> bool

Inserts a value into the set.

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

If the set did have this key present, false is returned and the old value is not updated. This matters for types that can be == without being identical. See the standard library documentation for details.

§Examples
use papaya::HashSet;

let set = HashSet::new();
assert_eq!(set.pin().insert(37), true);
assert_eq!(set.pin().is_empty(), false);

set.pin().insert(37);
assert_eq!(set.pin().insert(37), false);
assert_eq!(set.pin().get(&37), Some(&37));
Source

pub fn remove<Q>(&self, key: &Q, guard: &impl Guard) -> bool
where Q: Equivalent<K> + Hash + ?Sized,

Removes a key from the set, returning the value at the key if the key was previously in the set.

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

§Examples
use papaya::HashSet;

let set = HashSet::new();
set.pin().insert(1);
assert_eq!(set.pin().remove(&1), true);
assert_eq!(set.pin().remove(&1), false);
Source

pub fn reserve(&self, additional: usize, guard: &impl Guard)

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

After calling this method, the set should be able to hold at least capacity elements before resizing. However, the capacity is an estimate, and the set may prematurely resize due to poor hash distribution. The collection may also reserve more space to avoid frequent reallocations.

§Panics

Panics if the new allocation size overflows usize.

§Examples
use papaya::HashSet;

let set: HashSet<&str> = HashSet::new();
set.pin().reserve(10);
Source

pub fn clear(&self, guard: &impl Guard)

Clears the set, removing all values.

Note that this method will block until any in-progress resizes are completed before proceeding. See the consistency section for details.

§Examples
use papaya::HashSet;

let set = HashSet::new();

set.pin().insert(1);
set.pin().clear();
assert!(set.pin().is_empty());
Source

pub fn retain<F>(&mut self, f: F, guard: &impl Guard)
where F: FnMut(&K) -> bool,

Retains only the elements specified by the predicate.

In other words, remove all values v for which f(&v) returns false. The elements are visited in unsorted (and unspecified) order.

Note the function may be called more than once for a given key if its value is concurrently modified during removal.

Additionally, this method will block until any in-progress resizes are completed before proceeding. See the consistency section for details.

§Examples
use papaya::HashSet;

let mut set: HashSet<i32> = (0..8).collect();
set.pin().retain(|&v| v % 2 == 0);
assert_eq!(set.len(), 4);
assert_eq!(set.pin().contains(&1), false);
assert_eq!(set.pin().contains(&2), true);
Source

pub fn iter<'g, G>(&self, guard: &'g G) -> Iter<'g, K, G>
where G: Guard,

An iterator visiting all values in arbitrary order.

Note that this method will block until any in-progress resizes are completed before proceeding. See the consistency section for details.

§Examples
use papaya::HashSet;

let set = HashSet::from([
    "a",
    "b",
    "c"
]);

for val in set.pin().iter() {
    println!("val: {val}");
}

Trait Implementations§

Source§

impl<K, S> Clone for HashSet<K, S>
where K: Clone + Hash + Eq, S: BuildHasher + Clone,

Source§

fn clone(&self) -> HashSet<K, 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<K, S> Debug for HashSet<K, S>
where K: Hash + Eq + Debug, S: BuildHasher,

Source§

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

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

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

Source§

fn default() -> Self

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

impl<'a, K, S> Extend<&'a K> for &HashSet<K, S>
where K: Copy + Hash + Eq + 'a, S: BuildHasher,

Source§

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

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<K, S> Extend<K> for &HashSet<K, S>
where K: Hash + Eq, S: BuildHasher,

Source§

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

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<K, const N: usize> From<[K; N]> for HashSet<K, RandomState>
where K: Hash + Eq,

Source§

fn from(arr: [K; N]) -> Self

Converts to this type from the input type.
Source§

impl<K, S> FromIterator<K> for HashSet<K, S>
where K: Hash + Eq, S: BuildHasher + Default,

Source§

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

Creates a value from an iterator. Read more
Source§

impl<K, S> PartialEq for HashSet<K, S>
where K: Hash + Eq, S: BuildHasher,

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

impl<K: Send, S: Send> Send for HashSet<K, S>

Source§

impl<K: Sync, S: Sync> Sync for HashSet<K, S>

Auto Trait Implementations§

§

impl<K, S = RandomState> !Freeze for HashSet<K, S>

§

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

§

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

§

impl<K, S = RandomState> !UnwindSafe for HashSet<K, 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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,

Source§

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>,

Source§

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>,

Source§

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.