Skip to content

Commit 91b3232

Browse files
committed
rollup merge of rust-lang#19993: bluss/setalgebraitems
This removes the type SetAlgebraItems and replaces it with the structs Intersection and Difference. Rename the existing HashSet iterators according to RFC rust-lang#344: * SetItems -> Iter * SetMoveItems -> IntoIter * Remaining set combination iterators renamed to Union and SymmetricDifference
2 parents 67ea1dc + cf350ea commit 91b3232

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

Lines changed: 83 additions & 46 deletions
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
@@ -625,12 +619,12 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Default for HashSet<T, H> {
625619
}
626620

627621
/// HashSet iterator
628-
pub struct SetItems<'a, K: 'a> {
622+
pub struct Iter<'a, K: 'a> {
629623
iter: Keys<'a, K, ()>
630624
}
631625

632626
/// HashSet move iterator
633-
pub struct SetMoveItems<K> {
627+
pub struct IntoIter<K> {
634628
iter: Map<(K, ()), K, MoveEntries<K, ()>, fn((K, ())) -> K>
635629
}
636630

@@ -639,34 +633,38 @@ pub struct Drain<'a, K: 'a> {
639633
iter: Map<(K, ()), K, map::Drain<'a, K, ()>, fn((K, ())) -> K>,
640634
}
641635

642-
// `Repeat` is used to feed the filter closure an explicit capture
643-
// of a reference to the other set
644-
/// Set operations iterator, used directly for intersection and difference
645-
pub struct SetAlgebraItems<'a, T: 'a, H: 'a> {
646-
iter: FilterMap<
647-
(&'a HashSet<T, H>, &'a T),
648-
&'a T,
649-
Zip<Repeat<&'a HashSet<T, H>>, SetItems<'a, T>>,
650-
for<'b> fn((&HashSet<T, H>, &'b T)) -> Option<&'b T>,
651-
>
636+
/// Intersection iterator
637+
pub struct Intersection<'a, T: 'a, H: 'a> {
638+
// iterator of the first set
639+
iter: Iter<'a, T>,
640+
// the second set
641+
other: &'a HashSet<T, H>,
642+
}
643+
644+
/// Difference iterator
645+
pub struct Difference<'a, T: 'a, H: 'a> {
646+
// iterator of the first set
647+
iter: Iter<'a, T>,
648+
// the second set
649+
other: &'a HashSet<T, H>,
652650
}
653651

654652
/// Symmetric difference iterator.
655-
pub struct SymDifferenceItems<'a, T: 'a, H: 'a> {
656-
iter: Chain<SetAlgebraItems<'a, T, H>, SetAlgebraItems<'a, T, H>>
653+
pub struct SymmetricDifference<'a, T: 'a, H: 'a> {
654+
iter: Chain<Difference<'a, T, H>, Difference<'a, T, H>>
657655
}
658656

659657
/// Set union iterator.
660-
pub struct UnionItems<'a, T: 'a, H: 'a> {
661-
iter: Chain<SetItems<'a, T>, SetAlgebraItems<'a, T, H>>
658+
pub struct Union<'a, T: 'a, H: 'a> {
659+
iter: Chain<Iter<'a, T>, Difference<'a, T, H>>
662660
}
663661

664-
impl<'a, K> Iterator<&'a K> for SetItems<'a, K> {
662+
impl<'a, K> Iterator<&'a K> for Iter<'a, K> {
665663
fn next(&mut self) -> Option<&'a K> { self.iter.next() }
666664
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
667665
}
668666

669-
impl<K> Iterator<K> for SetMoveItems<K> {
667+
impl<K> Iterator<K> for IntoIter<K> {
670668
fn next(&mut self) -> Option<K> { self.iter.next() }
671669
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
672670
}
@@ -676,17 +674,56 @@ impl<'a, K: 'a> Iterator<K> for Drain<'a, K> {
676674
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
677675
}
678676

679-
impl<'a, T, H> Iterator<&'a T> for SetAlgebraItems<'a, T, H> {
680-
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
681-
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
677+
impl<'a, T, S, H> Iterator<&'a T> for Intersection<'a, T, H>
678+
where T: Eq + Hash<S>, H: Hasher<S>
679+
{
680+
fn next(&mut self) -> Option<&'a T> {
681+
loop {
682+
match self.iter.next() {
683+
None => return None,
684+
Some(elt) => if self.other.contains(elt) {
685+
return Some(elt)
686+
},
687+
}
688+
}
689+
}
690+
691+
fn size_hint(&self) -> (uint, Option<uint>) {
692+
let (_, upper) = self.iter.size_hint();
693+
(0, upper)
694+
}
695+
}
696+
697+
impl<'a, T, S, H> Iterator<&'a T> for Difference<'a, T, H>
698+
where T: Eq + Hash<S>, H: Hasher<S>
699+
{
700+
fn next(&mut self) -> Option<&'a T> {
701+
loop {
702+
match self.iter.next() {
703+
None => return None,
704+
Some(elt) => if !self.other.contains(elt) {
705+
return Some(elt)
706+
},
707+
}
708+
}
709+
}
710+
711+
fn size_hint(&self) -> (uint, Option<uint>) {
712+
let (_, upper) = self.iter.size_hint();
713+
(0, upper)
714+
}
682715
}
683716

684-
impl<'a, T, H> Iterator<&'a T> for SymDifferenceItems<'a, T, H> {
717+
impl<'a, T, S, H> Iterator<&'a T> for SymmetricDifference<'a, T, H>
718+
where T: Eq + Hash<S>, H: Hasher<S>
719+
{
685720
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
686721
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
687722
}
688723

689-
impl<'a, T, H> Iterator<&'a T> for UnionItems<'a, T, H> {
724+
impl<'a, T, S, H> Iterator<&'a T> for Union<'a, T, H>
725+
where T: Eq + Hash<S>, H: Hasher<S>
726+
{
690727
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
691728
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
692729
}

0 commit comments

Comments
 (0)