swak 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/swak/interval.rb +54 -5
  2. metadata +2 -2
@@ -13,14 +13,13 @@ module Swak
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)
17
- list1 = list1.sort_by(&Interval::sort_by_start)
18
- list2 = list2.sort_by(&Interval::sort_by_start)
19
-
16
+ def Interval.lists_intersect_nonoverlap(list1, list2)
20
17
  results = []
21
-
22
18
  return results if list1.size == 0 || list2.size == 0
23
19
 
20
+ list1 = list1.sort_by(&Interval::sort_by_start)
21
+ list2 = list2.sort_by(&Interval::sort_by_start)
22
+
24
23
  for int1 in list1
25
24
  # Throw out anything in list2 that ends before int1 starts
26
25
  while list2.size > 0 && list2.first[1] <= int1[0]
@@ -41,5 +40,55 @@ module Swak
41
40
  return results
42
41
  end
43
42
 
43
+ # Assumes that boths lists dont overlap with themselves
44
+ def Interval.lists_intersect(list1, list2)
45
+ results = []
46
+ return results if list1.size == 0 || list2.size == 0
47
+
48
+ list1 = list1.sort_by(&Interval::sort_by_start)
49
+ list2 = list2.sort_by(&Interval::sort_by_end)
50
+
51
+
52
+ # Compute the minimum of all start values at each position or after
53
+ # This will help us terminate early after pass our query interval
54
+ # Note: this will not help in the degenerate case when there is 1 interval that spans the entire first list
55
+ min_starts = Array.new(list2.size, nil)
56
+ min_starts[list2.size - 1] = list2[list2.size - 1][0]
57
+ if list2.size >= 2
58
+ i = list2.size - 2
59
+ while i >= 0
60
+ min_starts[i] = [min_starts[i+1], list2[i][0]].min
61
+ i -= 1
62
+ end
63
+ end
64
+
65
+
66
+ for int1 in list1
67
+ while list2.size > 0 && list2.first[1] <= int1[0]
68
+ list2.shift
69
+ min_starts.shift
70
+ end
71
+ break if list2.size == 0
72
+
73
+ list2.each_with_index do |int2, i|
74
+ break if min_starts[i] >= int2[1] # We can stop because no intervals (this one or later) start before the current interval ends
75
+ results << [int1, int2] if intersect(int1, int2)
76
+ end
77
+ end
78
+
79
+ return results
80
+
81
+ end
82
+
83
+ def Interval.lists_intersect_brute(list1, list2)
84
+ results = []
85
+ for int1 in list1
86
+ for int2 in list2
87
+ results << [int1, int2] if intersect(int1, int2)
88
+ end
89
+ end
90
+ return results
91
+ end
92
+
44
93
  end
45
94
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: swak
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jesse Rodriguez
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-06-15 00:00:00 Z
13
+ date: 2012-06-16 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: Random tools and mixins