Skip to content

Commit c053387

Browse files
committed
Add ExactSizeIterator impls for Hash{Map, Set, Table}
This commit also changes the return types of all `size_hint()` impls in these files from (uint, Option<uint>) to (usize, Option<usize>).
1 parent 099b411 commit c053387

File tree

3 files changed

+98
-30
lines changed

3 files changed

+98
-30
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use cmp::{max, Eq, PartialEq};
2020
use default::Default;
2121
use fmt::{self, Show};
2222
use hash::{self, Hash, SipHasher};
23-
use iter::{self, Iterator, IteratorExt, FromIterator, Extend, Map};
23+
use iter::{self, Iterator, ExactSizeIterator, IteratorExt, FromIterator, Extend, Map};
2424
use marker::Sized;
2525
use mem::{self, replace};
2626
use num::{Int, UnsignedInt};
@@ -1384,53 +1384,71 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
13841384
type Item = (&'a K, &'a V);
13851385

13861386
#[inline] fn next(&mut self) -> Option<(&'a K, &'a V)> { self.inner.next() }
1387-
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
1387+
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1388+
}
1389+
#[stable]
1390+
impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
1391+
#[inline] fn len(&self) -> usize { self.inner.len() }
13881392
}
13891393

13901394
#[stable]
13911395
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
13921396
type Item = (&'a K, &'a mut V);
13931397

13941398
#[inline] fn next(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next() }
1395-
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
1399+
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1400+
}
1401+
#[stable]
1402+
impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
1403+
#[inline] fn len(&self) -> usize { self.inner.len() }
13961404
}
13971405

13981406
#[stable]
13991407
impl<K, V> Iterator for IntoIter<K, V> {
14001408
type Item = (K, V);
14011409

14021410
#[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
1403-
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
1411+
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1412+
}
1413+
#[stable]
1414+
impl<K, V> ExactSizeIterator for IntoIter<K, V> {
1415+
#[inline] fn len(&self) -> usize { self.inner.len() }
14041416
}
14051417

14061418
#[stable]
14071419
impl<'a, K, V> Iterator for Keys<'a, K, V> {
14081420
type Item = &'a K;
14091421

14101422
#[inline] fn next(&mut self) -> Option<(&'a K)> { self.inner.next() }
1411-
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
1423+
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1424+
}
1425+
#[stable]
1426+
impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
1427+
#[inline] fn len(&self) -> usize { self.inner.len() }
14121428
}
14131429

14141430
#[stable]
14151431
impl<'a, K, V> Iterator for Values<'a, K, V> {
14161432
type Item = &'a V;
14171433

14181434
#[inline] fn next(&mut self) -> Option<(&'a V)> { self.inner.next() }
1419-
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
1435+
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1436+
}
1437+
#[stable]
1438+
impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {
1439+
#[inline] fn len(&self) -> usize { self.inner.len() }
14201440
}
14211441

14221442
#[stable]
1423-
impl<'a, K: 'a, V: 'a> Iterator for Drain<'a, K, V> {
1443+
impl<'a, K, V> Iterator for Drain<'a, K, V> {
14241444
type Item = (K, V);
14251445

1426-
#[inline]
1427-
fn next(&mut self) -> Option<(K, V)> {
1428-
self.inner.next()
1429-
}
1430-
#[inline]
1431-
fn size_hint(&self) -> (uint, Option<uint>) {
1432-
self.inner.size_hint()
1433-
}
1446+
#[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
1447+
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1448+
}
1449+
#[stable]
1450+
impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
1451+
#[inline] fn len(&self) -> usize { self.inner.len() }
14341452
}
14351453

14361454
#[unstable = "matches collection reform v2 specification, waiting for dust to settle"]
@@ -2135,6 +2153,19 @@ mod test_map {
21352153
assert_eq!(iter.size_hint(), (3, Some(3)));
21362154
}
21372155

