diff options
| author | bors <bors@rust-lang.org> | 2024-01-20 17:53:26 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-01-20 17:53:26 +0000 |
| commit | 1828461982f37d1a0053d156cedad5522b0f8a97 (patch) | |
| tree | cd2c231427d6baccf46bcd9c6958cc8bdc3b2ee1 | |
| parent | 159bdc1e9313d63ed97ae79fd7c6037393f3ab88 (diff) | |
| parent | 7d369f98301047ca66d761549b53bde33b45ab7d (diff) | |
| download | rust-1828461982f37d1a0053d156cedad5522b0f8a97.tar.gz rust-1828461982f37d1a0053d156cedad5522b0f8a97.zip | |
Auto merge of #117756 - a1phyr:hashmap_fold, r=the8472
`HashMap`/`HashSet`: forward `fold` implementations of iterators Use [rust-lang/hasbrown#480](https://github.com/rust-lang/hashbrown/pull/480) in `std` Note: this needs a version bump of hashbrown before merging
| -rw-r--r-- | library/std/src/collections/hash/map.rs | 104 | ||||
| -rw-r--r-- | library/std/src/collections/hash/set.rs | 70 |
2 files changed, 174 insertions, 0 deletions
diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 39e94902cfe..fc27b6a67bf 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -2232,6 +2232,18 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> { fn size_hint(&self) -> (usize, Option<usize>) { self.base.size_hint() } + #[inline] + fn count(self) -> usize { + self.base.len() + } + #[inline] + fn fold<B, F>(self, init: B, f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.base.fold(init, f) + } } #[stable(feature = "rust1", since = "1.0.0")] impl<K, V> ExactSizeIterator for Iter<'_, K, V> { @@ -2256,6 +2268,18 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> { fn size_hint(&self) -> (usize, Option<usize>) { self.base.size_hint() } + #[inline] + fn count(self) -> usize { + self.base.len() + } + #[inline] + fn fold<B, F>(self, init: B, f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.base.fold(init, f) + } } #[stable(feature = "rust1", since = "1.0.0")] impl<K, V> ExactSizeIterator for IterMut<'_, K, V> { @@ -2290,6 +2314,18 @@ impl<K, V> Iterator for IntoIter<K, V> { fn size_hint(&self) -> (usize, Option<usize>) { self.base.size_hint() } + #[inline] + fn count(self) -> usize { + self.base.len() + } + #[inline] + fn fold<B, F>(self, init: B, f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.base.fold(init, f) + } } #[stable(feature = "rust1", since = "1.0.0")] impl<K, V> ExactSizeIterator for IntoIter<K, V> { @@ -2320,6 +2356,18 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> { fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() } + #[inline] + fn count(self) -> usize { + self.inner.len() + } + #[inline] + fn fold<B, F>(self, init: B, mut f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.inner.fold(init, |acc, (k, _)| f(acc, k)) + } } #[stable(feature = "rust1", since = "1.0.0")] impl<K, V> ExactSizeIterator for Keys<'_, K, V> { @@ -2343,6 +2391,18 @@ impl<'a, K, V> Iterator for Values<'a, K, V> { fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() } + #[inline] + fn count(self) -> usize { + self.inner.len() + } + #[inline] + fn fold<B, F>(self, init: B, mut f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.inner.fold(init, |acc, (_, v)| f(acc, v)) + } } #[stable(feature = "rust1", since = "1.0.0")] impl<K, V> ExactSizeIterator for Values<'_, K, V> { @@ -2366,6 +2426,18 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> { fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() } + #[inline] + fn count(self) -> usize { + self.inner.len() + } + #[inline] + fn fold<B, F>(self, init: B, mut f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.inner.fold(init, |acc, (_, v)| f(acc, v)) + } } #[stable(feature = "map_values_mut", since = "1.10.0")] impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> { @@ -2396,6 +2468,18 @@ impl<K, V> Iterator for IntoKeys<K, V> { fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() } + #[inline] + fn count(self) -> usize { + self.inner.len() + } + #[inline] + fn fold<B, F>(self, init: B, mut f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.inner.fold(init, |acc, (k, _)| f(acc, k)) + } } #[stable(feature = "map_into_keys_values", since = "1.54.0")] impl<K, V> ExactSizeIterator for IntoKeys<K, V> { @@ -2426,6 +2510,18 @@ impl<K, V> Iterator for IntoValues<K, V> { fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() } + #[inline] + fn count(self) -> usize { + self.inner.len() + } + #[inline] + fn fold<B, F>(self, init: B, mut f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.inner.fold(init, |acc, (_, v)| f(acc, v)) + } } #[stable(feature = "map_into_keys_values", since = "1.54.0")] impl<K, V> ExactSizeIterator for IntoValues<K, V> { @@ -2456,6 +2552,14 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> { fn size_hint(&self) -> (usize, Option<usize>) { self.base.size_hint() } + #[inline] + fn fold<B, F>(self, init: B, f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.base.fold(init, f) + } } #[stable(feature = "drain", since = "1.6.0")] impl<K, V> ExactSizeIterator for Drain<'_, K, V> { diff --git a/library/std/src/collections/hash/set.rs b/library/std/src/collections/hash/set.rs index 8bc59608290..dcb2fa0f771 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -1500,6 +1500,18 @@ impl<'a, K> Iterator for Iter<'a, K> { fn size_hint(&self) -> (usize, Option<usize>) { self.base.size_hint() } + #[inline] + fn count(self) -> usize { + self.base.len() + } + #[inline] + fn fold<B, F>(self, init: B, f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.base.fold(init, f) + } } #[stable(feature = "rust1", since = "1.0.0")] impl<K> ExactSizeIterator for Iter<'_, K> { @@ -1530,6 +1542,18 @@ impl<K> Iterator for IntoIter<K> { fn size_hint(&self) -> (usize, Option<usize>) { self.base.size_hint() } + #[inline] + fn count(self) -> usize { + self.base.len() + } + #[inline] + fn fold<B, F>(self, init: B, f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.base.fold(init, f) + } } #[stable(feature = "rust1", since = "1.0.0")] impl<K> ExactSizeIterator for IntoIter<K> { @@ -1560,6 +1584,14 @@ impl<'a, K> Iterator for Drain<'a, K> { fn size_hint(&self) -> (usize, Option<usize>) { self.base.size_hint() } + #[inline] + fn fold<B, F>(self, init: B, f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.base.fold(init, f) + } } #[stable(feature = "rust1", since = "1.0.0")] impl<K> ExactSizeIterator for Drain<'_, K> { @@ -1639,6 +1671,15 @@ where let (_, upper) = self.iter.size_hint(); (0, upper) } + + #[inline] + fn fold<B, F>(self, init: B, mut f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.iter.fold(init, |acc, elt| if self.other.contains(elt) { f(acc, elt) } else { acc }) + } } #[stable(feature = "std_debug", since = "1.16.0")] @@ -1691,6 +1732,15 @@ where let (_, upper) = self.iter.size_hint(); (0, upper) } + + #[inline] + fn fold<B, F>(self, init: B, mut f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.iter.fold(init, |acc, elt| if self.other.contains(elt) { acc } else { f(acc, elt) }) + } } #[stable(feature = "fused", since = "1.26.0")] @@ -1736,6 +1786,14 @@ where fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } + #[inline] + fn fold<B, F>(self, init: B, f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.iter.fold(init, f) + } } #[stable(feature = "fused", since = "1.26.0")] @@ -1800,6 +1858,18 @@ where fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } + #[inline] + fn count(self) -> usize { + self.iter.count() + } + #[inline] + fn fold<B, F>(self, init: B, f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.iter.fold(init, f) + } } #[allow(dead_code)] |
