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-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);
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-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);
sourcepub fn remove_if<Q, F: FnOnce() -> bool>(
&self,
key_ref: &Q,
condition: F
) -> Option<K> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub fn remove_if<Q, F: FnOnce() -> bool>(
&self,
key_ref: &Q,
condition: F
) -> Option<K> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
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);
sourcepub fn read<Q, R, F: FnOnce(&K) -> R>(
&self,
key_ref: &Q,
reader: F
) -> Option<R> where
K: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub fn read<Q, R, F: FnOnce(&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 fn read_with<'b, Q, R, F: FnOnce(&'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: FnOnce(&'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 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);
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 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.
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 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);
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