Skip to content

Commit 9fb4908

Browse files
thestingergraydon
authored andcommitted
make is_disjoint O(n+m) instead of O(n*log(m))
1 parent 8935771 commit 9fb4908

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/libstd/treemap.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,24 @@ impl <T: Ord> TreeSet<T> {
237237
/// Return true if the set has no elements in common with `other`.
238238
/// This is equivalent to checking for an empty intersection.
239239
pure fn is_disjoint(&self, other: &TreeSet<T>) -> bool {
240-
// FIXME: this is a naive O(n*log(m)) implementation, could be O(n + m)
241-
!iter::any(self, |x| other.contains(x))
240+
let mut x = self.iter();
241+
let mut y = other.iter();
242+
unsafe { // purity workaround
243+
let mut a = x.next();
244+
let mut b = y.next();
245+
while a.is_some() && b.is_some() {
246+
let a1 = a.unwrap();
247+
let b1 = b.unwrap();
248+
if a1 < b1 {
249+
a = x.next();
250+
} else if b1 < a1 {
251+
b = y.next();
252+
} else {
253+
return false;
254+
}
255+
}
256+
}
257+
true
242258
}
243259

244260
/// Check of the set is a subset of another

0 commit comments

Comments
 (0)