2156+
#[test]
2157+
fn test_iter_len() {
2158+
let xs = [(1i, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2159+
2160+
let map: HashMap<int, int> = xs.iter().map(|&x| x).collect();
2161+
2162+
let mut iter = map.iter();
2163+
2164+
for _ in iter.by_ref().take(3) {}
2165+
2166+
assert_eq!(iter.len(), 3);
2167+
}
2168+
21382169
#[test]
21392170
fn test_mut_size_hint() {
21402171
let xs = [(1i, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
@@ -2148,6 +2179,19 @@ mod test_map {
21482179
assert_eq!(iter.size_hint(), (3, Some(3)));
21492180
}
21502181

2182+
#[test]
2183+
fn test_iter_mut_len() {
2184+
let xs = [(1i, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2185+
2186+
let mut map: HashMap<int, int> = xs.iter().map(|&x| x).collect();
2187+
2188+
let mut iter = map.iter_mut();
2189+
2190+
for _ in iter.by_ref().take(3) {}
2191+
2192+
assert_eq!(iter.len(), 3);
2193+
}
2194+
21512195
#[test]
21522196
fn test_index() {
21532197
let mut map: HashMap<int, int> = HashMap::new();

src/libstd/collections/hash/set.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use default::Default;
1818
use fmt::Show;
1919
use fmt;
2020
use hash::{self, Hash};
21-
use iter::{Iterator, IteratorExt, FromIterator, Map, Chain, Extend};
21+
use iter::{Iterator, ExactSizeIterator, IteratorExt, FromIterator, Map, Chain, Extend};
2222
use ops::{BitOr, BitAnd, BitXor, Sub};
2323
use option::Option::{Some, None, self};
2424

@@ -837,23 +837,35 @@ impl<'a, K> Iterator for Iter<'a, K> {
837837
type Item = &'a K;
838838

839839
fn next(&mut self) -> Option<&'a K> { self.iter.next() }
840-
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
840+
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
841+
}
842+
#[stable]
843+
impl<'a, K> ExactSizeIterator for Iter<'a, K> {
844+
fn len(&self) -> usize { self.iter.len() }
841845
}
842846

843847
#[stable]
844848
impl<K> Iterator for IntoIter<K> {
845849
type Item = K;
846850

847851
fn next(&mut self) -> Option<K> { self.iter.next() }
848-
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
852+
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
853+
}
854+
#[stable]
855+
impl<K> ExactSizeIterator for IntoIter<K> {
856+
fn len(&self) -> usize { self.iter.len() }
849857
}
850858

851859
#[stable]
852-
impl<'a, K: 'a> Iterator for Drain<'a, K> {
860+
impl<'a, K> Iterator for Drain<'a, K> {
853861
type Item = K;
854862

855863
fn next(&mut self) -> Option<K> { self.iter.next() }
856-
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
864+
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
865+
}
866+
#[stable]
867+
impl<'a, K> ExactSizeIterator for Drain<'a, K> {
868+
fn len(&self) -> usize { self.iter.len() }
857869
}
858870

859871
#[stable]
@@ -875,7 +887,7 @@ impl<'a, T, S, H> Iterator for Intersection<'a, T, S>
875887
}
876888
}
877889

878-
fn size_hint(&self) -> (uint, Option<uint>) {
890+
fn size_hint(&self) -> (usize, Option<usize>) {
879891
let (_, upper) = self.iter.size_hint();
880892
(0, upper)
881893
}
@@ -900,7 +912,7 @@ impl<'a, T, S, H> Iterator for Difference<'a, T, S>
900912
}
901913
}
902914

903-
fn size_hint(&self) -> (uint, Option<uint>) {
915+
fn size_hint(&self) -> (usize, Option<usize>) {
904916
let (_, upper) = self.iter.size_hint();
905917
(0, upper)
906918
}
@@ -915,7 +927,7 @@ impl<'a, T, S, H> Iterator for SymmetricDifference<'a, T, S>
915927
type Item = &'a T;
916928

917929
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
918-
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
930+
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
919931
}
920932

921933
#[stable]
@@ -927,7 +939,7 @@ impl<'a, T, S, H> Iterator for Union<'a, T, S>
927939
type Item = &'a T;
928940

929941
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
930-
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
942+
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
931943
}
932944

933945
#[cfg(test)]

src/libstd/collections/hash/table.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use self::BucketState::*;
1515
use clone::Clone;
1616
use cmp;
1717
use hash::{Hash, Hasher};
18-
use iter::{Iterator, count};
18+
use iter::{Iterator, ExactSizeIterator, count};
1919
use marker::{Copy, Sized, self};
2020
use mem::{min_align_of, size_of};
2121
use mem;
@@ -838,10 +838,13 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
838838
})
839839
}
840840

841-
fn size_hint(&self) -> (uint, Option<uint>) {
841+
fn size_hint(&self) -> (usize, Option<usize>) {
842842
(self.elems_left, Some(self.elems_left))
843843
}
844844
}
845+
impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
846+
fn len(&self) -> usize { self.elems_left }
847+
}
845848

846849
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
847850
type Item = (&'a K, &'a mut V);
@@ -856,10 +859,13 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
856859
})
857860
}
858861

859-
fn size_hint(&self) -> (uint, Option<uint>) {
862+
fn size_hint(&self) -> (usize, Option<usize>) {
860863
(self.elems_left, Some(self.elems_left))
861864
}
862865
}
866+
impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
867+
fn len(&self) -> usize { self.elems_left }
868+
}
863869

864870
impl<K, V> Iterator for IntoIter<K, V> {
865871
type Item = (SafeHash, K, V);
@@ -879,13 +885,16 @@ impl<K, V> Iterator for IntoIter<K, V> {
879885
})
880886
}
881887

882-
fn size_hint(&self) -> (uint, Option<uint>) {
888+
fn size_hint(&self) -> (usize, Option<usize>) {
883889
let size = self.table.size();
884890
(size, Some(size))
885891
}
886892
}
893+
impl<K, V> ExactSizeIterator for IntoIter<K, V> {
894+
fn len(&self) -> usize { self.table.size() }
895+
}
887896

888-
impl<'a, K: 'a, V: 'a> Iterator for Drain<'a, K, V> {
897+
impl<'a, K, V> Iterator for Drain<'a, K, V> {
889898
type Item = (SafeHash, K, V);
890899

891900
#[inline]
@@ -904,11 +913,14 @@ impl<'a, K: 'a, V: 'a> Iterator for Drain<'a, K, V> {
904913
})
905914
}
906915

907-
fn size_hint(&self) -> (uint, Option<uint>) {
916+
fn size_hint(&self) -> (usize, Option<usize>) {
908917
let size = self.table.size();
909918
(size, Some(size))
910919
}
911920
}
921+
impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
922+
fn len(&self) -> usize { self.table.size() }
923+
}
912924

913925
#[unsafe_destructor]
914926
impl<'a, K: 'a, V: 'a> Drop for Drain<'a, K, V> {

0 commit comments

Comments
 (0)