nekolib/ds/
cuckoo_hash_set.rs1use super::cuckoo_hash_map;
4
5use std::hash::Hash;
6use std::iter::FromIterator;
7
8use cuckoo_hash_map::CuckooHashMap;
9
10#[derive(Clone, Debug)]
12pub struct CuckooHashSet<K>(CuckooHashMap<K, ()>);
13
14impl<K: Eq + Hash> CuckooHashSet<K> {
15 pub fn new() -> Self { Self(CuckooHashMap::new()) }
16 pub fn contains(&self, key: &K) -> bool { self.0.contains_key(key) }
17 pub fn insert(&mut self, key: K) -> bool {
18 self.0.insert(key, ()).is_none()
19 }
20 pub fn remove(&mut self, key: &K) -> bool { self.0.remove(key).is_some() }
21 pub fn len(&self) -> usize { self.0.len() }
22 pub fn is_empty(&self) -> bool { self.0.is_empty() }
23}
24
25impl<K: Eq + Hash> FromIterator<K> for CuckooHashSet<K> {
26 fn from_iter<I>(iter: I) -> Self
27 where
28 I: IntoIterator<Item = K>,
29 {
30 let mut res = CuckooHashSet::new();
31 for k in iter {
32 res.insert(k);
33 }
34 res
35 }
36}
37
38impl<K: Eq + Hash> Extend<K> for CuckooHashSet<K> {
39 fn extend<I>(&mut self, iter: I)
40 where
41 I: IntoIterator<Item = K>,
42 {
43 for k in iter {
44 self.insert(k);
45 }
46 }
47}