-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
range.cr
429 lines (381 loc) · 8.4 KB
/
range.cr
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# A `Range` represents an interval: a set of values with a beginning and an end.
#
# Ranges may be constructed using the usual `new` method or with literals:
#
# ```
# x..y # an inclusive range, in mathematics: [x, y]
# x...y # an exclusive range, in mathematics: [x, y)
# ```
#
# An easy way to remember which one is inclusive and which one is exclusive it
# to think of the extra dot as if it pushes *y* further away, thus leaving it outside of the range.
#
# Ranges typically involve integers, but can be created using arbitrary objects
# as long as they define `succ` (or `pred` for `reverse_each`), to get the
# next element in the range, and `<` and `==`, to know when the range reached the end:
#
# ```
# # Represents a string of 'x's.
# struct Xs
# include Comparable(Xs)
#
# getter size
#
# def initialize(@size : Int32)
# end
#
# def succ
# Xs.new(@size + 1)
# end
#
# def <=>(other)
# @size <=> other.size
# end
#
# def inspect(io)
# @size.times { io << 'x' }
# end
#
# def to_s(io)
# io << @size << ' '
# inspect(io)
# end
# end
# ```
#
# An example of using `Xs` to construct a range:
#
# ```
# r = Xs.new(3)..Xs.new(6)
# r.to_s # => "xxx..xxxxxx"
# r.to_a # => [Xs.new(3), Xs.new(4), Xs.new(5), Xs.new(6)]
# r.includes?(Xs.new(5)) # => true
# ```
struct Range(B, E)
include Enumerable(B)
include Iterable(B)
# Returns the object that defines the beginning of this range.
#
# ```
# (1..10).begin # => 1
# (1...10).begin # => 1
# ```
getter begin : B
# Returns the object that defines the end of the range.
#
# ```
# (1..10).end # => 10
# (1...10).end # => 10
# ```
getter end : E
# Returns `true` if the range is exclusive.
# Returns `false` otherwise (default).
getter? exclusive : Bool
# Constructs a `Range` using the given beginning and end.
#
# ```
# Range.new(1, 10) # => 1..10
# Range.new(1, 10, exclusive: true) # => 1...10
# ```
def initialize(@begin : B, @end : E, @exclusive : Bool = false)
end
# Returns an `Iterator` that cycles over the values of this range.
#
# ```
# (1..3).cycle.first(5).to_a # => [1, 2, 3, 1, 2]
# ```
def cycle
each.cycle
end
# Iterates over the elements of this range, passing each in turn to the block.
#
# ```
# (10..15).each { |n| print n, ' ' }
# # prints: 10 11 12 13 14 15
# ```
def each : Nil
current = @begin
while current < @end
yield current
current = current.succ
end
yield current if !@exclusive && current == @end
end
# Returns an `Iterator` over the elements of this range.
#
# ```
# (1..3).each.skip(1).to_a # => [2, 3]
# ```
def each
ItemIterator.new(self)
end
# Iterates over the elements of this range in reverse order,
# passing each in turn to the block.
#
# ```
# (10...15).reverse_each { |n| print n, ' ' }
# # prints: 14 13 12 11 10
# ```
def reverse_each : Nil
yield @end if !@exclusive && !(@end < @begin)
current = @end
while @begin < current
current = current.pred
yield current
end
end
# Returns a reverse `Iterator` over the elements of this range.
#
# ```
# (1..3).reverse_each.skip(1).to_a # => [2, 1]
# ```
def reverse_each
ReverseIterator.new(self)
end
# Iterates over this range, passing each nth element to the block.
#
# ```
# range = Xs.new(1)..Xs.new(10)
# range.step(2) { |x| puts x }
# puts
# range.step(3) { |x| puts x }
# ```
#
# Produces:
#
# ```text
# 1 x
# 3 xxx
# 5 xxxxx
# 7 xxxxxxx
# 9 xxxxxxxxx
#
# 1 x
# 4 xxxx
# 7 xxxxxxx
# 10 xxxxxxxxxx
# ```
#
# See `Range`'s overview for the definition of `Xs`.
def step(by = 1)
current = @begin
while current < @end
yield current
by.times { current = current.succ }
end
yield current if !@exclusive && current == @end
self
end
# Returns an `Iterator` that returns each nth element in this range.
#
# ```
# (1..10).step(3).skip(1).to_a # => [4, 7, 10]
# ```
def step(by = 1)
StepIterator(self, B, typeof(by)).new(self, by)
end
# Returns `true` if this range excludes the *end* element.
#
# ```
# (1..10).excludes_end? # => false
# (1...10).excludes_end? # => true
# ```
def excludes_end?
@exclusive
end
# Returns `true` if this range includes the given *value*.
#
# ```
# (1..10).includes?(4) # => true
# (1..10).includes?(10) # => true
# (1..10).includes?(11) # => false
#
# (1...10).includes?(9) # => true
# (1...10).includes?(10) # => false
# ```
def includes?(value)
if @exclusive
@begin <= value < @end
else
@begin <= value <= @end
end
end
# Same as `includes?`.
def covers?(value)
includes?(value)
end
# Same as `includes?`, useful for the `case` expression.
#
# ```
# case 79
# when 1..50 then puts "low"
# when 51..75 then puts "medium"
# when 76..100 then puts "high"
# end
# ```
#
# Produces:
#
# ```text
# high
# ```
#
# See also: `Object#===`.
def ===(value)
includes?(value)
end
# :nodoc:
def to_s(io : IO)
@begin.inspect(io)
io << (@exclusive ? "..." : "..")
@end.inspect(io)
end
# :nodoc:
def inspect(io)
to_s(io)
end
# If `self` is a `Int` range, it provides O(1) implementation,
# otherwise it is same as `Enumerable#sum`.
def sum(initial)
b = self.begin
e = self.end
if b.is_a?(Int) && e.is_a?(Int)
e -= 1 if @exclusive
n = e - b + 1
if n >= 0
initial + n * (b + e) / 2
else
initial
end
else
super
end
end
# Returns a new `Range` with `begin` and `end` cloned.
def clone
Range.new(@begin.clone, @end.clone, @exclusive)
end
# :nodoc:
def map(&block : B -> U) forall U
b = self.begin
e = self.end
# Optimized implementation for int range
if b.is_a?(Int) && e.is_a?(Int)
e -= 1 if @exclusive
n = e - b + 1
n = 0 if n < 0
Array(U).new(n) { |i| yield b + i }
else
super { |i| yield i }
end
end
# :nodoc:
def size
b = self.begin
e = self.end
# Optimized implementation for int range
if b.is_a?(Int) && e.is_a?(Int)
e -= 1 if @exclusive
n = e - b + 1
n < 0 ? 0 : n
else
super
end
end
private class ItemIterator(B, E)
include Iterator(B)
@range : Range(B, E)
@current : B
@reached_end : Bool
def initialize(@range : Range(B, E), @current = range.begin, @reached_end = false)
end
def next
return stop if @reached_end
if @current < @range.end
value = @current
@current = @current.succ
value
else
@reached_end = true
if !@range.excludes_end? && @current == @range.end
@current
else
stop
end
end
end
def rewind
@current = @range.begin
@reached_end = false
self
end
end
private class ReverseIterator(B, E)
include Iterator(E)
@range : Range(B, E)
@current : E
def initialize(@range : Range(B, E), @current = range.end)
rewind
end
def next
return stop if @current <= @range.begin
return @current = @current.pred
end
def rewind
if @range.excludes_end?
@current = @range.end
else
@current = @range.end.succ
end
self
end
end
private class StepIterator(R, B, N)
include Iterator(B)
@range : R
@step : N
@current : B
@reached_end : Bool
def initialize(@range, @step, @current = range.begin, @reached_end = false)
end
def next
return stop if @reached_end
if @current < @range.end
value = @current
@step.times { @current = @current.succ }
value
else
@reached_end = true
if !@range.excludes_end? && @current == @range.end
@current
else
stop
end
end
end
def rewind
@current = @range.begin
@reached_end = false
self
end
def sum(initial)
super if @reached_end
b = @current
e = @range.end
d = @step
if b.is_a?(Int) && e.is_a?(Int) && d.is_a?(Int)
e -= 1 if @range.excludes_end?
n = (e - b) / d + 1
if n >= 0
e = b + (n - 1) * d
initial + n * (b + e) / 2
else
initial
end
else
super
end
end
end
end
require "./range/*"