pub struct HashSet<K, H = RandomState> where
    K: 'static + Eq + Hash + Sync,
    H: BuildHasher
{ /* private fields */ }
Expand description

Scalable concurrent hash set.

HashSet is a concurrent hash set based on HashMap. It internally uses a HashMap as its key container, thus sharing all the characteristics of HashMap.

Implementations

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);

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);

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);

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);

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);

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);

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);

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);

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());

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);

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);

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));

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);

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);

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}"));

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);

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));

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));

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);

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);

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();

Returns the number of entries in the HashSet.

It scans the entire array to calculate the number of valid entries, making its time complexity O(N).

Examples
use scc::HashSet;

let hashset: HashSet<u64> = HashSet::default();

assert!(hashset.insert(1).is_ok());
assert_eq!(hashset.len(), 1);

Returns true if the HashSet is empty.

It scans the entire array to calculate the number of valid entries, making its time complexity O(N).

Examples
use scc::HashSet;

let hashset: HashSet<u64> = HashSet::default();

assert!(hashset.is_empty());

Returns the capacity of the HashSet.

Examples
use scc::HashSet;
use std::collections::hash_map::RandomState;

let hashset: HashSet<u64, RandomState> = HashSet::new(1000000, RandomState::new());
assert_eq!(hashset.capacity(), 1048576);

Trait Implementations

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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.