Skip to content

Commit 0902079

Browse files
committed
---
yaml --- r: 41319 b: refs/heads/snap-stage3 c: 99eb4dd h: refs/heads/master i: 41317: 10a53c0 41315: 5518cf6 41311: cd071e6 v: v3
1 parent e9b9cec commit 0902079

File tree

5 files changed

+181
-38
lines changed

5 files changed

+181
-38
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 09bb07bed9166105ea961a42b5fff7739ae0d2e9
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: ad3506bd1e6d0b3fe686dfc5ae26010a10af6e05
4+
refs/heads/snap-stage3: 99eb4ddddd68e9ffa86eb8df264934925e27d737
55
refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/AUTHORS.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ Eric Holmes <[email protected]>
5353
Erick Tryzelaar <[email protected]>
5454
Erik Rose <[email protected]>
5555
Evan McClanahan <[email protected]>
56-
Felix S. Klock II <[email protected]>
5756
Francisco Souza <[email protected]>
5857
Franklin Chen <[email protected]>
5958

branches/snap-stage3/src/libcore/container.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,20 @@ 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 has no elements in common with `other`.
70+
/// This is equivalent to checking for an empty intersection.
71+
pure fn is_disjoint(&self, other: &self) -> bool;
72+
73+
/// Return true if the set is a subset of another
74+
pure fn is_subset(&self, other: &self) -> bool;
75+
76+
/// Return true if the set is a superset of another
77+
pure fn is_superset(&self, other: &self) -> bool;
78+
79+
/// Visit the values representing the difference
80+
pure fn difference(&self, other: &self, f: fn(&T) -> bool);
81+
82+
/// Visit the values representing the symmetric difference
83+
pure fn symmetric_difference(&self, other: &self, f: fn(&T) -> bool);
6884
}

branches/snap-stage3/src/libcore/hashmap.rs

Lines changed: 141 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
#[forbid(deprecated_mode)];
1515
#[forbid(deprecated_pattern)];
1616

17+
use container::{Container, Mutable, Map, Set};
1718
use cmp::Eq;
1819
use hash::Hash;
1920
use to_bytes::IterBytes;
2021

