1use core::alloc::Allocator;
23use core::hash::{BuildHasher, Hash};
24
25use hashbrown::{HashMap, HashSet, hash_map};
26
27use super::{
28 Collection, CollectionAlloc, CollectionBase, CollectionCapacity, CollectionEntry,
29 CollectionMap, CollectionSet, MapEntry,
30};
31
32impl<T: Eq + Hash, S: BuildHasher, A: Allocator> CollectionBase for HashSet<T, S, A> {
33 type Item = T;
34
35 fn item_count(&self) -> usize {
36 HashSet::len(self)
37 }
38
39 fn push(&mut self, elem: Self::Item) {
40 HashSet::insert(self, elem);
41 }
42
43 fn clear(&mut self) {
44 HashSet::clear(self)
45 }
46}
47
48impl<T: Eq + Hash, S: BuildHasher + Default> Collection for HashSet<T, S> {
49 fn with_capacity(capacity: usize) -> Self {
50 HashSet::with_capacity_and_hasher(capacity, S::default())
51 }
52}
53
54impl<T: Eq + Hash, S: BuildHasher, A: Allocator> CollectionSet for HashSet<T, S, A> {
55 fn contains(&self, item: &Self::Item) -> bool {
56 HashSet::contains(self, item)
57 }
58
59 fn get(&self, item: &Self::Item) -> Option<&Self::Item> {
60 HashSet::get(self, item)
61 }
62
63 fn iter(&self) -> impl Iterator<Item = &Self::Item> {
64 HashSet::iter(self)
65 }
66
67 fn insert(&mut self, item: Self::Item) -> bool {
68 HashSet::insert(self, item)
69 }
70
71 fn replace(&mut self, item: Self::Item) -> Option<Self::Item> {
72 HashSet::replace(self, item)
73 }
74
75 fn remove(&mut self, item: &Self::Item) -> bool {
76 HashSet::remove(self, item)
77 }
78
79 fn take(&mut self, item: &Self::Item) -> Option<Self::Item> {
80 HashSet::take(self, item)
81 }
82
83 fn retain(&mut self, f: impl FnMut(&Self::Item) -> bool) {
84 HashSet::retain(self, f)
85 }
86}
87
88impl<T: Eq + Hash, S: BuildHasher, A: Allocator> CollectionCapacity for HashSet<T, S, A> {
89 type ReserveError = hashbrown::TryReserveError;
90
91 fn capacity(&self) -> usize {
92 HashSet::capacity(self)
93 }
94
95 fn reserve(&mut self, additional: usize) {
96 HashSet::reserve(self, additional)
97 }
98
99 fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError> {
100 HashSet::try_reserve(self, additional)
101 }
102}
103
104impl<T: Eq + Hash, S: BuildHasher + Default, A: Allocator> CollectionAlloc for HashSet<T, S, A> {
105 type Allocator = A;
106
107 fn with_capacity_in(capacity: usize, allocator: Self::Allocator) -> Self {
108 HashSet::with_capacity_and_hasher_in(capacity, S::default(), allocator)
109 }
110
111 fn allocator(&self) -> &Self::Allocator {
112 HashSet::allocator(self)
113 }
114}
115
116impl<K: Eq + Hash, V, S: BuildHasher, A: Allocator> CollectionBase for HashMap<K, V, S, A> {
117 type Item = (K, V);
118
119 fn item_count(&self) -> usize {
120 HashMap::len(self)
121 }
122
123 fn push(&mut self, elem: Self::Item) {
124 HashMap::insert(self, elem.0, elem.1);
125 }
126
127 fn clear(&mut self) {
128 HashMap::clear(self)
129 }
130}
131
132impl<K: Eq + Hash, V, S: BuildHasher + Default> Collection for HashMap<K, V, S> {
133 fn with_capacity(capacity: usize) -> Self {
134 HashMap::with_capacity_and_hasher(capacity, S::default())
135 }
136}
137
138impl<K: Eq + Hash, V, S: BuildHasher, A: Allocator> CollectionMap for HashMap<K, V, S, A> {
139 type Key = K;
140 type Value = V;
141
142 fn contains_key(&self, key: &Self::Key) -> bool {
143 HashMap::contains_key(self, key)
144 }
145
146 fn get(&self, key: &Self::Key) -> Option<&Self::Value> {
147 HashMap::get(self, key)
148 }
149
150 fn get_mut(&mut self, key: &Self::Key) -> Option<&mut Self::Value> {
151 HashMap::get_mut(self, key)
152 }
153
154 fn iter(&self) -> impl Iterator<Item = (&Self::Key, &Self::Value)> {
155 HashMap::iter(self)
156 }
157
158 fn keys(&self) -> impl Iterator<Item = &Self::Key> {
159 HashMap::keys(self)
160 }
161
162 fn values(&self) -> impl Iterator<Item = &Self::Value> {
163 HashMap::values(self)
164 }
165
166 fn iter_mut(&mut self) -> impl Iterator<Item = (&Self::Key, &mut Self::Value)> {
167 HashMap::iter_mut(self)
168 }
169
170 fn values_mut(&mut self) -> impl Iterator<Item = &mut Self::Value> {
171 HashMap::values_mut(self)
172 }
173
174 fn insert(&mut self, key: Self::Key, value: Self::Value) -> Option<Self::Value> {
175 HashMap::insert(self, key, value)
176 }
177
178 fn remove(&mut self, key: &Self::Key) -> Option<Self::Value> {
179 HashMap::remove(self, key)
180 }
181
182 fn retain(&mut self, f: impl FnMut(&K, &mut V) -> bool) {
183 HashMap::retain(self, f)
184 }
185}
186
187impl<K: Eq + Hash, V, S: BuildHasher, A: Allocator> CollectionCapacity for HashMap<K, V, S, A> {
188 type ReserveError = hashbrown::TryReserveError;
189
190 fn capacity(&self) -> usize {
191 HashMap::capacity(self)
192 }
193
194 fn reserve(&mut self, additional: usize) {
195 HashMap::reserve(self, additional)
196 }
197
198 fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError> {
199 HashMap::try_reserve(self, additional)
200 }
201}
202
203impl<K: Eq + Hash, V, S: BuildHasher + Default, A: Allocator> CollectionAlloc
204 for HashMap<K, V, S, A>
205{
206 type Allocator = A;
207
208 fn with_capacity_in(capacity: usize, allocator: Self::Allocator) -> Self {
209 HashMap::with_capacity_and_hasher_in(capacity, S::default(), allocator)
210 }
211
212 fn allocator(&self) -> &Self::Allocator {
213 HashMap::allocator(self)
214 }
215}
216
217impl<K: Eq + Hash, V, S: BuildHasher, A: Allocator> CollectionEntry for HashMap<K, V, S, A> {
218 type Entry<'a>
219 = hash_map::Entry<'a, K, V, S, A>
220 where
221 K: 'a,
222 V: 'a,
223 S: 'a,
224 A: 'a;
225
226 fn entry(&mut self, key: Self::Key) -> Self::Entry<'_> {
227 HashMap::entry(self, key)
228 }
229}
230
231impl<'a, K: Hash + 'a, V: 'a, S: BuildHasher + 'a, A: Allocator + 'a> MapEntry<'a>
232 for hash_map::Entry<'a, K, V, S, A>
233{
234 type Key = K;
235 type Value = V;
236
237 fn key(&self) -> &Self::Key {
238 hash_map::Entry::key(self)
239 }
240
241 fn or_insert(self, default: Self::Value) -> &'a mut Self::Value {
242 hash_map::Entry::or_insert(self, default)
243 }
244
245 fn or_insert_with<F: FnOnce() -> Self::Value>(self, default: F) -> &'a mut Self::Value {
246 hash_map::Entry::or_insert_with(self, default)
247 }
248
249 fn or_default(self) -> &'a mut Self::Value
250 where
251 Self::Value: Default,
252 {
253 hash_map::Entry::or_default(self)
254 }
255
256 fn and_modify<F: FnOnce(&mut Self::Value)>(self, f: F) -> Self {
257 hash_map::Entry::and_modify(self, f)
258 }
259}