From 9fb49088b35ae82c290ed112cdc87b71986d3d7e Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Tue, 15 Jan 2013 15:55:27 -0500 Subject: [PATCH] make is_disjoint O(n+m) instead of O(n*log(m)) --- src/libstd/treemap.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index f8abdaac2797..686a2bf9b234 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -237,8 +237,24 @@ impl TreeSet { /// 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) -> 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