better_ranges 1.0.0 → 1.1.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
2
  SHA1:
3
- metadata.gz: c468002e6d99d8b363b9d9d8633e06b38ff87b3a
4
- data.tar.gz: e3f6c169d6cc951555576ec423f068a2d2dc3fab
3
+ metadata.gz: d04f8f961e79e072b4f76c04a5d24c56a2dc6ed5
4
+ data.tar.gz: 6c0041e4a5e835d3d48e4e45644700fbe2f837f3
5
5
  SHA512:
6
- metadata.gz: bcf7070b2a67d0aafc67552b9f794a15ffc853aee82905d1406cf8bdfeb1655f857cbe992b1f31143fab4ad04b13699f3e7dc9cfa52240362f6a68cafcc95307
7
- data.tar.gz: 2a248c8da4946747fcbb047006f921736862177e4603992d42a4fd13b3ee9c85096f920e3643457a1ce048d9d503deae26000e586a5a16e50c51afe3ba249d22
6
+ metadata.gz: 673874f7f3571d50b407737720a377d1294901a83301643395eddf1c458022dce9e3ad028153bd49798bc9cc659a3d0c6bf369b5f5bde22614dfaf29ecfe1da0
7
+ data.tar.gz: 91c59ea36adddae6243a8033992560c9b35d2baf7177309e0252239893f93a715efcb5037a44182cfe50062a18b760b81935e9c4d2e3f4a2587ee4d06559a737
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Gem Version](https://badge.fury.io/rb/better_ranges.png)](http://badge.fury.io/rb/better_ranges)
2
+
1
3
  # BetterRanges
2
4
 
3
5
  Adds support for basic set operations to the ruby Range class through the addition of a new SparseRange class.
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = BetterRanges::VERSION
9
9
  spec.authors = ['Michael Ziminsky']
10
10
  spec.email = ['mgziminsky@gmail.com']
11
- spec.summary = 'Add support for set operations to the ruby Range class'
11
+ spec.summary = 'Adds support for basic set operations to the ruby Range class through the addition of a new SparseRange class'
12
12
  spec.homepage = 'https://github.com/mgziminsky/better_ranges'
13
13
  spec.license = 'MIT'
14
14
 
@@ -2,19 +2,23 @@ require 'better_ranges/sparse_range'
2
2
 
3
3
  module BetterRanges
4
4
  module RangeOperators
5
- def |(x)
6
- SparseRange.new(self, *x)
5
+ def |(other)
6
+ SparseRange.new(self, *other)
7
7
  end
8
8
 
9
- def -(x)
10
- SparseRange.new(self) - x
9
+ def -(other)
10
+ SparseRange.new(self) - other
11
11
  end
12
12
 
13
- def &(x)
14
- SparseRange.new(self) & x
13
+ def &(other)
14
+ SparseRange.new(self) & other
15
15
  end
16
16
 
17
- alias :+ :|
17
+ alias_method :+, :|
18
+ alias_method :union, :|
19
+
20
+ alias_method :minus, :-
21
+ alias_method :intersect, :&
18
22
  end
19
23
  end
20
24
 
@@ -3,13 +3,10 @@ module BetterRanges
3
3
  include Enumerable
4
4
 
5
5
  def initialize(*data)
6
- @ranges = [*data].map! do |x|
7
- if (x.is_a?(Enumerable))
8
- if (x.none?)
9
- return nil
10
- elsif (x.is_a?(SparseRange))
11
- return x.ranges.clone
12
- end
6
+ @data = [*data].map! do |x|
7
+ if x.is_a?(Enumerable)
8
+ return nil if x.none?
9
+ return x.data.clone if x.is_a?(SparseRange)
13
10
  end
14
11
  x
15
12
  end
@@ -18,8 +15,8 @@ module BetterRanges
18
15
 
19
16
  def each(&block)
20
17
  Enumerator.new do |yielder|
21
- @ranges.each do |r|
22
- yield_each(r){ |x| yielder.yield x}
18
+ @data.each do |r|
19
+ yield_each(r) { |x| yielder.yield x }
23
20
  end
24
21
  end.each(&block)
25
22
  end
@@ -27,9 +24,9 @@ module BetterRanges
27
24
  def step(num = 1, &block)
28
25
  i = 0
29
26
  Enumerator.new do |yielder|
30
- @ranges.each do |r|
27
+ @data.each do |r|
31
28
  yield_each(r) do |x|
32
- yielder.yield x if ((i % num) == 0)
29
+ yielder.yield x if (i % num) == 0
33
30
  i += 1
34
31
  end
35
32
  end
@@ -37,33 +34,33 @@ module BetterRanges
37
34
  end
38
35
 
39
36
  def last
40
- read_val(@ranges.last).last
37
+ read_val(@data.last).last
41
38
  end
42
39
 
43
40
  def |(x)
44
- SparseRange.new(@ranges, *x)
41
+ SparseRange.new(@data, *x)
45
42
  end
46
43
 
47
44
  def -(x)
48
45
  diff = SparseRange.new
49
- diff_data = diff.ranges
46
+ diff_data = diff.data
50
47
 
51
48
  i = 0
52
49
  next_val = lambda do
53
- throw :done unless i < @ranges.length
54
- v = read_val(@ranges[i])
50
+ throw :done unless i < @data.length
51
+ v = read_val(@data[i])
55
52
  i += 1
56
53
  v
57
54
  end
58
55
 
59
56
  catch(:done) do
60
- other = (x.is_a?(SparseRange) ? x : SparseRange.new(x)).ranges
57
+ other = (x.is_a?(SparseRange) ? x : SparseRange.new(x)).data
61
58
 
62
59
  start, finish = next_val.call
63
60
  other.each do |r|
64
61
  other_start, other_finish = read_val(r)
65
62
 
66
- while (finish < other_start)
63
+ while finish < other_start
67
64
  diff_data << write_val(start, finish)
68
65
  start, finish = next_val.call
69
66
  end
@@ -83,23 +80,23 @@ module BetterRanges
83
80
 
84
81
  def &(x)
85
82
  intersect = SparseRange.new
86
- intersect_data = intersect.ranges
83
+ intersect_data = intersect.data
87
84
 
88
85
  i = 0
89
86
  next_val = lambda do
90
- throw :done unless i < @ranges.length
91
- v = read_val(@ranges[i])
87
+ throw :done unless i < @data.length
88
+ v = read_val(@data[i])
92
89
  i += 1
93
90
  v
94
91
  end
95
92
 
96
93
  catch(:done) do
97
- other = (x.is_a?(SparseRange) ? x : SparseRange.new(x)).ranges
94
+ other = (x.is_a?(SparseRange) ? x : SparseRange.new(x)).data
98
95
 
99
96
  start, finish = next_val.call
100
97
  other.each do |r|
101
98
  other_start, other_finish = read_val(r)
102
- start, finish = next_val.call while (finish < other_start)
99
+ start, finish = next_val.call while finish < other_start
103
100
 
104
101
  until other_finish < start
105
102
  first = [start, other_start].max
@@ -120,34 +117,34 @@ module BetterRanges
120
117
  end
121
118
 
122
119
  def <<(x)
123
- @ranges << [*(x.is_a?(SparseRange) ? x.ranges : x)]
120
+ @data << [*(x.is_a?(SparseRange) ? x.data : x)]
124
121
  optimize
125
122
 
126
123
  self
127
124
  end
128
125
 
129
126
  def inspect
130
- @ranges.inspect
127
+ @data.inspect
131
128
  end
132
129
 
133
130
  def include?(x)
134
- @ranges.any? do |r|
131
+ @data.any? do |r|
135
132
  r.is_a?(Range) ? r.include?(x) : x == r
136
133
  end
137
134
  end
138
135
 
139
136
  def empty?
140
- @ranges.empty?
137
+ @data.empty? || size == 0
141
138
  end
142
139
 
143
140
  def size
144
- @ranges.inject(0){|c, x| c + (x.is_a?(Range) ? x.count : 1)}
141
+ @data.reduce(0) { |a, e| a + (e.is_a?(Range) ? e.count : 1) }
145
142
  end
146
143
 
147
144
  def ==(x)
148
- other = (x.is_a?(SparseRange) ? x : SparseRange.new(x)).ranges
145
+ other = (x.is_a?(SparseRange) ? x : SparseRange.new(x)).data
149
146
  i = 0
150
- (@ranges.length == other.length) && @ranges.all? do |e|
147
+ (@data.length == other.length) && @data.all? do |e|
151
148
  o = other[i]
152
149
  i += 1
153
150
  (e == o) || (read_val(e) == read_val(o))
@@ -156,28 +153,29 @@ module BetterRanges
156
153
 
157
154
  # TODO: Calculate hash without creating the temp array
158
155
  def hash
159
- @ranges.map(&method(:read_val)).hash
156
+ @data.map(&method(:read_val)).hash
160
157
  end
161
158
 
162
- alias :+ :|
163
- alias :union :|
159
+ alias_method :+, :|
160
+ alias_method :union, :|
164
161
 
165
- alias :minus :-
166
- alias :intersect :&
162
+ alias_method :minus, :-
163
+ alias_method :intersect, :&
167
164
 
168
- alias :add :<<
165
+ alias_method :add, :<<
169
166
 
170
- alias :cover? :include?
171
- alias :=== :include?
167
+ alias_method :cover?, :include?
168
+ alias_method :===, :include?
172
169
 
173
- alias :eql? :==
170
+ alias_method :eql?, :==
174
171
 
175
172
  protected
176
- attr_reader :ranges
173
+
174
+ attr_reader :data
177
175
 
178
176
  def yield_each(e)
179
- if (e.is_a?(Range))
180
- e.each{|x| yield x}
177
+ if e.is_a?(Range)
178
+ e.each { |x| yield x }
181
179
  else
182
180
  yield e
183
181
  end
@@ -186,7 +184,11 @@ module BetterRanges
186
184
  private
187
185
 
188
186
  def read_val(x)
189
- x.is_a?(Range) ? [x.first, (x.exclude_end? ? x.max : x.last)] : [x, x]
187
+ if x.is_a?(Range)
188
+ [x.first, (x.exclude_end? ? x.max : x.last)]
189
+ else
190
+ [x, x]
191
+ end
190
192
  end
191
193
 
192
194
  def write_val(start, finish)
@@ -194,17 +196,17 @@ module BetterRanges
194
196
  end
195
197
 
196
198
  def optimize
197
- @ranges.flatten!
198
- @ranges.compact!
199
- @ranges.sort!{|a,b| (a <=> b) || (read_val(a) <=> read_val(b))}
199
+ @data.flatten!
200
+ @data.compact!
201
+ @data.sort! { |a, b| (a <=> b) || (read_val(a) <=> read_val(b)) }
200
202
 
201
203
  fixed = []
202
- start, finish = read_val(@ranges.first)
204
+ start, finish = read_val(@data.first)
203
205
 
204
- for i in (1...@ranges.length)
205
- first, last = read_val(@ranges[i])
206
+ for i in (1...@data.length)
207
+ first, last = read_val(@data[i])
206
208
 
207
- if (finish.succ >= first)
209
+ if finish.succ >= first
208
210
  finish = last if last > finish
209
211
  else
210
212
  fixed << write_val(start, finish)
@@ -213,7 +215,7 @@ module BetterRanges
213
215
  end
214
216
  fixed << write_val(start, finish) if finish
215
217
 
216
- @ranges = fixed
218
+ @data = fixed
217
219
  end
218
220
  end
219
221
  end
@@ -223,7 +225,7 @@ class Range
223
225
 
224
226
  def <=>(other)
225
227
  comp = nil
226
- if (other.is_a?(Range) || other.is_a?(BetterRanges::SparseRange))
228
+ if other.is_a?(Range) || other.is_a?(BetterRanges::SparseRange)
227
229
  comp = (first <=> other.first)
228
230
  comp = (last <=> other.last) if comp == 0
229
231
  if comp == 0
@@ -237,4 +239,4 @@ class Range
237
239
  end
238
240
  comp
239
241
  end
240
- end
242
+ end
@@ -1,3 +1,3 @@
1
1
  module BetterRanges
2
- VERSION = "1.0.0"
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_ranges
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Ziminsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-05 00:00:00.000000000 Z
11
+ date: 2014-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,7 +97,8 @@ rubyforge_project:
97
97
  rubygems_version: 2.2.2
98
98
  signing_key:
99
99
  specification_version: 4
100
- summary: Add support for set operations to the ruby Range class
100
+ summary: Adds support for basic set operations to the ruby Range class through the
101
+ addition of a new SparseRange class
101
102
  test_files:
102
103
  - spec/better_ranges_spec.rb
103
104
  - spec/sparse_range_spec.rb