/
range.rb
333 lines (291 loc) · 8.05 KB
/
range.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
##
# A Range represents an interval, a set of values with a start and an end.
#
# Ranges may be constructed using the <tt>s..e</tt> and <tt>s...e</tt>
# literals, or with Range::new.
#
# Ranges constructed using <tt>..</tt> run from the start to the end
# inclusively. Those created using <tt>...</tt> exclude the end value. When
# used as an iterator, ranges return each value in the sequence.
#
# (-1..-5).to_a #=> []
# (-5..-1).to_a #=> [-5, -4, -3, -2, -1]
# ('a'..'e').to_a #=> ["a", "b", "c", "d", "e"]
# ('a'...'e').to_a #=> ["a", "b", "c", "d"]
#
# Ranges can be constructed using objects of any type, as long as the objects
# can be compared using their <tt><=></tt> operator and they support the
# <tt>succ</tt> method to return the next object in sequence.
#
# class Xs # represent a string of 'x's
# include Comparable
# attr :length
# def initialize(n)
# @length = n
# end
# def succ
# Xs.new(@length + 1)
# end
# def <=>(other)
# @length <=> other.length
# end
# def to_s
# sprintf "%2d #{inspect}", @length
# end
# def inspect
# 'x'# @length
# end
# end
#
# r = Xs.new(3)..Xs.new(6) #=> xxx..xxxxxx
# r.to_a #=> [xxx, xxxx, xxxxx, xxxxxx]
# r.member?(Xs.new(5)) #=> true
#
# In the previous code example, class Xs includes the Comparable module. This
# is because Enumerable#member? checks for equality using ==. Including
# Comparable ensures that the == method is defined in terms of the <=> method
# implemented in Xs.
class Range
include Enumerable
##
# Constructs a range using the given +start+ and +end+.
#
# If the third parameter is omitted or is false, the range will include the
# end object; otherwise, it will be excluded.
def initialize(first, last, exclude_end = false)
raise NameError, "`initialize' called twice" if @begin
unless first.kind_of?(Fixnum) && last.kind_of?(Fixnum)
begin
raise ArgumentError, "bad value for range" unless first <=> last
rescue
raise ArgumentError, "bad value for range"
end
end
@begin = first
@end = last
@excl = exclude_end
end
private :initialize
# Returns <tt>true</tt> only if <em>obj</em> is a Range, has
# equivalent beginning and end items (by comparing them with
# <tt>==</tt>), and has the same #exclude_end? setting as <i>rng</t>.
#
# (0..2) == (0..2) #=> true
# (0..2) == Range.new(0,2) #=> true
# (0..2) == (0...2) #=> false
def ==(other)
return true if equal? other
other.kind_of?(Range) and
self.first == other.first and
self.last == other.last and
self.exclude_end? == other.exclude_end?
end
alias_method :eql?, :==
# Returns <tt>true</tt> if <em>obj</em> is an element of <em>rng</em>,
# <tt>false</tt> otherwise. Conveniently, <tt>===</tt> is the
# comparison operator used by <tt>case</tt> statements.
#
# case 79
# when 1..50 then print "low\n"
# when 51..75 then print "medium\n"
# when 76..100 then print "high\n"
# end
#
# <em>produces:</em>
#
# high
def ===(value)
# MRI uses <=> to compare, so must we.
beg_compare = (@begin <=> value)
return false unless beg_compare
if Comparable.compare_int(beg_compare) <= 0
end_compare = (value <=> @end)
if @excl
return true if Comparable.compare_int(end_compare) < 0
else
return true if Comparable.compare_int(end_compare) <= 0
end
end
return false
end
alias_method :member?, :===
alias_method :include?, :===
##
# :call-seq:
# rng.exclude_end? => true or false
#
# Returns true if +rng+ excludes its end value.
attr_reader_specific :excl, :exclude_end?
##
# :call-seq:
# rng.each { |i| block } => rng
#
# Iterates over the elements +rng+, passing each in turn to the block. You
# can only iterate if the start object of the range supports the
# succ method (which means that you can't iterate over ranges of
# Float objects).
#
# (10..15).each do |n|
# print n, ' '
# end
#
# produces:
#
# 10 11 12 13 14 15
def each(&block)
return to_enum unless block_given?
first, last = @begin, @end
raise TypeError, "can't iterate from #{first.class}" unless first.respond_to? :succ
case first
when Fixnum
last -= 1 if @excl
i = first
while i <= last
yield i
i += 1
end
when String
first.upto(last) do |s|
yield s unless @excl && s == last
end
when Symbol
current = first
if @excl
while (current <=> last) < 0
yield current
current = (current.to_s.bytes.to_a.last + 1).chr.to_sym
end
else
while (c = current <=> last) && c <= 0
yield current
break if c == 0
current = (current.to_s.bytes.to_a.last + 1).chr.to_sym
end
end
else
current = first
if @excl
while (current <=> last) < 0
yield current
current = current.succ
end
else
while (c = current <=> last) && c <= 0
yield current
break if c == 0
current = current.succ
end
end
end
return self
end
##
# :call-seq:
# rng.first => obj
# rng.begin => obj
#
# Returns the first object in +rng+.
attr_reader :begin
alias_method :first, :begin
# Generate a hash value such that two ranges with the same start and
# end points, and the same value for the "exclude end" flag, generate
# the same hash value.
def hash
excl = @excl ? 1 : 0
hash = excl
hash ^= @begin.hash << 1
hash ^= @end.hash << 9
hash ^= excl << 24;
# Are we throwing away too much here for a good hash value distribution?
return hash & Fixnum::MAX
end
# Convert this range object to a printable form (using
# <tt>inspect</tt> to convert the start and end objects).
def inspect
"#{@begin.inspect}#{@excl ? "..." : ".."}#{@end.inspect}"
end
# Returns the object that defines the end of <em>rng</em>.
#
# (1..10).end #=> 10
# (1...10).end #=> 10
attr_reader :end
alias_method :last, :end
##
# :call-seq:
# rng.step(n = 1) { |obj| block } => rng
#
# Iterates over +rng+, passing each +n+th element to the block. If the range
# contains numbers or strings, natural ordering is used. Otherwise
# +step+ invokes +succ+ to iterate through range elements. The following
# code uses class Xs, which is defined in the class-level documentation.
#
# range = Xs.new(1)..Xs.new(10)
# range.step(2) { |x| puts x }
# range.step(3) { |x| puts x }
#
# produces:
#
# 1 x
# 3 xxx
# 5 xxxxx
# 7 xxxxxxx
# 9 xxxxxxxxx
# 1 x
# 4 xxxx
# 7 xxxxxxx
# 10 xxxxxxxxxx
def step(step_size=1) # :yields: object
return to_enum(:step, step_size) unless block_given?
first = @begin
last = @end
if first.kind_of? Float
step_size = Float(step_size)
else
step_size = Integer(step_size)
end
if step_size <= 0
raise ArgumentError, "step can't be negative" if step_size < 0
raise ArgumentError, "step can't be 0"
end
if first.kind_of?(Numeric)
i = 0
while (@excl && (step_size * i + first < last)) ||
(!@excl && (step_size * i + first <= last))
yield step_size * i + first
i += 1
end
else
counter = 0
each do |o|
yield o if counter % step_size == 0
counter += 1
end
end
return self
end
##
# Convert this range object to a printable form.
def to_s
"#{@begin}#{@excl ? "..." : ".."}#{@end}"
end
def to_a
if @begin.kind_of? Fixnum and @end.kind_of? Fixnum
fin = @end
fin += 1 unless @excl
size = fin - @begin
ary = Array.new(size)
i = 0
while i < size
ary[i] = @begin + i
i += 1
end
return ary
end
super
end
def max &a
return super(&a) if block_given?
return nil if @end < @begin
@end
end
end