epitools 0.5.95 → 0.5.96

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '09ec6a706e691b75f78ba25055e8c79b2beb3944'
4
- data.tar.gz: 4ebd4dad0730f0ff7434583261d45e50a7427320
3
+ metadata.gz: 6bc0ff24a8ad6f3dc81034a1d358c5fc1f7130ac
4
+ data.tar.gz: b834243e264b44d0fa33492e6e80697342d558ee
5
5
  SHA512:
6
- metadata.gz: 2c43291944e5e7c7e1d409e9df8e33d30d9ff02cfcc1d29b478f231476b8c1d4fa72edae5219f63bad5bbd7da4e4d2aee50794b1b3b1977fbfae3bcc10f0db71
7
- data.tar.gz: 78595a9e4d78eea0f8b626827dd14dddd4a4c29f4c6b286ca50def65aeeaa778f51fdbb26713ec80f3d908239c70c4d245602fac969757a6ac69d91e83bbf229
6
+ metadata.gz: 1108d095b65008e83a72b7e2f3904067550804e57d9d4351905f77aea5ffdf1880df671f161353ce2eade31a3c507c2311ca96e5bc20202b0d1f88af2bf363e4
7
+ data.tar.gz: f597a50f0cb08bf959db4c05f416de5bbd9ba9ca1219cc907800c2d9942535a9c02fb599c0f9572e26b0b7baba3e211843a71cce1ece531aab08e2c5d7fc159b
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.95
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
- mins, maxes = minmax.zip(other.minmax)
32
- (mins.min..maxes.max)
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.last and other.first <= self.last
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
@@ -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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epitools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.95
4
+ version: 0.5.96
5
5
  platform: ruby
6
6
  authors:
7
7
  - epitron