1use crate::compat::*;
4
5use super::by_ptr::*;
6use super::traits::*;
7use super::weak_key_hash_map as base;
8
9pub use super::PtrWeakKeyHashMap;
10pub use super::weak_key_hash_map::{Entry, Iter, IterMut, Keys, Values, ValuesMut, Drain, IntoIter};
11
12impl <K: WeakElement, V> PtrWeakKeyHashMap<K, V, RandomState>
13 where K::Strong: Deref
14{
15 pub fn new() -> Self {
19 PtrWeakKeyHashMap(base::WeakKeyHashMap::new())
20 }
21
22 pub fn with_capacity(capacity: usize) -> Self {
26 PtrWeakKeyHashMap(base::WeakKeyHashMap::with_capacity(capacity))
27 }
28}
29
30impl <K: WeakElement, V, S: BuildHasher> PtrWeakKeyHashMap<K, V, S>
31 where K::Strong: Deref
32{
33 pub fn with_hasher(hash_builder: S) -> Self {
37 PtrWeakKeyHashMap(base::WeakKeyHashMap::with_hasher(hash_builder))
38 }
39
40 pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
44 PtrWeakKeyHashMap(base::WeakKeyHashMap::with_capacity_and_hasher(capacity, hash_builder))
45 }
46
47 pub fn hasher(&self) -> &S {
51 self.0.hasher()
52 }
53
54 pub fn capacity(&self) -> usize {
58 self.0.capacity()
59 }
60
61 pub fn remove_expired(&mut self) {
65 self.0.remove_expired()
66 }
67
68 pub fn reserve(&mut self, additional_capacity: usize) {
72 self.0.reserve(additional_capacity)
73 }
74
75 pub fn shrink_to_fit(&mut self) {
79 self.0.shrink_to_fit()
80 }
81
82 pub fn len(&self) -> usize {
86 self.0.len()
87 }
88
89 pub fn is_empty(&self) -> bool {
96 self.len() == 0
97 }
98
99 pub fn load_factor(&self) -> f32 {
105 self.0.load_factor()
106 }
107
108 pub fn entry(&mut self, key: K::Strong) -> Entry<ByPtr<K>, V> {
112 self.0.entry(key)
113 }
114
115 pub fn clear(&mut self) {
119 self.0.clear()
120 }
121
122 pub fn get(&self, key: &K::Strong) -> Option<&V> {
126 self.0.get(&(key.deref() as *const _))
127 }
128
129 pub fn contains_key(&self, key:&K::Strong) -> bool {
133 self.0.contains_key(&(key.deref() as *const _))
134 }
135
136 pub fn get_mut(&mut self, key: &K::Strong) -> Option<&mut V> {
140 self.0.get_mut(&(key.deref() as *const _))
141 }
142
143 pub fn insert(&mut self, key: K::Strong, value: V) -> Option<V> {
148 self.0.insert(key, value)
149 }
150
151 pub fn remove(&mut self, key: &K::Strong) -> Option<V> {
155 self.0.remove(&(key.deref() as *const _))
156 }
157
158 pub fn retain<F>(&mut self, f: F)
164 where F: FnMut(K::Strong, &mut V) -> bool
165 {
166 self.0.retain(f)
167 }
168
169 pub fn submap_with<F, S1, V1>(&self, other: &PtrWeakKeyHashMap<K, V1, S1>, value_equal: F) -> bool
178 where F: FnMut(&V, &V1) -> bool,
179 S1: BuildHasher
180 {
181 self.0.is_submap_with(&other.0, value_equal)
182 }
183
184 pub fn is_submap<V1, S1>(&self, other: &PtrWeakKeyHashMap<K, V1, S1>) -> bool
190 where V: PartialEq<V1>,
191 S1: BuildHasher
192 {
193 self.0.is_submap(&other.0)
194 }
195
196 pub fn domain_is_subset<V1, S1>(&self, other: &PtrWeakKeyHashMap<K, V1, S1>) -> bool
202 where S1: BuildHasher
203 {
204 self.0.domain_is_subset(&other.0)
205 }
206}
207
208impl<K: WeakElement, V, S> PtrWeakKeyHashMap<K, V, S>
209 where K::Strong: Deref
210{
211 pub fn iter(&self) -> Iter<ByPtr<K>, V> {
215 self.0.iter()
216 }
217
218 pub fn keys(&self) -> Keys<ByPtr<K>, V> {
222 self.0.keys()
223 }
224
225 pub fn values(&self) -> Values<ByPtr<K>, V> {
229 self.0.values()
230 }
231
232 pub fn iter_mut(&mut self) -> IterMut<ByPtr<K>, V> {
236 self.0.iter_mut()
237 }
238
239 pub fn values_mut(&mut self) -> ValuesMut<ByPtr<K>, V> {
243 self.0.values_mut()
244 }
245
246 pub fn drain(&mut self) -> Drain<ByPtr<K>, V> {
250 self.0.drain()
251 }
252}
253
254impl<K, V, V1, S, S1> PartialEq<PtrWeakKeyHashMap<K, V1, S1>>
255 for PtrWeakKeyHashMap<K, V, S>
256 where K: WeakElement,
257 K::Strong: Deref,
258 V: PartialEq<V1>,
259 S: BuildHasher,
260 S1: BuildHasher
261{
262 fn eq(&self, other: &PtrWeakKeyHashMap<K, V1, S1>) -> bool {
263 self.0 == other.0
264 }
265}
266
267impl<K: WeakElement, V: Eq, S: BuildHasher> Eq for PtrWeakKeyHashMap<K, V, S>
268 where K::Strong: Deref
269{ }
270
271impl<K: WeakElement, V, S: BuildHasher + Default> Default for PtrWeakKeyHashMap<K, V, S>
272 where K::Strong: Deref
273{
274 fn default() -> Self {
275 PtrWeakKeyHashMap(base::WeakKeyHashMap::<ByPtr<K>, V, S>::default())
276 }
277}
278
279impl<'a, K, V, S> Index<&'a K::Strong> for PtrWeakKeyHashMap<K, V, S>
280 where K: WeakElement,
281 K::Strong: Deref,
282 S: BuildHasher
283{
284 type Output = V;
285
286 fn index(&self, index: &'a K::Strong) -> &Self::Output {
287 self.0.index(&(index.deref() as *const _))
288 }
289}
290
291impl<'a, K, V, S> IndexMut<&'a K::Strong> for PtrWeakKeyHashMap<K, V, S>
292 where
293 K: WeakElement,
294 K::Strong: Deref,
295 S: BuildHasher
296{
297 fn index_mut(&mut self, index: &'a K::Strong) -> &mut Self::Output {
298 self.0.index_mut(&(index.deref() as *const _))
299 }
300}
301
302impl<K, V, S> FromIterator<(K::Strong, V)> for PtrWeakKeyHashMap<K, V, S>
303 where K: WeakElement,
304 K::Strong: Deref,
305 S: BuildHasher + Default
306{
307 fn from_iter<T: IntoIterator<Item=(K::Strong, V)>>(iter: T) -> Self {
308 PtrWeakKeyHashMap(base::WeakKeyHashMap::<ByPtr<K>, V, S>::from_iter(iter))
309 }
310}
311
312impl<K, V, S> Extend<(K::Strong, V)> for PtrWeakKeyHashMap<K, V, S>
313 where K: WeakElement,
314 K::Strong: Deref,
315 S: BuildHasher
316{
317 fn extend<T: IntoIterator<Item=(K::Strong, V)>>(&mut self, iter: T) {
318 self.0.extend(iter)
319 }
320}
321
322impl<'a, K, V, S> Extend<(&'a K::Strong, &'a V)> for PtrWeakKeyHashMap<K, V, S>
323 where K: 'a + WeakElement,
324 K::Strong: Clone + Deref,
325 V: 'a + Clone,
326 S: BuildHasher
327{
328 fn extend<T: IntoIterator<Item=(&'a K::Strong, &'a V)>>(&mut self, iter: T) {
329 self.0.extend(iter)
330 }
331}
332
333impl<K, V: Debug, S> Debug for PtrWeakKeyHashMap<K, V, S>
334 where K: WeakElement,
335 K::Strong: Debug
336{
337 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
338 self.0.fmt(f)
339 }
340}
341
342impl<K: WeakElement, V, S> IntoIterator for PtrWeakKeyHashMap<K, V, S> {
343 type Item = (K::Strong, V);
344 type IntoIter = IntoIter<ByPtr<K>, V>;
345
346 fn into_iter(self) -> Self::IntoIter {
350 self.0.into_iter()
351 }
352}
353
354impl<'a, K: WeakElement, V, S> IntoIterator for &'a PtrWeakKeyHashMap<K, V, S> {
355 type Item = (K::Strong, &'a V);
356 type IntoIter = Iter<'a, ByPtr<K>, V>;
357
358 fn into_iter(self) -> Self::IntoIter {
362 (&self.0).into_iter()
363 }
364}
365
366impl<'a, K: WeakElement, V, S> IntoIterator for &'a mut PtrWeakKeyHashMap<K, V, S> {
367 type Item = (K::Strong, &'a mut V);
368 type IntoIter = IterMut<'a, ByPtr<K>, V>;
369
370 fn into_iter(self) -> Self::IntoIter {
374 (&mut self.0).into_iter()
375 }
376}
377
378#[cfg(test)]
379mod test {
380 use crate::compat::{
381 eprintln,
382 rc::{Rc, Weak},
383 Vec,
384 };
385 use super::{Entry, PtrWeakKeyHashMap};
386
387#[test]
396 fn insert_and_check() {
397 let mut rcs: Vec<Rc<u32>> = Vec::new();
398
399 for i in 0 .. 200 {
400 rcs.push(Rc::new(i));
401 }
402
403 let mut weakmap: PtrWeakKeyHashMap<Weak<u32>, f32> = PtrWeakKeyHashMap::new();
404
405 for item in rcs.iter().cloned() {
406 let f = *item as f32 + 0.1;
407 weakmap.insert(item, f);
408 }
409
410 let mut count = 0;
411
412 for item in &rcs {
413 assert!(weakmap.contains_key(item));
414
415 match weakmap.entry(Rc::clone(item)) {
416 Entry::Occupied(_) => count += 1,
417 Entry::Vacant(_) => eprintln!("PointerWeakKeyHashMap: missing: {}", *item),
418 }
419 }
420
421 assert_eq!( count, rcs.len() );
422 }
423}