Skip to content

Commit

Permalink
Merge pull request rust-lang#18 from bluss/values
Browse files Browse the repository at this point in the history
Implement .values() and .values_mut()
  • Loading branch information
bluss authored Mar 1, 2017
2 parents 9053d59 + 22f4a15 commit 63a4509
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 6 deletions.
95 changes: 89 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,23 +762,38 @@ impl<K, V, S> OrderMap<K, V, S>
}
}

/// Return an iterator over the key-value pairs of the map, in their order
pub fn iter(&self) -> Iter<K, V> {
Iter {
iter: self.entries.iter()
}
}

/// Return an iterator over the key-value pairs of the map, in their order
pub fn iter_mut(&mut self) -> IterMut<K, V> {
IterMut {
iter: self.entries.iter_mut()
}
}

/// Return an iterator over the keys of the map, in their order
pub fn keys(&self) -> Keys<K, V> {
Keys {
iter: self.entries.iter()
}
}

/// Return an iterator over the key-value pairs of the map, in their order
pub fn iter(&self) -> Iter<K, V> {
Iter {
/// Return an iterator over the values of the map, in their order
pub fn values(&self) -> Values<K, V> {
Values {
iter: self.entries.iter()
}
}

/// Return an iterator over the key-value pairs of the map, in their order
pub fn iter_mut(&mut self) -> IterMut<K, V> {
IterMut {
/// Return an iterator over mutable references to the the values of the map,
/// in their order
pub fn values_mut(&mut self) -> ValuesMut<K, V> {
ValuesMut {
iter: self.entries.iter_mut()
}
}
Expand Down Expand Up @@ -1166,6 +1181,74 @@ impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {
}
}

pub struct Values<'a, K: 'a, V: 'a> {
iter: SliceIter<'a, Bucket<K, V>>,
}

impl<'a, K, V> Iterator for Values<'a, K, V> {
type Item = &'a V;

fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|ent| &ent.value)
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}

fn count(self) -> usize {
self.iter.len()
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.iter.nth(n).map(|ent| &ent.value)
}

fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}

impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(|ent| &ent.value)
}
}

pub struct ValuesMut<'a, K: 'a, V: 'a> {
iter: SliceIterMut<'a, Bucket<K, V>>,
}

impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
type Item = &'a mut V;

fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|ent| &mut ent.value)
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}

fn count(self) -> usize {
self.iter.len()
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.iter.nth(n).map(|ent| &mut ent.value)
}

fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}

impl<'a, K, V> DoubleEndedIterator for ValuesMut<'a, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(|ent| &mut ent.value)
}
}

pub struct Iter<'a, K: 'a, V: 'a> {
iter: SliceIter<'a, Bucket<K, V>>,
}
Expand Down
29 changes: 29 additions & 0 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,35 @@ quickcheck! {
assert_maps_equivalent(&map, &reference)
}

fn keys_values(ops: Large<Vec<Op<i8, i8>>>) -> bool {
let mut map = OrderMap::new();
let mut reference = HashMap::new();
do_ops(&ops, &mut map, &mut reference);
let mut visit = OrderMap::new();
for (k, v) in map.keys().zip(map.values()) {
assert_eq!(&map[k], v);
assert!(!visit.contains_key(k));
visit.insert(*k, *v);
}
assert_eq!(visit.len(), reference.len());
true
}

fn keys_values_mut(ops: Large<Vec<Op<i8, i8>>>) -> bool {
let mut map = OrderMap::new();
let mut reference = HashMap::new();
do_ops(&ops, &mut map, &mut reference);
let mut visit = OrderMap::new();
let keys = Vec::from_iter(map.keys().cloned());
for (k, v) in keys.iter().zip(map.values_mut()) {
assert_eq!(&reference[k], v);
assert!(!visit.contains_key(k));
visit.insert(*k, *v);
}
assert_eq!(visit.len(), reference.len());
true
}

fn retain(keys: Large<Vec<i8>>, remove: Large<Vec<i8>>) -> bool {
let mut map = ordermap(keys.iter());
let remove_map = ordermap(remove.iter());
Expand Down

0 comments on commit 63a4509

Please sign in to comment.