forked from flowonyx/runtemplate
/
fast_int_queue.go
997 lines (846 loc) · 25.5 KB
/
fast_int_queue.go
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
// A queue or fifo that holds int, implemented via a ring buffer. Unlike the list collections, these
// have a fixed size (although this can be changed when needed). For mutable collection that need frequent
// appending, the fixed size is a benefit because the memory footprint is constrained. However, this is
// not usable unless the rate of removing items from the queue is, over time, the same as the rate of addition.
// For similar reasons, there is no immutable variant of a queue.
//
// The queue provides a method to sort its elements.
//
// Not thread-safe.
//
// Generated from fast/queue.tpl with Type=int
// options: Comparable:<no value> Numeric:<no value> Ordered:<no value> Sorted:<no value> Stringer:<no value>
// ToList:<no value> ToSet:<no value>
// by runtemplate v3.10.1
// See https://github.com/rickb777/runtemplate/blob/master/BUILTIN.md
package examples
import (
"sort"
)
// FastIntQueue is a ring buffer containing a slice of type int. It is optimised
// for FIFO operations.
type FastIntQueue struct {
m []int
read int
write int
length int
capacity int
overwrite bool
less func(i, j int) bool
}
// NewFastIntQueue returns a new queue of int. The behaviour when adding
// to the queue depends on overwrite. If true, the push operation overwrites oldest values up to
// the space available, when the queue is full. Otherwise, it refuses to overfill the queue.
func NewFastIntQueue(capacity int, overwrite bool) *FastIntQueue {
return NewFastIntSortedQueue(capacity, overwrite, nil)
}
// NewFastIntSortedQueue returns a new queue of int. The behaviour when adding
// to the queue depends on overwrite. If true, the push operation overwrites oldest values up to
// the space available, when the queue is full. Otherwise, it refuses to overfill the queue.
// If the 'less' comparison function is not nil, elements can be easily sorted.
func NewFastIntSortedQueue(capacity int, overwrite bool, less func(i, j int) bool) *FastIntQueue {
return &FastIntQueue{
m: make([]int, capacity),
read: 0,
write: 0,
length: 0,
capacity: capacity,
overwrite: overwrite,
less: less,
}
}
// BuildFastIntQueueFromChan constructs a new FastIntQueue from a channel that supplies
// a sequence of values until it is closed. The function doesn't return until then.
func BuildFastIntQueueFromChan(source <-chan int) *FastIntQueue {
queue := NewFastIntQueue(0, false)
for v := range source {
queue.m = append(queue.m, v)
}
queue.length = len(queue.m)
queue.capacity = cap(queue.m)
return queue
}
//-------------------------------------------------------------------------------------------------
// Reallocate adjusts the allocated capacity of the queue and allows the overwriting behaviour to be changed.
//
// If the new queue capacity is different to the current capacity, the queue is re-allocated to the new
// capacity. If this is less than the current number of elements, the oldest items in the queue are
// discarded so that the remaining data can fit in the new space available.
//
// If the new queue capacity is the same as the current capacity, the queue is not altered except for adopting
// the new overwrite flag's value. Therefore this is the means to change the overwriting behaviour.
//
// Reallocate adjusts the storage space but does not clone the underlying elements.
//
// The queue must not be nil.
func (queue *FastIntQueue) Reallocate(capacity int, overwrite bool) *FastIntQueue {
if capacity < 1 {
panic("capacity must be at least 1")
}
return queue.doReallocate(capacity, overwrite)
}
func (queue *FastIntQueue) doReallocate(capacity int, overwrite bool) *FastIntQueue {
queue.overwrite = overwrite
if capacity < queue.length {
// existing data is too big and has to be trimmed to fit
n := queue.length - capacity
queue.read = (queue.read + n) % queue.capacity
queue.length -= n
}
if capacity != queue.capacity {
oldLength := queue.length
queue.m = queue.toSlice(make([]int, capacity))
if oldLength > len(queue.m) {
oldLength = len(queue.m)
}
queue.read = 0
queue.write = oldLength
queue.length = oldLength
queue.capacity = capacity
}
return queue
}
// Space returns the space available in the queue.
func (queue *FastIntQueue) Space() int {
if queue == nil {
return 0
}
return queue.capacity - queue.length
}
// Cap gets the capacity of this queue.
func (queue *FastIntQueue) Cap() int {
if queue == nil {
return 0
}
return queue.capacity
}
//-------------------------------------------------------------------------------------------------
// IsSequence returns true for ordered lists and queues.
func (queue *FastIntQueue) IsSequence() bool {
return true
}
// IsSet returns false for lists or queues.
func (queue *FastIntQueue) IsSet() bool {
return false
}
// ToSlice returns the elements of the queue as a slice. The queue is not altered.
func (queue *FastIntQueue) ToSlice() []int {
if queue == nil {
return nil
}
return queue.toSlice(make([]int, queue.length))
}
func (queue *FastIntQueue) toSlice(s []int) []int {
front, back := queue.frontAndBack()
copy(s, front)
if len(back) > 0 && len(s) >= len(front) {
copy(s[len(front):], back)
}
return s
}
// ToInterfaceSlice returns the elements of the queue as a slice of arbitrary type.
// The queue is not altered.
func (queue *FastIntQueue) ToInterfaceSlice() []interface{} {
if queue == nil {
return nil
}
front, back := queue.frontAndBack()
s := make([]interface{}, 0, queue.length)
for _, v := range front {
s = append(s, v)
}
for _, v := range back {
s = append(s, v)
}
return s
}
// Clone returns a shallow copy of the queue. It does not clone the underlying elements.
func (queue *FastIntQueue) Clone() *FastIntQueue {
if queue == nil {
return nil
}
buffer := queue.toSlice(make([]int, queue.capacity))
return queue.doClone(buffer[:queue.length])
}
func (queue *FastIntQueue) doClone(buffer []int) *FastIntQueue {
w := 0
if len(buffer) < cap(buffer) {
w = len(buffer)
}
return &FastIntQueue{
m: buffer,
read: 0,
write: w,
length: len(buffer),
capacity: cap(buffer),
overwrite: queue.overwrite,
less: queue.less,
}
}
//-------------------------------------------------------------------------------------------------
// Get gets the specified element in the queue.
// Panics if the index is out of range or the queue is nil.
func (queue *FastIntQueue) Get(i int) int {
ri := (queue.read + i) % queue.capacity
return queue.m[ri]
}
// Head gets the first element in the queue. Head is the opposite of Last.
// Panics if queue is empty or nil.
func (queue *FastIntQueue) Head() int {
return queue.m[queue.read]
}
// HeadOption returns the oldest item in the queue without removing it. If the queue
// is nil or empty, it returns the zero value instead.
func (queue *FastIntQueue) HeadOption() (int, bool) {
if queue == nil {
return 0, false
}
if queue.length == 0 {
return 0, false
}
return queue.m[queue.read], true
}
// Last gets the the newest item in the queue (i.e. last element pushed) without removing it.
// Last is the opposite of Head.
// Panics if queue is empty or nil.
func (queue *FastIntQueue) Last() int {
i := queue.write - 1
if i < 0 {
i = queue.capacity - 1
}
return queue.m[i]
}
// LastOption returns the newest item in the queue without removing it. If the queue
// is nil empty, it returns the zero value instead.
func (queue *FastIntQueue) LastOption() (int, bool) {
if queue == nil {
return 0, false
}
if queue.length == 0 {
return 0, false
}
i := queue.write - 1
if i < 0 {
i = queue.capacity - 1
}
return queue.m[i], true
}
//-------------------------------------------------------------------------------------------------
// IsOverwriting returns true if the queue is overwriting, false if refusing.
func (queue *FastIntQueue) IsOverwriting() bool {
if queue == nil {
return false
}
return queue.overwrite
}
// IsFull returns true if the queue is full.
func (queue *FastIntQueue) IsFull() bool {
if queue == nil {
return false
}
return queue.length == queue.capacity
}
// IsEmpty returns true if the queue is empty.
func (queue *FastIntQueue) IsEmpty() bool {
if queue == nil {
return true
}
return queue.length == 0
}
// NonEmpty returns true if the queue is not empty.
func (queue *FastIntQueue) NonEmpty() bool {
if queue == nil {
return false
}
return queue.length > 0
}
// Size gets the number of elements currently in this queue. This is an alias for Len.
func (queue *FastIntQueue) Size() int {
if queue == nil {
return 0
}
return queue.length
}
// Len gets the current length of this queue. This is an alias for Size.
func (queue *FastIntQueue) Len() int {
return queue.Size()
}
// Swap swaps the elements with indexes i and j.
// The queue must not be empty.
func (queue *FastIntQueue) Swap(i, j int) {
ri := (queue.read + i) % queue.capacity
rj := (queue.read + j) % queue.capacity
queue.m[ri], queue.m[rj] = queue.m[rj], queue.m[ri]
}
// Less reports whether the element with index i should sort before the element with index j.
// The queue must have been created with a non-nil 'less' comparison function and it must not
// be empty.
func (queue *FastIntQueue) Less(i, j int) bool {
ri := (queue.read + i) % queue.capacity
rj := (queue.read + j) % queue.capacity
return queue.less(queue.m[ri], queue.m[rj])
}
// Sort sorts the queue using the 'less' comparison function, which must not be nil.
// This function will panic if the collection was created with a nil 'less' function
// (see NewFastIntSortedQueue).
func (queue *FastIntQueue) Sort() {
sort.Sort(queue)
}
// StableSort sorts the queue using the 'less' comparison function, which must not be nil.
// The result is stable so that repeated calls will not arbitrarily swap equal items.
// This function will panic if the collection was created with a nil 'less' function
// (see NewFastIntSortedQueue).
func (queue *FastIntQueue) StableSort() {
sort.Stable(queue)
}
// frontAndBack gets the front and back portions of the queue. The front portion starts
// from the read index. The back portion ends at the write index.
func (queue *FastIntQueue) frontAndBack() ([]int, []int) {
if queue == nil || queue.length == 0 {
return nil, nil
}
if queue.write > queue.read {
return queue.m[queue.read:queue.write], nil
}
return queue.m[queue.read:], queue.m[:queue.write]
}
// indexes gets the indexes for the front and back portions of the queue. The front
// portion starts from the read index. The back portion ends at the write index.
func (queue *FastIntQueue) indexes() []int {
if queue == nil || queue.length == 0 {
return nil
}
if queue.write > queue.read {
return []int{queue.read, queue.write}
}
return []int{queue.read, queue.capacity, 0, queue.write}
}
//-------------------------------------------------------------------------------------------------
// Clear the entire queue.
func (queue *FastIntQueue) Clear() {
if queue != nil {
queue.read = 0
queue.write = 0
queue.length = 0
}
}
// Add adds items to the queue. This is a synonym for Push.
func (queue *FastIntQueue) Add(more ...int) {
queue.Push(more...)
}
// Push appends items to the end of the queue. If the queue does not have enough space,
// more will be allocated: how this happens depends on the overwriting mode.
//
// When overwriting, the oldest items are overwritten with the new data; it expands the queue
// only if there is still not enough space.
//
// Otherwise, the queue might be reallocated if necessary, ensuring that all the data is pushed
// without any older items being affected.
//
// The modified queue is returned.
func (queue *FastIntQueue) Push(items ...int) *FastIntQueue {
n := queue.capacity
if queue.overwrite && len(items) > queue.capacity {
n = len(items)
// no rounding in this case because the old items are expected to be overwritten
} else if !queue.overwrite && len(items) > (queue.capacity-queue.length) {
n = len(items) + queue.length
// rounded up to multiple of 128 to reduce repeated reallocation
n = ((n + 127) / 128) * 128
}
if n > queue.capacity {
queue = queue.doReallocate(n, queue.overwrite)
}
overflow := queue.doPush(items...)
if len(overflow) > 0 {
panic(len(overflow))
}
return queue
}
// Offer appends as many items to the end of the queue as it can.
// If the queue is already full, what happens depends on whether the queue is configured
// to overwrite. If it is, the oldest items will be overwritten. Otherwise, it will be
// filled to capacity and any unwritten items are returned.
//
// If the capacity is too small for the number of items, the excess items are returned.
// The queue capacity is never altered.
func (queue *FastIntQueue) Offer(items ...int) []int {
return queue.doPush(items...)
}
func (queue *FastIntQueue) doPush(items ...int) []int {
n := len(items)
space := queue.capacity - queue.length
overwritten := n - space
if queue.overwrite {
space = queue.capacity
}
if space < n {
// there is too little space; reject surplus elements
surplus := items[space:]
queue.doPush(items[:space]...)
return surplus
}
if n <= queue.capacity-queue.write {
// easy case: enough space at end for all items
copy(queue.m[queue.write:], items)
queue.write = (queue.write + n) % queue.capacity
queue.length += n
return nil
}
// not yet full
end := queue.capacity - queue.write
copy(queue.m[queue.write:], items[:end])
copy(queue.m, items[end:])
queue.write = n - end
queue.length += n
if queue.length > queue.capacity {
queue.length = queue.capacity
}
if overwritten > 0 {
queue.read = (queue.read + overwritten) % queue.capacity
}
return nil
}
// Pop1 removes and returns the oldest item from the queue. If the queue is
// empty, it returns the zero value instead.
// The boolean is true only if the element was available.
func (queue *FastIntQueue) Pop1() (int, bool) {
if queue.length == 0 {
return 0, false
}
v := queue.m[queue.read]
queue.read = (queue.read + 1) % queue.capacity
queue.length--
return v, true
}
// Pop removes and returns the oldest items from the queue. If the queue is
// empty, it returns a nil slice. If n is larger than the current queue length,
// it returns all the available elements, so in this case the returned slice
// will be shorter than n.
func (queue *FastIntQueue) Pop(n int) []int {
return queue.doPop(n)
}
func (queue *FastIntQueue) doPop(n int) []int {
if queue.length == 0 {
return nil
}
if n > queue.length {
n = queue.length
}
s := make([]int, n)
front, back := queue.frontAndBack()
// note the length copied is whichever is shorter
copy(s, front)
if n > len(front) {
copy(s[len(front):], back)
}
queue.read = (queue.read + n) % queue.capacity
queue.length -= n
return s
}
//-------------------------------------------------------------------------------------------------
// Exists verifies that one or more elements of FastIntQueue return true for the predicate p.
// The function should not alter the values via side-effects.
func (queue *FastIntQueue) Exists(p func(int) bool) bool {
if queue == nil {
return false
}
front, back := queue.frontAndBack()
for _, v := range front {
if p(v) {
return true
}
}
for _, v := range back {
if p(v) {
return true
}
}
return false
}
// Forall verifies that all elements of FastIntQueue return true for the predicate p.
// The function should not alter the values via side-effects.
func (queue *FastIntQueue) Forall(p func(int) bool) bool {
if queue == nil {
return true
}
front, back := queue.frontAndBack()
for _, v := range front {
if !p(v) {
return false
}
}
for _, v := range back {
if !p(v) {
return false
}
}
return true
}
// Foreach iterates over FastIntQueue and executes function f against each element.
// The function can safely alter the values via side-effects.
func (queue *FastIntQueue) Foreach(f func(int)) {
if queue == nil {
return
}
front, back := queue.frontAndBack()
for _, v := range front {
f(v)
}
for _, v := range back {
f(v)
}
}
// Send returns a channel that will send all the elements in order.
// A goroutine is created to send the elements; this only terminates when all the elements
// have been consumed. The channel will be closed when all the elements have been sent.
func (queue *FastIntQueue) Send() <-chan int {
ch := make(chan int)
go func() {
if queue != nil {
front, back := queue.frontAndBack()
for _, v := range front {
ch <- v
}
for _, v := range back {
ch <- v
}
}
close(ch)
}()
return ch
}
//-------------------------------------------------------------------------------------------------
// DoKeepWhere modifies a FastIntQueue by retaining only those elements that match
// the predicate p. This is very similar to Filter but alters the queue in place.
//
// The queue is modified and the modified queue is returned.
func (queue *FastIntQueue) DoKeepWhere(p func(int) bool) *FastIntQueue {
if queue == nil {
return nil
}
if queue.length == 0 {
return queue
}
return queue.doKeepWhere(p)
}
func (queue *FastIntQueue) doKeepWhere(p func(int) bool) *FastIntQueue {
last := queue.capacity
if queue.write > queue.read {
// only need to process the front of the queue
last = queue.write
}
r := queue.read
w := r
n := 0
// 1st loop: front of queue (from queue.read)
for r < last {
if p(queue.m[r]) {
if w != r {
queue.m[w] = queue.m[r]
}
w++
n++
}
r++
}
w = w % queue.capacity
if queue.write > queue.read {
// only needed to process the front of the queue
queue.write = w
queue.length = n
return queue
}
// 2nd loop: back of queue (from 0 to queue.write)
r = 0
for r < queue.write {
if p(queue.m[r]) {
if w != r {
queue.m[w] = queue.m[r]
}
w = (w + 1) % queue.capacity
n++
}
r++
}
queue.write = w
queue.length = n
return queue
}
//-------------------------------------------------------------------------------------------------
// Find returns the first int that returns true for predicate p.
// False is returned if none match.
func (queue *FastIntQueue) Find(p func(int) bool) (int, bool) {
if queue == nil {
return 0, false
}
front, back := queue.frontAndBack()
for _, v := range front {
if p(v) {
return v, true
}
}
for _, v := range back {
if p(v) {
return v, true
}
}
var empty int
return empty, false
}
// Filter returns a new FastIntQueue whose elements return true for predicate p.
//
// The original queue is not modified. See also DoKeepWhere (which does modify the original queue).
func (queue *FastIntQueue) Filter(p func(int) bool) *FastIntQueue {
if queue == nil {
return nil
}
result := NewFastIntSortedQueue(queue.length, queue.overwrite, queue.less)
i := 0
front, back := queue.frontAndBack()
for _, v := range front {
if p(v) {
result.m[i] = v
i++
}
}
for _, v := range back {
if p(v) {
result.m[i] = v
i++
}
}
result.length = i
result.write = i
return result
}
// Partition returns two new FastIntQueues whose elements return true or false for the predicate, p.
// The first result consists of all elements that satisfy the predicate and the second result consists of
// all elements that don't. The relative order of the elements in the results is the same as in the
// original queue.
//
// The original queue is not modified
func (queue *FastIntQueue) Partition(p func(int) bool) (*FastIntQueue, *FastIntQueue) {
if queue == nil {
return nil, nil
}
matching := NewFastIntSortedQueue(queue.length, queue.overwrite, queue.less)
others := NewFastIntSortedQueue(queue.length, queue.overwrite, queue.less)
m, o := 0, 0
front, back := queue.frontAndBack()
for _, v := range front {
if p(v) {
matching.m[m] = v
m++
} else {
others.m[o] = v
o++
}
}
for _, v := range back {
if p(v) {
matching.m[m] = v
m++
} else {
others.m[o] = v
o++
}
}
matching.length = m
matching.write = m
others.length = o
others.write = o
return matching, others
}
// Map returns a new FastIntQueue by transforming every element with function f.
// The resulting queue is the same size as the original queue.
// The original queue is not modified.
//
// This is a domain-to-range mapping function. For bespoke transformations to other types, copy and modify
// this method appropriately.
func (queue *FastIntQueue) Map(f func(int) int) *FastIntQueue {
if queue == nil {
return nil
}
slice := make([]int, queue.length)
i := 0
front, back := queue.frontAndBack()
for _, v := range front {
slice[i] = f(v)
i++
}
for _, v := range back {
slice[i] = f(v)
i++
}
return queue.doClone(slice)
}
// MapToString returns a new []string by transforming every element with function f.
// The resulting slice is the same size as the queue.
// The queue is not modified.
//
// This is a domain-to-range mapping function. For bespoke transformations to other types, copy and modify
// this method appropriately.
func (queue *FastIntQueue) MapToString(f func(int) string) []string {
if queue == nil {
return nil
}
result := make([]string, 0, queue.length)
front, back := queue.frontAndBack()
for _, v := range front {
result = append(result, f(v))
}
for _, v := range back {
result = append(result, f(v))
}
return result
}
// MapToInt64 returns a new []int64 by transforming every element with function f.
// The resulting slice is the same size as the queue.
// The queue is not modified.
//
// This is a domain-to-range mapping function. For bespoke transformations to other types, copy and modify
// this method appropriately.
func (queue *FastIntQueue) MapToInt64(f func(int) int64) []int64 {
if queue == nil {
return nil
}
result := make([]int64, 0, queue.length)
front, back := queue.frontAndBack()
for _, v := range front {
result = append(result, f(v))
}
for _, v := range back {
result = append(result, f(v))
}
return result
}
// FlatMap returns a new FastIntQueue by transforming every element with function f that
// returns zero or more items in a slice. The resulting queue may have a different size to the original queue.
// The original queue is not modified.
//
// This is a domain-to-range mapping function. For bespoke transformations to other types, copy and modify
// this method appropriately.
func (queue *FastIntQueue) FlatMap(f func(int) []int) *FastIntQueue {
if queue == nil {
return nil
}
slice := make([]int, 0, queue.length)
front, back := queue.frontAndBack()
for _, v := range front {
slice = append(slice, f(v)...)
}
for _, v := range back {
slice = append(slice, f(v)...)
}
return queue.doClone(slice)
}
// FlatMapToString returns a new []string by transforming every element with function f that
// returns zero or more items in a slice. The resulting slice may have a different size to the queue.
// The queue is not modified.
//
// This is a domain-to-range mapping function. For bespoke transformations to other types, copy and modify
// this method appropriately.
func (queue *FastIntQueue) FlatMapToString(f func(int) []string) []string {
if queue == nil {
return nil
}
result := make([]string, 0, 32)
for _, v := range queue.m {
result = append(result, f(v)...)
}
return result
}
// FlatMapToInt64 returns a new []int64 by transforming every element with function f that
// returns zero or more items in a slice. The resulting slice may have a different size to the queue.
// The queue is not modified.
//
// This is a domain-to-range mapping function. For bespoke transformations to other types, copy and modify
// this method appropriately.
func (queue *FastIntQueue) FlatMapToInt64(f func(int) []int64) []int64 {
if queue == nil {
return nil
}
result := make([]int64, 0, 32)
for _, v := range queue.m {
result = append(result, f(v)...)
}
return result
}
// CountBy gives the number elements of FastIntQueue that return true for the predicate p.
func (queue *FastIntQueue) CountBy(p func(int) bool) (result int) {
if queue == nil {
return 0
}
front, back := queue.frontAndBack()
for _, v := range front {
if p(v) {
result++
}
}
for _, v := range back {
if p(v) {
result++
}
}
return
}
// Fold aggregates all the values in the queue using a supplied function, starting from some initial value.
func (queue *FastIntQueue) Fold(initial int, fn func(int, int) int) int {
if queue == nil {
return initial
}
m := initial
front, back := queue.frontAndBack()
for _, v := range front {
m = fn(m, v)
}
for _, v := range back {
m = fn(m, v)
}
return m
}
// MinBy returns an element of FastIntQueue containing the minimum value, when compared to other elements
// using a passed func defining ‘less’. In the case of multiple items being equally minimal, the first such
// element is returned. Panics if there are no elements.
func (queue *FastIntQueue) MinBy(less func(int, int) bool) int {
if queue.length == 0 {
panic("Cannot determine the minimum of an empty queue.")
}
indexes := queue.indexes()
m := indexes[0]
for len(indexes) > 1 {
f := indexes[0]
for i := f; i < indexes[1]; i++ {
if i != m {
if less(queue.m[i], queue.m[m]) {
m = i
}
}
}
indexes = indexes[2:]
}
return queue.m[m]
}
// MaxBy returns an element of FastIntQueue containing the maximum value, when compared to other elements
// using a passed func defining ‘less’. In the case of multiple items being equally maximal, the first such
// element is returned. Panics if there are no elements.
func (queue *FastIntQueue) MaxBy(less func(int, int) bool) int {
if queue.length == 0 {
panic("Cannot determine the maximum of an empty queue.")
}
indexes := queue.indexes()
m := indexes[0]
for len(indexes) > 1 {
f := indexes[0]
for i := f; i < indexes[1]; i++ {
if i != m {
if less(queue.m[m], queue.m[i]) {
m = i
}
}
}
indexes = indexes[2:]
}
return queue.m[m]
}