composable_indexes/index/generic/
keyset.rs

1use crate::Key;
2#[cfg(feature = "imbl")]
3use crate::ShallowClone;
4
5pub type DefaultKeySet = alloc::collections::BTreeSet<Key>;
6
7#[cfg(feature = "imbl")]
8pub type DefaultImmutableKeySet = imbl::OrdSet<Key>;
9
10pub trait KeySet: Default {
11    type Iter<'a>: Iterator<Item = Key>
12    where
13        Self: 'a;
14
15    fn insert(&mut self, key: Key);
16    fn remove(&mut self, key: &Key);
17    fn contains(&self, key: &Key) -> bool;
18    fn iter(&self) -> Self::Iter<'_>;
19    fn is_empty(&self) -> bool;
20    fn count(&self) -> usize;
21}
22
23impl KeySet for alloc::collections::BTreeSet<Key> {
24    type Iter<'a>
25        = core::iter::Copied<alloc::collections::btree_set::Iter<'a, Key>>
26    where
27        Self: 'a;
28
29    fn insert(&mut self, key: Key) {
30        alloc::collections::BTreeSet::insert(self, key);
31    }
32
33    fn remove(&mut self, key: &Key) {
34        alloc::collections::BTreeSet::remove(self, key);
35    }
36
37    fn contains(&self, key: &Key) -> bool {
38        alloc::collections::BTreeSet::contains(self, key)
39    }
40
41    fn iter(&self) -> Self::Iter<'_> {
42        alloc::collections::BTreeSet::iter(self).copied()
43    }
44
45    fn is_empty(&self) -> bool {
46        alloc::collections::BTreeSet::is_empty(self)
47    }
48
49    fn count(&self) -> usize {
50        alloc::collections::BTreeSet::len(self)
51    }
52}
53
54impl<S: core::hash::BuildHasher + Default> KeySet for hashbrown::HashSet<Key, S> {
55    type Iter<'a>
56        = core::iter::Copied<hashbrown::hash_set::Iter<'a, Key>>
57    where
58        Self: 'a;
59
60    fn insert(&mut self, key: Key) {
61        hashbrown::HashSet::insert(self, key);
62    }
63
64    fn remove(&mut self, key: &Key) {
65        hashbrown::HashSet::remove(self, key);
66    }
67
68    fn contains(&self, key: &Key) -> bool {
69        hashbrown::HashSet::contains(self, key)
70    }
71
72    fn iter(&self) -> Self::Iter<'_> {
73        hashbrown::HashSet::iter(self).copied()
74    }
75
76    fn is_empty(&self) -> bool {
77        hashbrown::HashSet::is_empty(self)
78    }
79
80    fn count(&self) -> usize {
81        hashbrown::HashSet::len(self)
82    }
83}
84
85#[cfg(feature = "std")]
86impl<S: core::hash::BuildHasher + Default> KeySet for std::collections::HashSet<Key, S> {
87    type Iter<'a>
88        = core::iter::Copied<std::collections::hash_set::Iter<'a, Key>>
89    where
90        Self: 'a;
91
92    fn insert(&mut self, key: Key) {
93        std::collections::HashSet::insert(self, key);
94    }
95
96    fn remove(&mut self, key: &Key) {
97        std::collections::HashSet::remove(self, key);
98    }
99
100    fn contains(&self, key: &Key) -> bool {
101        std::collections::HashSet::contains(self, key)
102    }
103
104    fn iter(&self) -> Self::Iter<'_> {
105        std::collections::HashSet::iter(self).copied()
106    }
107
108    fn is_empty(&self) -> bool {
109        std::collections::HashSet::is_empty(self)
110    }
111
112    fn count(&self) -> usize {
113        std::collections::HashSet::len(self)
114    }
115}
116
117#[cfg(feature = "imbl")]
118impl KeySet for imbl::OrdSet<Key> {
119    type Iter<'a>
120        = core::iter::Copied<imbl::ordset::Iter<'a, Key, imbl::shared_ptr::DefaultSharedPtr>>
121    where
122        Self: 'a;
123
124    fn insert(&mut self, key: Key) {
125        imbl::OrdSet::insert(self, key);
126    }
127
128    fn remove(&mut self, key: &Key) {
129        imbl::OrdSet::remove(self, key);
130    }
131
132    fn contains(&self, key: &Key) -> bool {
133        imbl::OrdSet::contains(self, key)
134    }
135
136    fn iter(&self) -> Self::Iter<'_> {
137        imbl::OrdSet::iter(self).copied()
138    }
139
140    fn is_empty(&self) -> bool {
141        imbl::OrdSet::is_empty(self)
142    }
143
144    fn count(&self) -> usize {
145        imbl::OrdSet::len(self)
146    }
147}
148
149#[cfg(feature = "imbl")]
150impl ShallowClone for imbl::OrdSet<Key> {}
151
152#[cfg(feature = "imbl")]
153impl KeySet for imbl::HashSet<Key> {
154    type Iter<'a>
155        = core::iter::Copied<imbl::hashset::Iter<'a, Key, imbl::shared_ptr::DefaultSharedPtr>>
156    where
157        Self: 'a;
158
159    fn insert(&mut self, key: Key) {
160        imbl::HashSet::insert(self, key);
161    }
162
163    fn remove(&mut self, key: &Key) {
164        imbl::HashSet::remove(self, key);
165    }
166
167    fn contains(&self, key: &Key) -> bool {
168        imbl::HashSet::contains(self, key)
169    }
170
171    fn iter(&self) -> Self::Iter<'_> {
172        imbl::HashSet::iter(self).copied()
173    }
174
175    fn is_empty(&self) -> bool {
176        imbl::HashSet::is_empty(self)
177    }
178
179    fn count(&self) -> usize {
180        imbl::HashSet::len(self)
181    }
182}
183
184#[cfg(feature = "imbl")]
185impl ShallowClone for imbl::HashSet<Key> {}
186
187#[cfg(feature = "roaring")]
188impl KeySet for roaring::RoaringTreemap {
189    type Iter<'a>
190        = RoaringIter<'a>
191    where
192        Self: 'a;
193
194    fn insert(&mut self, key: Key) {
195        roaring::RoaringTreemap::insert(self, key.as_u64());
196    }
197
198    fn remove(&mut self, key: &Key) {
199        roaring::RoaringTreemap::remove(self, key.as_u64());
200    }
201
202    fn contains(&self, key: &Key) -> bool {
203        roaring::RoaringTreemap::contains(self, key.as_u64())
204    }
205
206    fn iter(&self) -> Self::Iter<'_> {
207        RoaringIter {
208            inner: roaring::RoaringTreemap::iter(self),
209        }
210    }
211
212    fn is_empty(&self) -> bool {
213        roaring::RoaringTreemap::is_empty(self)
214    }
215
216    fn count(&self) -> usize {
217        roaring::RoaringTreemap::len(self) as usize
218    }
219}
220
221#[cfg(feature = "roaring")]
222pub struct RoaringIter<'a> {
223    inner: roaring::treemap::Iter<'a>,
224}
225
226#[cfg(feature = "roaring")]
227impl<'a> Iterator for RoaringIter<'a> {
228    type Item = Key;
229    fn next(&mut self) -> Option<Self::Item> {
230        self.inner.next().map(Key::unsafe_from_u64)
231    }
232}