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