pub struct HashSet<K, H = RandomState> where
K: 'static + Eq + Hash + Sync,
H: BuildHasher, { /* private fields */ }
Expand description
Implementations
sourceimpl<K, H> HashSet<K, H> where
K: 'static + Eq + Hash + Sync,
H: BuildHasher,
impl<K, H> HashSet<K, H> where
K: 'static + Eq + Hash + Sync,
H: BuildHasher,
sourcepub fn new(capacity: usize, build_hasher: H) -> HashSet<K, H>
pub fn new(capacity: usize, build_hasher: H) -> HashSet<K, H>
Creates an empty HashSet
with the given capacity and BuildHasher
.
The actual capacity is equal to or greater than the given capacity.
Panics
Panics if memory allocation fails.
Examples
use scc::HashSet;
use std::collections::hash_map::RandomState;
let hashset: HashSet<u64, RandomState> = HashSet::new(1000, RandomState::new());
let result = hashset.capacity();
assert_eq!(result, 1024);
let hashset: HashSet<u64> = HashSet::default();
let result = hashset.capacity();
assert_eq!(result, 64);
sourcepub fn reserve(&self, capacity: usize) -> Option<Ticket<'_, K, H>>
pub fn reserve(&self, capacity: usize) -> Option<Ticket<'_, K, H>>
Temporarily increases the minimum capacity of the HashSet
.
The reserved space is not exclusively owned by the Ticket
, thus can be overtaken.
Unused space is immediately reclaimed when the Ticket
is dropped.
Errors
Returns None
if a too large number is given.
Examples
use scc::HashSet;
use std::collections::hash_map::RandomState;
let hashset: HashSet<usize, RandomState> = HashSet::new(1000, RandomState::new());
assert_eq!(hashset.capacity(), 1024);
let ticket = hashset.reserve(10000);
assert!(ticket.is_some());
assert_eq!(hashset.capacity(), 16384);
for i in 0..16 {
assert!(hashset.insert(i).is_ok());
}
drop(ticket);
assert_eq!(hashset.capacity(), 1024);
sourcepub fn insert(&self, key: K) -> Result<(), K>
pub fn insert(&self, key: K) -> Result<(), K>
Inserts a key into the HashSet
.
Errors
Returns an error along with the supplied key if the key exists.
Panics
Panics if memory allocation fails, or the number of entries in the target cell reaches
u32::MAX
.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
assert!(hashset.insert(1).is_ok());
assert_eq!(hashset.insert(1).unwrap_err(), 1);
sourcepub async fn insert_async(&self, key: K) -> Result<(), K>
pub async fn insert_async(&self, key: K) -> Result<(), K>
Inserts a key into the HashSet
.
It is an asynchronous method returning an impl Future
for the caller to await or poll.
Errors
Returns an error along with the supplied key if the key exists.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
let future_insert = hashset.insert_async(11);
sourcepub fn remove<Q>(&self, key_ref: &Q) -> Option<K> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub fn remove<Q>(&self, key_ref: &Q) -> Option<K> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
Removes a key if the key exists.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
assert!(hashset.remove(&1).is_none());
assert!(hashset.insert(1).is_ok());
assert_eq!(hashset.remove(&1).unwrap(), 1);
sourcepub async fn remove_async<Q>(&self, key_ref: &Q) -> Option<K> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub async fn remove_async<Q>(&self, key_ref: &Q) -> Option<K> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
Removes a key if the key exists.
It is an asynchronous method returning an impl Future
for the caller to await or poll.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
let future_insert = hashset.insert_async(11);
let future_remove = hashset.remove_async(&11);
sourcepub fn remove_if<Q, F: FnMut() -> bool>(
&self,
key_ref: &Q,
condition: F
) -> Option<K> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub fn remove_if<Q, F: FnMut() -> bool>(
&self,
key_ref: &Q,
condition: F
) -> Option<K> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
Removes a key if the key exists and the given condition is met.
The key is locked while evaluating the condition.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
assert!(hashset.insert(1).is_ok());
assert!(hashset.remove_if(&1, || false).is_none());
assert_eq!(hashset.remove_if(&1, || true).unwrap(), 1);
sourcepub async fn remove_if_async<Q, F: FnMut() -> bool>(
&self,
key_ref: &Q,
condition: F
) -> Option<K> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub async fn remove_if_async<Q, F: FnMut() -> bool>(
&self,
key_ref: &Q,
condition: F
) -> Option<K> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
Removes a key if the key exists and the given condition is met.
It is an asynchronous method returning an impl Future
for the caller to await or poll.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
let future_insert = hashset.insert_async(11);
let future_remove = hashset.remove_if_async(&11, || true);
sourcepub fn read<Q, R, F: FnMut(&K) -> R>(&self, key_ref: &Q, reader: F) -> Option<R> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub fn read<Q, R, F: FnMut(&K) -> R>(&self, key_ref: &Q, reader: F) -> Option<R> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
Reads a key.
It returns None
if the key does not exist.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
assert!(hashset.read(&1, |_| true).is_none());
assert!(hashset.insert(1).is_ok());
assert!(hashset.read(&1, |_| true).unwrap());
sourcepub async fn read_async<Q, R, F: FnMut(&K) -> R>(
&self,
key_ref: &Q,
reader: F
) -> Option<R> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub async fn read_async<Q, R, F: FnMut(&K) -> R>(
&self,
key_ref: &Q,
reader: F
) -> Option<R> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
Reads a key.
It returns None
if the key does not exist. It is an asynchronous method returning an
impl Future
for the caller to await or poll.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
let future_insert = hashset.insert_async(11);
let future_read = hashset.read_async(&11, |k| *k);
sourcepub fn read_with<'b, Q, R, F: FnMut(&'b K) -> R>(
&self,
key_ref: &Q,
reader: F,
barrier: &'b Barrier
) -> Option<R> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub fn read_with<'b, Q, R, F: FnMut(&'b K) -> R>(
&self,
key_ref: &Q,
reader: F,
barrier: &'b Barrier
) -> Option<R> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
Reads a key using the supplied Barrier
.
It returns None
if the key does not exist.
Examples
use scc::ebr::Barrier;
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
assert!(hashset.insert(1).is_ok());
let barrier = Barrier::new();
let key_ref = hashset.read_with(&1, |k| k, &barrier).unwrap();
assert_eq!(*key_ref, 1);
sourcepub fn contains<Q>(&self, key: &Q) -> bool where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub fn contains<Q>(&self, key: &Q) -> bool where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
Checks if the key exists.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
assert!(!hashset.contains(&1));
assert!(hashset.insert(1).is_ok());
assert!(hashset.contains(&1));
sourcepub async fn contains_async<Q>(&self, key: &Q) -> bool where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub async fn contains_async<Q>(&self, key: &Q) -> bool where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
Checks if the key exists.
It is an asynchronous method returning an impl Future
for the caller to await or poll.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
let future_contains = hashset.contains_async(&1);
sourcepub fn scan<F: FnMut(&K)>(&self, scanner: F)
pub fn scan<F: FnMut(&K)>(&self, scanner: F)
Scans all the keys.
Keys that have existed since the invocation of the method are guaranteed to be visited if
they are not removed, however the same key can be visited more than once if the HashSet
gets resized by another thread.
Examples
use scc::HashSet;
let hashset: HashSet<usize> = HashSet::default();
assert!(hashset.insert(1).is_ok());
assert!(hashset.insert(2).is_ok());
let mut sum = 0;
hashset.scan(|k| { sum += *k; });
assert_eq!(sum, 3);
sourcepub async fn scan_async<F: FnMut(&K)>(&self, scanner: F)
pub async fn scan_async<F: FnMut(&K)>(&self, scanner: F)
Scans all the keys.
Keys that have existed since the invocation of the method are guaranteed to be visited if
they are not removed, however the same key can be visited more than once if the HashSet
gets resized by another task.
Examples
use scc::HashSet;
let hashset: HashSet<usize> = HashSet::default();
let future_insert = hashset.insert_async(1);
let future_retain = hashset.scan_async(|k| println!("{k}"));
sourcepub fn for_each<F: FnMut(&K)>(&self, f: F)
pub fn for_each<F: FnMut(&K)>(&self, f: F)
Iterates over all the keys in the HashSet
.
Keys that have existed since the invocation of the method are guaranteed to be visited if
they are not removed, however the same key can be visited more than once if the HashSet
gets resized by another thread.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
assert!(hashset.insert(1).is_ok());
assert!(hashset.insert(2).is_ok());
let mut acc = 0;
hashset.for_each(|k| { acc += *k; });
assert_eq!(acc, 3);
sourcepub async fn for_each_async<F: FnMut(&K)>(&self, f: F)
pub async fn for_each_async<F: FnMut(&K)>(&self, f: F)
Iterates over all the keys in the HashSet
.
Keys that have existed since the invocation of the method are guaranteed to be visited if
they are not removed, however the same key can be visited more than once if the HashSet
gets resized by another task.
It is an asynchronous method returning an impl Future
for the caller to await or poll.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
let future_insert = hashset.insert_async(1);
let future_for_each = hashset.for_each_async(|k| println!("{}", k));
sourcepub fn retain<F: FnMut(&K) -> bool>(&self, filter: F) -> (usize, usize)
pub fn retain<F: FnMut(&K) -> bool>(&self, filter: F) -> (usize, usize)
Retains keys that satisfy the given predicate.
Keys that have existed since the invocation of the method are guaranteed to be visited if
they are not removed, however the same key can be visited more than once if the HashSet
gets resized by another thread.
It returns the number of keys remaining and removed.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
assert!(hashset.insert(1).is_ok());
assert!(hashset.insert(2).is_ok());
assert!(hashset.insert(3).is_ok());
assert_eq!(hashset.retain(|k| *k == 1), (1, 2));
sourcepub async fn retain_async<F: FnMut(&K) -> bool>(
&self,
filter: F
) -> (usize, usize)
pub async fn retain_async<F: FnMut(&K) -> bool>(
&self,
filter: F
) -> (usize, usize)
Retains keys that satisfy the given predicate.
Keys that have existed since the invocation of the method are guaranteed to be visited if
they are not removed, however the same key can be visited more than once if the HashSet
gets resized by another task.
It returns the number of entries remaining and removed. It is an asynchronous method
returning an impl Future
for the caller to await or poll.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
let future_insert = hashset.insert_async(1);
let future_retain = hashset.retain_async(|k| *k == 1);
sourcepub fn clear(&self) -> usize
pub fn clear(&self) -> usize
Clears all the keys.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
assert!(hashset.insert(1).is_ok());
assert_eq!(hashset.clear(), 1);
sourcepub async fn clear_async(&self) -> usize
pub async fn clear_async(&self) -> usize
Clears all the keys.
It is an asynchronous method returning an impl Future
for the caller to await or poll.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
let future_insert = hashset.insert_async(1);
let future_clear = hashset.clear_async();
Trait Implementations
sourceimpl<K: 'static + Eq + Hash + Sync> Default for HashSet<K, RandomState>
impl<K: 'static + Eq + Hash + Sync> Default for HashSet<K, RandomState>
sourcefn default() -> Self
fn default() -> Self
Creates a HashSet
with the default parameters.
The default hash builder is RandomState
, and the default capacity is 64
.
Panics
Panics if memory allocation fails.
Examples
use scc::HashSet;
let hashset: HashSet<u64> = HashSet::default();
let result = hashset.capacity();
assert_eq!(result, 64);
Auto Trait Implementations
impl<K, H> RefUnwindSafe for HashSet<K, H> where
H: RefUnwindSafe,
impl<K, H> Send for HashSet<K, H> where
H: Send,
impl<K, H> Sync for HashSet<K, H> where
H: Sync,
impl<K, H> Unpin for HashSet<K, H> where
H: Unpin,
impl<K, H = RandomState> !UnwindSafe for HashSet<K, H>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more