multi_range 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +9 -5
- data/lib/multi_range.rb +26 -15
- data/lib/multi_range/version.rb +1 -1
- data/multi_range.gemspec +2 -2
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9dce52f266fe1dd26eaccbac184ee8c1ed5cc160322d92ded5471fb3efda0ee4
|
4
|
+
data.tar.gz: d001904ffda1cf620638042afd72da56b1b810b457df6227bd1d9fb3e7986a4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36fb852c41b4802589fd950b3beadbccad986e8e5e31da5993acfd11e19339e7034eba7099bcd36903e74e592c397e4501682cae47f482c74ad9d86d136b25cc
|
7
|
+
data.tar.gz: 0a763c262c12b7b45fc0c47f50bbbee929a19c2399e9e9725f70d49222bcdf1d0860c8b1162d8a04e2f2b992e42287b2deacbbf6dbf9c051f98c3fad04ec0488
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
## Change Log
|
2
2
|
|
3
|
+
### [v1.1.0](https://github.com/khiav223577/multi_range/compare/v1.0.0...v1.1.0) 2020/10/15
|
4
|
+
- [#13](https://github.com/khiav223577/multi_range/pull/13) Rename flatten => merge_overlaps (@khiav223577)
|
5
|
+
|
3
6
|
### [v1.0.0](https://github.com/khiav223577/multi_range/compare/v0.0.4...v1.0.0) 2020/10/15
|
4
7
|
- [#12](https://github.com/khiav223577/multi_range/pull/12) Implement #overlaps? (@khiav223577)
|
5
8
|
- [#11](https://github.com/khiav223577/multi_range/pull/11) Support difference other multi_range object (@khiav223577)
|
data/README.md
CHANGED
@@ -25,7 +25,7 @@ Or install it yourself as:
|
|
25
25
|
|
26
26
|
## Usage
|
27
27
|
|
28
|
-
Allow you to manipulate a group of ranges.
|
28
|
+
Allow you to manipulate a group of ranges. Such as merging overlapping ranges, doing ranges union and difference.
|
29
29
|
|
30
30
|
### Sample a number
|
31
31
|
```rb
|
@@ -34,7 +34,7 @@ multi_range.sample
|
|
34
34
|
# => equals to [1, 2, 3, 4, 5, 10, 11, 12].sample
|
35
35
|
```
|
36
36
|
|
37
|
-
###
|
37
|
+
### Difference of ranges
|
38
38
|
```rb
|
39
39
|
multi_range = MultiRange.new([1..10])
|
40
40
|
multi_range -= 5..7
|
@@ -56,7 +56,7 @@ multi_range.ranges
|
|
56
56
|
# => [1...5, 61..70, 86..100]
|
57
57
|
```
|
58
58
|
|
59
|
-
###
|
59
|
+
### Union ranges
|
60
60
|
|
61
61
|
```rb
|
62
62
|
multi_range = MultiRange.new([1..5])
|
@@ -72,14 +72,18 @@ multi_range.ranges
|
|
72
72
|
# => [1..6, 10..25, 30..30]
|
73
73
|
```
|
74
74
|
|
75
|
-
###
|
75
|
+
### Merge overlaps
|
76
76
|
```rb
|
77
77
|
multi_range = MultiRange.new([1, 2, 4..6, 7, 8..12])
|
78
78
|
multi_range.merge_overlaps.ranges
|
79
79
|
# => [1..2, 4..12]
|
80
|
+
|
81
|
+
multi_range = MultiRange.new([1.2..1.5, 1.7..1.9, 1.8..2.2])
|
82
|
+
multi_range.merge_overlaps.ranges
|
83
|
+
# => [1.2..1.5, 1.7..2.2]
|
80
84
|
```
|
81
85
|
|
82
|
-
###
|
86
|
+
### Check if it overlaps with the other
|
83
87
|
|
84
88
|
```rb
|
85
89
|
multi_range = MultiRange.new([1..5, 10..15, 20..25])
|
data/lib/multi_range.rb
CHANGED
@@ -22,7 +22,8 @@ class MultiRange
|
|
22
22
|
attr_reader :ranges
|
23
23
|
|
24
24
|
def initialize(ranges)
|
25
|
-
@ranges = ranges.map{|s| s.is_a?(
|
25
|
+
@ranges = ranges.map{|s| s.is_a?(Numeric) ? s..s : s }.sort_by(&:begin).freeze
|
26
|
+
@is_float = @ranges.any?{|range| range.begin.is_a?(Float) || range.end.is_a?(Float) }
|
26
27
|
end
|
27
28
|
|
28
29
|
def merge_overlaps
|
@@ -33,13 +34,13 @@ class MultiRange
|
|
33
34
|
|
34
35
|
@ranges.each do |range|
|
35
36
|
next current_range = range if current_range == nil
|
36
|
-
next if range.
|
37
|
+
next if range.end <= current_range.end
|
37
38
|
|
38
|
-
if current_range
|
39
|
+
if can_combine?(current_range, range)
|
40
|
+
current_range = range.exclude_end? ? current_range.begin...range.end : current_range.begin..range.end
|
41
|
+
else
|
39
42
|
new_ranges << current_range
|
40
43
|
current_range = range
|
41
|
-
else
|
42
|
-
current_range = current_range.min..range.max
|
43
44
|
end
|
44
45
|
end
|
45
46
|
|
@@ -57,24 +58,26 @@ class MultiRange
|
|
57
58
|
new_ranges = @ranges.dup
|
58
59
|
|
59
60
|
return MultiRange.new(new_ranges) if new_ranges.empty?
|
60
|
-
return MultiRange.new(new_ranges) if other.
|
61
|
-
return MultiRange.new(new_ranges) if other.
|
61
|
+
return MultiRange.new(new_ranges) if other.begin > @ranges.last.end # 大於最大值
|
62
|
+
return MultiRange.new(new_ranges) if other.end < @ranges.first.begin # 小於最小值
|
62
63
|
|
63
64
|
changed_size = 0
|
64
65
|
@ranges.each_with_index do |range, idx|
|
65
|
-
next if other.
|
66
|
-
break if other.
|
66
|
+
next if other.begin > range.end # 大於這個 range
|
67
|
+
break if other.end < range.begin # 小於這個 range
|
68
|
+
|
69
|
+
sub_range1 = range.begin...other.begin
|
67
70
|
|
68
|
-
|
69
|
-
sub_range2 =
|
71
|
+
sub_range2_begin = other.exclude_end? ? other.end : other.end + (other.end.is_a?(Float) ? Float::EPSILON : 1)
|
72
|
+
sub_range2 = range.exclude_end? ? sub_range2_begin...range.end : sub_range2_begin..range.end
|
70
73
|
|
71
74
|
sub_ranges = []
|
72
|
-
sub_ranges << sub_range1 if sub_range1.
|
73
|
-
sub_ranges << sub_range2 if sub_range2.
|
75
|
+
sub_ranges << sub_range1 if sub_range1.begin <= sub_range1.end
|
76
|
+
sub_ranges << sub_range2 if sub_range2.begin <= sub_range2.end
|
74
77
|
|
75
78
|
new_ranges[idx + changed_size, 1] = sub_ranges
|
76
79
|
changed_size += sub_ranges.size - 1
|
77
|
-
break if other.
|
80
|
+
break if other.end <= range.end # 沒有超過一個 range 的範圍
|
78
81
|
end
|
79
82
|
|
80
83
|
return MultiRange.new(new_ranges)
|
@@ -87,7 +90,7 @@ class MultiRange
|
|
87
90
|
|
88
91
|
def overlaps?(other)
|
89
92
|
multi_range = merge_overlaps
|
90
|
-
return multi_range.
|
93
|
+
return multi_range.ranges != (multi_range - other).ranges
|
91
94
|
end
|
92
95
|
|
93
96
|
def sample
|
@@ -140,4 +143,12 @@ class MultiRange
|
|
140
143
|
range = @ranges.last
|
141
144
|
return range.max if range
|
142
145
|
end
|
146
|
+
|
147
|
+
private
|
148
|
+
|
149
|
+
# make sure that range1.begin <= range2.begin
|
150
|
+
def can_combine?(range1, range2)
|
151
|
+
return range1.end >= range2.begin if @is_float
|
152
|
+
return range1.end + 1 >= range2.begin
|
153
|
+
end
|
143
154
|
end
|
data/lib/multi_range/version.rb
CHANGED
data/multi_range.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ['khiav reoy']
|
10
10
|
spec.email = ['mrtmrt15xn@yahoo.com.tw']
|
11
11
|
|
12
|
-
spec.summary = '
|
13
|
-
spec.description = '
|
12
|
+
spec.summary = 'Allow you to manipulate a group of ranges. Such as merging overlapping ranges, doing ranges union and difference.'
|
13
|
+
spec.description = 'Allow you to manipulate a group of ranges. Such as merging overlapping ranges, doing ranges union and difference.'
|
14
14
|
spec.homepage = 'https://github.com/khiav223577/multi_range'
|
15
15
|
spec.license = 'MIT'
|
16
16
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi_range
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- khiav reoy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,8 +86,8 @@ dependencies:
|
|
86
86
|
- - "~>"
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: 1.1.1
|
89
|
-
description:
|
90
|
-
|
89
|
+
description: Allow you to manipulate a group of ranges. Such as merging overlapping
|
90
|
+
ranges, doing ranges union and difference.
|
91
91
|
email:
|
92
92
|
- mrtmrt15xn@yahoo.com.tw
|
93
93
|
executables: []
|
@@ -136,6 +136,6 @@ requirements: []
|
|
136
136
|
rubygems_version: 3.0.3
|
137
137
|
signing_key:
|
138
138
|
specification_version: 4
|
139
|
-
summary:
|
140
|
-
|
139
|
+
summary: Allow you to manipulate a group of ranges. Such as merging overlapping ranges,
|
140
|
+
doing ranges union and difference.
|
141
141
|
test_files: []
|