From 6318cbbe49f916c33bc3f4e5febe6cf0fcb7095a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20du=20Garreau?= Date: Thu, 9 Nov 2023 16:21:03 +0100 Subject: [PATCH 1/2] `HashMap`/`HashSet`: forward `fold` implementations of iterators --- library/std/src/collections/hash/map.rs | 72 +++++++++++++++++++++++++ library/std/src/collections/hash/set.rs | 58 ++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 39e94902cfe5f..b83c4d89c1e83 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -2232,6 +2232,14 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> { fn size_hint(&self) -> (usize, Option) { self.base.size_hint() } + #[inline] + fn fold(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 ExactSizeIterator for Iter<'_, K, V> { @@ -2256,6 +2264,14 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> { fn size_hint(&self) -> (usize, Option) { self.base.size_hint() } + #[inline] + fn fold(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 ExactSizeIterator for IterMut<'_, K, V> { @@ -2290,6 +2306,14 @@ impl Iterator for IntoIter { fn size_hint(&self) -> (usize, Option) { self.base.size_hint() } + #[inline] + fn fold(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 ExactSizeIterator for IntoIter { @@ -2320,6 +2344,14 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> { fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } + #[inline] + fn fold(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 ExactSizeIterator for Keys<'_, K, V> { @@ -2343,6 +2375,14 @@ impl<'a, K, V> Iterator for Values<'a, K, V> { fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } + #[inline] + fn fold(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 ExactSizeIterator for Values<'_, K, V> { @@ -2366,6 +2406,14 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> { fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } + #[inline] + fn fold(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 ExactSizeIterator for ValuesMut<'_, K, V> { @@ -2396,6 +2444,14 @@ impl Iterator for IntoKeys { fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } + #[inline] + fn fold(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 ExactSizeIterator for IntoKeys { @@ -2426,6 +2482,14 @@ impl Iterator for IntoValues { fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } + #[inline] + fn fold(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 ExactSizeIterator for IntoValues { @@ -2456,6 +2520,14 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> { fn size_hint(&self) -> (usize, Option) { self.base.size_hint() } + #[inline] + fn fold(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 ExactSizeIterator for Drain<'_, K, V> { diff --git a/library/std/src/collections/hash/set.rs b/library/std/src/collections/hash/set.rs index 8bc5960829066..ea9239f0c4764 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -1500,6 +1500,14 @@ impl<'a, K> Iterator for Iter<'a, K> { fn size_hint(&self) -> (usize, Option) { self.base.size_hint() } + #[inline] + fn fold(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 ExactSizeIterator for Iter<'_, K> { @@ -1530,6 +1538,14 @@ impl Iterator for IntoIter { fn size_hint(&self) -> (usize, Option) { self.base.size_hint() } + #[inline] + fn fold(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 ExactSizeIterator for IntoIter { @@ -1560,6 +1576,14 @@ impl<'a, K> Iterator for Drain<'a, K> { fn size_hint(&self) -> (usize, Option) { self.base.size_hint() } + #[inline] + fn fold(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 ExactSizeIterator for Drain<'_, K> { @@ -1639,6 +1663,15 @@ where let (_, upper) = self.iter.size_hint(); (0, upper) } + + #[inline] + fn fold(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 +1724,15 @@ where let (_, upper) = self.iter.size_hint(); (0, upper) } + + #[inline] + fn fold(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 +1778,14 @@ where fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } + #[inline] + fn fold(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 +1850,14 @@ where fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } + #[inline] + fn fold(self, init: B, f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.iter.fold(init, f) + } } #[allow(dead_code)] From 7d369f98301047ca66d761549b53bde33b45ab7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20du=20Garreau?= Date: Mon, 27 Nov 2023 10:30:12 +0100 Subject: [PATCH 2/2] Specialize `count` too --- library/std/src/collections/hash/map.rs | 32 +++++++++++++++++++++++++ library/std/src/collections/hash/set.rs | 12 ++++++++++ 2 files changed, 44 insertions(+) diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index b83c4d89c1e83..fc27b6a67bfcb 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -2233,6 +2233,10 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> { self.base.size_hint() } #[inline] + fn count(self) -> usize { + self.base.len() + } + #[inline] fn fold(self, init: B, f: F) -> B where Self: Sized, @@ -2265,6 +2269,10 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> { self.base.size_hint() } #[inline] + fn count(self) -> usize { + self.base.len() + } + #[inline] fn fold(self, init: B, f: F) -> B where Self: Sized, @@ -2307,6 +2315,10 @@ impl Iterator for IntoIter { self.base.size_hint() } #[inline] + fn count(self) -> usize { + self.base.len() + } + #[inline] fn fold(self, init: B, f: F) -> B where Self: Sized, @@ -2345,6 +2357,10 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> { self.inner.size_hint() } #[inline] + fn count(self) -> usize { + self.inner.len() + } + #[inline] fn fold(self, init: B, mut f: F) -> B where Self: Sized, @@ -2376,6 +2392,10 @@ impl<'a, K, V> Iterator for Values<'a, K, V> { self.inner.size_hint() } #[inline] + fn count(self) -> usize { + self.inner.len() + } + #[inline] fn fold(self, init: B, mut f: F) -> B where Self: Sized, @@ -2407,6 +2427,10 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> { self.inner.size_hint() } #[inline] + fn count(self) -> usize { + self.inner.len() + } + #[inline] fn fold(self, init: B, mut f: F) -> B where Self: Sized, @@ -2445,6 +2469,10 @@ impl Iterator for IntoKeys { self.inner.size_hint() } #[inline] + fn count(self) -> usize { + self.inner.len() + } + #[inline] fn fold(self, init: B, mut f: F) -> B where Self: Sized, @@ -2483,6 +2511,10 @@ impl Iterator for IntoValues { self.inner.size_hint() } #[inline] + fn count(self) -> usize { + self.inner.len() + } + #[inline] fn fold(self, init: B, mut f: F) -> B where Self: Sized, diff --git a/library/std/src/collections/hash/set.rs b/library/std/src/collections/hash/set.rs index ea9239f0c4764..dcb2fa0f771b0 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -1501,6 +1501,10 @@ impl<'a, K> Iterator for Iter<'a, K> { self.base.size_hint() } #[inline] + fn count(self) -> usize { + self.base.len() + } + #[inline] fn fold(self, init: B, f: F) -> B where Self: Sized, @@ -1539,6 +1543,10 @@ impl Iterator for IntoIter { self.base.size_hint() } #[inline] + fn count(self) -> usize { + self.base.len() + } + #[inline] fn fold(self, init: B, f: F) -> B where Self: Sized, @@ -1851,6 +1859,10 @@ where self.iter.size_hint() } #[inline] + fn count(self) -> usize { + self.iter.count() + } + #[inline] fn fold(self, init: B, f: F) -> B where Self: Sized,