Skip to content

Commit cf350ea

Browse files
author
bluss
committed
hashset: Clean up and rename the HashSet iterators
This removes the type SetAlgebraItems and replaces it with the structs Intersection and Difference. Rename the existing HashSet iterators according to RFC #344: * SetItems -> Iter * SetMoveItems -> IntoIter * Remaining set combination iterators renamed to Union and SymmetricDifference [breaking-change]
1 parent 22a9f25 commit cf350ea

File tree

1 file changed

+83
-46
lines changed
  • src/libstd/collections/hash

1 file changed

+83
-46
lines changed

src/libstd/collections/hash/set.rs

+83-46
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use default::Default;
1717
use fmt::Show;
1818
use fmt;
1919
use hash::{Hash, Hasher, RandomSipHasher};
20-
use iter::{Iterator, IteratorExt, FromIterator, Map, FilterMap, Chain, Repeat, Zip, Extend, repeat};
20+
use iter::{Iterator, IteratorExt, FromIterator, Map, Chain, Extend};
2121
use option::Option::{Some, None, mod};
2222
use result::Result::{Ok, Err};
2323

@@ -250,8 +250,8 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
250250
/// }
251251
/// ```
252252
#[unstable = "matches collection reform specification, waiting for dust to settle"]
253-
pub fn iter<'a>(&'a self) -> SetItems<'a, T> {
254-
SetItems { iter: self.map.keys() }
253+
pub fn iter<'a>(&'a self) -> Iter<'a, T> {
254+
Iter { iter: self.map.keys() }
255255
}
256256

257257
/// Creates a consuming iterator, that is, one that moves each value out
@@ -275,10 +275,10 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
275275
/// }
276276
/// ```
277277
#[unstable = "matches collection reform specification, waiting for dust to settle"]
278-
pub fn into_iter(self) -> SetMoveItems<T> {
278+
pub fn into_iter(self) -> IntoIter<T> {
279279
fn first<A, B>((a, _): (A, B)) -> A { a }
280280

281-
SetMoveItems { iter: self.map.into_iter().map(first) }
281+
IntoIter { iter: self.map.into_iter().map(first) }
282282
}
283283

284284
/// Visit the values representing the difference.
@@ -304,14 +304,11 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
304304
/// assert_eq!(diff, [4i].iter().map(|&x| x).collect());
305305
/// ```
306306
#[unstable = "matches collection reform specification, waiting for dust to settle"]
307-
pub fn difference<'a>(&'a self, other: &'a HashSet<T, H>) -> SetAlgebraItems<'a, T, H> {
308-
fn filter<'a, T, S, H>((other, elt): (&HashSet<T, H>, &'a T)) -> Option<&'a T> where
309-
T: Eq + Hash<S>, H: Hasher<S>
310-
{
311-
if !other.contains(elt) { Some(elt) } else { None }
307+
pub fn difference<'a>(&'a self, other: &'a HashSet<T, H>) -> Difference<'a, T, H> {
308+
Difference {
309+
iter: self.iter(),
310+
other: other,
312311
}
313-
314-
SetAlgebraItems { iter: repeat(other).zip(self.iter()).filter_map(filter) }
315312
}
316313

317314
/// Visit the values representing the symmetric difference.
@@ -336,8 +333,8 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
336333
/// ```
337334
#[unstable = "matches collection reform specification, waiting for dust to settle"]
338335
pub fn symmetric_difference<'a>(&'a self, other: &'a HashSet<T, H>)
339-
-> SymDifferenceItems<'a, T, H> {
340-
SymDifferenceItems { iter: self.difference(other).chain(other.difference(self)) }
336+
-> SymmetricDifference<'a, T, H> {
337+
SymmetricDifference { iter: self.difference(other).chain(other.difference(self)) }
341338
}
342339

