swak 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/swak/interval.rb +17 -13
  2. 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
- def Interval.lists_intersect(list1, list2)
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::sort_by_end)
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
- j = 0
24
- list1.each_with_index do |int1, i|
25
- next if int1[1] <= list2[j][0] # int1 is strictly less than int2, so walk int1 forward
26
-
27
- # int2 is strictly less than int1, walk it forward
28
- while j < list2.size && list2[j][1] <= int1[0]
29
- j += 1
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
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: swak
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jesse Rodriguez