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>
impl<K> HashSet<K>
Sourcepub fn new() -> HashSet<K>
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();
Sourcepub fn with_capacity(capacity: usize) -> HashSet<K>
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);
Sourcepub fn builder() -> HashSetBuilder<K>
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>
impl<K, S> HashSet<K, S>
Sourcepub fn with_hasher(hash_builder: S) -> HashSet<K, S>
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);
Sourcepub fn with_capacity_and_hasher(
capacity: usize,
hash_builder: S,
) -> HashSet<K, S>
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);
Sourcepub fn pin(&self) -> HashSetRef<'_, K, S, LocalGuard<'_>>
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.
Sourcepub fn pin_owned(&self) -> HashSetRef<'_, K, S, OwnedGuard<'_>>
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.
Sourcepub fn guard(&self) -> LocalGuard<'_>
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.
Sourcepub fn owned_guard(&self) -> OwnedGuard<'_>
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>
impl<K, S> HashSet<K, S>
Sourcepub fn len(&self) -> usize
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);
Sourcepub fn is_empty(&self) -> bool
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());
Sourcepub fn contains<Q>(&self, key: &Q, guard: &impl Guard) -> bool
pub fn contains<Q>(&self, key: &Q, guard: &impl Guard) -> bool
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);
Sourcepub fn get<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<&'g K>
pub fn get<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<&'g K>
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);
Sourcepub fn insert(&self, key: K, guard: &impl Guard) -> bool
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));
Sourcepub fn remove<Q>(&self, key: &Q, guard: &impl Guard) -> bool
pub fn remove<Q>(&self, key: &Q, guard: &impl Guard) -> bool
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);
Sourcepub fn reserve(&self, additional: usize, guard: &impl Guard)
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);
Sourcepub fn clear(&self, guard: &impl Guard)
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());
Sourcepub fn retain<F>(&mut self, f: F, guard: &impl Guard)
pub fn retain<F>(&mut self, f: F, guard: &impl Guard)
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);
Sourcepub fn iter<'g, G>(&self, guard: &'g G) -> Iter<'g, K, G>where
G: Guard,
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<'a, K, S> Extend<&'a K> for &HashSet<K, S>
impl<'a, K, S> Extend<&'a K> for &HashSet<K, S>
Source§fn extend<T: IntoIterator<Item = &'a K>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = &'a K>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<K, S> Extend<K> for &HashSet<K, S>
impl<K, S> Extend<K> for &HashSet<K, S>
Source§fn extend<T: IntoIterator<Item = K>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = K>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<K, S> FromIterator<K> for HashSet<K, S>
impl<K, S> FromIterator<K> for HashSet<K, S>
Source§fn from_iter<T: IntoIterator<Item = K>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = K>>(iter: T) -> Self
impl<K, S> Eq for HashSet<K, S>
impl<K: Send, S: Send> Send for HashSet<K, S>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.