Skip to content

Commit 2fef18a

Browse files
committed
auto merge of #5310 : thestinger/rust/treeset, r=graydon
2 parents 9b9ffd5 + 9b1a9ec commit 2fef18a

File tree

1 file changed

+94
-113
lines changed

1 file changed

+94
-113
lines changed

src/libstd/treemap.rs

Lines changed: 94 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,13 @@ pure fn lt<K: Ord + TotalOrd, V>(a: &TreeMap<K, V>,
7777

7878
impl<K: Ord + TotalOrd, V> Ord for TreeMap<K, V> {
7979
#[inline(always)]
80-
pure fn lt(&self, other: &TreeMap<K, V>) -> bool {
81-
lt(self, other)
82-
}
80+
pure fn lt(&self, other: &TreeMap<K, V>) -> bool { lt(self, other) }
8381
#[inline(always)]
84-
pure fn le(&self, other: &TreeMap<K, V>) -> bool {
85-
!lt(other, self)
86-
}
82+
pure fn le(&self, other: &TreeMap<K, V>) -> bool { !lt(other, self) }
8783
#[inline(always)]
88-
pure fn ge(&self, other: &TreeMap<K, V>) -> bool {
89-
!lt(self, other)
90-
}
84+
pure fn ge(&self, other: &TreeMap<K, V>) -> bool { !lt(self, other) }
9185
#[inline(always)]
92-
pure fn gt(&self, other: &TreeMap<K, V>) -> bool {
93-
lt(other, self)
94-
}
86+
pure fn gt(&self, other: &TreeMap<K, V>) -> bool { lt(other, self) }
9587
}
9688

9789
impl<'self, K: TotalOrd, V> BaseIter<(&'self K, &'self V)> for TreeMap<K, V> {
@@ -149,9 +141,9 @@ impl<K: TotalOrd, V> Map<K, V> for TreeMap<K, V> {
149141
match *current {
150142
Some(ref r) => {
151143
match key.cmp(&r.key) {
152-
Less => current = &r.left,
153-
Greater => current = &r.right,
154-
Equal => return Some(&r.value)
144+
Less => current = &r.left,
145+
Greater => current = &r.right,
146+
Equal => return Some(&r.value)
155147
}
156148
}
157149
None => return None
@@ -244,19 +236,24 @@ pub struct TreeSet<T> {
244236

245237
impl<T: TotalOrd> BaseIter<T> for TreeSet<T> {
246238
/// Visit all values in order
239+
#[inline(always)]
247240
pure fn each(&self, f: &fn(&T) -> bool) { self.map.each_key(f) }
241+
#[inline(always)]
248242
pure fn size_hint(&self) -> Option<uint> { Some(self.len()) }
249243
}
250244

251245
impl<T: TotalOrd> ReverseIter<T> for TreeSet<T> {
252246
/// Visit all values in reverse order
247+
#[inline(always)]
253248
pure fn each_reverse(&self, f: &fn(&T) -> bool) {
254249
self.map.each_key_reverse(f)
255250
}
256251
}
257252

258253
impl<T: Eq + TotalOrd> Eq for TreeSet<T> {
254+
#[inline(always)]
259255
pure fn eq(&self, other: &TreeSet<T>) -> bool { self.map == other.map }
256+
#[inline(always)]
260257
pure fn ne(&self, other: &TreeSet<T>) -> bool { self.map != other.map }
261258
}
262259

@@ -273,29 +270,35 @@ impl<T: Ord + TotalOrd> Ord for TreeSet<T> {
273270

274271
impl<T: TotalOrd> Container for TreeSet<T> {
275272
/// Return the number of elements in the set
273+
#[inline(always)]
276274
pure fn len(&self) -> uint { self.map.len() }
277275

278276
/// Return true if the set contains no elements
277+
#[inline(always)]
279278
pure fn is_empty(&self) -> bool { self.map.is_empty() }
280279
}
281280

282281
impl<T: TotalOrd> Mutable for TreeSet<T> {
283282
/// Clear the set, removing all values.
283+
#[inline(always)]
284284
fn clear(&mut self) { self.map.clear() }
285285
}
286286

287287
impl<T: TotalOrd> Set<T> for TreeSet<T> {
288288
/// Return true if the set contains a value
289+
#[inline(always)]
289290
pure fn contains(&self, value: &T) -> bool {
290291
self.map.contains_key(value)
291292
}
292293

293294
/// Add a value to the set. Return true if the value was not already
294295
/// present in the set.
296+
#[inline(always)]
295297
fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
296298

297299
/// Remove a value from the set. Return true if the value was
298300
/// present in the set.
301+
#[inline(always)]
299302
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
300303

301304
/// Return true if the set has no elements in common with `other`.
@@ -320,6 +323,7 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
320323
}
321324

322325
/// Return true if the set is a subset of another
326+
#[inline(always)]
323327
pure fn is_subset(&self, other: &TreeSet<T>) -> bool {
324328
other.is_superset(self)
325329
}
@@ -482,16 +486,21 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
482486
a = set_next(&mut x);
483487
}
484488
}
489+
do b.while_some |b1| {
490+
if f(b1) { set_next(&mut y) } else { None }
491+
}
485492
}
486493
}
487494
}
488495

