better_ranges 1.0.0 → 1.1.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 +4 -4
- data/README.md +2 -0
- data/better_ranges.gemspec +1 -1
- data/lib/better_ranges/range_operators.rb +11 -7
- data/lib/better_ranges/sparse_range.rb +55 -53
- data/lib/better_ranges/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d04f8f961e79e072b4f76c04a5d24c56a2dc6ed5
|
4
|
+
data.tar.gz: 6c0041e4a5e835d3d48e4e45644700fbe2f837f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 673874f7f3571d50b407737720a377d1294901a83301643395eddf1c458022dce9e3ad028153bd49798bc9cc659a3d0c6bf369b5f5bde22614dfaf29ecfe1da0
|
7
|
+
data.tar.gz: 91c59ea36adddae6243a8033992560c9b35d2baf7177309e0252239893f93a715efcb5037a44182cfe50062a18b760b81935e9c4d2e3f4a2587ee4d06559a737
|
data/README.md
CHANGED
data/better_ranges.gemspec
CHANGED
@@ -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 = '
|
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 |(
|
6
|
-
SparseRange.new(self, *
|
5
|
+
def |(other)
|
6
|
+
SparseRange.new(self, *other)
|
7
7
|
end
|
8
8
|
|
9
|
-
def -(
|
10
|
-
SparseRange.new(self) -
|
9
|
+
def -(other)
|
10
|
+
SparseRange.new(self) - other
|
11
11
|
end
|
12
12
|
|
13
|
-
def &(
|
14
|
-
SparseRange.new(self) &
|
13
|
+
def &(other)
|
14
|
+
SparseRange.new(self) & other
|
15
15
|
end
|
16
16
|
|
17
|
-
|
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
|
-
@
|
7
|
-
if
|
8
|
-
if
|
9
|
-
|
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
|
-
@
|
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
|
-
@
|
27
|
+
@data.each do |r|
|
31
28
|
yield_each(r) do |x|
|
32
|
-
yielder.yield x if (
|
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(@
|
37
|
+
read_val(@data.last).last
|
41
38
|
end
|
42
39
|
|
43
40
|
def |(x)
|
44
|
-
SparseRange.new(@
|
41
|
+
SparseRange.new(@data, *x)
|
45
42
|
end
|
46
43
|
|
47
44
|
def -(x)
|
48
45
|
diff = SparseRange.new
|
49
|
-
diff_data = diff.
|
46
|
+
diff_data = diff.data
|
50
47
|
|
51
48
|
i = 0
|
52
49
|
next_val = lambda do
|
53
|
-
throw :done unless i < @
|
54
|
-
v = read_val(@
|
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)).
|
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
|
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.
|
83
|
+
intersect_data = intersect.data
|
87
84
|
|
88
85
|
i = 0
|
89
86
|
next_val = lambda do
|
90
|
-
throw :done unless i < @
|
91
|
-
v = read_val(@
|
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)).
|
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
|
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
|
-
@
|
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
|
-
@
|
127
|
+
@data.inspect
|
131
128
|
end
|
132
129
|
|
133
130
|
def include?(x)
|
134
|
-
@
|
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
|
-
@
|
137
|
+
@data.empty? || size == 0
|
141
138
|
end
|
142
139
|
|
143
140
|
def size
|
144
|
-
@
|
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)).
|
145
|
+
other = (x.is_a?(SparseRange) ? x : SparseRange.new(x)).data
|
149
146
|
i = 0
|
150
|
-
(@
|
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
|
-
@
|
156
|
+
@data.map(&method(:read_val)).hash
|
160
157
|
end
|
161
158
|
|
162
|
-
|
163
|
-
|
159
|
+
alias_method :+, :|
|
160
|
+
alias_method :union, :|
|
164
161
|
|
165
|
-
|
166
|
-
|
162
|
+
alias_method :minus, :-
|
163
|
+
alias_method :intersect, :&
|
167
164
|
|
168
|
-
|
165
|
+
alias_method :add, :<<
|
169
166
|
|
170
|
-
|
171
|
-
|
167
|
+
alias_method :cover?, :include?
|
168
|
+
alias_method :===, :include?
|
172
169
|
|
173
|
-
|
170
|
+
alias_method :eql?, :==
|
174
171
|
|
175
172
|
protected
|
176
|
-
|
173
|
+
|
174
|
+
attr_reader :data
|
177
175
|
|
178
176
|
def yield_each(e)
|
179
|
-
if
|
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)
|
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
|
-
@
|
198
|
-
@
|
199
|
-
@
|
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(@
|
204
|
+
start, finish = read_val(@data.first)
|
203
205
|
|
204
|
-
for i in (1...@
|
205
|
-
first, last = read_val(@
|
206
|
+
for i in (1...@data.length)
|
207
|
+
first, last = read_val(@data[i])
|
206
208
|
|
207
|
-
if
|
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
|
-
@
|
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
|
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
|
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.
|
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-
|
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:
|
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
|