Skip to content

Commit f92c832

Browse files
committed
Merge pull request #19770 from csouth3/iterator-wrapperstructs
Use wrapper structs for iterators in BTreeMap/Set and HashMap/Set Reviewed-by: Gankro
2 parents 1a5880b + 341cf40 commit f92c832

File tree

4 files changed

+146
-70
lines changed

4 files changed

+146
-70
lines changed

src/libcollections/btree/map.rs

+28-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use std::hash::{Writer, Hash};
2929
use core::default::Default;
3030
use core::{iter, fmt, mem};
3131
use core::fmt::Show;
32+
use core::iter::Map;
3233

3334
use ring_buf::RingBuf;
3435

@@ -110,12 +111,14 @@ pub struct MoveEntries<K, V> {
110111
}
111112

112113
/// An iterator over a BTreeMap's keys.
113-
pub type Keys<'a, K, V> =
114-
iter::Map<(&'a K, &'a V), &'a K, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>;
114+
pub struct Keys<'a, K: 'a, V: 'a> {
115+
inner: Map<(&'a K, &'a V), &'a K, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
116+
}
115117

116118
/// An iterator over a BTreeMap's values.
117-
pub type Values<'a, K, V> =
118-
iter::Map<(&'a K, &'a V), &'a V, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>;
119+
pub struct Values<'a, K: 'a, V: 'a> {
120+
inner: Map<(&'a K, &'a V), &'a V, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
121+
}
119122

120123
/// A view into a single entry in a map, which may either be vacant or occupied.
121124
pub enum Entry<'a, K:'a, V:'a> {
@@ -1054,6 +1057,25 @@ impl<K, V> DoubleEndedIterator<(K, V)> for MoveEntries<K, V> {
10541057
impl<K, V> ExactSizeIterator<(K, V)> for MoveEntries<K, V> {}
10551058

10561059

1060+
impl<'a, K, V> Iterator<&'a K> for Keys<'a, K, V> {
1061+
fn next(&mut self) -> Option<(&'a K)> { self.inner.next() }
1062+
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
1063+
}
1064+
impl<'a, K, V> DoubleEndedIterator<&'a K> for Keys<'a, K, V> {
1065+
fn next_back(&mut self) -> Option<(&'a K)> { self.inner.next_back() }
1066+
}
1067+
impl<'a, K, V> ExactSizeIterator<&'a K> for Keys<'a, K, V> {}
1068+
1069+
1070+
impl<'a, K, V> Iterator<&'a V> for Values<'a, K, V> {
1071+
fn next(&mut self) -> Option<(&'a V)> { self.inner.next() }
1072+
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
1073+
}
1074+
impl<'a, K, V> DoubleEndedIterator<&'a V> for Values<'a, K, V> {
1075+
fn next_back(&mut self) -> Option<(&'a V)> { self.inner.next_back() }
1076+
}
1077+
impl<'a, K, V> ExactSizeIterator<&'a V> for Values<'a, K, V> {}
1078+
10571079

10581080
impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
10591081
/// Sets the value of the entry with the VacantEntry's key,
@@ -1204,7 +1226,7 @@ impl<K, V> BTreeMap<K, V> {
12041226
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
12051227
fn first<A, B>((a, _): (A, B)) -> A { a }
12061228

1207-
self.iter().map(first)
1229+
Keys { inner: self.iter().map(first) }
12081230
}
12091231

12101232
/// Gets an iterator over the values of the map.
@@ -1225,7 +1247,7 @@ impl<K, V> BTreeMap<K, V> {
12251247
pub fn values<'a>(&'a self) -> Values<'a, K, V> {
12261248
fn second<A, B>((_, b): (A, B)) -> B { b }
12271249

1228-
self.iter().map(second)
1250+
Values { inner: self.iter().map(second) }
12291251
}
12301252

12311253
/// Return the number of elements in the map.

src/libcollections/btree/set.rs