489496
pub impl <T: TotalOrd> TreeSet<T> {
490497
/// Create an empty TreeSet
498+
#[inline(always)]
491499
static pure fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
492500

493501
/// Get a lazy iterator over the values in the set.
494502
/// Requires that it be frozen (immutable).
503+
#[inline(always)]
495504
pure fn iter(&self) -> TreeSetIterator/&self<T> {
496505
TreeSetIterator{iter: self.map.iter()}
497506
}
@@ -504,13 +513,15 @@ pub struct TreeSetIterator<T> {
504513

505514
/// Advance the iterator to the next node (in order). If this iterator is
506515
/// finished, does nothing.
516+
#[inline(always)]
507517
pub fn set_next<T>(iter: &mut TreeSetIterator/&r<T>) -> Option<&r/T> {
508518
do map_next(&mut iter.iter).map |&(value, _)| { value }
509519
}
510520

511521
/// Advance the iterator through the set
512-
fn set_advance<T>(iter: &mut TreeSetIterator/&r<T>,
513-
f: &fn(&r/T) -> bool) {
522+
#[inline(always)]
523+
pub fn set_advance<T>(iter: &mut TreeSetIterator/&r<T>,
524+
f: &fn(&r/T) -> bool) {
514525
do map_advance(&mut iter.iter) |(k, _)| { f(k) }
515526
}
516527

@@ -532,15 +543,15 @@ pub impl<K: TotalOrd, V> TreeNode<K, V> {
532543
}
533544

534545
pure fn each<K: TotalOrd, V>(node: &r/Option<~TreeNode<K, V>>,
535-
f: &fn(&(&r/K, &r/V)) -> bool) {
546+
f: &fn(&(&r/K, &r/V)) -> bool) {
536547
for node.each |x| {
537548
each(&x.left, f);
538549
if f(&(&x.key, &x.value)) { each(&x.right, f) }
539550
}
540551
}
541552

542553
pure fn each_reverse<K: TotalOrd, V>(node: &r/Option<~TreeNode<K, V>>,
543-
f: &fn(&(&r/K, &r/V)) -> bool) {
554+
f: &fn(&(&r/K, &r/V)) -> bool) {
544555
for node.each |x| {
545556
each_reverse(&x.right, f);
546557
if f(&(&x.key, &x.value)) { each_reverse(&x.left, f) }
@@ -665,20 +676,20 @@ fn remove<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>,
665676
skew(save);
666677

667678
match save.right {
668-
Some(ref mut right) => {
669-
skew(right);
670-
match right.right {
671-
Some(ref mut x) => { skew(x) },
672-
None => ()
673-
}
679+
Some(ref mut right) => {
680+
skew(right);
681+
match right.right {
682+
Some(ref mut x) => { skew(x) },
683+
None => ()
674684
}
675-
None => ()
685+
}
686+
None => ()
676687
}
677688

678689
split(save);
679690
match save.right {
680-
Some(ref mut x) => { split(x) },
681-
None => ()
691+
Some(ref mut x) => { split(x) },
692+
None => ()
682693
}
683694
}
684695

@@ -1101,112 +1112,82 @@ mod test_set {
11011112
}
11021113
}
11031114

1104-
#[test]
1105-
fn test_intersection() {
1106-
let mut a = TreeSet::new();
1107-
let mut b = TreeSet::new();
1115+
fn check(a: &[int], b: &[int], expected: &[int],
1116+
f: &fn(&TreeSet<int>, &TreeSet<int>, f: &fn(&int) -> bool)) {
1117+
let mut set_a = TreeSet::new();
1118+
let mut set_b = TreeSet::new();
11081119

1109-
fail_unless!(a.insert(11));
1110-
fail_unless!(a.insert(1));
1111-
fail_unless!(a.insert(3));
1112-
fail_unless!(a.insert(77));
1113-
fail_unless!(a.insert(103));
1114-
fail_unless!(a.insert(5));
1115-
fail_unless!(a.insert(-5));
1116-
1117-
fail_unless!(b.insert(2));
1118-
fail_unless!(b.insert(11));
1119-
fail_unless!(b.insert(77));
1120-
fail_unless!(b.insert(-9));
1121-
fail_unless!(b.insert(-42));
1122-
fail_unless!(b.insert(5));
1123-
fail_unless!(b.insert(3));
1120+
for a.each |x| { fail_unless!(set_a.insert(*x)) }
1121+
for b.each |y| { fail_unless!(set_b.insert(*y)) }
11241122

11251123
let mut i = 0;
1126-
let expected = [3, 5, 11, 77];
1127-
for a.intersection(&b) |x| {
1124+
for f(&set_a, &set_b) |x| {
11281125
fail_unless!(*x == expected[i]);
1129-
i += 1
1126+
i += 1;
11301127
}
11311128
fail_unless!(i == expected.len());
11321129
}
11331130

11341131
#[test]
1135-
fn test_difference() {
1136-
let mut a = TreeSet::new();
1137-
let mut b = TreeSet::new();
1138-
1139-
fail_unless!(a.insert(1));
1140-
fail_unless!(a.insert(3));
1141-
fail_unless!(a.insert(5));
1142-
fail_unless!(a.insert(9));
1143-
fail_unless!(a.insert(11));
1132+
fn test_intersection() {
1133+
fn check_intersection(a: &[int], b: &[int], expected: &[int]) {
1134+
check(a, b, expected, |x, y, z| x.intersection(y, z))
1135+
}
11441136

1145-
fail_unless!(b.insert(3));
1146-
fail_unless!(b.insert(9));
1137+
check_intersection([], [], []);
1138+
check_intersection([1, 2, 3], [], []);
1139+
check_intersection([], [1, 2, 3], []);
1140+
check_intersection([2], [1, 2, 3], [2]);
1141+
check_intersection([1, 2, 3], [2], [2]);
1142+
check_intersection([11, 1, 3, 77, 103, 5, -5],
1143+
[2, 11, 77, -9, -42, 5, 3],
1144+
[3, 5, 11, 77]);
1145+
}
11471146

1148-
let mut i = 0;
1149-
let expected = [1, 5, 11];
1150-
for a.difference(&b) |x| {
1151-
fail_unless!(*x == expected[i]);
1152-
i += 1
1147+
#[test]
1148+
fn test_difference() {
1149+
fn check_difference(a: &[int], b: &[int], expected: &[int]) {
1150+
check(a, b, expected, |x, y, z| x.difference(y, z))
11531151
}
1154-
fail_unless!(i == expected.len());
1152+
1153+
check_difference([], [], []);
1154+
check_difference([1, 12], [], [1, 12]);
1155+
check_difference([], [1, 2, 3, 9], []);
1156+
check_difference([1, 3, 5, 9, 11],
1157+
[3, 9],
1158+
[1, 5, 11]);
1159+
check_difference([-5, 11, 22, 33, 40, 42],
1160+
[-12, -5, 14, 23, 34, 38, 39, 50],
1161+
[11, 22, 33, 40, 42]);
11551162
}
11561163

11571164
#[test]
11581165
fn test_symmetric_difference() {
1159-
let mut a = TreeSet::new();
1160-
let mut b = TreeSet::new();
1161-
1162-
fail_unless!(a.insert(1));
1163-
fail_unless!(a.insert(3));
1164-
fail_unless!(a.insert(5));
1165-
fail_unless!(a.insert(9));
1166-
fail_unless!(a.insert(11));
1167-
1168-
fail_unless!(b.insert(-2));
1169-
fail_unless!(b.insert(3));
1170-
fail_unless!(b.insert(9));
1171-
fail_unless!(b.insert(14));
1172-
fail_unless!(b.insert(22));
1173-
1174-
let mut i = 0;
1175-
let expected = [-2, 1, 5, 11, 14, 22];
1176-
for a.symmetric_difference(&b) |x| {
1177-
fail_unless!(*x == expected[i]);
1178-
i += 1
1166+
fn check_symmetric_difference(a: &[int], b: &[int],
1167+
expected: &[int]) {
1168+
check(a, b, expected, |x, y, z| x.symmetric_difference(y, z))
11791169
}
1180-
fail_unless!(i == expected.len());
1170+
1171+
check_symmetric_difference([], [], []);
1172+
check_symmetric_difference([1, 2, 3], [2], [1, 3]);
1173+
check_symmetric_difference([2], [1, 2, 3], [1, 3]);
1174+
check_symmetric_difference([1, 3, 5, 9, 11],
1175+
[-2, 3, 9, 14, 22],
1176+
[-2, 1, 5, 11, 14, 22]);
11811177
}
11821178

11831179
#[test]
11841180
fn test_union() {
1185-
let mut a = TreeSet::new();
1186-
let mut b = TreeSet::new();
1187-
1188-
fail_unless!(a.insert(1));
1189-
fail_unless!(a.insert(3));
1190-
fail_unless!(a.insert(5));
1191-
fail_unless!(a.insert(9));
1192-
fail_unless!(a.insert(11));
1193-
fail_unless!(a.insert(16));
1194-
fail_unless!(a.insert(19));
1195-
fail_unless!(a.insert(24));
1196-
1197-
fail_unless!(b.insert(-2));
1198-
fail_unless!(b.insert(1));
1199-
fail_unless!(b.insert(5));
1200-
fail_unless!(b.insert(9));
1201-
fail_unless!(b.insert(13));
1202-
fail_unless!(b.insert(19));
1203-
1204-
let mut i = 0;
1205-
let expected = [-2, 1, 3, 5, 9, 11, 13, 16, 19, 24];
1206-
for a.union(&b) |x| {
1207-
fail_unless!(*x == expected[i]);
1208-
i += 1
1181+
fn check_union(a: &[int], b: &[int],
1182+
expected: &[int]) {
1183+
check(a, b, expected, |x, y, z| x.union(y, z))
12091184
}
1210-
fail_unless!(i == expected.len());
1185+
1186+
check_union([], [], []);
1187+
check_union([1, 2, 3], [2], [1, 2, 3]);
1188+
check_union([2], [1, 2, 3], [1, 2, 3]);
1189+
check_union([1, 3, 5, 9, 11, 16, 19, 24],
1190+
[-2, 1, 5, 9, 13, 19],
1191+
[-2, 1, 3, 5, 9, 11, 13, 16, 19, 24]);
12111192
}
12121193
}

0 commit comments

Comments
 (0)