2122
/// Open addressing with linear probing.
2223
pub mod linear {
24+
use super::*;
2325
use iter::BaseIter;
24-
use container::{Container, Mutable, Map, Set};
25-
use cmp::Eq;
26-
use cmp;
2726
use hash::Hash;
27+
use iter;
2828
use kinds::Copy;
2929
use option::{None, Option, Some};
3030
use option;
@@ -453,6 +453,38 @@ pub mod linear {
453453
/// Remove a value from the set. Return true if the value was
454454
/// present in the set.
455455
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
456+
457+
/// Return true if the set has no elements in common with `other`.
458+
/// This is equivalent to checking for an empty intersection.
459+
pure fn is_disjoint(&self, other: &LinearSet<T>) -> bool {
460+
iter::all(self, |v| !other.contains(v))
461+
}
462+
463+
/// Return true if the set is a subset of another
464+
pure fn is_subset(&self, other: &LinearSet<T>) -> bool {
465+
iter::all(self, |v| other.contains(v))
466+
}
467+
468+
/// Return true if the set is a superset of another
469+
pure fn is_superset(&self, other: &LinearSet<T>) -> bool {
470+
other.is_subset(self)
471+
}
472+
473+
/// Visit the values representing the difference
474+
pure fn difference(&self, other: &LinearSet<T>, f: fn(&T) -> bool) {
475+
for self.each |v| {
476+
if !other.contains(v) {
477+
if !f(v) { return; }
478+
}
479+
}
480+
}
481+
482+
/// Visit the values representing the symmetric difference
483+
pure fn symmetric_difference(&self, other: &LinearSet<T>,
484+
f: fn(&T) -> bool) {
485+
self.difference(other, f);
486+
other.difference(self, f);
487+
}
456488
}
457489

458490
pub impl <T: Hash IterBytes Eq> LinearSet<T> {
@@ -462,7 +494,7 @@ pub mod linear {
462494
}
463495

464496
#[test]
465-
pub mod test {
497+
mod test_map {
466498
use container::{Container, Mutable, Map, Set};
467499
use option::{None, Some};
468500
use hashmap::linear::LinearMap;
@@ -610,3 +642,108 @@ pub mod test {
610642
assert !m.is_empty();
611643
}
612644
}
645+
646+
#[test]
647+
mod test_set {
648+
use super::*;
649+
650+
#[test]
651+
fn test_disjoint() {
652+
let mut xs = linear::LinearSet::new();
653+
let mut ys = linear::LinearSet::new();
654+
assert xs.is_disjoint(&ys);
655+
assert ys.is_disjoint(&xs);
656+
assert xs.insert(5);
657+
assert ys.insert(11);
658+
assert xs.is_disjoint(&ys);
659+
assert ys.is_disjoint(&xs);
660+
assert xs.insert(7);
661+
assert xs.insert(19);
662+
assert xs.insert(4);
663+
assert ys.insert(2);
664+
assert ys.insert(-11);
665+
assert xs.is_disjoint(&ys);
666+
assert ys.is_disjoint(&xs);
667+
assert ys.insert(7);
668+
assert !xs.is_disjoint(&ys);
669+
assert !ys.is_disjoint(&xs);
670+
}
671+
672+
#[test]
673+
fn test_subset_and_superset() {
674+
let mut a = linear::LinearSet::new();
675+
assert a.insert(0);
676+
assert a.insert(5);
677+
assert a.insert(11);
678+
assert a.insert(7);
679+
680+
let mut b = linear::LinearSet::new();
681+
assert b.insert(0);
682+
assert b.insert(7);
683+
assert b.insert(19);
684+
assert b.insert(250);
685+
assert b.insert(11);
686+
assert b.insert(200);
687+
688+
assert !a.is_subset(&b);
689+
assert !a.is_superset(&b);
690+
assert !b.is_subset(&a);
691+
assert !b.is_superset(&a);
692+
693+
assert b.insert(5);
694+
695+
assert a.is_subset(&b);
696+
assert !a.is_superset(&b);
697+
assert !b.is_subset(&a);
698+
assert b.is_superset(&a);
699+
}
700+
701+
#[test]
702+
fn test_difference() {
703+
let mut a = linear::LinearSet::new();
704+
let mut b = linear::LinearSet::new();
705+
706+
assert a.insert(1);
707+
assert a.insert(3);
708+
assert a.insert(5);
709+
assert a.insert(9);
710+
assert a.insert(11);
711+
712+
assert b.insert(3);
713+
assert b.insert(9);
714+
715+
let mut i = 0;
716+
let expected = [1, 5, 11];
717+
for a.difference(&b) |x| {
718+
assert vec::contains(expected, x);
719+
i += 1
720+
}
721+
assert i == expected.len();
722+
}
723+
724+
#[test]
725+
fn test_symmetric_difference() {
726+
let mut a = linear::LinearSet::new();
727+
let mut b = linear::LinearSet::new();
728+
729+
assert a.insert(1);
730+
assert a.insert(3);
731+
assert a.insert(5);
732+
assert a.insert(9);
733+
assert a.insert(11);
734+
735+
assert b.insert(-2);
736+
assert b.insert(3);
737+
assert b.insert(9);
738+
assert b.insert(14);
739+
assert b.insert(22);
740+
741+
let mut i = 0;
742+
let expected = [-2, 1, 5, 11, 14, 22];
743+
for a.symmetric_difference(&b) |x| {
744+
assert vec::contains(expected, x);
745+
i += 1
746+
}
747+
assert i == expected.len();
748+
}
749+
}

branches/snap-stage3/src/libstd/treemap.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ use core::prelude::*;
2929
// range search - O(log n) retrieval of an iterator from some key
3030

3131
// (possibly) implement the overloads Python does for sets:
32-
// * union: |
3332
// * intersection: &
3433
// * difference: -
3534
// * symmetric difference: ^
35+
// * union: |
3636
// These would be convenient since the methods work like `each`
3737

3838
pub struct TreeMap<K, V> {
@@ -49,10 +49,9 @@ impl <K: Eq Ord, V: Eq> TreeMap<K, V>: Eq {
4949
let mut y = other.iter();
5050
for self.len().times {
5151
unsafe { // unsafe as a purity workaround
52-
// ICE: x.next() != y.next()
53-
5452
x = x.next();
5553
y = y.next();
54+
// FIXME: #4492 (ICE), x.get() == y.get()
5655
let (x1, x2) = x.get().unwrap();
5756
let (y1, y2) = y.get().unwrap();
5857

@@ -292,22 +291,6 @@ impl <T: Ord> TreeSet<T>: Set<T> {
292291
/// Remove a value from the set. Return true if the value was
293292
/// present in the set.
294293
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
295-
}
296-
297-
impl <T: Ord> TreeSet<T> {
298-
/// Create an empty TreeSet
299-
static pure fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
300-
301-
/// Visit all values in reverse order
302-
pure fn each_reverse(&self, f: fn(&T) -> bool) {
303-
self.map.each_key_reverse(f)
304-
}
305-
306-
/// Get a lazy iterator over the values in the set.
307-
/// Requires that it be frozen (immutable).
308-
pure fn iter(&self) -> TreeSetIterator/&self<T> {
309-
TreeSetIterator{iter: self.map.iter()}
310-
}
311294

312295
/// Return true if the set has no elements in common with `other`.
313296
/// This is equivalent to checking for an empty intersection.
@@ -336,12 +319,12 @@ impl <T: Ord> TreeSet<T> {
336319
true
337320
}
338321

339-
/// Check of the set is a subset of another
322+
/// Return true if the set is a subset of another
340323
pure fn is_subset(&self, other: &TreeSet<T>) -> bool {
341324
other.is_superset(self)
342325
}
343326

344-
/// Check of the set is a superset of another
327+
/// Return true if the set is a superset of another
345328
pure fn is_superset(&self, other: &TreeSet<T>) -> bool {
346329
let mut x = self.iter();
347330
let mut y = other.iter();
@@ -449,6 +432,22 @@ impl <T: Ord> TreeSet<T> {
449432
}
450433
}
451434
}
435+
}
436+
437+
impl <T: Ord> TreeSet<T> {
438+
/// Create an empty TreeSet
439+
static pure fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
440+
441+
/// Visit all values in reverse order
442+
pure fn each_reverse(&self, f: fn(&T) -> bool) {
443+
self.map.each_key_reverse(f)
444+
}
445+
446+
/// Get a lazy iterator over the values in the set.
447+
/// Requires that it be frozen (immutable).
448+
pure fn iter(&self) -> TreeSetIterator/&self<T> {
449+
TreeSetIterator{iter: self.map.iter()}
450+
}
452451

453452
/// Visit the values (in-order) representing the intersection
454453
pure fn intersection(&self, other: &TreeSet<T>, f: fn(&T) -> bool) {
@@ -652,14 +651,12 @@ fn remove<K: Ord, V>(node: &mut Option<~TreeNode<K, V>>, key: &K) -> bool {
652651
let mut left = save.left.swap_unwrap();
653652
if left.right.is_some() {
654653
heir_swap(save, &mut left.right);
655-
save.left = Some(left);
656-
remove(&mut save.left, key);
657654
} else {
658655
save.key <-> left.key;
659656
save.value <-> left.value;
660-
save.left = Some(left);
661-
remove(&mut save.left, key);
662657
}
658+
save.left = Some(left);
659+
remove(&mut save.left, key);
663660
} else {
664661
save = save.left.swap_unwrap();
665662
}
@@ -969,9 +966,7 @@ mod test_treemap {
969966
let m = m;
970967
let mut iter = m.iter();
971968

972-
// ICE:
973-
//assert iter.next() == Some((&x1, &y1));
974-
//assert iter.next().eq(&Some((&x1, &y1)));
969+
// FIXME: #4492 (ICE): iter.next() == Some((&x1, &y1))
975970

976971
iter = iter.next();
977972
assert iter.get().unwrap() == (&x1, &y1);
@@ -984,10 +979,6 @@ mod test_treemap {
984979
iter = iter.next();
985980
assert iter.get().unwrap() == (&x5, &y5);
986981

987-
// ICE:
988-
//assert iter.next() == None;
989-
//assert iter.next().eq(&None);
990-
991982
iter = iter.next();
992983
assert iter.get().is_none();
993984
}

0 commit comments

Comments
 (0)