+29-7
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use btree_map::{BTreeMap, Keys, MoveEntries};
1717
use std::hash::Hash;
1818
use core::borrow::BorrowFrom;
1919
use core::default::Default;
20-
use core::{iter, fmt};
21-
use core::iter::Peekable;
20+
use core::fmt;
21+
use core::iter::{Peekable, Map};
2222
use core::fmt::Show;
2323

2424
// FIXME(conventions): implement bounded iterators
@@ -33,11 +33,14 @@ pub struct BTreeSet<T>{
3333
}
3434

3535
/// An iterator over a BTreeSet's items.
36-
pub type Items<'a, T> = Keys<'a, T, ()>;
36+
pub struct Items<'a, T: 'a> {
37+
iter: Keys<'a, T, ()>
38+
}
3739

3840
/// An owning iterator over a BTreeSet's items.
39-
pub type MoveItems<T> =
40-
iter::Map<(T, ()), T, MoveEntries<T, ()>, fn((T, ())) -> T>;
41+
pub struct MoveItems<T> {
42+
iter: Map<(T, ()), T, MoveEntries<T, ()>, fn((T, ())) -> T>
43+
}
4144

4245
/// A lazy iterator producing elements in the set difference (in-order).
4346
pub struct DifferenceItems<'a, T:'a> {
@@ -105,7 +108,7 @@ impl<T> BTreeSet<T> {
105108
/// ```
106109
#[unstable = "matches collection reform specification, waiting for dust to settle"]
107110
pub fn iter<'a>(&'a self) -> Items<'a, T> {
108-
self.map.keys()
111+
Items { iter: self.map.keys() }
109112
}
110113

111114
/// Gets an iterator for moving out the BtreeSet's contents.
@@ -124,7 +127,7 @@ impl<T> BTreeSet<T> {
124127
pub fn into_iter(self) -> MoveItems<T> {
125128
fn first<A, B>((a, _): (A, B)) -> A { a }
126129

127-
self.map.into_iter().map(first)
130+
MoveItems { iter: self.map.into_iter().map(first) }
128131
}
129132
}
130133

@@ -635,6 +638,25 @@ impl<T: Show> Show for BTreeSet<T> {
635638
}
636639
}
637640

641+
impl<'a, T> Iterator<&'a T> for Items<'a, T> {
642+
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
643+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
644+
}
645+
impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> {
646+
fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() }
647+
}
648+
impl<'a, T> ExactSizeIterator<&'a T> for Items<'a, T> {}
649+
650+
651+
impl<T> Iterator<T> for MoveItems<T> {
652+
fn next(&mut self) -> Option<T> { self.iter.next() }
653+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
654+
}
655+
impl<T> DoubleEndedIterator<T> for MoveItems<T> {
656+
fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
657+
}
658+
impl<T> ExactSizeIterator<T> for MoveItems<T> {}
659+
638660
/// Compare `x` and `y`, but return `short` if x is None and `long` if y is None
639661
fn cmp_opt<T: Ord>(x: Option<&T>, y: Option<&T>,
640662
short: Ordering, long: Ordering) -> Ordering {

src/libstd/collections/hash/map.rs

+29-35
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use cmp::{max, Eq, Equiv, PartialEq};
2020
use default::Default;
2121
use fmt::{mod, Show};
2222
use hash::{Hash, Hasher, RandomSipHasher};
23-
use iter::{mod, Iterator, IteratorExt, FromIterator, Extend};
23+
use iter::{mod, Iterator, IteratorExt, FromIterator, Extend, Map};
2424
use kinds::Sized;
2525
use mem::{mod, replace};
2626
use num::{Int, UnsignedInt};
@@ -859,7 +859,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
859859
pub fn keys(&self) -> Keys<K, V> {
860860
fn first<A, B>((a, _): (A, B)) -> A { a }
861861

862-
self.iter().map(first)
862+
Keys { inner: self.iter().map(first) }
863863
}
864864

865865
/// An iterator visiting all values in arbitrary order.
@@ -883,7 +883,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
883883
pub fn values(&self) -> Values<K, V> {
884884
fn second<A, B>((_, b): (A, B)) -> B { b }
885885

886-
self.iter().map(second)
886+
Values { inner: self.iter().map(second) }
887887
}
888888

889889
/// An iterator visiting all key-value pairs in arbitrary order.
@@ -1335,6 +1335,16 @@ pub struct MoveEntries<K, V> {
13351335
>
13361336
}
13371337

1338+
/// HashMap keys iterator
1339+
pub struct Keys<'a, K: 'a, V: 'a> {
1340+
inner: Map<(&'a K, &'a V), &'a K, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
1341+
}
1342+
1343+
/// HashMap values iterator
1344+
pub struct Values<'a, K: 'a, V: 'a> {
1345+
inner: Map<(&'a K, &'a V), &'a V, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
1346+
}
1347+
13381348
/// A view into a single occupied location in a HashMap
13391349
pub struct OccupiedEntry<'a, K:'a, V:'a> {
13401350
elem: FullBucket<K, V, &'a mut RawTable<K, V>>,
@@ -1365,36 +1375,28 @@ enum VacantEntryState<K, V, M> {
13651375
}
13661376

