multi_range 0.0.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 201ed1151cccd779945250523ac3872fc7496d6b
4
- data.tar.gz: 8fe06100aeffb7392877ad7c5ce51561eabc65b4
2
+ SHA256:
3
+ metadata.gz: 82aa8d60271246bff1316449278536578153d944fd33bb8c1247175c055d98c4
4
+ data.tar.gz: bac913e6497fc532f69a5a4dfca6066b9856b5ab82f80279a9d3669013a5620d
5
5
  SHA512:
6
- metadata.gz: 341b27fe3a106b36021fc5480d6e3529941c3b32e18e2d7280bb1264a8d2cebf694d6fda12f2eba0a27812cb668eb48e0e4503fedd144b1836bdebc7b715d17d
7
- data.tar.gz: ce029763753f86cf0ad073bcccb8ed66c482f288b5bd50d1229179b5afd744d285267ba9364f63728847852ff35bc5a58c7332a9c4b0185ef90b180f587e3ffa
6
+ metadata.gz: 99c502e9fb9998ef235af41f696cdb41cdfb0559ed4431baff0b28e4873564c1c44e60c86638a7f72ff3f1f247b41effb807defb83aa1c91ed56225ff46f70f4
7
+ data.tar.gz: 4804d00e47dbaec3c2f1a5013489fcd4a4a2f86204419176676b7c11e88c4fa9114c9f13956ad8c2e491a05619813af2f1de923eb334edec2b0a370a145d55a1
@@ -76,10 +76,6 @@ Layout/EndAlignment:
76
76
  Description: 'Align ends correctly.'
77
77
  Enabled: true
78
78
 
79
- Lint/EndInMethod:
80
- Description: 'END blocks should not be placed inside method definitions.'
81
- Enabled: true
82
-
83
79
  Lint/EnsureReturn:
84
80
  Description: 'Do not use return in an ensure block.'
85
81
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-return-ensure'
@@ -581,9 +577,12 @@ Style/For:
581
577
  Style/FormatString:
582
578
  Description: 'Enforce the use of Kernel#sprintf, Kernel#format or String#%.'
583
579
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#sprintf'
584
- EnforcedStyle: sprintf
580
+ EnforcedStyle: percent
585
581
  Enabled: true
586
582
 
583
+ Style/FormatStringToken:
584
+ Enabled: false
585
+
587
586
  Style/GlobalVars:
588
587
  Description: 'Do not introduce global variables.'
589
588
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#instance-vars'
@@ -809,7 +808,7 @@ Style/PercentQLiterals:
809
808
  Style/PerlBackrefs:
810
809
  Description: 'Avoid Perl-style regex back references.'
811
810
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-perl-regexp-last-matchers'
812
- Enabled: true
811
+ Enabled: false
813
812
 
814
813
  Naming/PredicateName:
815
814
  Description: 'Check the names of predicate methods.'
@@ -1,5 +1,8 @@
1
1
  ## Change Log
2
2
 
