Skip to content

Commit 3fe6faa

Browse files
thestingergraydon
authored andcommitted
make is_superset/is_subset O(n+m) instead of O(n*log(m))
1 parent 4f92d8f commit 3fe6faa

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/libstd/treemap.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,35 @@ impl <T: Ord> TreeSet<T> {
259259

260260
/// Check of the set is a subset of another
261261
pure fn is_subset(&self, other: &TreeSet<T>) -> bool {
262-
// FIXME: this is a naive O(n*log(m)) implementation, could be O(n + m)
263-
!iter::any(self, |x| !other.contains(x))
262+
other.is_superset(self)
264263
}
265264

266265
/// Check of the set is a superset of another
267266
pure fn is_superset(&self, other: &TreeSet<T>) -> bool {
268-
other.is_subset(self)
267+
let mut x = self.iter();
268+
let mut y = other.iter();
269+
unsafe { // purity workaround
270+
let mut a = x.next();
271+
let mut b = y.next();
272+
while b.is_some() {
273+
if a.is_none() {
274+
return false
275+
}
276+
277+
let a1 = a.unwrap();
278+
let b1 = b.unwrap();
279+
280+
if b1 < a1 {
281+
return false
282+
}
283+
284+
if !(a1 < b1) {
285+
b = y.next();
286+
}
287+
a = x.next();
288+
}
289+
}
290+
true
269291
}
270292

271293
/// Visit the values (in-order) representing the difference

0 commit comments

Comments
 (0)