epitools 0.5.95 → 0.5.96
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/epitools/core_ext/range.rb +37 -5
- data/spec/core_ext_spec.rb +5 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6bc0ff24a8ad6f3dc81034a1d358c5fc1f7130ac
|
4
|
+
data.tar.gz: b834243e264b44d0fa33492e6e80697342d558ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1108d095b65008e83a72b7e2f3904067550804e57d9d4351905f77aea5ffdf1880df671f161353ce2eade31a3c507c2311ca96e5bc20202b0d1f88af2bf363e4
|
7
|
+
data.tar.gz: f597a50f0cb08bf959db4c05f416de5bbd9ba9ca1219cc907800c2d9942535a9c02fb599c0f9572e26b0b7baba3e211843a71cce1ece531aab08e2c5d7fc159b
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.96
|
@@ -1,5 +1,12 @@
|
|
1
1
|
class Range
|
2
2
|
|
3
|
+
#
|
4
|
+
# The actual last value in the range (eg: `(1...10).actual_last == 9`)
|
5
|
+
#
|
6
|
+
def actual_last
|
7
|
+
exclude_end? ? last - 1 : last
|
8
|
+
end
|
9
|
+
|
3
10
|
#
|
4
11
|
# Pick a random number from the range.
|
5
12
|
#
|
@@ -25,15 +32,21 @@ class Range
|
|
25
32
|
end
|
26
33
|
|
27
34
|
#
|
28
|
-
# Return a new range which is the union of the two ranges
|
35
|
+
# Return a new range which is the union of the two ranges (even if the two ranges don't overlap)
|
29
36
|
#
|
30
37
|
def |(other)
|
31
|
-
|
32
|
-
|
38
|
+
vals = [
|
39
|
+
first,
|
40
|
+
other.first,
|
41
|
+
actual_last,
|
42
|
+
other.actual_last
|
43
|
+
].sort
|
44
|
+
|
45
|
+
(vals.first..vals.last)
|
33
46
|
end
|
34
47
|
|
35
48
|
#
|
36
|
-
# Merge two ranges
|
49
|
+
# Merge this range with another (if the two ranges overlap, then it returns an array containing a single merged range; if the two ranges are disjoint, an array with the two ranges is returned)
|
37
50
|
#
|
38
51
|
def merge(other)
|
39
52
|
if self.overlaps?(other)
|
@@ -48,7 +61,26 @@ class Range
|
|
48
61
|
#
|
49
62
|
def overlaps?(other)
|
50
63
|
# overlap == start < finish' AND start' < finish
|
51
|
-
self.first <= other.
|
64
|
+
self.first <= other.actual_last and other.first <= self.actual_last
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# Takes an array of ranges, and returns a new array where all overlapping ranges are combined into a single range
|
69
|
+
#
|
70
|
+
def self.optimize(ranges)
|
71
|
+
ranges = ranges.sort_by(&:first)
|
72
|
+
|
73
|
+
result = [ranges.first]
|
74
|
+
|
75
|
+
ranges[1..-1].each do |elem|
|
76
|
+
if result[-1].overlaps?(elem)
|
77
|
+
result[-1] = (result[-1] | elem)
|
78
|
+
else
|
79
|
+
result << elem
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
result
|
52
84
|
end
|
53
85
|
|
54
86
|
end
|
data/spec/core_ext_spec.rb
CHANGED
@@ -851,6 +851,11 @@ describe Range do
|
|
851
851
|
(10..30).overlaps?(1..15).should == true
|
852
852
|
end
|
853
853
|
|
854
|
+
it "optimizes" do
|
855
|
+
ranges = [ 1..5, 2..6, 7..10, 7..20, 25...50, 50..55 ].shuffle
|
856
|
+
Range.optimize(ranges).should == [ 1..6, 7..20, 25...50, 50..55 ]
|
857
|
+
end
|
858
|
+
|
854
859
|
end
|
855
860
|
|
856
861
|
|