Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented IntoIterator for MultiMap (ref, mut ref and consuming) #4

Merged
merged 4 commits into from
Mar 24, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 105 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::borrow::Borrow;
use std::collections::HashMap;
use std::collections::hash_map::Keys;
use std::collections::hash_map::{Keys, IntoIter};
use std::fmt::{self, Debug};
use std::iter::Iterator;
use std::iter::{Iterator, IntoIterator};
use std::hash::Hash;
use std::ops::Index;

Expand Down Expand Up @@ -480,6 +480,40 @@ impl<K, V> PartialEq for MultiMap<K, V> where K: Eq + Hash, V: PartialEq {

impl<K, V> Eq for MultiMap<K, V> where K: Eq + Hash, V: Eq {}

impl<'a, K, V> IntoIterator for &'a MultiMap<K, V>
where K: Eq + Hash
{
type Item = (&'a K, &'a Vec<V>);
type IntoIter = IterAll<'a, K, Vec<V>>;

fn into_iter(self) -> IterAll<'a, K, Vec<V>> {
self.iter_all()
}
}

impl<'a, K, V> IntoIterator for &'a mut MultiMap<K, V>
where K: Eq + Hash
{

type Item = (&'a K, &'a mut Vec<V>);
type IntoIter = IterAllMut<'a, K, Vec<V>>;

fn into_iter(mut self) -> IterAllMut<'a, K, Vec<V>> {
self.inner.iter_mut()
}
}

impl<K, V> IntoIterator for MultiMap<K, V>
where K: Eq + Hash
{
type Item = (K, Vec<V>);
type IntoIter = IntoIter<K, Vec<V>>;

fn into_iter(self) -> IntoIter<K, Vec<V>> {
self.inner.into_iter()
}
}

#[derive(Clone)]
pub struct Iter<'a, K: 'a, V: 'a> {
inner: IterAll<'a,K, Vec<V>>,
Expand Down Expand Up @@ -704,6 +738,75 @@ fn iter() {
assert_eq!(iter.len(), 1);
}

#[test]
fn intoiterator_for_reference_type() {
let mut m: MultiMap<usize, usize> = MultiMap::new();
m.insert(1,42);
m.insert(1,43);
m.insert(4,42);
m.insert(8,42);

let keys = vec![1,4,8];

for (key, value) in &m {
assert!(keys.contains(key));

if key == &1 {
assert_eq!(value, &vec![42, 43]);
}
else {
assert_eq!(value, &vec![42]);
}
}
}

#[test]
fn intoiterator_for_mutable_reference_type() {
let mut m: MultiMap<usize, usize> = MultiMap::new();
m.insert(1,42);
m.insert(1,43);
m.insert(4,42);
m.insert(8,42);

let keys = vec![1,4,8];

for (key, value) in &mut m {
assert!(keys.contains(key));

if key == &1 {
assert_eq!(value, &vec![42, 43]);
value.push(666);
}
else {
assert_eq!(value, &vec![42]);
}
}

assert_eq!(m.get_vec(&1), Some(&vec![42, 43, 666]));
}

#[test]
fn intoiterator_consuming() {
let mut m: MultiMap<usize, usize> = MultiMap::new();
m.insert(1,42);
m.insert(1,43);
m.insert(4,42);
m.insert(8,42);

let keys = vec![1,4,8];

for (key, value) in m {
assert!(keys.contains(&key));

if key == 1 {
assert_eq!(value, vec![42, 43]);
}
else {
assert_eq!(value, vec![42]);
}
}
}

#[test]
fn test_fmt_debug() {
let mut map = MultiMap::new();
Expand Down