diff --git a/src/libextra/bitv.rs b/src/libextra/bitv.rs index 44e5ca56c43..85002029fc6 100644 --- a/src/libextra/bitv.rs +++ b/src/libextra/bitv.rs @@ -717,6 +717,41 @@ impl 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 @@ impl Set for BitvSet { 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 for BitvSet { diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs index 4e66870a947..8a2d6cfcf49 100644 --- a/src/libextra/treemap.rs +++ b/src/libextra/treemap.rs @@ -396,9 +396,40 @@ impl Set for TreeSet { } true } +} + +impl MutableSet for TreeSet { + /// 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 TreeSet { + /// Create an empty TreeSet + #[inline] + pub fn new() -> TreeSet { 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, f: &fn(&T) -> bool) -> bool { + pub fn difference(&self, other: &TreeSet, f: &fn(&T) -> bool) -> bool { let mut x = self.iter(); let mut y = other.iter(); @@ -427,7 +458,7 @@ impl Set for TreeSet { } /// Visit the values (in-order) representing the symmetric difference - fn symmetric_difference(&self, other: &TreeSet, + pub fn symmetric_difference(&self, other: &TreeSet, f: &fn(&T) -> bool) -> bool { let mut x = self.iter(); let mut y = other.iter(); @@ -461,7 +492,7 @@ impl Set for TreeSet { } /// Visit the values (in-order) representing the intersection - fn intersection(&self, other: &TreeSet, f: &fn(&T) -> bool) -> bool { + pub fn intersection(&self, other: &TreeSet, f: &fn(&T) -> bool) -> bool { let mut x = self.iter(); let mut y = other.iter(); @@ -487,7 +518,7 @@ impl Set for TreeSet { } /// Visit the values (in-order) representing the union - fn union(&self, other: &TreeSet, f: &fn(&T) -> bool) -> bool { + pub fn union(&self, other: &TreeSet, f: &fn(&T) -> bool) -> bool { let mut x = self.iter(); let mut y = other.iter(); @@ -519,37 +550,6 @@ impl Set for TreeSet { } } -impl MutableSet for TreeSet { - /// 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 TreeSet { - /// Create an empty TreeSet - #[inline] - pub fn new() -> TreeSet { 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, ()> diff --git a/src/libstd/container.rs b/src/libstd/container.rs index d855beea50b..10f3fc6586f 100644 --- a/src/libstd/container.rs +++ b/src/libstd/container.rs @@ -87,17 +87,7 @@ pub trait Set: 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 diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index 8c06f23b8c1..ca61f3e5ad8 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -672,28 +672,6 @@ impl Set for HashSet { fn is_superset(&self, other: &HashSet) -> bool { other.is_subset(self) } - - /// Visit the values representing the difference - fn difference(&self, other: &HashSet, f: &fn(&T) -> bool) -> bool { - self.difference_iter(other).advance(f) - } - - /// Visit the values representing the symmetric difference - fn symmetric_difference(&self, - other: &HashSet, - f: &fn(&T) -> bool) -> bool { - self.symmetric_difference_iter(other).advance(f) - } - - /// Visit the values representing the intersection - fn intersection(&self, other: &HashSet, f: &fn(&T) -> bool) -> bool { - self.intersection_iter(other).advance(f) - } - - /// Visit the values representing the union - fn union(&self, other: &HashSet, f: &fn(&T) -> bool) -> bool { - self.union_iter(other).advance(f) - } } impl MutableSet for HashSet {