提交 9fb49088 编写于 作者: D Daniel Micay 提交者: Graydon Hoare

make is_disjoint O(n+m) instead of O(n*log(m))

上级 89357713
......@@ -237,8 +237,24 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
/// Return true if the set has no elements in common with `other`.
/// This is equivalent to checking for an empty intersection.
pure fn is_disjoint(&self, other: &TreeSet<T>) -> bool {
// FIXME: this is a naive O(n*log(m)) implementation, could be O(n + m)
!iter::any(self, |x| other.contains(x))
let mut x = self.iter();
let mut y = other.iter();
unsafe { // purity workaround
let mut a = x.next();
let mut b = y.next();
while a.is_some() && b.is_some() {
let a1 = a.unwrap();
let b1 = b.unwrap();
if a1 < b1 {
a = x.next();
} else if b1 < a1 {
b = y.next();
} else {
return false;
}
}
}
true
}
/// Check of the set is a subset of another
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册