1use core::borrow::Borrow;
6use core::fmt;
7
8use kevy_hash::KevyHash;
9
10use crate::iter::Iter;
11use crate::map::KevyMap;
12
13#[derive(Clone)]
20pub struct KevySet<K>(KevyMap<K, ()>);
21
22impl<K> KevySet<K> {
23 pub fn new() -> Self {
25 Self(KevyMap::new())
26 }
27
28 pub fn with_capacity(cap_hint: usize) -> Self {
30 Self(KevyMap::with_capacity(cap_hint))
31 }
32
33 #[inline]
35 pub fn len(&self) -> usize {
36 self.0.len()
37 }
38 #[inline]
40 pub fn is_empty(&self) -> bool {
41 self.0.is_empty()
42 }
43 #[inline]
45 pub fn capacity(&self) -> usize {
46 self.0.capacity()
47 }
48 pub fn clear(&mut self) {
50 self.0.clear();
51 }
52
53 pub fn iter(&self) -> SetIter<'_, K> {
55 SetIter(self.0.iter())
56 }
57
58 pub fn iter_from_slot(&self, start: usize) -> impl Iterator<Item = &K> {
71 let cap = self.0.capacity();
72 let start = if cap == 0 { 0 } else { start % cap };
73 self.0.iter_from_bucket(start).chain(self.0.iter().take(start)).map(|(k, ())| k)
74 }
75
76 pub fn as_map(&self) -> &KevyMap<K, ()> {
78 &self.0
79 }
80}
81
82impl<K: KevyHash + Eq> KevySet<K> {
83 pub fn insert(&mut self, key: K) -> bool {
86 self.0.insert(key, ()).is_none()
87 }
88}
89
90impl<K> KevySet<K> {
91 pub fn contains<Q>(&self, key: &Q) -> bool
93 where
94 K: Borrow<Q>,
95 Q: KevyHash + Eq + ?Sized,
96 {
97 self.0.contains_key(key)
98 }
99
100 pub fn remove<Q>(&mut self, key: &Q) -> bool
102 where
103 K: Borrow<Q>,
104 Q: KevyHash + Eq + ?Sized,
105 {
106 self.0.remove(key).is_some()
107 }
108}
109
110impl<K> Default for KevySet<K> {
111 fn default() -> Self {
112 Self::new()
113 }
114}
115
116impl<K: fmt::Debug> fmt::Debug for KevySet<K> {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118 f.debug_set().entries(self.iter()).finish()
119 }
120}
121
122pub struct SetIter<'a, K>(Iter<'a, K, ()>);
124
125impl<'a, K> Iterator for SetIter<'a, K> {
126 type Item = &'a K;
127 fn next(&mut self) -> Option<Self::Item> {
128 self.0.next().map(|(k, ())| k)
129 }
130}
131
132impl<'a, K> IntoIterator for &'a KevySet<K> {
133 type Item = &'a K;
134 type IntoIter = SetIter<'a, K>;
135 fn into_iter(self) -> Self::IntoIter {
136 self.iter()
137 }
138}
139
140impl<K: KevyHash + Eq> FromIterator<K> for KevySet<K> {
141 fn from_iter<I: IntoIterator<Item = K>>(iter: I) -> Self {
142 let iter = iter.into_iter();
143 let mut s = match iter.size_hint() {
144 (lo, Some(hi)) if hi <= lo.saturating_mul(2) => Self::with_capacity(hi),
145 (lo, _) => Self::with_capacity(lo),
146 };
147 for k in iter {
148 s.insert(k);
149 }
150 s
151 }
152}
153
154impl<K: KevyHash + Eq> Extend<K> for KevySet<K> {
155 fn extend<I: IntoIterator<Item = K>>(&mut self, iter: I) {
156 for k in iter {
157 self.insert(k);
158 }
159 }
160}