forked from crystal-lang/crystal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeque.cr
616 lines (560 loc) · 15 KB
/
deque.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# A Deque ("[double-ended queue](https://en.wikipedia.org/wiki/Double-ended_queue)") is a collection of objects of type
# T that behaves much like an Array.
#
# Deque has a subset of Array's API. It performs better than an `Array` when there are frequent insertions or deletions
# of items near the beginning or the end.
#
# The most typical use case of a Deque is a queue: use `push` to add items to the end of the queue and `shift` to get
# and remove the item at the beginning of the queue.
#
# This Deque is implemented with a [dynamic array](http://en.wikipedia.org/wiki/Dynamic_array) used as a
# [circular buffer](https://en.wikipedia.org/wiki/Circular_buffer).
class Deque(T)
include Indexable::Mutable(T)
# This Deque is based on a circular buffer. It works like a normal array, but when an item is removed from the left
# side, instead of shifting all the items, only the start position is shifted. This can lead to configurations like:
# [234---01] @start = 6, size = 5, @capacity = 8
# (this Deque has 5 items, each equal to their index)
@start = 0
protected setter size
protected getter buffer
# Creates a new empty Deque
def initialize
@size = 0
@capacity = 0
@buffer = Pointer(T).null
end
# Creates a new empty `Deque` backed by a buffer that is initially `initial_capacity` big.
#
# The `initial_capacity` is useful to avoid unnecessary reallocations of the internal buffer in case of growth. If you
# have an estimate of the maximum number of elements a deque will hold, you should initialize it with that capacity
# for improved execution performance.
#
# ```
# deq = Deque(Int32).new(5)
# deq.size # => 0
# ```
def initialize(initial_capacity : Int)
if initial_capacity < 0
raise ArgumentError.new("Negative deque capacity: #{initial_capacity}")
end
@size = 0
@capacity = initial_capacity.to_i
if @capacity == 0
@buffer = Pointer(T).null
else
@buffer = Pointer(T).malloc(@capacity)
end
end
# Creates a new `Deque` of the given size filled with the same value in each position.
#
# ```
# Deque.new(3, 'a') # => Deque{'a', 'a', 'a'}
# ```
def initialize(size : Int, value : T)
if size < 0
raise ArgumentError.new("Negative deque size: #{size}")
end
@size = size.to_i
@capacity = size.to_i
if @capacity == 0
@buffer = Pointer(T).null
else
@buffer = Pointer(T).malloc(@capacity, value)
end
end
# Creates a new `Deque` of the given size and invokes the block once for
# each index of the deque, assigning the block's value in that index.
#
# ```
# Deque.new(3) { |i| (i + 1) ** 2 } # => Deque{1, 4, 9}
# ```
def self.new(size : Int, & : Int32 -> T)
if size < 0
raise ArgumentError.new("Negative deque size: #{size}")
end
deque = Deque(T).new(size)
deque.size = size
size.to_i.times do |i|
deque.buffer[i] = yield i
end
deque
end
# Creates a new `Deque` that copies its items from an Array.
#
# ```
# Deque.new([1, 2, 3]) # => Deque{1, 2, 3}
# ```
def self.new(array : Array(T))
Deque(T).new(array.size) { |i| array[i] }
end
# Returns `true` if it is passed a `Deque` and `equals?` returns `true`
# for both deques, the caller and the argument.
#
# ```
# deq = Deque{2, 3}
# deq.unshift 1
# deq == Deque{1, 2, 3} # => true
# deq == Deque{2, 3} # => false
# ```
def ==(other : Deque)
equals?(other) { |x, y| x == y }
end
# Concatenation. Returns a new `Deque` built by concatenating
# two deques together to create a third. The type of the new deque
# is the union of the types of both the other deques.
def +(other : Deque(U)) forall U
Deque(T | U).new.concat(self).concat(other)
end
# :nodoc:
def +(other : Deque(T))
dup.concat other
end
# Returns the additive identity of this type.
#
# This is an empty deque.
def self.additive_identity : self
self.new
end
# Alias for `push`.
def <<(value : T)
push(value)
end
def unsafe_fetch(index : Int) : T
index += @start
index -= @capacity if index >= @capacity
@buffer[index]
end
def unsafe_put(index : Int, value : T)
index += @start
index -= @capacity if index >= @capacity
@buffer[index] = value
end
# Removes all elements from `self`.
def clear
halfs do |r|
(@buffer + r.begin).clear(r.end - r.begin)
end
@size = 0
@start = 0
self
end
# Returns a new `Deque` that has this deque's elements cloned.
# That is, it returns a deep copy of this deque.
#
# Use `#dup` if you want a shallow copy.
def clone
{% if T == ::Bool || T == ::Char || T == ::String || T == ::Symbol || T < ::Number::Primitive %}
Deque(T).new(size) { |i| self[i].clone.as(T) }
{% else %}
exec_recursive_clone do |hash|
clone = Deque(T).new(size)
hash[object_id] = clone.object_id
each do |element|
clone << element.clone
end
clone
end
{% end %}
end
# Appends the elements of *other* to `self`, and returns `self`.
def concat(other : Enumerable(T))
other.each do |x|
push x
end
self
end
# Removes all items from `self` that are equal to *obj*.
#
# ```
# a = Deque{"a", "b", "b", "b", "c"}
# a.delete("b") # => true
# a # => Deque{"a", "c"}
# ```
def delete(obj) : Bool
match = internal_delete { |i| i == obj }
!match.nil?
end
# Modifies `self`, keeping only the elements in the collection for which the
# passed block returns `true`. Returns `self`.
#
# ```
# a = Deque{1, 6, 2, 4, 8}
# a.select! { |x| x > 3 }
# a # => Deque{6, 4, 8}
# ```
#
# See also: `Deque#select`.
def select!(& : T ->) : self
reject! { |elem| !yield(elem) }
end
# Modifies `self`, keeping only the elements in the collection for which
# `pattern === element`.
#
# ```
# ary = [1, 6, 2, 4, 8]
# ary.select!(3..7)
# ary # => [6, 4]
# ```
#
# See also: `Deque#select`.
def select!(pattern) : self
self.select! { |elem| pattern === elem }
end
# Modifies `self`, deleting the elements in the collection for which the
# passed block returns `true`. Returns `self`.
#
# ```
# a = Deque{1, 6, 2, 4, 8}
# a.reject! { |x| x > 3 }
# a # => Deque{1, 2}
# ```
#
# See also: `Deque#reject`.
def reject!(& : T ->) : self
internal_delete { |e| yield e }
self
end
# Modifies `self`, deleting the elements in the collection for which
# `pattern === element`.
#
# ```
# a = Deque{1, 6, 2, 4, 8}
# a.reject!(3..7)
# a # => Deque{1, 2, 8}
# ```
#
# See also: `Deque#reject`.
def reject!(pattern) : self
reject! { |elem| pattern === elem }
self
end
# `reject!` and `delete` implementation:
# returns the last matching element, or nil
private def internal_delete
match = nil
i = 0
while i < @size
e = self[i]
if yield e
match = e
delete_at(i)
else
i += 1
end
end
match
end
# Deletes the item that is present at the *index*. Items to the right
# of this one will have their indices decremented.
# Raises `IndexError` if trying to delete an element outside the deque's range.
#
# ```
# a = Deque{1, 2, 3}
# a.delete_at(1) # => 2
# a # => Deque{1, 3}
# ```
def delete_at(index : Int) : T
if index < 0
index += @size
end
unless 0 <= index < @size
raise IndexError.new
end
return shift if index == 0
return pop if index == @size - 1
rindex = @start + index
rindex -= @capacity if rindex >= @capacity
value = @buffer[rindex]
if index > @size // 2
# Move following items to the left, starting with the first one
# [56-01234] -> [6x-01235]
dst = rindex
finish = (@start + @size - 1) % @capacity
loop do
src = dst + 1
src -= @capacity if src >= @capacity
@buffer[dst] = @buffer[src]
break if src == finish
dst = src
end
(@buffer + finish).clear
else
# Move preceding items to the right, starting with the last one
# [012345--] -> [x01345--]
dst = rindex
finish = @start
@start += 1
@start -= @capacity if @start >= @capacity
loop do
src = dst - 1
src += @capacity if src < 0
@buffer[dst] = @buffer[src]
break if src == finish
dst = src
end
(@buffer + finish).clear
end
@size -= 1
value
end
# Returns a new `Deque` that has exactly this deque's elements.
# That is, it returns a shallow copy of this deque.
def dup
Deque(T).new(size) { |i| self[i].as(T) }
end
# Yields each item in this deque, from first to last.
#
# Do not modify the deque while using this variant of `each`!
def each(& : T ->) : Nil
halfs do |r|
r.each do |i|
yield @buffer[i]
end
end
end
# Insert a new item before the item at *index*. Items to the right
# of this one will have their indices incremented.
#
# ```
# a = Deque{0, 1, 2}
# a.insert(1, 7) # => Deque{0, 7, 1, 2}
# ```
def insert(index : Int, value : T) : self
if index < 0
index += @size + 1
end
unless 0 <= index <= @size
raise IndexError.new
end
return unshift(value) if index == 0
return push(value) if index == @size
increase_capacity if @size >= @capacity
rindex = @start + index
rindex -= @capacity if rindex >= @capacity
if index > @size // 2
# Move following items to the right, starting with the last one
# [56-01234] -> [4560123^]
dst = @start + @size
dst -= @capacity if dst >= @capacity
loop do
src = dst - 1
src += @capacity if src < 0
@buffer[dst] = @buffer[src]
break if src == rindex
dst = src
end
else
# Move preceding items to the left, starting with the first one
# [01234---] -> [1^234--0]
@start -= 1
@start += @capacity if @start < 0
rindex -= 1
rindex += @capacity if rindex < 0
dst = @start
loop do
src = dst + 1
src -= @capacity if src >= @capacity
@buffer[dst] = @buffer[src]
break if src == rindex
dst = src
end
end
@size += 1
@buffer[rindex] = value
self
end
def inspect(io : IO) : Nil
executed = exec_recursive(:inspect) do
io << "Deque{"
join io, ", ", &.inspect(io)
io << '}'
end
io << "Deque{...}" unless executed
end
def pretty_print(pp)
executed = exec_recursive(:inspect) do
pp.list("Deque{", self, "}")
end
pp.text "Deque{...}" unless executed
end
# Returns the number of elements in the deque.
#
# ```
# Deque{:foo, :bar}.size # => 2
# ```
def size : Int32
@size
end
# Removes and returns the last item. Raises `IndexError` if empty.
#
# ```
# a = Deque{1, 2, 3}
# a.pop # => 3
# a # => Deque{1, 2}
# ```
def pop : T
pop { raise IndexError.new }
end
# Removes and returns the last item, if not empty, otherwise executes
# the given block and returns its value.
def pop
if @size == 0
yield
else
@size -= 1
index = @start + @size
index -= @capacity if index >= @capacity
value = @buffer[index]
(@buffer + index).clear
value
end
end
# Removes and returns the last item, if not empty, otherwise `nil`.
def pop? : T?
pop { nil }
end
# Removes the last *n* (at most) items in the deque.
def pop(n : Int) : Nil
if n < 0
raise ArgumentError.new("Can't pop negative count")
end
n = Math.min(n, @size)
n.times { pop }
nil
end
# Adds an item to the end of the deque.
#
# ```
# a = Deque{1, 2}
# a.push 3 # => Deque{1, 2, 3}
# ```
def push(value : T)
increase_capacity if @size >= @capacity
index = @start + @size
index -= @capacity if index >= @capacity
@buffer[index] = value
@size += 1
self
end
# :inherit:
def rotate!(n : Int = 1) : Nil
return if @size <= 1
if @size == @capacity
@start = (@start + n) % @capacity
else
# Turn *n* into an equivalent index in range -size/2 .. size/2
half = @size // 2
if n.abs >= half
n = (n + half) % @size - half
end
while n > 0
push(shift)
n -= 1
end
while n < 0
n += 1
unshift(pop)
end
end
end
# Removes and returns the first item. Raises `IndexError` if empty.
#
# ```
# a = Deque{1, 2, 3}
# a.shift # => 1
# a # => Deque{2, 3}
# ```
def shift
shift { raise IndexError.new }
end
# Removes and returns the first item, if not empty, otherwise executes
# the given block and returns its value.
def shift
if @size == 0
yield
else
value = @buffer[@start]
(@buffer + @start).clear
@size -= 1
@start += 1
@start -= @capacity if @start >= @capacity
value
end
end
# Removes and returns the first item, if not empty, otherwise `nil`.
def shift?
shift { nil }
end
# Removes the first *n* (at most) items in the deque.
def shift(n : Int) : Nil
if n < 0
raise ArgumentError.new("Can't shift negative count")
end
n = Math.min(n, @size)
n.times { shift }
nil
end
def to_s(io : IO) : Nil
inspect(io)
end
# Adds an item to the beginning of the deque.
#
# ```
# a = Deque{1, 2}
# a.unshift 0 # => Deque{0, 1, 2}
# ```
def unshift(value : T) : self
increase_capacity if @size >= @capacity
@start -= 1
@start += @capacity if @start < 0
@buffer[@start] = value
@size += 1
self
end
private def halfs
# For [----] yields nothing
# For contiguous [-012] yields 1...4
# For separated [234---01] yields 6...8, 0...3
return if empty?
a = @start
b = @start + size
b -= @capacity if b > @capacity
if a < b
yield a...b
else
yield a...@capacity
yield 0...b
end
end
private def increase_capacity
unless @buffer
@capacity = 4
@buffer = Pointer(T).malloc(@capacity)
return
end
old_capacity = @capacity
@capacity *= 2
@buffer = @buffer.realloc(@capacity)
finish = @start + @size
if finish > old_capacity
# If the deque is separated into two parts, we get something like [2301----] after resize, so additional action is
# needed, to turn it into [23----01] or [--0123--].
# To do the moving we can use `copy_from` because the old and new locations will never overlap (assuming we're
# multiplying the capacity by 2 or more). Due to the same assumption, we can clear all of the old locations.
finish -= old_capacity
if old_capacity - @start >= @start
# [3012----] -> [-0123---]
(@buffer + old_capacity).copy_from(@buffer, finish)
@buffer.clear(finish)
else
# [1230----] -> [123----0]
to_move = old_capacity - @start
new_start = @capacity - to_move
(@buffer + new_start).copy_from(@buffer + @start, to_move)
(@buffer + @start).clear(to_move)
@start = new_start
end
end
end
end