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

Scalable concurrent hash set data structure.

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

Removes a key-value pair 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-value pair 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);

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 using the supplied Barrier.

It enables the caller to use the value reference outside the method. 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));

Iterates over all the keys in the HashSet.

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

Retains keys that satisfy the given predicate.

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

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

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.