Skip to content

Commit 5150cf0

Browse files
committed
---
yaml --- r: 42111 b: refs/heads/master c: bfa9c9a h: refs/heads/master i: 42109: 6d697e7 42107: 6807641 42103: 9b15c94 42095: 8b45c67 42079: df6601b 42047: f810075 41983: 3f927ed v: v3
1 parent 4c69db7 commit 5150cf0

File tree

4 files changed

+90
-39
lines changed

4 files changed

+90
-39
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 456af7a79da586a3c64d810a9157bba0616b6b53
2+
refs/heads/master: bfa9c9a00f0a301a5e936ae14c6a95e98f3bf5ee
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2f46b763da2c098913884f101b6d71d69af41b49
55
refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650

trunk/src/libcore/container.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,10 @@ pub trait Set<T>: Mutable {
6565
/// Remove a value from the set. Return true if the value was
6666
/// present in the set.
6767
fn remove(&mut self, value: &T) -> bool;
68+
69+
/// Return true if the set is a subset of another
70+
pure fn is_subset(&self, other: &self) -> bool;
71+
72+
/// Return true if the set is a superset of another
73+
pure fn is_superset(&self, other: &self) -> bool;
6874
}

trunk/src/libcore/hashmap.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub mod linear {
2525
use cmp::Eq;
2626
use cmp;
2727
use hash::Hash;
28+
use iter;
2829
use kinds::Copy;
2930
use option::{None, Option, Some};
3031
use option;
@@ -453,6 +454,16 @@ pub mod linear {
453454
/// Remove a value from the set. Return true if the value was
454455
/// present in the set.
455456
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
457+
458+
/// Return true if the set is a subset of another
459+
pure fn is_subset(&self, other: &LinearSet<T>) -> bool {
460+
iter::all(self, |v| other.contains(v))
461+
}
462+
463+
/// Return true if the set is a superset of another
464+
pure fn is_superset(&self, other: &LinearSet<T>) -> bool {
465+
other.is_subset(self)
466+
}
456467
}
457468

458469
pub impl <T: Hash IterBytes Eq> LinearSet<T> {
@@ -462,7 +473,7 @@ pub mod linear {
462473
}
463474

464475
#[test]
465-
pub mod test {
476+
mod test_map {
466477
use container::{Container, Mutable, Map, Set};
467478
use option::{None, Some};
468479
use hashmap::linear::LinearMap;
@@ -610,3 +621,37 @@ pub mod test {
610621
assert !m.is_empty();
611622
}
612623
}
624+
625+
#[test]
626+
mod test_set {
627+
use super::*;
628+
629+
#[test]
630+
fn test_subset_and_superset() {
631+
let mut a = linear::LinearSet::new();
632+
assert a.insert(0);
633+
assert a.insert(5);
634+
assert a.insert(11);
635+
assert a.insert(7);
636+
637+
let mut b = linear::LinearSet::new();
638+
assert b.insert(0);
639+
assert b.insert(7);
640+
assert b.insert(19);
641+
assert b.insert(250);
642+
assert b.insert(11);
643+
assert b.insert(200);
644+
645+
assert !a.is_subset(&b);
646+
assert !a.is_superset(&b);
647+
assert !b.is_subset(&a);
648+
assert !b.is_superset(&a);
649+
650+
assert b.insert(5);
651+
652+
assert a.is_subset(&b);
653+
assert !a.is_superset(&b);
654+
assert !b.is_subset(&a);
655+
assert b.is_superset(&a);
656+
}
657+
}

trunk/src/libstd/treemap.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,43 @@ impl <T: Ord> TreeSet<T>: Set<T> {
291291
/// Remove a value from the set. Return true if the value was
292292
/// present in the set.
293293
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
294+
295+
/// Return true if the set is a subset of another
296+
pure fn is_subset(&self, other: &TreeSet<T>) -> bool {
297+
other.is_superset(self)
298+
}
299+
300+
/// Return true if the set is a superset of another
301+
pure fn is_superset(&self, other: &TreeSet<T>) -> bool {
302+
let mut x = self.iter();
303+
let mut y = other.iter();
304+
unsafe { // purity workaround
305+
x = x.next();
306+
y = y.next();
307+
let mut a = x.get();
308+
let mut b = y.get();
309+
while b.is_some() {
310+
if a.is_none() {
311+
return false
312+
}
313+
314+
let a1 = a.unwrap();
315+
let b1 = b.unwrap();
316+
317+
if b1 < a1 {
318+
return false
319+
}
320+
321+
if !(a1 < b1) {
322+
y = y.next();
323+
b = y.get();
324+
}
325+
x = x.next();
326+
a = x.get();
327+
}
328+
}
329+
true
330+
}
294331
}
295332

296333
impl <T: Ord> TreeSet<T> {
@@ -335,43 +372,6 @@ impl <T: Ord> TreeSet<T> {
335372
true
336373
}
337374

338-
/// Check of the set is a subset of another
339-
pure fn is_subset(&self, other: &TreeSet<T>) -> bool {
340-
other.is_superset(self)
341-
}
342-
343-
/// Check of the set is a superset of another
344-
pure fn is_superset(&self, other: &TreeSet<T>) -> bool {
345-
let mut x = self.iter();
346-
let mut y = other.iter();
347-
unsafe { // purity workaround
348-
x = x.next();
349-
y = y.next();
350-
let mut a = x.get();
351-
let mut b = y.get();
352-
while b.is_some() {
353-
if a.is_none() {
354-
return false
355-
}
356-
357-
let a1 = a.unwrap();
358-
let b1 = b.unwrap();
359-
360-
if b1 < a1 {
361-
return false
362-
}
363-
364-
if !(a1 < b1) {
365-
y = y.next();
366-
b = y.get();
367-
}
368-
x = x.next();
369-
a = x.get();
370-
}
371-
}
372-
true
373-
}
374-
375375
/// Visit the values (in-order) representing the difference
376376
pure fn difference(&self, other: &TreeSet<T>, f: fn(&T) -> bool) {
377377
let mut x = self.iter();

0 commit comments

Comments
 (0)