1use super::size_policy::*;
5use super::traits::*;
6use super::*;
7
8pub use super::WeakWeakHashMap;
9
10#[allow(clippy::exhaustive_enums)]
12pub enum Entry<'a, K: 'a + WeakKey, V: 'a + WeakElement> {
13 Occupied(OccupiedEntry<'a, K, V>),
15 Vacant(VacantEntry<'a, K, V>),
17}
18
19pub struct OccupiedEntry<'a, K: 'a + WeakKey, V: 'a + WeakElement>(
21 inner::OccupiedEntry<'a, inner::WeakK<K>, inner::WeakV<V>>,
22);
23
24pub struct VacantEntry<'a, K: 'a + WeakKey, V: 'a + WeakElement>(
26 inner::VacantEntry<'a, inner::WeakK<K>, inner::WeakV<V>>,
27);
28
29#[derive(Clone, Debug)]
31pub struct Iter<'a, K: 'a, V: 'a>(inner::Iter<'a, inner::WeakK<K>, inner::WeakV<V>>);
32
33impl<'a, K: WeakElement, V: WeakElement> Iterator for Iter<'a, K, V> {
34 type Item = (K::Strong, V::Strong);
35
36 fn next(&mut self) -> Option<Self::Item> {
37 self.0.next()
38 }
39
40 fn size_hint(&self) -> (usize, Option<usize>) {
41 self.0.size_hint()
42 }
43}
44
45#[derive(Clone, Debug)]
47pub struct Keys<'a, K: 'a, V: 'a>(Iter<'a, K, V>);
48
49impl<'a, K: WeakElement, V: WeakElement> Iterator for Keys<'a, K, V> {
50 type Item = K::Strong;
51
52 fn next(&mut self) -> Option<Self::Item> {
53 self.0.next().map(|(k, _)| k)
54 }
55
56 fn size_hint(&self) -> (usize, Option<usize>) {
57 self.0.size_hint()
58 }
59}
60
61#[derive(Clone, Debug)]
63pub struct Values<'a, K: 'a, V: 'a>(Iter<'a, K, V>);
64
65impl<'a, K: WeakElement, V: WeakElement> Iterator for Values<'a, K, V> {
66 type Item = V::Strong;
67
68 fn next(&mut self) -> Option<Self::Item> {
69 self.0.next().map(|(_, v)| v)
70 }
71
72 fn size_hint(&self) -> (usize, Option<usize>) {
73 self.0.size_hint()
74 }
75}
76
77#[derive(Debug)]
78pub struct Drain<'a, K: 'a, V: 'a>(inner::Drain<'a, inner::WeakK<K>, inner::WeakV<V>>);
83
84impl<'a, K: WeakElement, V: WeakElement> Iterator for Drain<'a, K, V> {
85 type Item = (K::Strong, V::Strong);
86
87 fn next(&mut self) -> Option<Self::Item> {
88 self.0.next()
89 }
90
91 fn size_hint(&self) -> (usize, Option<usize>) {
92 self.0.size_hint()
93 }
94}
95
96pub struct IntoIter<K, V>(inner::IntoIter<inner::WeakK<K>, inner::WeakV<V>>);
98
99impl<K: WeakElement, V: WeakElement> Iterator for IntoIter<K, V> {
100 type Item = (K::Strong, V::Strong);
101
102 fn next(&mut self) -> Option<Self::Item> {
103 self.0.next()
104 }
105
106 fn size_hint(&self) -> (usize, Option<usize>) {
107 self.0.size_hint()
108 }
109}
110
111impl<K: WeakKey, V: WeakElement> WeakWeakHashMap<K, V, RandomState> {
112 pub fn new() -> Self {
116 Self::with_capacity(DEFAULT_INITIAL_CAPACITY)
117 }
118
119 pub fn with_capacity(capacity: usize) -> Self {
123 Self::with_capacity_and_hasher(capacity, Default::default())
124 }
125}
126
127impl<K: WeakKey, V: WeakElement, S: BuildHasher> WeakWeakHashMap<K, V, S> {
128 pub fn with_hasher(hash_builder: S) -> Self {
132 Self::with_capacity_and_hasher(DEFAULT_INITIAL_CAPACITY, hash_builder)
133 }
134
135 pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
139 WeakWeakHashMap(inner::Table::new(capacity, hash_builder))
140 }
141
142 pub fn hasher(&self) -> &S {
146 self.0.hasher()
147 }
148
149 pub fn capacity(&self) -> usize {
153 self.0.capacity()
154 }
155
156 pub fn remove_expired(&mut self) {
160 self.retain(|_, _| true);
161 }
162
163 pub fn reserve(&mut self, additional_capacity: usize) {
170 self.0
171 .try_reserve(additional_capacity)
172 .expect("try_reserve failed");
173 }
174
175 pub fn shrink_to_fit(&mut self) {
179 self.0.shrink_to_fit();
180 }
181
182 pub fn len(&self) -> usize {
189 self.0.len()
190 }
191
192 pub fn is_empty(&self) -> bool {
199 self.len() == 0
200 }
201
202 pub fn load_factor(&self) -> f32 {
208 (self.len() as f32 + 1.0) / self.capacity() as f32
209 }
210
211 pub fn entry(&mut self, key: K::Strong) -> Entry<'_, K, V> {
217 match self.0.entry(key) {
218 inner::Entry::Occupied(occupied) => Entry::Occupied(OccupiedEntry(occupied)),
219 inner::Entry::Vacant(vacant) => Entry::Vacant(VacantEntry(vacant)),
220 }
221 }
222
223 pub fn clear(&mut self) {
227 self.0.clear();
228 }
229
230 pub fn get<Q>(&self, key: &Q) -> Option<V::Strong>
236 where
237 Q: ?Sized + Hash + Eq,
238 K::Key: Borrow<Q>,
239 {
240 Some(self.0.find(key)?.1)
241 }
242
243 pub fn get_key<Q>(&self, key: &Q) -> Option<K::Strong>
247 where
248 Q: ?Sized + Hash + Eq,
249 K::Key: Borrow<Q>,
250 {
251 Some(self.0.find(key)?.0)
252 }
253
254 pub fn get_both<Q>(&self, key: &Q) -> Option<(K::Strong, V::Strong)>
258 where
259 Q: ?Sized + Hash + Eq,
260 K::Key: Borrow<Q>,
261 {
262 self.0.find(key)
263 }
264
265 pub fn contains_key<Q>(&self, key: &Q) -> bool
269 where
270 Q: ?Sized + Hash + Eq,
271 K::Key: Borrow<Q>,
272 {
273 self.0.find(key).is_some()
274 }
275
276 pub fn insert(&mut self, key: K::Strong, value: V::Strong) -> Option<V::Strong> {
282 match self.entry(key) {
283 Entry::Occupied(mut occupied) => Some(occupied.insert(value)),
284 Entry::Vacant(vacant) => {
285 vacant.insert(value);
286 None
287 }
288 }
289 }
290
291 pub fn remove<Q>(&mut self, key: &Q) -> Option<V::Strong>
295 where
296 Q: ?Sized + Hash + Eq,
297 K::Key: Borrow<Q>,
298 {
299 self.0.find_entry(key).map(|occupied| occupied.remove().1)
300 }
301
302 pub fn retain<F>(&mut self, mut f: F)
308 where
309 F: FnMut(K::Strong, V::Strong) -> bool,
310 {
311 self.0.table.retain(|(k, v)| {
314 if let (Some(k), Some(v)) = (k.val.view(), v.val.view()) {
315 f(k, v)
316 } else {
317 false
318 }
319 });
320 }
321
322 pub fn is_submap_with<F, S1, V1>(
331 &self,
332 other: &WeakWeakHashMap<K, V1, S1>,
333 mut value_equal: F,
334 ) -> bool
335 where
336 V1: WeakElement,
337 F: FnMut(V::Strong, V1::Strong) -> bool,
338 S1: BuildHasher,
339 {
340 for (key, value1) in self {
341 if let Some(value2) = K::with_key(&key, |k| other.get(k)) {
342 if !value_equal(value1, value2) {
343 return false;
344 }
345 } else {
346 return false;
347 }
348 }
349
350 true
351 }
352
353 pub fn is_submap<V1, S1>(&self, other: &WeakWeakHashMap<K, V1, S1>) -> bool
359 where
360 V1: WeakElement,
361 V::Strong: PartialEq<V1::Strong>,
362 S1: BuildHasher,
363 {
364 self.is_submap_with(other, |v, v1| v == v1)
365 }
366
367 pub fn domain_is_subset<V1, S1>(&self, other: &WeakWeakHashMap<K, V1, S1>) -> bool
373 where
374 V1: WeakElement,
375 S1: BuildHasher,
376 {
377 self.is_submap_with(other, |_, _| true)
378 }
379}
380
381impl<K, V, V1, S, S1> PartialEq<WeakWeakHashMap<K, V1, S1>> for WeakWeakHashMap<K, V, S>
382where
383 K: WeakKey,
384 V: WeakElement,
385 V1: WeakElement,
386 V::Strong: PartialEq<V1::Strong>,
387 S: BuildHasher,
388 S1: BuildHasher,
389{
390 fn eq(&self, other: &WeakWeakHashMap<K, V1, S1>) -> bool {
391 self.is_submap(other) && other.domain_is_subset(self)
392 }
393}
394
395impl<K: WeakKey, V: WeakElement, S: BuildHasher> Eq for WeakWeakHashMap<K, V, S> where V::Strong: Eq {}
396
397impl<K: WeakKey, V: WeakElement, S: BuildHasher + Default> Default for WeakWeakHashMap<K, V, S> {
398 fn default() -> Self {
399 WeakWeakHashMap::with_hasher(Default::default())
400 }
401}
402
403impl<K, V, S> iter::FromIterator<(K::Strong, V::Strong)> for WeakWeakHashMap<K, V, S>
404where
405 K: WeakKey,
406 V: WeakElement,
407 S: BuildHasher + Default,
408{
409 fn from_iter<T: IntoIterator<Item = (K::Strong, V::Strong)>>(iter: T) -> Self {
410 let iter = iter.into_iter();
411 let min_size = iter.size_hint().0;
412 let mut result = WeakWeakHashMap::with_capacity_and_hasher(min_size, Default::default());
413 result.extend(iter);
414 result
415 }
416}
417
418impl<K, V, S> Extend<(K::Strong, V::Strong)> for WeakWeakHashMap<K, V, S>
419where
420 K: WeakKey,
421 V: WeakElement,
422 S: BuildHasher,
423{
424 fn extend<T: IntoIterator<Item = (K::Strong, V::Strong)>>(&mut self, iter: T) {
425 let iter = iter.into_iter();
426 let min_size = iter.size_hint().0;
427 self.reserve(min_size);
428 for (key, value) in iter {
429 self.insert(key, value);
430 }
431 }
432}
433
434impl<'a, K: WeakKey, V: WeakElement> Entry<'a, K, V> {
435 pub fn or_insert(self, default: V::Strong) -> V::Strong {
441 self.or_insert_with(|| default)
442 }
443
444 pub fn or_insert_with<F: FnOnce() -> V::Strong>(self, default: F) -> V::Strong {
450 match self {
451 Entry::Occupied(occupied) => occupied.get_strong(),
452 Entry::Vacant(vacant) => vacant.insert(default()),
453 }
454 }
455
456 pub fn key(&self) -> &K::Strong {
460 match *self {
461 Entry::Occupied(ref occupied) => occupied.key(),
462 Entry::Vacant(ref vacant) => vacant.key(),
463 }
464 }
465}
466
467impl<'a, K: WeakKey, V: WeakElement> OccupiedEntry<'a, K, V> {
468 pub fn key(&self) -> &K::Strong {
472 self.0.get().0
473 }
474
475 pub fn remove_entry(self) -> (K::Strong, V::Strong) {
479 self.0.remove()
480 }
481
482 pub fn get(&self) -> &V::Strong {
486 self.0.get().1
487 }
488
489 pub fn get_strong(&self) -> V::Strong {
493 V::clone(self.get())
494 }
495
496 pub fn insert(&mut self, value: V::Strong) -> V::Strong {
502 self.0.insert(value)
503 }
504
505 pub fn remove(self) -> V::Strong {
509 self.remove_entry().1
510 }
511}
512
513impl<'a, K: WeakKey, V: WeakElement> VacantEntry<'a, K, V> {
514 pub fn key(&self) -> &K::Strong {
519 self.0.key()
520 }
521
522 pub fn into_key(self) -> K::Strong {
526 self.0.into_key()
527 }
528
529 pub fn insert(self, value: V::Strong) -> V::Strong {
533 V::clone(self.0.insert(value).get().1)
534 }
535}
536
537impl<K: WeakElement, V: WeakElement, S> Debug for WeakWeakHashMap<K, V, S>
538where
539 K::Strong: Debug,
540 V::Strong: Debug,
541{
542 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
543 f.debug_map().entries(self.iter()).finish()
544 }
545}
546
547impl<'a, K: WeakKey, V: WeakElement> Debug for Entry<'a, K, V>
548where
549 K::Strong: Debug,
550 V::Strong: Debug,
551{
552 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
553 match *self {
554 Entry::Occupied(ref e) => e.fmt(f),
555 Entry::Vacant(ref e) => e.fmt(f),
556 }
557 }
558}
559
560impl<'a, K: WeakKey, V: WeakElement> Debug for OccupiedEntry<'a, K, V>
561where
562 K::Strong: Debug,
563 V::Strong: Debug,
564{
565 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
566 self.0.fmt(f)
567 }
568}
569
570impl<'a, K: WeakKey, V: WeakElement> Debug for VacantEntry<'a, K, V>
571where
572 K::Strong: Debug,
573 V::Strong: Debug,
574{
575 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
576 self.0.fmt(f)
577 }
578}
579
580impl<K: WeakElement, V: WeakElement, S> IntoIterator for WeakWeakHashMap<K, V, S> {
581 type Item = (K::Strong, V::Strong);
582 type IntoIter = IntoIter<K, V>;
583
584 fn into_iter(self) -> Self::IntoIter {
588 IntoIter(self.0.into_iter())
589 }
590}
591
592impl<'a, K: WeakElement, V: WeakElement, S> IntoIterator for &'a WeakWeakHashMap<K, V, S> {
593 type Item = (K::Strong, V::Strong);
594 type IntoIter = Iter<'a, K, V>;
595
596 fn into_iter(self) -> Self::IntoIter {
600 Iter(self.0.iter())
601 }
602}
603
604impl<K: WeakElement, V: WeakElement, S> WeakWeakHashMap<K, V, S> {
605 pub fn iter(&self) -> Iter<'_, K, V> {
609 self.into_iter()
610 }
611
612 pub fn keys(&self) -> Keys<'_, K, V> {
616 Keys(self.iter())
617 }
618
619 pub fn values(&self) -> Values<'_, K, V> {
623 Values(self.iter())
624 }
625
626 pub fn drain(&mut self) -> Drain<'_, K, V> {
630 Drain(self.0.drain())
631 }
632}