Skip to content

Commit 1828461

Browse files
committed
Auto merge of rust-lang#117756 - a1phyr:hashmap_fold, r=the8472
`HashMap`/`HashSet`: forward `fold` implementations of iterators Use [rust-lang/hasbrown#480](rust-lang/hashbrown#480) in `std` Note: this needs a version bump of hashbrown before merging
2 parents 159bdc1 + 7d369f9 commit 1828461

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

Diff for: library/std/src/collections/hash/map.rs

+104
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,18 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
22322232
fn size_hint(&self) -> (usize, Option<usize>) {
22332233
self.base.size_hint()
22342234
}
2235+
#[inline]
2236+
fn count(self) -> usize {
2237+
self.base.len()
2238+
}
2239+
#[inline]
2240+
fn fold<B, F>(self, init: B, f: F) -> B
2241+
where
2242+
Self: Sized,
2243+
F: FnMut(B, Self::Item) -> B,
2244+
{
2245+
self.base.fold(init, f)
2246+
}
22352247
}
22362248
#[stable(feature = "rust1", since = "1.0.0")]
22372249
impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
@@ -2256,6 +2268,18 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
22562268
fn size_hint(&self) -> (usize, Option<usize>) {
22572269
self.base.size_hint()
22582270
}
2271+
#[inline]
2272+
fn count(self) -> usize {
2273+
self.base.len()
2274+
}
2275+
#[inline]
2276+
fn fold<B, F>(self, init: B, f: F) -> B
2277+
where
2278+
Self: Sized,
2279+
F: FnMut(B, Self::Item) -> B,
2280+
{
2281+
self.base.fold(init, f)
2282+
}
22592283
}
22602284
#[stable(feature = "rust1", since = "1.0.0")]
22612285
impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
@@ -2290,6 +2314,18 @@ impl<K, V> Iterator for IntoIter<K, V> {
22902314
fn size_hint(&self) -> (usize, Option<usize>) {
22912315
self.base.size_hint()
22922316
}
2317+
#[inline]
2318+
fn count(self) -> usize {
2319+
self.base.len()
2320+
}
2321+
#[inline]
2322+
fn fold<B, F>(self, init: B, f: F) -> B
2323+
where
2324+
Self: Sized,
2325+
F: FnMut(B, Self::Item) -> B,
2326+
{
2327+
self.base.fold(init, f)
2328+
}
22932329
}
22942330
#[stable(feature = "rust1", since = "1.0.0")]
22952331
impl<K, V> ExactSizeIterator for IntoIter<K, V> {
@@ -2320,6 +2356,18 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
23202356
fn size_hint(&self) -> (usize, Option<usize>) {
23212357
self.inner.size_hint()
23222358
}
2359+
#[inline]
2360+
fn count(self) -> usize {
2361+
self.inner.len()
2362+
}
2363+
#[inline]
2364+
fn fold<B, F>(self, init: B, mut f: F) -> B
2365+
where
2366+
Self: Sized,
2367+
F: FnMut(B, Self::Item) -> B,
2368+
{
2369+
self.inner.fold(init, |acc, (k, _)| f(acc, k))
2370+
}
23232371
}
23242372
#[stable(feature = "rust1", since = "1.0.0")]
23252373
impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
@@ -2343,6 +2391,18 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
23432391
fn size_hint(&self) -> (usize, Option<usize>) {
23442392
self.inner.size_hint()
23452393
}
2394+
#[inline]
2395+
fn count(self) -> usize {
2396+
self.inner.len()
2397+
}
2398+
#[inline]
2399+
fn fold<B, F>(self, init: B, mut f: F) -> B
2400+
where
2401+
Self: Sized,
2402+
F: FnMut(B, Self::Item) -> B,
2403+
{
2404+
self.inner.fold(init, |acc, (_, v)| f(acc, v))
2405+
}
23462406
}
23472407
#[stable(feature = "rust1", since = "1.0.0")]
23482408
impl<K, V> ExactSizeIterator for Values<'_, K, V> {
@@ -2366,6 +2426,18 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
23662426
fn size_hint(&self) -> (usize, Option<usize>) {
23672427
self.inner.size_hint()
23682428
}
2429+
#[inline]
2430+
fn count(self) -> usize {
2431+
self.inner.len()
2432+
}
2433+
#[inline]
2434+
fn fold<B, F>(self, init: B, mut f: F) -> B
2435+
where
2436+
Self: Sized,
2437+
F: FnMut(B, Self::Item) -> B,
2438+
{
2439+
self.inner.fold(init, |acc, (_, v)| f(acc, v))
2440+
}
23692441
}
23702442
#[stable(feature = "map_values_mut", since = "1.10.0")]
23712443
impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
@@ -2396,6 +2468,18 @@ impl<K, V> Iterator for IntoKeys<K, V> {
23962468
fn size_hint(&self) -> (usize, Option<usize>) {
23972469
self.inner.size_hint()
23982470
}
2471+
#[inline]
2472+
fn count(self) -> usize {
2473+
self.inner.len()
2474+
}
2475+
#[inline]
2476+
fn fold<B, F>(self, init: B, mut f: F) -> B
2477+
where
2478+
Self: Sized,
2479+
F: FnMut(B, Self::Item) -> B,
2480+
{
2481+
self.inner.fold(init, |acc, (k, _)| f(acc, k))
2482+
}
23992483
}
24002484
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
24012485
impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
@@ -2426,6 +2510,18 @@ impl<K, V> Iterator for IntoValues<K, V> {
24262510
fn size_hint(&self) -> (usize, Option<usize>) {
24272511
self.inner.size_hint()
24282512
}
2513+
#[inline]
2514+
fn count(self) -> usize {
2515+
self.inner.len()
2516+
}
2517+
#[inline]
2518+
fn fold<B, F>(self, init: B, mut f: F) -> B
2519+
where
2520+
Self: Sized,
2521+
F: FnMut(B, Self::Item) -> B,
2522+
{
2523+
self.inner.fold(init, |acc, (_, v)| f(acc, v))
2524+
}
24292525
}
24302526
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
24312527
impl<K, V> ExactSizeIterator for IntoValues<K, V> {
@@ -2456,6 +2552,14 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> {
24562552
fn size_hint(&self) -> (usize, Option<usize>) {
24572553
self.base.size_hint()
24582554
}
2555+
#[inline]
2556+
fn fold<B, F>(self, init: B, f: F) -> B
2557+
where
2558+
Self: Sized,
2559+
F: FnMut(B, Self::Item) -> B,
2560+
{
2561+
self.base.fold(init, f)
2562+
}
24592563
}
24602564
#[stable(feature = "drain", since = "1.6.0")]
24612565
impl<K, V> ExactSizeIterator for Drain<'_, K, V> {

Diff for: library/std/src/collections/hash/set.rs

+70
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,18 @@ impl<'a, K> Iterator for Iter<'a, K> {
15001500
fn size_hint(&self) -> (usize, Option<usize>) {
15011501
self.base.size_hint()
15021502
}
1503+
#[inline]
1504+
fn count(self) -> usize {
1505+
self.base.len()
1506+
}
1507+
#[inline]
1508+
fn fold<B, F>(self, init: B, f: F) -> B
1509+
where
1510+
Self: Sized,
1511+
F: FnMut(B, Self::Item) -> B,
1512+
{
1513+
self.base.fold(init, f)
1514+
}
15031515
}
15041516
#[stable(feature = "rust1", since = "1.0.0")]
15051517
impl<K> ExactSizeIterator for Iter<'_, K> {
@@ -1530,6 +1542,18 @@ impl<K> Iterator for IntoIter<K> {
15301542
fn size_hint(&self) -> (usize, Option<usize>) {
15311543
self.base.size_hint()
15321544
}
1545+
#[inline]
1546+
fn count(self) -> usize {
1547+
self.base.len()
1548+
}
1549+
#[inline]
1550+
fn fold<B, F>(self, init: B, f: F) -> B
1551+
where
1552+
Self: Sized,
1553+
F: FnMut(B, Self::Item) -> B,
1554+
{
1555+
self.base.fold(init, f)
1556+
}
15331557
}
15341558
#[stable(feature = "rust1", since = "1.0.0")]
15351559
impl<K> ExactSizeIterator for IntoIter<K> {
@@ -1560,6 +1584,14 @@ impl<'a, K> Iterator for Drain<'a, K> {
15601584
fn size_hint(&self) -> (usize, Option<usize>) {
15611585
self.base.size_hint()
15621586
}
1587+
#[inline]
1588+
fn fold<B, F>(self, init: B, f: F) -> B
1589+
where
1590+
Self: Sized,
1591+
F: FnMut(B, Self::Item) -> B,
1592+
{
1593+
self.base.fold(init, f)
1594+
}
15631595
}
15641596
#[stable(feature = "rust1", since = "1.0.0")]
15651597
impl<K> ExactSizeIterator for Drain<'_, K> {
@@ -1639,6 +1671,15 @@ where
16391671
let (_, upper) = self.iter.size_hint();
16401672
(0, upper)
16411673
}
1674+
1675+
#[inline]
1676+
fn fold<B, F>(self, init: B, mut f: F) -> B
1677+
where
1678+
Self: Sized,
1679+
F: FnMut(B, Self::Item) -> B,
1680+
{
1681+
self.iter.fold(init, |acc, elt| if self.other.contains(elt) { f(acc, elt) } else { acc })
1682+
}
16421683
}
16431684

