Skip to content

Commit 67ae3a4

Browse files
committed
Clean up libcollections::VecMap
- Introduce a named type for the return type of `VecMap::move_iter` - Rename all type parameters to `V` for "Value". - Remove unnecessary call to an `Option::unwrap`, use pattern matching instead. - Remove incorrect `Hash` implementation which took the `VecMap`'s capacity into account. This is a [breaking-change], however whoever used the `Hash` implementation relied on an incorrect implementation.
1 parent 95d1771 commit 67ae3a4

File tree

1 file changed

+35
-54
lines changed

1 file changed

+35
-54
lines changed

src/libcollections/vec_map.rs

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ use core::mem::replace;
2323

2424
use {vec, slice};
2525
use vec::Vec;
26-
use hash;
27-
use hash::Hash;
2826

2927
// FIXME(conventions): capacity management???
3028

@@ -62,8 +60,8 @@ use hash::Hash;
6260
/// assert!(months.is_empty());
6361
/// ```
6462
#[deriving(PartialEq, Eq)]
65-
pub struct VecMap<T> {
66-
v: Vec<Option<T>>,
63+
pub struct VecMap<V> {
64+
v: Vec<Option<V>>,
6765
}
6866

6967
impl<V> Default for VecMap<V> {
@@ -83,12 +81,6 @@ impl<V:Clone> Clone for VecMap<V> {
8381
}
8482
}
8583

86-
impl <S: hash::Writer, T: Hash<S>> Hash<S> for VecMap<T> {
87-
fn hash(&self, state: &mut S) {
88-
self.v.hash(state)
89-
}
90-
}
91-
9284
impl<V> VecMap<V> {
9385
/// Creates an empty `VecMap`.
9486
///
@@ -99,7 +91,7 @@ impl<V> VecMap<V> {
9991
/// let mut map: VecMap<&str> = VecMap::new();
10092
/// ```
10193
#[unstable = "matches collection reform specification, waiting for dust to settle"]
102-
pub fn new() -> VecMap<V> { VecMap{v: vec!()} }
94+
pub fn new() -> VecMap<V> { VecMap { v: vec![] } }
10395

10496
/// Creates an empty `VecMap` with space for at least `capacity`
10597
/// elements before resizing.
@@ -207,10 +199,7 @@ impl<V> VecMap<V> {
207199
/// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
208200
/// ```
209201
#[unstable = "matches collection reform specification, waiting for dust to settle"]
210-
pub fn into_iter(&mut self)
211-
-> FilterMap<(uint, Option<V>), (uint, V),
212-
Enumerate<vec::MoveItems<Option<V>>>>
213-
{
202+
pub fn into_iter(&mut self) -> MoveItems<V> {
214203
let values = replace(&mut self.v, vec!());
215204
values.into_iter().enumerate().filter_map(|(i, v)| {
216205
v.map(|v| (i, v))
@@ -523,16 +512,19 @@ impl<V> IndexMut<uint, V> for VecMap<V> {
523512

524513
macro_rules! iterator {
525514
(impl $name:ident -> $elem:ty, $($getter:ident),+) => {
526-
impl<'a, T> Iterator<$elem> for $name<'a, T> {
515+
impl<'a, V> Iterator<$elem> for $name<'a, V> {
527516
#[inline]
528517
fn next(&mut self) -> Option<$elem> {
529518
while self.front < self.back {
530519
match self.iter.next() {
531520
Some(elem) => {
532-
if elem.is_some() {
533-
let index = self.front;
534-
self.front += 1;
535-
return Some((index, elem $(. $getter ())+));
521+
match elem$(. $getter ())+ {
522+
Some(x) => {
523+
let index = self.front;
524+
self.front += 1;
525+
return Some((index, x));
526+
},
527+
None => {},
536528
}
537529
}
538530
_ => ()
@@ -552,15 +544,18 @@ macro_rules! iterator {
552544

553545
macro_rules! double_ended_iterator {
554546
(impl $name:ident -> $elem:ty, $($getter:ident),+) => {
555-
impl<'a, T> DoubleEndedIterator<$elem> for $name<'a, T> {
547+
impl<'a, V> DoubleEndedIterator<$elem> for $name<'a, V> {
556548
#[inline]
557549
fn next_back(&mut self) -> Option<$elem> {
558550
while self.front < self.back {
559551
match self.iter.next_back() {
560552
Some(elem) => {
561-
if elem.is_some() {
562-
self.back -= 1;
563-
return Some((self.back, elem$(. $getter ())+));
553+
match elem$(. $getter ())+ {
554+
Some(x) => {
555+
self.back -= 1;
556+
return Some((self.back, x));
557+
},
558+
None => {},
564559
}
565560
}
566561
_ => ()
@@ -574,39 +569,42 @@ macro_rules! double_ended_iterator {
574569
}
575570

576571
/// Forward iterator over a map.
577-
pub struct Entries<'a, T:'a> {
572+
pub struct Entries<'a, V:'a> {
578573
front: uint,
579574
back: uint,
580-
iter: slice::Items<'a, Option<T>>
575+
iter: slice::Items<'a, Option<V>>
581576
}
582577

583-
iterator!(impl Entries -> (uint, &'a T), as_ref, unwrap)
584-
double_ended_iterator!(impl Entries -> (uint, &'a T), as_ref, unwrap)
578+
iterator!(impl Entries -> (uint, &'a V), as_ref)
579+
double_ended_iterator!(impl Entries -> (uint, &'a V), as_ref)
585580

586581
/// Forward iterator over the key-value pairs of a map, with the
587582
/// values being mutable.
588-
pub struct MutEntries<'a, T:'a> {
583+
pub struct MutEntries<'a, V:'a> {
589584
front: uint,
590585
back: uint,
591-
iter: slice::MutItems<'a, Option<T>>
586+
iter: slice::MutItems<'a, Option<V>>
592587
}
593588

594-
iterator!(impl MutEntries -> (uint, &'a mut T), as_mut, unwrap)
595-
double_ended_iterator!(impl MutEntries -> (uint, &'a mut T), as_mut, unwrap)
589+
iterator!(impl MutEntries -> (uint, &'a mut V), as_mut)
590+
double_ended_iterator!(impl MutEntries -> (uint, &'a mut V), as_mut)
596591

597592
/// Forward iterator over the keys of a map
598-
pub type Keys<'a, T> =
599-
iter::Map<'static, (uint, &'a T), uint, Entries<'a, T>>;
593+
pub type Keys<'a, V> =
594+
iter::Map<'static, (uint, &'a V), uint, Entries<'a, V>>;
600595

601596
/// Forward iterator over the values of a map
602-
pub type Values<'a, T> =
603-
iter::Map<'static, (uint, &'a T), &'a T, Entries<'a, T>>;
597+
pub type Values<'a, V> =
598+
iter::Map<'static, (uint, &'a V), &'a V, Entries<'a, V>>;
599+
600+
/// Iterator over the key-value pairs of a map, the iterator consumes the map
601+
pub type MoveItems<V> =
602+
FilterMap<'static, (uint, Option<V>), (uint, V), Enumerate<vec::MoveItems<Option<V>>>>;
604603

605604
#[cfg(test)]
606605
mod test_map {
607606
use std::prelude::*;
608607
use vec::Vec;
609-
use hash;
610608

611609
use super::VecMap;
612610

@@ -920,23 +918,6 @@ mod test_map {
920918
assert!(a < b && a <= b);
921919
}
922920

923-
#[test]
924-
fn test_hash() {
925-
let mut x = VecMap::new();
926-
let mut y = VecMap::new();
927-
928-
assert!(hash::hash(&x) == hash::hash(&y));
929-
x.insert(1, 'a');
930-
x.insert(2, 'b');
931-
x.insert(3, 'c');
932-
933-
y.insert(3, 'c');
934-
y.insert(2, 'b');
935-
y.insert(1, 'a');
936-
937-
assert!(hash::hash(&x) == hash::hash(&y));
938-
}
939-
940921
#[test]
941922
fn test_from_iter() {
942923
let xs: Vec<(uint, char)> = vec![(1u, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')];

0 commit comments

Comments
 (0)