multi_range 1.1.0 → 1.2.0

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
  SHA256:
3
- metadata.gz: 282c94c92ba01c11fb4e1efb2adc24ebe368d466660bd741878023bd1fb9c857
4
- data.tar.gz: 2eb44495bac41d991ad6c29049b8f97395c848401e00ea96161ea0d101bb78ed
3
+ metadata.gz: 9dce52f266fe1dd26eaccbac184ee8c1ed5cc160322d92ded5471fb3efda0ee4
4
+ data.tar.gz: d001904ffda1cf620638042afd72da56b1b810b457df6227bd1d9fb3e7986a4e
5
5
  SHA512:
6
- metadata.gz: 1c6bda5d61296f7aaf9dbb743477119b01a037be1085a7df85178340c97096fb8629b7cf384f4163d55177cd20be12b28b04b9fb48240c85dd6d5f5495612984
7
- data.tar.gz: f23bf8796b02954a7b4724bf96b9bbe6779e284b70a563e4042996c97a62387125d120be5f6b1a451b1c7f982a20395774e7067544b4e7cb591785f096d49c4e
6
+ metadata.gz: 36fb852c41b4802589fd950b3beadbccad986e8e5e31da5993acfd11e19339e7034eba7099bcd36903e74e592c397e4501682cae47f482c74ad9d86d136b25cc
7
+ data.tar.gz: 0a763c262c12b7b45fc0c47f50bbbee929a19c2399e9e9725f70d49222bcdf1d0860c8b1162d8a04e2f2b992e42287b2deacbbf6dbf9c051f98c3fad04ec0488
@@ -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
- ### Range difference
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
- ### Range union
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
- ### Flatten
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
- ### Overlaps?
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])
@@ -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?(Integer) ? s..s : s }.sort_by(&:begin).freeze
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.max <= current_range.max
37
+ next if range.end <= current_range.end
37
38
 
38
- if current_range.max + 1 < range.min
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.min > @ranges.last.max # 大於最大值
61
- return MultiRange.new(new_ranges) if other.max < @ranges.first.min # 小於最小值
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.min > range.max # 大於這個 range
66
- break if other.max < range.min # 小於這個 range
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
- sub_range1 = range.min...other.min
69
- sub_range2 = (other.max + 1)..range.max
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.any?
73
- sub_ranges << sub_range2 if sub_range2.any?
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.max <= range.max # 沒有超過一個 range 的範圍
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.size != (multi_range - other).size
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class MultiRange
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -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 = 'Provides cross-rails methods for you to upgrade rails, backport features, create easy-to-maintain gems, and so on.'
13
- spec.description = 'Provides cross-rails methods for you to upgrade rails, backport features, create easy-to-maintain gems, and so on.'
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.1.0
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-15 00:00:00.000000000 Z
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: Provides cross-rails methods for you to upgrade rails, backport features,
90
- create easy-to-maintain gems, and so on.
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: Provides cross-rails methods for you to upgrade rails, backport features,
140
- create easy-to-maintain gems, and so on.
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: []