13671377
impl<'a, K, V> Iterator<(&'a K, &'a V)> for Entries<'a, K, V> {
1368-
#[inline]
1369-
fn next(&mut self) -> Option<(&'a K, &'a V)> {
1370-
self.inner.next()
1371-
}
1372-
#[inline]
1373-
fn size_hint(&self) -> (uint, Option<uint>) {
1374-
self.inner.size_hint()
1375-
}
1378+
#[inline] fn next(&mut self) -> Option<(&'a K, &'a V)> { self.inner.next() }
1379+
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
13761380
}
13771381

13781382
impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> {
1379-
#[inline]
1380-
fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
1381-
self.inner.next()
1382-
}
1383-
#[inline]
1384-
fn size_hint(&self) -> (uint, Option<uint>) {
1385-
self.inner.size_hint()
1386-
}
1383+
#[inline] fn next(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next() }
1384+
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
13871385
}
13881386

13891387
impl<K, V> Iterator<(K, V)> for MoveEntries<K, V> {
1390-
#[inline]
1391-
fn next(&mut self) -> Option<(K, V)> {
1392-
self.inner.next()
1393-
}
1394-
#[inline]
1395-
fn size_hint(&self) -> (uint, Option<uint>) {
1396-
self.inner.size_hint()
1397-
}
1388+
#[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
1389+
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
1390+
}
1391+
1392+
impl<'a, K, V> Iterator<&'a K> for Keys<'a, K, V> {
1393+
#[inline] fn next(&mut self) -> Option<(&'a K)> { self.inner.next() }
1394+
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
1395+
}
1396+
1397+
impl<'a, K, V> Iterator<&'a V> for Values<'a, K, V> {
1398+
#[inline] fn next(&mut self) -> Option<(&'a V)> { self.inner.next() }
1399+
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
13981400
}
13991401

14001402
impl<'a, K, V> OccupiedEntry<'a, K, V> {
@@ -1448,14 +1450,6 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
14481450
}
14491451
}
14501452

1451-
/// HashMap keys iterator
1452-
pub type Keys<'a, K, V> =
1453-
iter::Map<(&'a K, &'a V), &'a K, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>;
1454-
1455-
/// HashMap values iterator
1456-
pub type Values<'a, K, V> =
1457-
iter::Map<(&'a K, &'a V), &'a V, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>;
1458-
14591453
impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> FromIterator<(K, V)> for HashMap<K, V, H> {
14601454
fn from_iter<T: Iterator<(K, V)>>(iter: T) -> HashMap<K, V, H> {
14611455
let (lower, _) = iter.size_hint();

0 commit comments

Comments
 (0)