16441685
#[stable(feature = "std_debug", since = "1.16.0")]
@@ -1691,6 +1732,15 @@ where
16911732
let (_, upper) = self.iter.size_hint();
16921733
(0, upper)
16931734
}
1735+
1736+
#[inline]
1737+
fn fold<B, F>(self, init: B, mut f: F) -> B
1738+
where
1739+
Self: Sized,
1740+
F: FnMut(B, Self::Item) -> B,
1741+
{
1742+
self.iter.fold(init, |acc, elt| if self.other.contains(elt) { acc } else { f(acc, elt) })
1743+
}
16941744
}
16951745

16961746
#[stable(feature = "fused", since = "1.26.0")]
@@ -1736,6 +1786,14 @@ where
17361786
fn size_hint(&self) -> (usize, Option<usize>) {
17371787
self.iter.size_hint()
17381788
}
1789+
#[inline]
1790+
fn fold<B, F>(self, init: B, f: F) -> B
1791+
where
1792+
Self: Sized,
1793+
F: FnMut(B, Self::Item) -> B,
1794+
{
1795+
self.iter.fold(init, f)
1796+
}
17391797
}
17401798

17411799
#[stable(feature = "fused", since = "1.26.0")]
@@ -1800,6 +1858,18 @@ where
18001858
fn size_hint(&self) -> (usize, Option<usize>) {
18011859
self.iter.size_hint()
18021860
}
1861+
#[inline]
1862+
fn count(self) -> usize {
1863+
self.iter.count()
1864+
}
1865+
#[inline]
1866+
fn fold<B, F>(self, init: B, f: F) -> B
1867+
where
1868+
Self: Sized,
1869+
F: FnMut(B, Self::Item) -> B,
1870+
{
1871+
self.iter.fold(init, f)
1872+
}
18031873
}
18041874

18051875
#[allow(dead_code)]

0 commit comments

Comments
 (0)