提交 e5a64f2a 编写于 作者: B blake2-ppc

std: Remove the internal iterator methods from trait Set

.intersection(), .union() etc methods in trait std::container::Set use
internal iters. Remove these methods from the trait.

I reported issue #8154 for the reinstatement of iterator-based set algebra
methods to the Set trait.

For bitv and treemap, that lack Iterator implementations of set
operations, preserve them as methods directly on the types themselves.

For HashSet, these methods are replaced by the present .union_iter()
etc.
上级 310e0b6e
......@@ -717,6 +717,41 @@ pub fn symmetric_difference_with(&mut self, other: &BitvSet) {
pub fn iter<'a>(&'a self) -> BitvSetIterator<'a> {
BitvSetIterator {set: self, next_idx: 0}
}
pub fn difference(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
for self.common_iter(other).advance |(i, w1, w2)| {
if !iterate_bits(i, w1 & !w2, |b| f(&b)) {
return false;
}
}
/* everything we have that they don't also shows up */
self.outlier_iter(other).advance(|(mine, i, w)|
!mine || iterate_bits(i, w, |b| f(&b))
)
}
pub fn symmetric_difference(&self, other: &BitvSet,
f: &fn(&uint) -> bool) -> bool {
for self.common_iter(other).advance |(i, w1, w2)| {
if !iterate_bits(i, w1 ^ w2, |b| f(&b)) {
return false;
}
}
self.outlier_iter(other).advance(|(_, i, w)| iterate_bits(i, w, |b| f(&b)))
}
pub fn intersection(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
self.common_iter(other).advance(|(i, w1, w2)| iterate_bits(i, w1 & w2, |b| f(&b)))
}
pub fn union(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
for self.common_iter(other).advance |(i, w1, w2)| {
if !iterate_bits(i, w1 | w2, |b| f(&b)) {
return false;
}
}
self.outlier_iter(other).advance(|(_, i, w)| iterate_bits(i, w, |b| f(&b)))
}
}
impl cmp::Eq for BitvSet {
......@@ -785,41 +820,6 @@ fn is_subset(&self, other: &BitvSet) -> bool {
fn is_superset(&self, other: &BitvSet) -> bool {
other.is_subset(self)
}
fn difference(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
for self.common_iter(other).advance |(i, w1, w2)| {
if !iterate_bits(i, w1 & !w2, |b| f(&b)) {
return false;
}
}
/* everything we have that they don't also shows up */
self.outlier_iter(other).advance(|(mine, i, w)|
!mine || iterate_bits(i, w, |b| f(&b))
)
}
fn symmetric_difference(&self, other: &BitvSet,
f: &fn(&uint) -> bool) -> bool {
for self.common_iter(other).advance |(i, w1, w2)| {
if !iterate_bits(i, w1 ^ w2, |b| f(&b)) {
return false;
}
}
self.outlier_iter(other).advance(|(_, i, w)| iterate_bits(i, w, |b| f(&b)))
}
fn intersection(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
self.common_iter(other).advance(|(i, w1, w2)| iterate_bits(i, w1 & w2, |b| f(&b)))
}
fn union(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
for self.common_iter(other).advance |(i, w1, w2)| {
if !iterate_bits(i, w1 | w2, |b| f(&b)) {
return false;
}
}
self.outlier_iter(other).advance(|(_, i, w)| iterate_bits(i, w, |b| f(&b)))
}
}
impl MutableSet<uint> for BitvSet {
......
......@@ -396,9 +396,40 @@ fn is_superset(&self, other: &TreeSet<T>) -> bool {
}
true
}
}
impl<T: TotalOrd> MutableSet<T> for TreeSet<T> {
/// Add a value to the set. Return true if the value was not already
/// present in the set.
#[inline]
fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
/// Remove a value from the set. Return true if the value was
/// present in the set.
#[inline]
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
}
impl<T: TotalOrd> TreeSet<T> {
/// Create an empty TreeSet
#[inline]
pub fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
/// Get a lazy iterator over the values in the set.
/// Requires that it be frozen (immutable).
#[inline]
pub fn iter<'a>(&'a self) -> TreeSetIterator<'a, T> {
TreeSetIterator{iter: self.map.iter()}
}
/// Visit all values in reverse order
#[inline]
pub fn each_reverse(&self, f: &fn(&T) -> bool) -> bool {
self.map.each_key_reverse(f)
}
/// Visit the values (in-order) representing the difference
fn difference(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
pub fn difference(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
let mut x = self.iter();
let mut y = other.iter();
......@@ -427,7 +458,7 @@ fn difference(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
}
/// Visit the values (in-order) representing the symmetric difference
fn symmetric_difference(&self, other: &TreeSet<T>,
pub fn symmetric_difference(&self, other: &TreeSet<T>,
f: &fn(&T) -> bool) -> bool {
let mut x = self.iter();
let mut y = other.iter();
......@@ -461,7 +492,7 @@ fn symmetric_difference(&self, other: &TreeSet<T>,
}
/// Visit the values (in-order) representing the intersection
fn intersection(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
pub fn intersection(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
let mut x = self.iter();
let mut y = other.iter();
......@@ -487,7 +518,7 @@ fn intersection(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
}
/// Visit the values (in-order) representing the union
fn union(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
pub fn union(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
let mut x = self.iter();
let mut y = other.iter();
......@@ -519,37 +550,6 @@ fn union(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
}
}
impl<T: TotalOrd> MutableSet<T> for TreeSet<T> {
/// Add a value to the set. Return true if the value was not already
/// present in the set.
#[inline]
fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
/// Remove a value from the set. Return true if the value was
/// present in the set.
#[inline]
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
}
impl<T: TotalOrd> TreeSet<T> {
/// Create an empty TreeSet
#[inline]
pub fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
/// Get a lazy iterator over the values in the set.
/// Requires that it be frozen (immutable).
#[inline]
pub fn iter<'a>(&'a self) -> TreeSetIterator<'a, T> {
TreeSetIterator{iter: self.map.iter()}
}
/// Visit all values in reverse order
#[inline]
pub fn each_reverse(&self, f: &fn(&T) -> bool) -> bool {
self.map.each_key_reverse(f)
}
}
/// Lazy forward iterator over a set
pub struct TreeSetIterator<'self, T> {
priv iter: TreeMapIterator<'self, T, ()>
......
......@@ -87,17 +87,7 @@ pub trait Set<T>: Container {
/// Return true if the set is a superset of another
fn is_superset(&self, other: &Self) -> bool;
/// Visit the values representing the difference
fn difference(&self, other: &Self, f: &fn(&T) -> bool) -> bool;
/// Visit the values representing the symmetric difference
fn symmetric_difference(&self, other: &Self, f: &fn(&T) -> bool) -> bool;
/// Visit the values representing the intersection
fn intersection(&self, other: &Self, f: &fn(&T) -> bool) -> bool;
/// Visit the values representing the union
fn union(&self, other: &Self, f: &fn(&T) -> bool) -> bool;
// FIXME #8154: Add difference, sym. difference, intersection and union iterators
}
/// This trait represents actions which can be performed on sets to mutate
......
......@@ -672,28 +672,6 @@ fn is_subset(&self, other: &HashSet<T>) -> bool {
fn is_superset(&self, other: &HashSet<T>) -> bool {
other.is_subset(self)
}
/// Visit the values representing the difference
fn difference(&self, other: &HashSet<T>, f: &fn(&T) -> bool) -> bool {
self.difference_iter(other).advance(f)
}
/// Visit the values representing the symmetric difference
fn symmetric_difference(&self,
other: &HashSet<T>,
f: &fn(&T) -> bool) -> bool {
self.symmetric_difference_iter(other).advance(f)
}
/// Visit the values representing the intersection
fn intersection(&self, other: &HashSet<T>, f: &fn(&T) -> bool) -> bool {
self.intersection_iter(other).advance(f)
}
/// Visit the values representing the union
fn union(&self, other: &HashSet<T>, f: &fn(&T) -> bool) -> bool {
self.union_iter(other).advance(f)
}
}
impl<T:Hash + Eq> MutableSet<T> for HashSet<T> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册