swak 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/swak/interval.rb +17 -13
- metadata +1 -1
data/lib/swak/interval.rb
CHANGED
@@ -12,26 +12,30 @@ module Swak
|
|
12
12
|
return x[0] < y[1] && x[1] > y[0]
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
# Assumes that boths lists dont overlap with themselves
|
16
|
+
def Interval.nonoverlap_list_intersects_other_list(list1, list2)
|
16
17
|
list1 = list1.sort_by(&Interval::sort_by_start)
|
17
|
-
list2 = list2.sort_by(&Interval::
|
18
|
+
list2 = list2.sort_by(&Interval::sort_by_start)
|
18
19
|
|
19
20
|
results = []
|
20
21
|
|
21
22
|
return results if list1.size == 0 || list2.size == 0
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
24
|
+
for int1 in list1
|
25
|
+
# Throw out anything in list2 that ends before int1 starts
|
26
|
+
while list2.size > 0 && list2.first[1] <= int1[0]
|
27
|
+
list2.shift
|
28
|
+
end
|
29
|
+
break if list2.size == 0
|
30
|
+
|
31
|
+
# Now we know the first element of list2 ends somewhere after int1 starts
|
32
|
+
for int2 in list2
|
33
|
+
if int2[0] < int1[1] # It must overlap if int2 starts before int1 ends
|
34
|
+
results << [int1, int2]
|
35
|
+
else
|
36
|
+
break
|
37
|
+
end
|
30
38
|
end
|
31
|
-
|
32
|
-
break if j >= list2.size
|
33
|
-
|
34
|
-
results << [int1, list2[j]] if intersect(int1, list2[j])
|
35
39
|
end
|
36
40
|
|
37
41
|
return results
|