-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
modifiers.ts
889 lines (837 loc) · 25.1 KB
/
modifiers.ts
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
//================================================================
/**
* @packageDocumentation
* @module std
*/
//================================================================
import { IPointer } from "../functional/IPointer";
import { equal_to } from "../functional/comparators";
import { BinaryPredicator } from "../internal/functional/BinaryPredicator";
import { General } from "../internal/functional/General";
import { UnaryPredicator } from "../internal/functional/UnaryPredicator";
import { Writeonly } from "../internal/functional/Writeonly";
import { IBidirectionalIterator } from "../iterator/IBidirectionalIterator";
import { IForwardIterator } from "../iterator/IForwardIterator";
import { IRandomAccessIterator } from "../iterator/IRandomAccessIterator";
import { advance } from "../iterator/global";
import { randint } from "./random";
type UnaryOperatorInferrer<
InputIterator extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<OutputIterator>, OutputIterator>
>,
> = (
val: IPointer.ValueType<InputIterator>,
) => IPointer.ValueType<OutputIterator>;
type BinaryOperatorInferrer<
InputIterator1 extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator1>, InputIterator1>
>,
InputIterator2 extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator2>, InputIterator2>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<OutputIterator>, OutputIterator>
>,
> = (
x: IPointer.ValueType<InputIterator1>,
y: IPointer.ValueType<InputIterator2>,
) => IPointer.ValueType<OutputIterator>;
/* =========================================================
MODIFIERS (MODIFYING SEQUENCE)
- FILL
- REMOVE
- REPLACE & SWAP
- RE-ARRANGEMENT
============================================================
FILL
--------------------------------------------------------- */
/**
* Copy elements in range.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param output Output iterator of the first position.
*
* @return Output Iterator of the last position by advancing.
*/
export function copy<
InputIterator extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<InputIterator>, OutputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
output: OutputIterator,
): OutputIterator {
for (; !first.equals(last); first = first.next()) {
output.value = first.value;
output = output.next();
}
return output;
}
/**
* Copy *n* elements.
*
* @param first Input iteartor of the first position.
* @param n Number of elements to copy.
* @param output Output iterator of the first position.
*
* @return Output Iterator of the last position by advancing.
*/
export function copy_n<
InputIterator extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<InputIterator>, OutputIterator>
>,
>(first: InputIterator, n: number, output: OutputIterator): OutputIterator {
for (let i: number = 0; i < n; ++i) {
output.value = first.value;
first = first.next();
output = output.next();
}
return output;
}
/**
* Copy specific elements by a condition.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param output Output iterator of the first position.
* @param pred A function predicates the specific condition.
*
* @return Output Iterator of the last position by advancing.
*/
export function copy_if<
InputIterator extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<InputIterator>, OutputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
output: OutputIterator,
pred: UnaryPredicator<IPointer.ValueType<InputIterator>>,
): OutputIterator {
for (; !first.equals(last); first = first.next()) {
if (!pred(first.value)) continue;
output.value = first.value;
output = output.next();
}
return output;
}
/**
* Copy elements reversely.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param output Output iterator of the first position.
*
* @return Output Iterator of the last position by advancing.
*/
export function copy_backward<
InputIterator extends Readonly<
IBidirectionalIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
OutputIterator extends Writeonly<
IBidirectionalIterator<IPointer.ValueType<InputIterator>, OutputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
output: OutputIterator,
): OutputIterator {
last = last.prev();
while (!last.equals(first)) {
last = last.prev();
output = output.prev();
output.value = last.value;
}
return output;
}
/**
* Fill range elements
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param val The value to fill.
*
* @return Output Iterator of the last position by advancing.
*/
export function fill<
ForwardIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<ForwardIterator>, ForwardIterator>
>,
>(
first: ForwardIterator,
last: ForwardIterator,
val: IPointer.ValueType<ForwardIterator>,
): void {
for (; !first.equals(last); first = first.next()) first.value = val;
}
/**
* Fill *n* elements.
*
* @param first Input iteartor of the first position.
* @param n Number of elements to fill.
* @param val The value to fill.
*
* @return Output Iterator of the last position by advancing.
*/
export function fill_n<
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<OutputIterator>, OutputIterator>
>,
>(
first: OutputIterator,
n: number,
val: IPointer.ValueType<OutputIterator>,
): OutputIterator {
for (let i: number = 0; i < n; ++i) {
first.value = val;
first = first.next();
}
return first;
}
/**
* Transform elements.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param output Output iterator of the first position.
* @param op Unary function determines the transform.
*
* @return Output Iterator of the last position by advancing.
*/
export function transform<
InputIterator extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<OutputIterator>, OutputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
result: OutputIterator,
op: UnaryOperatorInferrer<InputIterator, OutputIterator>,
): OutputIterator;
/**
* Transform elements.
*
* @param first1 Input iteartor of the first position of the 1st range.
* @param last1 Input iterator of the last position of the 1st range.
* @param first2 Input iterator of the first position of the 2nd range.
* @param output Output iterator of the first position.
* @param op Binary function determines the transform.
*
* @return Output Iterator of the last position by advancing.
*/
export function transform<
InputIterator1 extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator1>, InputIterator1>
>,
InputIterator2 extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator2>, InputIterator2>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<OutputIterator>, OutputIterator>
>,
>(
first1: InputIterator1,
last1: InputIterator1,
first2: InputIterator2,
result: OutputIterator,
op: BinaryOperatorInferrer<InputIterator1, InputIterator2, OutputIterator>,
): OutputIterator;
export function transform(...args: any[]): any {
if (args.length === 4) return (_Unary_transform as Function)(...args);
// args: #5
else return (_Binary_transform as Function)(...args);
}
function _Unary_transform<
InputIterator extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<OutputIterator>, OutputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
result: OutputIterator,
op: UnaryOperatorInferrer<InputIterator, OutputIterator>,
): OutputIterator {
for (; !first.equals(last); first = first.next()) {
result.value = op(first.value);
result = result.next();
}
return result;
}
function _Binary_transform<
InputIterator1 extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator1>, InputIterator1>
>,
InputIterator2 extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator2>, InputIterator2>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<OutputIterator>, OutputIterator>
>,
>(
first1: InputIterator1,
last1: InputIterator1,
first2: InputIterator2,
result: OutputIterator,
binary_op: BinaryOperatorInferrer<
InputIterator1,
InputIterator2,
OutputIterator
>,
): OutputIterator {
while (!first1.equals(last1)) {
result.value = binary_op(first1.value, first2.value);
first1 = first1.next();
first2 = first2.next();
result = result.next();
}
return result;
}
/**
* Generate range elements.
*
* @param first Forward iteartor of the first position.
* @param last Forward iterator of the last position.
* @param gen The generator function.
*/
export function generate<
ForwardIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<ForwardIterator>, ForwardIterator>
>,
>(
first: ForwardIterator,
last: ForwardIterator,
gen: () => IPointer.ValueType<ForwardIterator>,
): void {
for (; !first.equals(last); first = first.next()) first.value = gen();
}
/**
* Generate *n* elements.
*
* @param first Forward iteartor of the first position.
* @param n Number of elements to generate.
* @param gen The generator function.
*
* @return Forward Iterator to the last position by advancing.
*/
export function generate_n<
ForwardIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<ForwardIterator>, ForwardIterator>
>,
>(
first: ForwardIterator,
n: number,
gen: () => IPointer.ValueType<ForwardIterator>,
): ForwardIterator {
while (n-- > 0) {
first.value = gen();
first = first.next();
}
return first;
}
/* ---------------------------------------------------------
REMOVE
--------------------------------------------------------- */
/**
* Test whether elements are unique in sorted range.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param pred A binary function predicates two arguments are equal. Default is {@link equal_to}.
*
* @returns Whether unique or not.
*/
export function is_unique<
InputIterator extends General<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
pred: BinaryPredicator<IPointer.ValueType<InputIterator>> = equal_to,
): boolean {
if (first.equals(last)) return true;
let next: InputIterator = first.next();
for (; !next.equals(last); next = next.next()) {
if (pred(first.value, next.value) === true) return false;
first = first.next();
}
return true;
}
/**
* Remove duplicated elements in sorted range.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param pred A binary function predicates two arguments are equal. Default is {@link equal_to}.
*
* @return Input iterator to the last element not removed.
*/
export function unique<
InputIterator extends General<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
pred: BinaryPredicator<IPointer.ValueType<InputIterator>> = equal_to,
): InputIterator {
if (first.equals(last)) return last;
let ret: InputIterator = first;
for (first = first.next(); !first.equals(last); first = first.next())
if (!pred(ret.value, first.value)) {
ret = ret.next();
ret.value = first.value;
}
return ret.next();
}
/**
* Copy elements in range without duplicates.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param output Output iterator of the last position.
* @param pred A binary function predicates two arguments are equal. Default is {@link equal_to}.
*
* @return Output Iterator of the last position by advancing.
*/
export function unique_copy<
InputIterator extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<InputIterator>, OutputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
output: OutputIterator,
pred: BinaryPredicator<IPointer.ValueType<InputIterator>> = equal_to,
): OutputIterator {
if (first.equals(last)) return output;
output.value = first.value;
first = first.next();
for (; !first.equals(last); first = first.next())
if (!pred(first.value, output.value)) {
output = output.next();
output.value = first.value;
}
return output.next();
}
/**
* Remove specific value in range.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param val The specific value to remove.
*
* @return Iterator tho the last element not removed.
*/
export function remove<
InputIterator extends General<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
val: IPointer.ValueType<InputIterator>,
): InputIterator {
return remove_if(first, last, (elem) => equal_to(elem, val));
}
/**
* Remove elements in range by a condition.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param pred An unary function predicates remove.
*
* @return Iterator tho the last element not removed.
*/
export function remove_if<
InputIterator extends General<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
pred: UnaryPredicator<IPointer.ValueType<InputIterator>>,
): InputIterator {
let ret: InputIterator = first;
while (!first.equals(last)) {
if (!pred(first.value)) {
ret.value = first.value;
ret = ret.next();
}
first = first.next();
}
return ret;
}
/**
* Copy range removing specific value.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param output Output iterator of the last position.
* @param val The condition predicates remove.
*
* @return Output Iterator of the last position by advancing.
*/
export function remove_copy<
InputIterator extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<InputIterator>, OutputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
output: OutputIterator,
val: IPointer.ValueType<InputIterator>,
): OutputIterator {
return remove_copy_if(first, last, output, (elem) => equal_to(elem, val));
}
/**
* Copy range removing elements by a condition.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param output Output iterator of the last position.
* @param pred An unary function predicates remove.
*
* @return Output Iterator of the last position by advancing.
*/
export function remove_copy_if<
InputIterator extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<InputIterator>, OutputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
output: OutputIterator,
pred: UnaryPredicator<IPointer.ValueType<InputIterator>>,
): OutputIterator {
for (; !first.equals(last); first = first.next()) {
if (pred(first.value)) continue;
output.value = first.value;
output = output.next();
}
return output;
}
/* ---------------------------------------------------------
REPLACE & SWAP
--------------------------------------------------------- */
/**
* Replace specific value in range.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param old_val Specific value to change
* @param new_val Specific value to be changed.
*/
export function replace<
InputIterator extends General<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
old_val: IPointer.ValueType<InputIterator>,
new_val: IPointer.ValueType<InputIterator>,
): void {
return replace_if(first, last, (elem) => equal_to(elem, old_val), new_val);
}
/**
* Replace specific condition in range.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param pred An unary function predicates the change.
* @param new_val Specific value to be changed.
*/
export function replace_if<
InputIterator extends General<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
pred: UnaryPredicator<IPointer.ValueType<InputIterator>>,
new_val: IPointer.ValueType<InputIterator>,
): void {
for (let it = first; !it.equals(last); it = it.next())
if (pred(it.value) === true) it.value = new_val;
}
/**
* Copy range replacing specific value.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param output Output iterator of the first position.
* @param old_val Specific value to change
* @param new_val Specific value to be changed.
*
* @return Output Iterator of the last position by advancing.
*/
export function replace_copy<
InputIterator extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<InputIterator>, OutputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
output: OutputIterator,
old_val: IPointer.ValueType<InputIterator>,
new_val: IPointer.ValueType<InputIterator>,
): OutputIterator {
return replace_copy_if(
first,
last,
output,
(elem) => equal_to(elem, old_val),
new_val,
);
}
/**
* Copy range replacing specfic condition.
*
* @param first Input iteartor of the first position.
* @param last Input iterator of the last position.
* @param output Output iterator of the first position.
* @param pred An unary function predicates the change.
* @param new_val Specific value to be changed.
*
* @return Output Iterator of the last position by advancing.
*/
export function replace_copy_if<
InputIterator extends Readonly<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<InputIterator>, OutputIterator>
>,
>(
first: InputIterator,
last: InputIterator,
result: OutputIterator,
pred: UnaryPredicator<IPointer.ValueType<InputIterator>>,
new_val: IPointer.ValueType<InputIterator>,
): OutputIterator {
for (; !first.equals(last); first = first.next()) {
if (pred(first.value)) result.value = new_val;
else result.value = first.value;
result = result.next();
}
return result;
}
/**
* Swap values of two iterators.
*
* @param x Forward iterator to swap its value.
* @param y Forward iterator to swap its value.
*/
export function iter_swap<
ForwardIterator1 extends General<
IForwardIterator<IPointer.ValueType<ForwardIterator1>, ForwardIterator1>
>,
ForwardIterator2 extends General<
IForwardIterator<IPointer.ValueType<ForwardIterator1>, ForwardIterator2>
>,
>(x: ForwardIterator1, y: ForwardIterator2): void {
[x.value, y.value] = [y.value, x.value];
}
/**
* Swap values of two ranges.
*
* @param first1 Forward iteartor of the first position of the 1st range.
* @param last1 Forward iterator of the last position of the 1st range.
* @param first2 Forward iterator of the first position of the 2nd range.
*
* @return Forward Iterator of the last position of the 2nd range by advancing.
*/
export function swap_ranges<
ForwardIterator1 extends General<
IForwardIterator<IPointer.ValueType<ForwardIterator1>, ForwardIterator1>
>,
ForwardIterator2 extends General<
IForwardIterator<IPointer.ValueType<ForwardIterator1>, ForwardIterator2>
>,
>(
first1: ForwardIterator1,
last1: ForwardIterator1,
first2: ForwardIterator2,
): ForwardIterator2 {
for (; !first1.equals(last1); first1 = first1.next()) {
iter_swap(first1, first2);
first2 = first2.next();
}
return first2;
}
/* ---------------------------------------------------------
RE-ARRANGEMENT
--------------------------------------------------------- */
/**
* Reverse elements in range.
*
* @param first Bidirectional iterator of the first position.
* @param last Bidirectional iterator of the last position.
*/
export function reverse<
BidirectionalIterator extends General<
IBidirectionalIterator<
IPointer.ValueType<BidirectionalIterator>,
BidirectionalIterator
>
>,
>(first: BidirectionalIterator, last: BidirectionalIterator): void {
// first !== last && first !== --last
while (
first.equals(last) === false &&
first.equals((last = last.prev())) === false
) {
iter_swap(first, last);
first = first.next();
}
}
/**
* Copy reversed elements in range.
*
* @param first Bidirectional iterator of the first position.
* @param last Bidirectional iterator of the last position.
* @param output Output iterator of the first position.
*
* @return Output Iterator of the last position by advancing.
*/
export function reverse_copy<
BidirectionalIterator extends Readonly<
IBidirectionalIterator<
IPointer.ValueType<BidirectionalIterator>,
BidirectionalIterator
>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<BidirectionalIterator>, OutputIterator>
>,
>(
first: BidirectionalIterator,
last: BidirectionalIterator,
output: OutputIterator,
): OutputIterator {
while (!last.equals(first)) {
last = last.prev();
output.value = last.value;
output = output.next();
}
return output;
}
export function shift_left<
ForwardIterator extends General<
IForwardIterator<IPointer.ValueType<ForwardIterator>, ForwardIterator>
>,
>(first: ForwardIterator, last: ForwardIterator, n: number): ForwardIterator {
const mid = advance(first, n);
return copy(mid, last, first);
}
export function shift_right<
ForwardIterator extends General<
IBidirectionalIterator<IPointer.ValueType<ForwardIterator>, ForwardIterator>
>,
>(first: ForwardIterator, last: ForwardIterator, n: number): ForwardIterator {
const mid = advance(last, -n);
return copy_backward(first, mid, last);
}
/**
* Rotate elements in range.
*
* @param first Input iteartor of the first position.
* @param middle Input iteartor of the initial position of the right side.
* @param last Input iteartor of the last position.
*
* @return Input iterator of the final position in the left side; *middle*.
*/
export function rotate<
InputIterator extends General<
IForwardIterator<IPointer.ValueType<InputIterator>, InputIterator>
>,
>(
first: InputIterator,
middle: InputIterator,
last: InputIterator,
): InputIterator {
while (!first.equals(middle) && !middle.equals(last)) {
iter_swap(first, middle);
first = first.next();
middle = middle.next();
}
return first;
}
/**
* Copy rotated elements in range.
*
* @param first Input iteartor of the first position.
* @param middle Input iteartor of the initial position of the right side.
* @param last Input iteartor of the last position.
* @param output Output iterator of the last position.
*
* @return Output Iterator of the last position by advancing.
*/
export function rotate_copy<
ForwardIterator extends Readonly<
IForwardIterator<IPointer.ValueType<ForwardIterator>, ForwardIterator>
>,
OutputIterator extends Writeonly<
IForwardIterator<IPointer.ValueType<ForwardIterator>, OutputIterator>
>,
>(
first: ForwardIterator,
middle: ForwardIterator,
last: ForwardIterator,
output: OutputIterator,
): OutputIterator {
output = copy(middle, last, output);
return copy(first, middle, output);
}
/**
* Shuffle elements in range.
*
* @param first Random access iteartor of the first position.
* @param last Random access iteartor of the last position.
*/
export function shuffle<
RandomAccessIterator extends General<
IRandomAccessIterator<
IPointer.ValueType<RandomAccessIterator>,
RandomAccessIterator
>
>,
>(first: RandomAccessIterator, last: RandomAccessIterator): void {
for (let it = first; !it.equals(last); it = it.next()) {
const rand_index: number = randint(first.index(), last.index() - 1);
if (it.index() !== rand_index) iter_swap(it, first.advance(rand_index));
}
}