Skip to content

Commit 750f32d

Browse files
author
blake2-ppc
committed
hashmap: Iterators for hashset diff, sym. diff, intersec, union
Implement the difference, union, etc iterators with the help of a custom iterator combinator with explicit closure environment. Reported issue #7814 to be able to use the std::iterator filter combinator.
1 parent cf4127f commit 750f32d

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

src/libstd/hashmap.rs

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
#[mutable_doc];
1717

1818
use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
19+
use clone::Clone;
1920
use cmp::{Eq, Equiv};
2021
use hash::Hash;
21-
use iterator::{Iterator, IteratorUtil, FromIterator};
22+
use iterator::{Iterator, IteratorUtil, FromIterator, ChainIterator};
2223
use num;
2324
use option::{None, Option, Some};
2425
use rand::RngUtil;
@@ -703,25 +704,24 @@ impl<T:Hash + Eq> Set<T> for HashSet<T> {
703704

704705
/// Visit the values representing the difference
705706
fn difference(&self, other: &HashSet<T>, f: &fn(&T) -> bool) -> bool {
706-
self.iter().advance(|v| other.contains(v) || f(v))
707+
self.difference_iter(other).advance(f)
707708
}
708709

709710
/// Visit the values representing the symmetric difference
710711
fn symmetric_difference(&self,
711712
other: &HashSet<T>,
712713
f: &fn(&T) -> bool) -> bool {
713-
self.difference(other, |t| f(t)) && other.difference(self, |t| f(t))
714+
self.symmetric_difference_iter(other).advance(f)
714715
}
715716

716717
/// Visit the values representing the intersection
717718
fn intersection(&self, other: &HashSet<T>, f: &fn(&T) -> bool) -> bool {
718-
self.iter().advance(|v| !other.contains(v) || f(v))
719+
self.intersection_iter(other).advance(f)
719720
}
720721

721722
/// Visit the values representing the union
722723
fn union(&self, other: &HashSet<T>, f: &fn(&T) -> bool) -> bool {
723-
self.iter().advance(|t| f(t)) &&
724-
other.iter().advance(|v| self.contains(v) || f(v))
724+
self.union_iter(other).advance(f)
725725
}
726726
}
727727

@@ -776,6 +776,33 @@ impl<T:Hash + Eq> HashSet<T> {
776776
pub fn iter<'a>(&'a self) -> HashSetIterator<'a, T> {
777777
HashSetIterator { iter: self.map.buckets.iter() }
778778
}
779+
780+
/// Visit the values representing the difference
781+
pub fn difference_iter<'a>(&'a self, other: &'a HashSet<T>)
782+
-> SetAlgebraIter<'a, T> {
783+
EnvFilterIterator{iter: self.iter(), env: other,
784+
filter: |elt, other| !other.contains(elt) }
785+
}
786+
787+
/// Visit the values representing the symmetric difference
788+
pub fn symmetric_difference_iter<'a>(&'a self, other: &'a HashSet<T>)
789+
-> ChainIterator<&'a T, SetAlgebraIter<'a, T>, SetAlgebraIter<'a, T>> {
790+
self.difference_iter(other).chain_(other.difference_iter(self))
791+
}
792+
793+
/// Visit the values representing the intersection
794+
pub fn intersection_iter<'a>(&'a self, other: &'a HashSet<T>)
795+
-> SetAlgebraIter<'a, T> {
796+
EnvFilterIterator{iter: self.iter(), env: other,
797+
filter: |elt, other| other.contains(elt) }
798+
}
799+
800+
/// Visit the values representing the union
801+
pub fn union_iter<'a>(&'a self, other: &'a HashSet<T>)
802+
-> ChainIterator<&'a T, HashSetIterator<'a, T>, SetAlgebraIter<'a, T>> {
803+
self.iter().chain_(other.difference_iter(self))
804+
}
805+
779806
}
780807

781808
impl<K: Eq + Hash, T: Iterator<K>> FromIterator<K, T> for HashSet<K> {
@@ -791,6 +818,39 @@ impl<K: Eq + Hash, T: Iterator<K>> FromIterator<K, T> for HashSet<K> {
791818
}
792819
}
793820

821+
// FIXME #7814: use std::iterator::FilterIterator
822+
/// Building block for Set operation iterators
823+
pub struct EnvFilterIterator<A, Env, I> {
824+
priv env: Env,
825+
priv filter: &'static fn(&A, Env) -> bool,
826+
priv iter: I,
827+
}
828+
829+
impl<'self, A, Env: Clone, I: Iterator<&'self A>> Iterator<&'self A>
830+
for EnvFilterIterator<A, Env, I> {
831+
#[inline]
832+
fn next(&mut self) -> Option<&'self A> {
833+
loop {
834+
match self.iter.next() {
835+
Some(elt) => if (self.filter)(elt, self.env.clone()) {
836+
return Some(elt)
837+
},
838+
None => return None,
839+
}
840+
}
841+
}
842+
843+
#[inline]
844+
fn size_hint(&self) -> (uint, Option<uint>) {
845+
let (_, upper) = self.iter.size_hint();
846+
(0, upper)
847+
}
848+
}
849+
850+
/// Set operations iterator
851+
pub type SetAlgebraIter<'self, T> =
852+
EnvFilterIterator<T, &'self HashSet<T>, HashSetIterator<'self, T>>;
853+
794854

795855
#[cfg(test)]
796856
mod test_map {
@@ -1126,7 +1186,7 @@ mod test_set {
11261186

11271187
let mut i = 0;
11281188
let expected = [3, 5, 11, 77];
1129-
for a.intersection(&b) |x| {
1189+
for a.intersection_iter(&b).advance |x| {
11301190
assert!(expected.contains(x));
11311191
i += 1
11321192
}
@@ -1149,7 +1209,7 @@ mod test_set {
11491209

11501210
let mut i = 0;
11511211
let expected = [1, 5, 11];
1152-
for a.difference(&b) |x| {
1212+
for a.difference_iter(&b).advance |x| {
11531213
assert!(expected.contains(x));
11541214
i += 1
11551215
}
@@ -1175,7 +1235,7 @@ mod test_set {
11751235

11761236
let mut i = 0;
11771237
let expected = [-2, 1, 5, 11, 14, 22];
1178-
for a.symmetric_difference(&b) |x| {
1238+
for a.symmetric_difference_iter(&b).advance |x| {
11791239
assert!(expected.contains(x));
11801240
i += 1
11811241
}
@@ -1205,7 +1265,7 @@ mod test_set {
12051265

12061266
let mut i = 0;
12071267
let expected = [-2, 1, 3, 5, 9, 11, 13, 16, 19, 24];
1208-
for a.union(&b) |x| {
1268+
for a.union_iter(&b).advance |x| {
12091269
assert!(expected.contains(x));
12101270
i += 1
12111271
}

0 commit comments

Comments
 (0)