343340
/// Visit the values representing the intersection.
@@ -358,14 +355,11 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
358355
/// assert_eq!(diff, [2i, 3].iter().map(|&x| x).collect());
359356
/// ```
360357
#[unstable = "matches collection reform specification, waiting for dust to settle"]
361-
pub fn intersection<'a>(&'a self, other: &'a HashSet<T, H>) -> SetAlgebraItems<'a, T, H> {
362-
fn filter<'a, T, S, H>((other, elt): (&HashSet<T, H>, &'a T)) -> Option<&'a T> where
363-
T: Eq + Hash<S>, H: Hasher<S>
364-
{
365-
if other.contains(elt) { Some(elt) } else { None }
358+
pub fn intersection<'a>(&'a self, other: &'a HashSet<T, H>) -> Intersection<'a, T, H> {
359+
Intersection {
360+
iter: self.iter(),
361+
other: other,
366362
}
367-
368-
SetAlgebraItems { iter: repeat(other).zip(self.iter()).filter_map(filter) }
369363
}
370364

371365
/// Visit the values representing the union.
@@ -386,8 +380,8 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
386380
/// assert_eq!(diff, [1i, 2, 3, 4].iter().map(|&x| x).collect());
387381
/// ```
388382
#[unstable = "matches collection reform specification, waiting for dust to settle"]
389-
pub fn union<'a>(&'a self, other: &'a HashSet<T, H>) -> UnionItems<'a, T, H> {
390-
UnionItems { iter: self.iter().chain(other.difference(self)) }
383+
pub fn union<'a>(&'a self, other: &'a HashSet<T, H>) -> Union<'a, T, H> {
384+
Union { iter: self.iter().chain(other.difference(self)) }
391385
}
392386

393387
/// Return the number of elements in the set
@@ -617,58 +611,101 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Default for HashSet<T, H> {
617611
}
618612

619613
/// HashSet iterator
620-
pub struct SetItems<'a, K: 'a> {
614+
pub struct Iter<'a, K: 'a> {
621615
iter: Keys<'a, K, ()>
622616
}
623617

624618
/// HashSet move iterator
625-
pub struct SetMoveItems<K> {
619+
pub struct IntoIter<K> {
626620
iter: Map<(K, ()), K, MoveEntries<K, ()>, fn((K, ())) -> K>
627621
}
628622

629-
// `Repeat` is used to feed the filter closure an explicit capture
630-
// of a reference to the other set
631-
/// Set operations iterator, used directly for intersection and difference
632-
pub struct SetAlgebraItems<'a, T: 'a, H: 'a> {
633-
iter: FilterMap<
634-
(&'a HashSet<T, H>, &'a T),
635-
&'a T,
636-
Zip<Repeat<&'a HashSet<T, H>>, SetItems<'a, T>>,
637-
for<'b> fn((&HashSet<T, H>, &'b T)) -> Option<&'b T>,
638-
>
623+
/// Intersection iterator
624+
pub struct Intersection<'a, T: 'a, H: 'a> {
625+
// iterator of the first set
626+
iter: Iter<'a, T>,
627+
// the second set
628+
other: &'a HashSet<T, H>,
629+
}
630+
631+
/// Difference iterator
632+
pub struct Difference<'a, T: 'a, H: 'a> {
633+
// iterator of the first set
634+
iter: Iter<'a, T>,
635+
// the second set
636+
other: &'a HashSet<T, H>,
639637
}
640638

641639
/// Symmetric difference iterator.
642-
pub struct SymDifferenceItems<'a, T: 'a, H: 'a> {
643-
iter: Chain<SetAlgebraItems<'a, T, H>, SetAlgebraItems<'a, T, H>>
640+
pub struct SymmetricDifference<'a, T: 'a, H: 'a> {
641+
iter: Chain<Difference<'a, T, H>, Difference<'a, T, H>>
644642
}
645643

646644
/// Set union iterator.
647-
pub struct UnionItems<'a, T: 'a, H: 'a> {
648-
iter: Chain<SetItems<'a, T>, SetAlgebraItems<'a, T, H>>
645+
pub struct Union<'a, T: 'a, H: 'a> {
646+
iter: Chain<Iter<'a, T>, Difference<'a, T, H>>
649647
}
650648

651-
impl<'a, K> Iterator<&'a K> for SetItems<'a, K> {
649+
impl<'a, K> Iterator<&'a K> for Iter<'a, K> {
652650
fn next(&mut self) -> Option<&'a K> { self.iter.next() }
653651
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
654652
}
655653

656-
impl<K> Iterator<K> for SetMoveItems<K> {
654+
impl<K> Iterator<K> for IntoIter<K> {
657655
fn next(&mut self) -> Option<K> { self.iter.next() }
658656
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
659657
}
660658

661-
impl<'a, T, H> Iterator<&'a T> for SetAlgebraItems<'a, T, H> {
662-
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
663-
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
659+
impl<'a, T, S, H> Iterator<&'a T> for Intersection<'a, T, H>
660+
where T: Eq + Hash<S>, H: Hasher<S>
661+
{
662+
fn next(&mut self) -> Option<&'a T> {
663+
loop {
664+
match self.iter.next() {
665+
None => return None,
666+
Some(elt) => if self.other.contains(elt) {
667+
return Some(elt)
668+
},
669+
}
670+
}
671+
}
672+
673+
fn size_hint(&self) -> (uint, Option<uint>) {
674+
let (_, upper) = self.iter.size_hint();
675+
(0, upper)
676+
}
677+
}
678+
679+
impl<'a, T, S, H> Iterator<&'a T> for Difference<'a, T, H>
680+
where T: Eq + Hash<S>, H: Hasher<S>
681+
{
682+
fn next(&mut self) -> Option<&'a T> {
683+
loop {
684+
match self.iter.next() {
685+
None => return None,
686+
Some(elt) => if !self.other.contains(elt) {
687+
return Some(elt)
688+
},
689+
}
690+
}
691+
}
692+
693+
fn size_hint(&self) -> (uint, Option<uint>) {
694+
let (_, upper) = self.iter.size_hint();
695+
(0, upper)
696+
}
664697
}
665698

666-
impl<'a, T, H> Iterator<&'a T> for SymDifferenceItems<'a, T, H> {
699+
impl<'a, T, S, H> Iterator<&'a T> for SymmetricDifference<'a, T, H>
700+
where T: Eq + Hash<S>, H: Hasher<S>
701+
{
667702
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
668703
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
669704
}
670705

671-
impl<'a, T, H> Iterator<&'a T> for UnionItems<'a, T, H> {
706+
impl<'a, T, S, H> Iterator<&'a T> for Union<'a, T, H>
707+
where T: Eq + Hash<S>, H: Hasher<S>
708+
{
672709
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
673710
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
674711
}

0 commit comments

Comments
 (0)