3
+ ### [v0.0.4](https://github.com/khiav223577/multi_range/compare/v0.0.3...v0.0.4) 2020/03/26
4
+ - [#7](https://github.com/khiav223577/multi_range/pull/7) Add flatten method (@khiav223577)
5
+
3
6
  ### [v0.0.3](https://github.com/khiav223577/multi_range/compare/v0.0.2...v0.0.3) 2020/03/02
4
7
  - [#6](https://github.com/khiav223577/multi_range/pull/6) freeze internal ranges variable (@khiav223577)
5
8
  - [#5](https://github.com/khiav223577/multi_range/pull/5) Add min, max, to_a method (@khiav223577)
data/README.md CHANGED
@@ -49,6 +49,29 @@ multi_range.ranges
49
49
  # => [1...5, 86..100]
50
50
  ```
51
51
 
52
+ ```rb
53
+ multi_range = MultiRange.new([1..10, 50..70, 80..100])
54
+ multi_range -= MultiRange.new([5..60, 75..85])
55
+ multi_range.ranges
56
+ # => [1...5, 61..70, 86..100]
57
+ ```
58
+
59
+ ### Range union
60
+
61
+ ```rb
62
+ multi_range = MultiRange.new([1..5])
63
+ multi_range |= 3..8
64
+ multi_range.ranges
65
+ # => [1..8]
66
+ ```
67
+
68
+ ```rb
69
+ multi_range = MultiRange.new([1..5, 10..15, 20..25])
70
+ multi_range |= MultiRange.new([3..6, 14..22, 30])
71
+ multi_range.ranges
72
+ # => [1..6, 10..25, 30..30]
73
+ ```
74
+
52
75
  ### Flatten
53
76
  ```rb
54
77
  multi_range = MultiRange.new([1, 2, 4..6, 7, 8..12])
@@ -56,6 +79,26 @@ multi_range.flatten.ranges
56
79
  # => [1..2, 4..12]
57
80
  ```
58
81
 
82
+ ### Overlaps?
83
+
84
+ ```rb
85
+ multi_range = MultiRange.new([1..5, 10..15, 20..25])
86
+ multi_range.overlaps?(7..8)
87
+ # => false
88
+
89
+ multi_range.overlaps?(3..8)
90
+ # => true
91
+
92
+ multi_range.overlaps?(7..12)
93
+ # => true
94
+ ```
95
+
96
+ ```rb
97
+ multi_range = MultiRange.new([1..5, 10..15, 20..25])
98
+ multi_range.overlaps?(MultiRange.new([6..8, 18..22]))
99
+ # => true
100
+ ```
101
+
59
102
  ### Range-like interface
60
103
 
61
104
  #### each
@@ -94,6 +137,50 @@ MultiRange.new([1..3, 6, 8..9]).to_a
94
137
  # => [1, 2, 3, 6, 8, 9]
95
138
  ```
96
139
 
140
+
141
+ #### size
142
+ ```rb
143
+ MultiRange.new([1..3, 6, 8..9]).size
144
+ # => 6
145
+ ```
146
+
147
+ ### Warning
148
+
149
+ The return value may be different when there are some overlapped ranges.
150
+ Call `flatten` if you want to merge overlapped ranges.
151
+
152
+ ```rb
153
+ MultiRange.new([1..5, 3..6]).to_a
154
+ # => [1, 2, 3, 4, 5, 3, 4, 5, 6]
155
+
156
+ MultiRange.new([1..5, 3..6]).flatten.to_a
157
+ # => [1, 2, 3, 4, 5, 6]
158
+ ```
159
+
160
+ ```rb
161
+ MultiRange.new([1..5, 3..6]).each{|s| print s }
162
+ # => 123453456
163
+
164
+ MultiRange.new([1..5, 3..6]).flatten.each{|s| print s }
165
+ # => 123456
166
+ ```
167
+
168
+ ```rb
169
+ MultiRange.new([1..5, 3..6]).map{|s| s * 2 }
170
+ # => [2, 4, 6, 8, 10, 6, 8, 10, 12]
171
+
172
+ MultiRange.new([1..5, 3..6]).flatten.map{|s| s * 2 }
173
+ # => [2, 4, 6, 8, 10, 12]
174
+ ```
175
+
176
+ ```rb
177
+ MultiRange.new([1..5, 3..6]).size
178
+ # => 9
179
+
180
+ MultiRange.new([1..5, 3..6]).flatten.size
181
+ # => 6
182
+ ```
183
+
97
184
  ## Development
98
185
 
99
186
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'multi_range/version'
2
4
  require 'roulette-wheel-selection'
3
5
 
@@ -19,12 +21,12 @@ class MultiRange
19
21
 
20
22
  attr_reader :ranges
21
23
 
22
- def initialize(ranges) # range 要由小到大排序,且各 range 不能重疊
23
- @ranges = ranges.map{|s| s.is_a?(Integer) ? s..s : s }.freeze
24
+ def initialize(ranges)
25
+ @ranges = ranges.map{|s| s.is_a?(Integer) ? s..s : s }.sort_by(&:begin).freeze
24
26
  end
25
27
 
26
28
  def flatten
27
- return if @ranges.size == 0
29
+ return MultiRange.new([]) if @ranges.size == 0
28
30
 
29
31
  new_ranges = []
30
32
  current_range = nil
@@ -46,6 +48,12 @@ class MultiRange
46
48
  end
47
49
 
48
50
  def -(other)
51
+ if other.is_a?(MultiRange)
52
+ new_multi_range = dup
53
+ other.ranges.each{|range| new_multi_range -= range }
54
+ return new_multi_range
55
+ end
56
+
49
57
  new_ranges = @ranges.dup
50
58
 
51
59
  return MultiRange.new(new_ranges) if new_ranges.empty?
@@ -72,6 +80,16 @@ class MultiRange
72
80
  return MultiRange.new(new_ranges)
73
81
  end
74
82
 
83
+ def |(other)
84
+ other_ranges = other.is_a?(MultiRange) ? other.ranges : [other]
85
+ return MultiRange.new(@ranges + other_ranges).flatten
86
+ end
87
+
88
+ def overlaps?(other)
89
+ multi_range = flatten
90
+ return multi_range.size != (multi_range - other).size
91
+ end
92
+
75
93
  def sample
76
94
  range = RouletteWheelSelection.sample(@ranges.map{|s| [s, s.size] }.to_h)
77
95
  return nil if range == nil
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class MultiRange
2
- VERSION = '0.0.4'
4
+ VERSION = '1.0.0'
3
5
  end
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: 0.0.4
4
+ version: 1.0.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-03-26 00:00:00.000000000 Z
11
+ date: 2020-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -133,8 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  - !ruby/object:Gem::Version
134
134
  version: '0'
135
135
  requirements: []
136
- rubyforge_project:
137
- rubygems_version: 2.6.14
136
+ rubygems_version: 3.0.3
138
137
  signing_key:
139
138
  specification_version: 4
140
139
  summary: Provides cross-rails methods for you to upgrade rails, backport features,