Skip to content

Commit c3ac929

Browse files
committed
Add a Drain iterator to VecMap
1 parent 2366dee commit c3ac929

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

src/libcollections/vec_map.rs

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<V> VecMap<V> {
186186
}
187187
}
188188

189-
/// Returns an iterator visiting all keys in ascending order by the keys.
189+
/// Returns an iterator visiting all keys in ascending order of the keys.
190190
/// The iterator's element type is `uint`.
191191
#[stable]
192192
pub fn keys<'r>(&'r self) -> Keys<'r, V> {
@@ -196,7 +196,7 @@ impl<V> VecMap<V> {
196196
Keys { iter: self.iter().map(first) }
197197
}
198198

199-
/// Returns an iterator visiting all values in ascending order by the keys.
199+
/// Returns an iterator visiting all values in ascending order of the keys.
200200
/// The iterator's element type is `&'r V`.
201201
#[stable]
202202
pub fn values<'r>(&'r self) -> Values<'r, V> {
@@ -206,7 +206,7 @@ impl<V> VecMap<V> {
206206
Values { iter: self.iter().map(second) }
207207
}
208208

209-
/// Returns an iterator visiting all key-value pairs in ascending order by the keys.
209+
/// Returns an iterator visiting all key-value pairs in ascending order of the keys.
210210
/// The iterator's element type is `(uint, &'r V)`.
211211
///
212212
/// # Examples
@@ -233,7 +233,7 @@ impl<V> VecMap<V> {
233233
}
234234
}
235235

236-
/// Returns an iterator visiting all key-value pairs in ascending order by the keys,
236+
/// Returns an iterator visiting all key-value pairs in ascending order of the keys,
237237
/// with mutable references to the values.
238238
/// The iterator's element type is `(uint, &'r mut V)`.
239239
///
@@ -264,7 +264,7 @@ impl<V> VecMap<V> {
264264
}
265265
}
266266

267-
/// Returns an iterator visiting all key-value pairs in ascending order by
267+
/// Returns an iterator visiting all key-value pairs in ascending order of
268268
/// the keys, consuming the original `VecMap`.
269269
/// The iterator's element type is `(uint, &'r V)`.
270270
///
@@ -278,7 +278,6 @@ impl<V> VecMap<V> {
278278
/// map.insert(3, "c");
279279
/// map.insert(2, "b");
280280
///
281-
/// // Not possible with .iter()
282281
/// let vec: Vec<(uint, &str)> = map.into_iter().collect();
283282
///
284283
/// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
@@ -293,6 +292,34 @@ impl<V> VecMap<V> {
293292
IntoIter { iter: self.v.into_iter().enumerate().filter_map(filter) }
294293
}
295294

295+
/// Returns an iterator visiting all key-value pairs in ascending order of
296+
/// the keys, emptying (but not consuming) the original `VecMap`.
297+
/// The iterator's element type is `(uint, &'r V)`. Keeps the allocated memory for reuse.
298+
///
299+
/// # Examples
300+
///
301+
/// ```
302+
/// use std::collections::VecMap;
303+
///
304+
/// let mut map = VecMap::new();
305+
/// map.insert(1, "a");
306+
/// map.insert(3, "c");
307+
/// map.insert(2, "b");
308+
///
309+
/// let vec: Vec<(uint, &str)> = map.drain().collect();
310+
///
311+
/// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
312+
/// ```
313+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
314+
pub fn drain<'a>(&'a mut self) -> Drain<'a, V> {
315+
fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
316+
v.map(|v| (i, v))
317+
}
318+
let filter: fn((uint, Option<V>)) -> Option<(uint, V)> = filter; // coerce to fn ptr
319+
320+
Drain { iter: self.v.drain().enumerate().filter_map(filter) }
321+
}
322+
296323
/// Return the number of elements in the map.
297324
///
298325
/// # Examples
@@ -672,6 +699,28 @@ pub struct IntoIter<V> {
672699
fn((uint, Option<V>)) -> Option<(uint, V)>>
673700
}
674701

702+
#[unstable]
703+
pub struct Drain<'a, V> {
704+
iter: FilterMap<
705+
(uint, Option<V>),
706+
(uint, V),
707+
Enumerate<vec::Drain<'a, Option<V>>>,
708+
fn((uint, Option<V>)) -> Option<(uint, V)>>
709+
}
710+
711+
#[unstable]
712+
impl<'a, V> Iterator for Drain<'a, V> {
713+
type Item = (uint, V);
714+
715+
fn next(&mut self) -> Option<(uint, V)> { self.iter.next() }
716+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
717+
}
718+
719+
#[unstable]
720+
impl<'a, V> DoubleEndedIterator for Drain<'a, V> {
721+
fn next_back(&mut self) -> Option<(uint, V)> { self.iter.next_back() }
722+
}
723+
675724
#[stable]
676725
impl<'a, V> Iterator for Keys<'a, V> {
677726
type Item = uint;

0 commit comments

Comments
 (0)