-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque.nim
1224 lines (1064 loc) · 37.4 KB
/
deque.nim
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
#
# Nim's Runtime Library
# (c) Copyright 2012 Andreas Rumpf
# customized by Charles Fout, October 2024
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## An implementation of a `Deque`:idx: (double-ended queue).
## The underlying implementation uses a `seq`.
##
## .. note:: None of the procs that get an individual value from the Deque should be used
## on an empty Deque.
##
## If compiled with the `boundChecks` option, those procs will raise an `IndexDefect`
## on such access. This should not be relied upon, as `-d:danger` or `--checks:off` will
## disable those checks and then the procs may return garbage or crash the program.
##
## As such, a check to see if the Deque is empty is needed before any
## access, unless your program logic guarantees it indirectly.
runnableExamples:
var a = [10, 20, 30, 40].toDeque
doAssertRaises(IndexDefect, echo a[4])
assert not isEmpty(a)
a.addLast(50)
assert $a == "[10, 20, 30, 40, 50]"
assert a.first == 10
assert a.last == 50
assert len(a) == 5
assert capacity(a) == 8
assert a.popFirst == 10
assert a.popLast == 50
assert a.len == 3
assert a.capacity == 8
assert a.high == 2
a.addFirst(11)
a.addFirst(22)
a.addFirst(33)
a.first = 44
assert a == @@[44, 22, 11, 20, 30, 40]
a.shrink(fromFirst = 1, fromLast = 2)
assert a == @@[22, 11, 20]
import std/[assertions, hashes] # sequtils removed, unused
from math import nextPowerOfTwo
from algorithm import reversed
type
Deque*[T] = object
## A double-ended queue backed with a ringed `seq` buffer.
##
## To initialize an empty Deque,
## use the `initDeque proc <#initDeque,int>`_.
data: seq[T]
# `head` and `tail` are masked only when accessing an element of `data`
# so that `tail - head == data.len` when the Deque is full.
# They are uint so that incrementing/decrementing them doesn't cause
# over/underflow. You can get a number of items with `tail - head`
# even if `tail` or `head` is wrapped around and `tail < head`, because
# `tail - head == (uint.high + 1 + tail) - head` when `tail < head`.
head, tail: uint
const
defaultInitialSize* = 4
template destroy(x: untyped) =
reset(x)
template `^^`(s, i: untyped): untyped =
(when i is BackwardsIndex: s.len - int(i) else: int(i))
template initImpl(result: typed, initialSize: Natural) =
let correctSize = nextPowerOfTwo(initialSize)
newSeq(result.data, correctSize)
template checkIfInitialized(deq: typed) =
if deq.data.len == 0:
initImpl(deq, defaultInitialSize)
template isEmpty*[T](deq: Deque[T]): bool =
## Returns true is `deq` is empty, false otherwise.
deq.head == deq.tail
template mask[T](deq: Deque[T]): uint =
uint(deq.data.len) - 1
proc initDeque*[T](initialSize: Natural = defaultInitialSize): Deque[T] =
## Creates a new empty Deque of capacity `initialSize`.
## The length of a newly created Deque will be 0.
## Capacity is always a power of two, with a minimum of two.
##
## (default and capacity: `defaultInitialSize <#defaultInitialSize>`_).
##
## **See also:**
## * `newDeque proc <#newDeque,Natural>`_
## * `toDeque proc <#toDeque,sinkopenArray[T]>`_
runnableExamples:
var deq1 = initDeque[int](6)
assert capacity(deq1) == 8
assert len(deq1) == 0
result.initImpl(max(initialSize, 2))
proc newDeque*[T](initialSize: Natural = defaultInitialSize): Deque[T] =
## Creates a new empty Deque of capacity `initialSize`.
## The length of a newly created Deque will be 0.
## Capacity is always a power of two, with a minimum of two.
##
## (default capacity: `defaultInitialSize <#defaultInitialSize>`_).
##
## **See also:**
## * `initDeque proc <#initDeque,Natural>`_
## * `toDeque proc <#toDeque,sinkopenArray[T]>`_
runnableExamples:
var deq1 = newDeque[int]()
assert capacity(deq1) == defaultInitialSize
assert len(deq1) == 0
result.initImpl(max(initialSize, 2))
template reset*[T](deq: var Deque[T], maxCap: Natural = defaultInitialSize) =
## This is a documentation comment.
## Resets `deq` so it is empty and sets its capacity to `maxCap`.
## Capacity is always a power of two.
##
## **See also:**
## * `clear template <#clear.t,Deque[T]>`_
## * `defaultInitialSize constant <#defaultInitialSize>`_
destroy(deq)
setLen(deq.data, nextPowerOfTwo(maxCap))
template clear*[T](deq: var Deque[T]) =
## Resets the Deque so that it is empty, but retains its capacity.
##
## **See also:**
## * `reset template <#reset.t,Deque[T],Natural>`_
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
assert $a == "[10, 20, 30, 40, 50]"
clear(a)
assert len(a) == 0
assert capacity(a) == 8
let maxCap = len(deq.data)
destroy(deq)
setLen(deq.data, maxCap)
template len*[T](deq: Deque[T]): int =
## Returns the number of elements in `deq`.
int(deq.tail - deq.head)
template high*[T](deq: Deque[T]): int =
## Returns the highest valid index in `deq`. This is the same as len(deq) - 1.
## If `deq` is empty, will return -1.
int(deq.tail - deq.head) - 1
template low*[T](deq: Deque[T]): int =
## Returns the lowest valid index in `deq`, normally 0.
## If `deq` is empty, will return -1.
if len(deq) == 0:
-1.int
else:
0.int
template capacity*[T](deq: Deque[T]): int =
## Returns the maximum capacity of the sequence backing `deq`.
## Capacity is always a power of two.
runnableExamples:
var deq = [1, 2, 3, 4].toDeque
assert deq.len == deq.capacity
deq.addLast(5)
assert deq.len == 5
assert deq.capacity == 8
assert len(deq.data) == capacity(deq.data)
len(deq.data)
template emptyCheck(deq) =
# Bounds check for the regular Deque access.
when compileOption("boundChecks"):
if unlikely(deq.len < 1):
raise newException(IndexDefect, "Empty Deque.")
template xBoundsCheck(deq, i) =
# Bounds check for the array like accesses.
when compileOption("boundChecks"): # `-d:danger` or `--checks:off` should disable this.
if unlikely(i >= deq.len): # x < deq.low is taken care by the Natural parameter
raise newException(IndexDefect,
"Deque index out of bounds: " & $i & " > " & $(deq.high))
if unlikely(i < 0): # when used with BackwardsIndex
raise newException(IndexDefect,
"Deque index out of bounds: " & $i & " < 0")
proc expandIfNeeded[T](deq: var Deque[T], count: Natural = 1)
proc normalize[T](target: var Deque[T])
proc makeRoom[T](target: var Deque[T], pos: Natural, howMany: Natural)
# forward declarations
proc `[]`*[T](deq: Deque[T], i: Natural): T {.inline.} =
## Accesses the `i`-th element of `deq`.
runnableExamples:
let a = [10, 20, 30, 40, 50].toDeque
assert a[0] == 10
assert a[3] == 40
doAssertRaises(IndexDefect, echo a[8])
xBoundsCheck(deq, i)
return deq.data[(deq.head + i.uint) and deq.mask]
proc `[]`*[T](deq: var Deque[T], i: Natural): var T {.inline.} =
## Accesses the `i`-th element of `deq` and returns a mutable
## reference to it.
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
inc(a[0])
assert a[0] == 11
xBoundsCheck(deq, i)
return deq.data[(deq.head + i.uint) and deq.mask]
proc `[]`*[T](deq: Deque[T], i: BackwardsIndex): T {.inline.} =
## Accesses the backwards indexed `i`-th element.
##
## `deq[^1]` is the last element.
runnableExamples:
let a = [10, 20, 30, 40, 50].toDeque
assert a[^1] == 50
assert a[^4] == 20
doAssertRaises(IndexDefect, echo a[^9])
xBoundsCheck(deq, deq.len - int(i))
return deq[deq.len - int(i)]
proc `[]`*[T](deq: var Deque[T], i: BackwardsIndex): var T {.inline.} =
## Accesses the backwards indexed `i`-th element and returns a mutable
## reference to it.
##
## `deq[^1]` is the last element.
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
inc(a[^1])
assert a[^1] == 51
xBoundsCheck(deq, deq.len - int(i))
return deq[deq.len - int(i)]
proc `[]`*[T; U, V: Ordinal](target: Deque[T], x: HSlice[U, V]): Deque[T] {.systemRaisesDefect.} =
## Slice operation for Deques.
## Returns the inclusive range `[target[start .. stop]`.
## If the slice indices are reversed, so will be the data.
## ```nim
## var s = @[1, 2, 3, 4].toDeque
## assert $s[0..2] == "[1, 2, 3]"
## assert $s[1..<4] == "[2, 3]"
## assert $s[^1..0] == "[4, 3, 2, 1]"
## ```
var start = target ^^ x.a
var stop = target ^^ x.b
var backwards = false
xBoundsCheck(target, start)
xBoundsCheck(target, stop)
if stop < start:
#raise newException(IndexDefect, "Deque indices reversed: " & $start & " > " & $stop)
swap(start, stop)
backwards = true
let howLong = stop - start + 1
var newData = newSeq[T](nextPowerOfTwo(howLong))
if not backwards:
for i in 0 ..< howLong: newData[i] = target[i + start]
else:
let last = howLong - 1
for i in 0 ..< howLong: newData[last - i] = target[i + start]
result.data = move newData
result.head = 0.uint
result.tail = howLong.uint
proc `[]=`*[T](deq: var Deque[T], i: Natural, val: sink T) {.inline.} =
## Sets the `i`-th element of `deq` to `val`.
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
a[0] = 99
a[3] = 66
assert $a == "[99, 20, 30, 66, 50]"
checkIfInitialized(deq)
xBoundsCheck(deq, i)
deq.data[(deq.head + i.uint) and deq.mask] = val
proc `[]=`*[T](deq: var Deque[T], i: BackwardsIndex, x: sink T) {.inline.} =
## Sets the backwards indexed `i`-th element of `deq` to `x`.
##
## `deq[^1]` is the last element.
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
a[^1] = 99
a[^3] = 77
assert $a == "[10, 20, 77, 40, 99]"
checkIfInitialized(deq)
xBoundsCheck(deq, deq.len - int(i))
deq[deq.len - int(i)] = x
proc `[]=`*[T; U, V: Ordinal](target: var Deque[T], x: HSlice[U, V], source: Deque[T]) {.systemRaisesDefect.} =
## Slice assignment for Deques from Deques.
##
## If `source` is longer than the slice, a slice of `source` is taken to fit.
## If `source` is shorter than the slice, `target` is shortened.
## If the slice indices are reversed, so will be the data.
runnableExamples:
var s = @[1, 2, 3, 4, 5].toDeque
s[1 .. ^2] = @[10, 20].toDeque
assert s == @[1, 10, 20, 5].toDeque
var start = target ^^ x.a
var stop = target ^^ x.b
var backwards = false
xBoundsCheck(target, start)
xBoundsCheck(target, stop)
if stop < start:
# raise newException(IndexDefect, "Deque indices reversed: " & $start & " > " & $stop)
swap(start, stop)
backwards = true
let howLong = stop - start + 1
var data1 = newSeq[T](len(target))
for count, _ in target: data1[count] = target[count]
var data2 = newSeq[T](len(source))
for count, _ in source: data2[count] = source[count]
if len(data2) <= howlong:
data1[start..stop] = if not backwards: data2 else: data2.reversed
else:
data1[start..stop] = if not backwards: data2[0..<howLong] else: data2[0..<howLong].reversed
target = data1.toDeque
proc `[]=`*[T; U, V: Ordinal](target: var Deque[T], x: HSlice[U, V], source: openArray[T]) {.systemRaisesDefect.} =
## Slice assignment for Deques from sequences.
##
## If `source` is longer than the slice, a slice of `source` is taken to fit.
## If `source` is shorter than the slice, `target` is shortened.
## If the slice indices are reversed, so will be the data.
runnableExamples:
var s = @[1, 2, 3, 4, 5].toDeque
s[1 .. ^2] = @[10, 20]
assert s == @[1, 10, 20, 5].toDeque
s[1..0] = @[100, 200]
assert s == @[200, 100, 20, 5].toDeque
var start = target ^^ x.a
var stop = target ^^ x.b
var backwards = false
xBoundscheck(target, start)
xBoundsCheck(target, stop)
if stop < start:
# raise newException(IndexDefect, "Deque indices reversed: " & $start & " > " & $stop)
swap(start, stop)
backwards = true
let howLong = stop - start + 1
var data1 = newSeq[T](len(target))
for count, _ in target: data1[count] = target[count]
if len(source) <= howlong:
data1[start..stop] = if not backwards: source else: source.reversed
else:
data1[start..stop] = if not backwards: source[0 ..< howLong] else: source[0 ..< howLong].reversed
target = data1.toDeque
iterator items*[T](deq: Deque[T]): T {.inline.} =
## Yields every element of `deq`.
##
## **See also:**
## * `mitems iterator <#mitems.i,Deque[T]>`_
## * `backwards iterator <#backwards.i,Deque[T]>`_
runnableExamples:
let a = [10, 20, 30, 40, 50].toDeque
var b: seq[int]
for item in a: b.add(item)
assert b == @[10, 20, 30, 40, 50]
assert $a == "[10, 20, 30, 40, 50]"
var c = 0
let stop = deq.len
while c < stop:
yield deq.data[(deq.head + c.uint) and deq.mask]
inc c
iterator mitems*[T](deq: var Deque[T]): var T {.inline.} =
## Yields every element of `deq`, which can be modified.
##
## **See also:**
## * `items iterator <#items.i,Deque[T]>`_
## * `backwardsMut iterator <#backwardsMut.i,Deque[T]>`_
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
assert $a == "[10, 20, 30, 40, 50]"
for x in mitems(a):
x = 5 * x - 1
assert $a == "[49, 99, 149, 199, 249]"
var c = 0
let stop = deq.len
while c < stop:
yield deq.data[(deq.head + c.uint) and deq.mask]
inc c
iterator pairs*[T](deq: Deque[T]): tuple[key: int, val: T] {.inline.} =
## Yields every `(position, value)`-pair of `deq`.
##
## **See also:**
## * `backwardsPairs iterator <#backwardsPairs.i,Deque[T]>`_
runnableExamples:
import std/sequtils
let a = [10, 20, 30].toDeque
assert toSeq(a.pairs) == @[(0, 10), (1, 20), (2, 30)]
var c = 0
let stop = deq.len
while c < stop:
yield (c, deq.data[(deq.head + c.uint) and deq.mask])
inc c
iterator backwards*[T](deq: Deque[T]): T {.inline.} =
## Yields each element of `deq` in reverse order.
##
## **See also:**
## * `items iterator <#items.i,Deque[T]>`_
## * `backwardsMut iterator <#backwardsMut.i,Deque[T]>`_
runnableExamples:
let thisDeq = [1, 2, 3, 4, 5].toDeque
var thatDeq = [6].toDeque
for item in backwards(thisDeq):
thatDeq.addLast(item)
assert thisDeq == [1, 2, 3, 4, 5].toDeque
assert thatdeq == [6, 5, 4, 3, 2, 1].toDeque
var c = deq.high
while c >= 0:
yield deq.data[(deq.head + c.uint) and deq.mask]
dec c
iterator backwardsMut*[T](deq: var Deque[T]): var T {.inline.} =
## Yields in reverse order a mutable version of every element of `deq`.
##
## **See also:**
## * `mitems iterator <#mitems.i,Deque[T]>`_
## * `backwards iterator <#backwards.i,Deque[T]>`_
runnableExamples:
var thisDeq = [1, 2, 3, 4, 5].toDeque
var thatDeq = [12].toDeque
var otherDeq = @@[6]
for item in backwardsMut(thisDeq):
otherDeq.addFirst(item)
item *= 2
thatDeq.addLast(item)
assert thisDeq == [2, 4, 6, 8, 10].toDeque
assert thatDeq == [12, 10, 8, 6, 4, 2].toDeque
assert otherDeq == @@[1, 2, 3, 4, 5, 6]
var c = deq.high
while c >= 0:
yield deq.data[(deq.head + c.uint) and deq.mask]
dec c
iterator backwardsPairs*[T](deq: Deque[T]): tuple[key: int, val: T] {.inline.} =
## Yields every `(position, value)`-pair of `deq` in reverse order.
##
## **See also:**
## * `pairs iterator <#pairs.i,Deque[T]>`_
runnableExamples:
import std/sequtils
let a = [10, 20, 30].toDeque
assert toSeq(a.backwardsPairs) == @[(2, 30), (1, 20), (0, 10)]
var c = deq.high
while c >= 0:
yield (c, deq.data[(deq.head + c.uint) and deq.mask])
dec c
proc contains*[T](deq: Deque[T], item: T): bool {.inline.} =
## Returns true if `item` is in `deq` or false if not found.
##
## Usually used via the `in` operator.
## It is the equivalent of `deq.find(item) >= 0`.
runnableExamples:
let q = [7, 9].toDeque
assert 7 in q
assert q.contains(7)
assert 8 notin q
for e in deq:
if e == item: return true
return false
proc expandIfNeeded[T](deq: var Deque[T], count: Natural = 1) =
checkIfInitialized(deq)
let cap = capacity(deq.data)
assert deq.data.len == cap
assert deq.len <= cap
if unlikely((deq.len + count) > cap):
var n = newSeq[T](nextPowerOfTwo(deq.len + count))
var i = 0
for x in mitems(deq):
when nimvm: n[i] = x # workaround for VM bug
else: n[i] = move(x)
inc i
deq.data = move(n)
deq.tail = len(deq).uint
deq.head = 0
proc addFirst*[T](deq: var Deque[T], item: sink T) =
## Adds an `item` to the beginning of `deq`.
##
## **See also:**
## * `addLast proc <#addLast,Deque[T],sinkT>`_
## * `& proc <#&,sinkDeque[T],sinkT>`_
runnableExamples:
var a = initDeque[int]()
for i in 1 .. 5:
a.addFirst(10 * i)
assert $a == "[50, 40, 30, 20, 10]"
expandIfNeeded(deq)
dec deq.head
deq.data[deq.head and deq.mask] = item
proc addFirst*[T](deq1: var Deque[T], deq2: sink Deque[T]) =
## Adds `deq2` to the beginning of `deq1` as concatenation.
##
## **See also:**
## * `addLast proc <#addLast,Deque[T],sinkDeque[T]>`_
runnableExamples:
var a = [1, 2, 3].toDeque
let b = [10, 20, 30].toDeque
a.addFirst(b)
assert $a == "[10, 20, 30, 1, 2, 3]"
expandIfNeeded(deq1, len(deq2))
let howMany = deq2.high
if howMany < 0: return
for i in 0 .. howMany:
deq1.addFirst(deq2[howMany - i])
proc addFirst*[T](deq1: var Deque[T], seq2: sink openArray[T]) =
## Adds `deq2` to the beginning of `deq1` as concatenation.
##
## **See also:**
## * `addLast proc <#addLast,Deque[T],sinkDeque[T]>`_
runnableExamples:
var a = [1, 2, 3].toDeque
let b = [10, 20, 30]
a.addFirst(b)
assert $a == "[10, 20, 30, 1, 2, 3]"
expandIfNeeded(deq1, len(seq2))
let howMany = seq2.high
if howMany < 0: return
for i in 0 .. howMany:
deq1.addFirst(seq2[howMany - i])
proc addLast*[T](deq: var Deque[T], item: sink T) =
## Adds an `item` to the end of `deq`.
##
## **See also:**
## * `addFirst proc <#addFirst,Deque[T],sinkT>`_
## * `& proc <#&,sinkDeque[T],sinkT>`_
## * `&= template <#&=.t,Deque[T],sinkT>`_
runnableExamples:
var a = initDeque[int]()
for i in 1 .. 5:
a.addLast(10 * i)
assert $a == "[10, 20, 30, 40, 50]"
expandIfNeeded(deq)
deq.data[deq.tail and deq.mask] = item
inc deq.tail
proc addLast*[T](deq1: var Deque[T], deq2: sink Deque[T]) =
## Adds `deq2` to the end of `deq1` as concatenation.
##
## **See also:**
## * `addFirst proc <#addFirst,Deque[T],sinkDeque[T]>`_
## * `& proc <#&,sinkDeque[T],sinkDeque[T]>`_
## * `&= template <#&=.t,Deque[T],sinkDeque[T]>`_
runnableExamples:
var a = [1, 2, 3].toDeque
let b = [10, 20, 30].toDeque
a.addLast(b)
assert $a == "[1, 2, 3, 10, 20, 30]"
expandIfNeeded(deq1, len(deq2))
let howMany = deq2.high
if howMany < 0: return
for i in 0 .. howMany:
deq1.addLast(deq2[i])
proc addLast*[T](deq1: var Deque[T], seq2: sink openArray[T]) =
## Adds `deq2` to the end of `deq1` as concatenation.
##
## **See also:**
## * `addFirst proc <#addFirst,Deque[T],sinkDeque[T]>`_
## * `& proc <#&,sinkDeque[T],sinkDeque[T]>`_
## * `&= template <#&=.t,Deque[T],sinkDeque[T]>`_
runnableExamples:
var a = [1, 2, 3].toDeque
let b = @[10, 20, 30]
a.addLast(b)
assert $a == "[1, 2, 3, 10, 20, 30]"
expandIfNeeded(deq1, len(seq2))
let howMany = seq2.high
if howMany < 0: return
for i in 0 .. howMany:
deq1.addLast(seq2[i])
proc toDeque*[T](x: sink openArray[T]): Deque[T] =
## Creates a new Deque that contains the elements of `x` (in the same order).
##
## **See also:**
## * `initDeque proc <#initDeque,Natural>`_
## * `newDeque proc <#newDeque,Natural>`_
## * `toDeque template <#toDeque.t,Deque[T]>`_
## * `@@ template <#@@.t,openArray[T]>`_
runnableExamples:
let a = toDeque([7, 8, 9])
assert len(a) == 3
assert $a == "[7, 8, 9]"
result.initImpl(x.len)
for item in items(x):
result.addLast(item)
template toDeque*[T](deq: Deque[T]): Deque[T] =
## Returns a copy of `deq`.
##
## **See also:**
## * `toDeque proc <#toDeque,sinkopenArray[T]>`_
## * `@@ template <#@@.t,Deque[T]>`_
deq
template `@@`*[T](x: openArray[T]): Deque[T] =
## Creates a new Deque that contains the elements of `x` (in the same order).
##
## **See also:**
## * `toDeque proc <#toDeque,sinkopenArray[T]>`_
runnableExamples:
let thisDeq = @@[1, 2, 3]
assert thisDeq == [1, 2, 3].todeque
assert $thisDeq == "[1, 2, 3]"
toDeque(x)
template `@@`*[T](deq: Deque[T]): Deque[T] =
## Returns a copy of `deq`.
##
## **See also:**
## * `toDeque template <#toDeque.t,Deque[T]>`_
deq
proc first*[T](deq: Deque[T]): T {.inline.} =
## Returns the first element of `deq`, but does not remove it from the Deque.
##
## **See also:**
## * `first proc <#first,Deque[T]_2>`_ which returns a mutable reference
## * `last proc <#last,Deque[T]>`_
runnableExamples:
let a = [10, 20, 30, 40, 50].toDeque
assert $a == "[10, 20, 30, 40, 50]"
assert a.first == 10
assert len(a) == 5
emptyCheck(deq)
result = deq.data[deq.head and deq.mask]
proc last*[T](deq: Deque[T]): T {.inline.} =
## Returns the last element of `deq`, but does not remove it from the Deque.
##
## **See also:**
## * `last proc <#last,Deque[T]_2>`_ which returns a mutable reference
## * `first proc <#first,Deque[T]>`_
runnableExamples:
let a = [10, 20, 30, 40, 50].toDeque
assert $a == "[10, 20, 30, 40, 50]"
assert a.last == 50
assert len(a) == 5
emptyCheck(deq)
result = deq.data[(deq.tail - 1) and deq.mask]
proc first*[T](deq: var Deque[T]): var T {.inline.} =
## Returns a mutable reference to the first element of `deq`,
## but does not remove it from the Deque.
##
## **See also:**
## * `first proc <#first,Deque[T]>`_
## * `last proc <#last,Deque[T]_2>`_
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
first(a) = 99
assert $a == "[99, 20, 30, 40, 50]"
inc a.first
assert $a == "[100, 20, 30, 40, 50]"
emptyCheck(deq)
result = deq.data[deq.head and deq.mask]
proc last*[T](deq: var Deque[T]): var T {.inline.} =
## Returns a mutable reference to the last element of `deq`,
## but does not remove it from the Deque.
##
## **See also:**
## * `first proc <#first,Deque[T]_2>`_
## * `last proc <#last,Deque[T]>`_
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
a.last() = 99
assert $a == "[10, 20, 30, 40, 99]"
inc a.last
assert $a == "[10, 20, 30, 40, 100]"
emptyCheck(deq)
result = deq.data[(deq.tail - 1) and deq.mask]
proc `first=`*[T](deq: var Deque[T], item: sink T) {.inline.} =
## Alters the first element of `deq`.
##
## **See also:**
## * `first proc <#first,Deque[T]>`_
## * `last proc <#last,Deque[T]_2>`_
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
a.first = 99
assert $a == "[99, 20, 30, 40, 50]"
emptyCheck(deq)
deq[0] = item
proc `last=`*[T](deq: var Deque[T], item: sink T){.inline.} =
## Alters the last element of `deq`.
##
## **See also:**
## * `first proc <#first,Deque[T]_2>`_
## * `last proc <#last,Deque[T]>`_
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
a.last = 99
assert $a == "[10, 20, 30, 40, 99]"
emptyCheck(deq)
deq[deq.high] = item
proc popFirst*[T](deq: var Deque[T]): T {.inline, discardable.} =
## Removes and returns the first element of the `deq`.
##
## See also:
## * `popLast proc <#popLast,Deque[T]>`_
## * `shrink proc <#shrink,Deque[T],int,int>`_
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
assert $a == "[10, 20, 30, 40, 50]"
assert a.popFirst == 10
assert $a == "[20, 30, 40, 50]"
emptyCheck(deq)
result = move deq.data[deq.head and deq.mask]
inc deq.head
proc popLast*[T](deq: var Deque[T]): T {.inline, discardable.} =
## Removes and returns the last element of the `deq`.
##
## **See also:**
## * `popFirst proc <#popFirst,Deque[T]>`_
## * `shrink proc <#shrink,Deque[T],int,int>`_
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
assert $a == "[10, 20, 30, 40, 50]"
assert a.popLast == 50
assert $a == "[10, 20, 30, 40]"
emptyCheck(deq)
dec deq.tail
result = move deq.data[deq.tail and deq.mask]
proc dropFirst*[T](deq: var Deque[T]) {.inline.} =
## Removes the first element of the `deq`.
##
## See also:
## * `popFirst proc <#popFirst,Deque[T]>`_
## * `dropLast proc <#dropLast,Deque[T]>`_
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
assert $a == "[10, 20, 30, 40, 50]"
a.dropFirst
assert $a == "[20, 30, 40, 50]"
emptyCheck(deq)
destroy deq.data[deq.head and deq.mask]
inc deq.head
proc dropLast*[T](deq: var Deque[T]) {.inline.} =
## Removes the last element of the `deq`.
##
## **See also:**
## * `dropFirst proc <#dropFirst,Deque[T]>`_
## * `popLast proc <#popLast,Deque[T]>`_
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
assert $a == "[10, 20, 30, 40, 50]"
a.dropLast
assert $a == "[10, 20, 30, 40]"
emptyCheck(deq)
dec deq.tail
destroy deq.data[deq.tail and deq.mask]
proc shrink*[T](deq: var Deque[T], fromFirst = 0, fromLast = 0) =
## Removes `fromFirst` elements from the front of the Deque and
## `fromLast` elements from the back.
##
## If the supplied number of elements exceeds the total number of elements
## in the Deque, the Deque will remain empty.
##
## **See also:**
## * `clear template <#clear.t,Deque[T]>`_
## * `dropFirst proc <#dropFirst,Deque[T]>`_
## * `dropLast proc <#dropLast,Deque[T]>`_
runnableExamples:
var a = [10, 20, 30, 40, 50].toDeque
assert $a == "[10, 20, 30, 40, 50]"
a.shrink(fromFirst = 2, fromLast = 1)
assert $a == "[30, 40]"
if fromFirst + fromLast > deq.len:
clear(deq)
return
for i in 0 ..< fromFirst:
destroy(deq.data[deq.head and deq.mask])
inc deq.head
for i in 0 ..< fromLast:
dec deq.tail
destroy(deq.data[deq.tail and deq.mask])
proc setLen*[T](target: var Deque[T], length: Natural) =
## Sets the length of `target` to `length`, increasing its capacity if needed.
let howLong = len(target)
if length == howLong: return
if length > howLong:
expandIfNeeded(target, length - howLong)
target.tail += (length - howLong).uint
else:
shrink(target, 0, howLong - length)
proc setCap*[T](target: var Deque[T], length: Natural) =
## Sets the capacity of `target` to `length`, shrinking or growing as needed.
## Capacity will always be a power of two.
let tLen = len(target)
let tCap = capacity(target)
if length == tCap: return
if length == 0:
target = initDeque[T](0)
if length > tcap:
expandIfNeeded(target, length - tcap)
else:
var seq1 = newSeq[T](nextPowerOfTwo(length))
for i in 0 ..< min(length, tLen):
seq1[i] = target[i]
destroy target.data
target.data = seq1
target.head = 0.uint
target.tail = min(length, tLen).uint
proc `$`*[T](deq: Deque[T]): string =
## Turns a Deque into its string representation.
runnableExamples:
let a = [10, 20, 30].toDeque
assert $a == "[10, 20, 30]"
result = "["
for x in deq:
if result.len > 1: result.add(", ")
result.addQuoted(x)
result.add("]")
func `==`*[T](deq1, deq2: Deque[T]): bool =
## The `==` operator for Deque.
## Returns `true` if both Deques contains the same values in the same order.
runnableExamples:
var a, b = initDeque[int]()
a.addFirst(2)
a.addFirst(1)
b.addLast(1)
b.addLast(2)
doAssert a == b
if deq1.len != deq2.len:
return false
for i in 0 ..< deq1.len:
if deq1.data[(deq1.head + i.uint) and deq1.mask] != deq2.data[(deq2.head +
i.uint) and deq2.mask]:
return false
true
func hash*[T](deq: Deque[T]): Hash =
## Hashing of Deque.
var h: Hash = 0
for x in deq:
h = h !& hash(x)
!$h
proc normalize[T](target: var Deque[T]) =
checkIfInitialized(target)
if len(target) == 0 or target.head == 0:
return
var newDeq = initDeque[T](capacity(target))
for item in target:
newDeq.addLast(item)
target = move newDeq
proc makeRoom[T](target: var Deque[T], pos: Natural, howMany: Natural) =
if howMany == 0:
return
expandIfNeeded(target, howMany)
let lastPos = target.high
target.tail += howMany.uint
if len(target) == howMany or pos == lastPos + 1: # Insert just after last element = concat
return
when compileOption("boundChecks"): # `-d:danger` or `--checks:off` should disable this.
if unlikely(pos > lastPos + 1): # pos < deq.low is taken care by the Natural parameter
raise newException(IndexDefect,
"Out of bounds: " & $pos & " > " & $(lastPos + 1))
for i in countdown(lastPos, pos):
target.data[i + howMany] = move target.data[i]
proc insert*[T](target: var Deque[T], source: sink T, pos: Natural) =
## Insert `source` element into `target` Deque in front of position `pos`.
checkIfInitialized(target)
normalize(target)
makeRoom(target, pos, 1)
target.data[pos] = source
proc insert*[T](target: var Deque[T], source: sink openArray[T], pos: Natural) =
## Insert `source` sequence into `target` Deque in front of position `pos`.
checkIfInitialized(target)
if unlikely len(source) == 0:
return
normalize(target)
makeRoom(target, pos, len(source))
for count, item in source:
target.data[pos + count] = item
proc insert*[T](target: var Deque[T], source: sink Deque[T], pos: Natural) =
## Insert `source` Deque into `target` Deque in front of position `pos`.
checkIfInitialized(target)
if unlikely len(source) == 0: return
normalize(target)
makeRoom(target, pos, len(source))
for count, item in source:
target.data[pos + count] = item
proc reverse*[T](target: var Deque[T]) =
## Reverses `target` in place.
checkIfInitialized(target)
var lo = target.low
var hi = target.high
while lo < hi:
swap(target[lo], target[hi])
dec(hi)
inc(lo)
proc reversed*[T](source: Deque[T]): Deque[T] =
## Returns a reversed copy of `source`.
if unlikely len(source) == 0: return
for item in source:
result.addFirst(item)
proc `&`*[T](x, y: sink Deque[T]): Deque[T] {.noSideEffect.} =
## Returns the concatenation of two Deques.
##
## See also:
## * `addLast(var Deque[T], sink Deque[T]) <#addLast,Deque[T],sinkDeque[T]>`_
## * `&= template <#&=.t,Deque[T],sinkDeque[T]>`_
result = x
result.addLast(y)