Skip to main content

rkyv/collections/swiss_table/
set.rs

1//! Archived hash set implementation using an archived SwissTable.
2
3use core::{
4    borrow::Borrow,
5    fmt,
6    hash::{Hash, Hasher},
7};
8
9use munge::munge;
10use rancor::{Fallible, Source};
11
12use crate::{
13    collections::swiss_table::map::{ArchivedHashMap, HashMapResolver, Keys},
14    hash::FxHasher64,
15    ser::{Allocator, Writer},
16    Place, Portable, Serialize,
17};
18
19/// An archived `HashSet`. This is a wrapper around a hash map with the same key
20/// and unit value.
21#[derive(Portable)]
22#[rkyv(crate)]
23#[cfg_attr(feature = "bytecheck", derive(bytecheck::CheckBytes))]
24#[repr(transparent)]
25pub struct ArchivedHashSet<K, H = FxHasher64> {
26    inner: ArchivedHashMap<K, (), H>,
27}
28
29impl<K, H> ArchivedHashSet<K, H> {
30    /// Gets the number of items in the hash set.
31    pub const fn len(&self) -> usize {
32        self.inner.len()
33    }
34
35    /// Returns whether there are no items in the hash set.
36    pub const fn is_empty(&self) -> bool {
37        self.inner.is_empty()
38    }
39
40    /// Gets an iterator over the keys of the underlying hash map.
41    pub fn iter(&self) -> Keys<'_, K, (), H> {
42        self.inner.keys()
43    }
44}
45
46impl<K, H: Hasher + Default> ArchivedHashSet<K, H> {
47    /// Gets the key corresponding to the given key in the hash set.
48    pub fn get<Q>(&self, k: &Q) -> Option<&K>
49    where
50        K: Borrow<Q>,
51        Q: Hash + Eq + ?Sized,
52    {
53        self.inner.get_key_value(k).map(|(k, _)| k)
54    }
55
56    /// Returns whether the given key is in the hash set.
57    pub fn contains<Q>(&self, k: &Q) -> bool
58    where
59        K: Borrow<Q>,
60        Q: Hash + Eq + ?Sized,
61    {
62        self.inner.contains_key(k)
63    }
64
65    /// Resolves an archived hash set from the given length and parameters.
66    pub fn resolve_from_len(
67        len: usize,
68        load_factor: (usize, usize),
69        resolver: HashSetResolver,
70        out: Place<Self>,
71    ) {
72        munge!(let ArchivedHashSet { inner } = out);
73        ArchivedHashMap::resolve_from_len(len, load_factor, resolver.0, inner);
74    }
75
76    /// Serializes an iterator of keys as a hash set.
77    pub fn serialize_from_iter<I, KU, S>(
78        iter: I,
79        load_factor: (usize, usize),
80        serializer: &mut S,
81    ) -> Result<HashSetResolver, S::Error>
82    where
83        I: Clone + ExactSizeIterator,
84        I::Item: Borrow<KU>,
85        KU: Serialize<S, Archived = K> + Hash + Eq,
86        S: Fallible + Writer + Allocator + ?Sized,
87        S::Error: Source,
88    {
89        Ok(HashSetResolver(
90            ArchivedHashMap::<K, (), H>::serialize_from_iter::<
91                _,
92                _,
93                (),
94                _,
95                _,
96                _,
97            >(iter.map(|x| (x, ())), load_factor, serializer)?,
98        ))
99    }
100}
101
102impl<K: fmt::Debug, H> fmt::Debug for ArchivedHashSet<K, H> {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        f.debug_set().entries(self.iter()).finish()
105    }
106}
107
108impl<K: Hash + Eq, H: Hasher + Default> PartialEq for ArchivedHashSet<K, H> {
109    fn eq(&self, other: &Self) -> bool {
110        self.inner == other.inner
111    }
112}
113
114impl<K: Hash + Eq, H: Hasher + Default> Eq for ArchivedHashSet<K, H> {}
115
116/// The resolver for archived hash sets.
117pub struct HashSetResolver(HashMapResolver);