/
tuple.d
844 lines (746 loc) · 21.9 KB
/
tuple.d
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
// Written in the D programming language
/**
This module contains different functions acting on tuples ($(M std.typecons.Tuple), not
type-tuples): converting to and from arrays, inserting fields, rotating fields,
inverting them, mapping/reducing/filtering them.
In many ways, it allows you to act on tuples as if they were polymorphic ranges.
The corresponding typetuples templates can be found in typetuple.d.
License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
Authors: Philippe Sigaud
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
module dranges.tuple;
import std.algorithm,
std.conv,
std.exception,
std.functional,
std.metastrings,
std.range,
std.stdio,
std.traits,
std.typecons,
std.typetuple;
import dranges.functional,
dranges.predicate,
dranges.templates,
dranges.traits,
dranges.typetuple,
dranges.variadic;
/**
Small helper functions to extract a field from a tuple. Mainly to make some expressions more readable.
*/
R[0] first(R...)(Tuple!R tup) if (R.length>0) { return tup.field[0];}
/// ditto
R[1] second(R...)(Tuple!R tup) if (R.length>1) { return tup.field[1];}
/// ditto
R[2] third(R...)(Tuple!R tup) if (R.length>2) { return tup.field[2];}
/**
n==0 -> tuple()
n=1 -> tuple(t)
n>1 -> tuple(t,t,...,t) n times
*/
Tuple!(TypeNuple!(T,n)) nuple(uint n, T)(T t)
{
TypeNuple!(T,n) tn;
foreach(i, Type; tn) tn[i] = t;
return tuple(tn);
}
/**
The first function transforms a tuple into a static array, the second
into a dynamic array. For both functions, the resulting array's element
type is the common type of the tuple's elements (it doesn't compile if there is no common type).
Example:
----
auto t = tuple(1, 2.0, 10000000000);
auto sa = tupleToStaticArray(t);
auto da = tupleToDynamicArray(t);
assert(is(typeof(sa) == double[3]));
assert(is(typeof(da) == double[]));
assert(sa == [1.0,2.0,10000000000.0]);
assert(da == [1.0,2.0,10000000000.0][]);
----
*/
CommonType!R[R.length] tupleToStaticArray(R...)(Tuple!(R) t) if (!is(CommonType!R == void))
{
alias CommonType!R CT;
CT[R.length] result; // 2.036: now we can return static arrays
foreach(i, Unused; R) { result[i] = t.field[i];}
return result;
}
/// ditto
CommonType!R[] tupleToDynamicArray(R...)(Tuple!(R) t) if (!is(CommonType!R == void))
{
alias CommonType!R CT;
CT[] result = new CT[R.length];
foreach(i, Unused; R) { result[i] = t.field[i];}
return result;
}
unittest
{
auto t = tuple(1, 2.0, 10000000000);
auto sa = tupleToStaticArray(t);
auto da = tupleToDynamicArray(t);
assert(is(typeof(sa) == double[3]));
assert(is(typeof(da) == double[]));
assert(sa == [1.0,2.0,10000000000.0]);
assert(da == [1.0,2.0,10000000000.0][]);
}
/**
The first function transforms a static array into a tuple, the second does
the same for a dynamic array. For the dynamic case, you must give it the array's length
at compile-time. There is no length-checking done, so be careful.
Example:
----
int[3] a = [1,2,3];
auto t1 = staticArrayToTuple(a);
assert(is(typeof(t1) == Tuple!(int,int,int)));
assert(t1 == tuple(1,2,3));
int[] b = [1,2,3];
auto t2 = dynamicArrayToTuple!3(b);
assert(is(typeof(t2) == Tuple!(int,int,int)));
assert(t2 == tuple(1,2,3));
----
*/
Tuple!(TypeNuple!(T,n)) staticArrayToTuple(size_t n, T)(T[n] arr) {
Tuple!(TypeNuple!(T,n)) tup;
foreach(i, Unused; TypeNuple!(T,n)) {
tup.field[i] = arr[i];
}
return tup;
}
/// ditto
Tuple!(TypeNuple!(T,n)) dynamicArrayToTuple(size_t n, T)(T[] arr) {
Tuple!(TypeNuple!(T,n)) tup;
foreach(i, Unused; TypeNuple!(T,n)) {
tup.field[i] = arr[i];
}
return tup;
}
unittest
{
int[3] a = [1,2,3];
auto t1 = staticArrayToTuple(a);
assert(is(typeof(t1) == Tuple!(int,int,int)));
assert(t1 == tuple(1,2,3));
int[] b = [1,2,3];
auto t2 = dynamicArrayToTuple!3(b);
assert(is(typeof(t2) == Tuple!(int,int,int)));
assert(t2 == tuple(1,2,3));
}
/**
Same as the previous functions: takes an input range and a length upTo
and converts the first upTo elements of the range into a tuple. The range
must have at least upTo elements.
Example:
----
auto r = [0,1,2,3,4,5];
auto f = filter!isOdd(r);
auto t = rangeToTuple!3(f);
assert(t == tuple(1,3,5));
// With an infinite range.
auto c = cycle(r[0..3]); // 0,1,2, 0,1,2, 0,1,2,...
auto t2 = rangeToTuple!7(c);
assert(t2 == tuple(0,1,2,0,1,2,0));
----
*/
Tuple!(TypeNuple!(ElementType!R,upTo)) rangeToTuple(size_t upTo, R)(R range) if (isInputRange!R)
{
return dynamicArrayToTuple!upTo(array(take(range, upTo)));
}
unittest
{
auto r = [0,1,2,3,4,5];
auto f = filter!isOdd(r);
auto t = rangeToTuple!3(f);
assert(t == tuple(1,3,5));
// With an infinite range.
auto c = cycle(r[0..3]); // 0,1,2, 0,1,2, 0,1,2,...
auto t2 = rangeToTuple!7(c);
assert(t2 == tuple(0,1,2,0,1,2,0));
auto t3 = rangeToTuple!0(r);
assert(t3 == tuple());
}
/**
Small functions to imitate the range 'API' with tuples.
*/
R[0] front(R...)(Tuple!R tup)
{
return tup.field[0];
}
/// ditto
Tuple!(R[1..$]) tail(R...)(Tuple!R tup)
{
return tuple(tup.expand[1..$]);
}
/// ditto
R[$-1] back(R...)(Tuple!R tup)
{
return tup.field[R.length-1];
}
/// ditto
size_t length(R...)(Tuple!R tup)
{
return R.length;
}
/// ditto
bool empty(R...)(Tuple!R tup)
{
return R.length == 0;
}
unittest
{
auto t = tuple('a',1, 3.1415);
assert(front(t) == 'a');
assert(tail(t) == tuple(1, 3.1415));
assert(back(t) == 3.1415);
assert(length(t) == 3);
}
/**
Inserts a new component into a tuple, at position n. Position 0 is before all other
components, position R.length is after all of them. n cannot be greater than R.length.
Example:
----
auto t = tuple(1,'a',3.14);
auto s0 = insertAtTuple!0(t, "qwer");
auto s1 = insertAtTuple!1(t, "qwer");
auto s2 = insertAtTuple!2(t, "qwer");
auto s3 = tupleInsertAt!3(t, "qwer");
assert(s0 == tuple("qwer",1,'a',3.14));
assert(s1 == tuple(1,"qwer",'a',3.14));
assert(s2 == tuple(1,'a',"qwer",3.14));
assert(s3 == tuple(1,'a',3.14, "qwer"));
----
*/
Tuple!(TypeTuple!(R[0..n],T,R[n..$])) insertAtTuple(size_t n, T, R...)(Tuple!R tup, T val) if (n <= R.length) {
alias TypeTuple!(R[0..n],T,R[n..$]) R2;
Tuple!R2 tup2;
foreach(i, Unused; R2[0..n]) tup2.field[i] = tup.field[i];
tup2.field[n] = val;
foreach(i, Unused; R2[n+1..$]) tup2.field[i+n+1] = tup.field[i+n];
return tup2;
}
unittest
{
auto t = tuple(1,'a',3.14);
auto s0 = insertAtTuple!0(t, "qwer");
auto s1 = insertAtTuple!1(t, "qwer");
auto s2 = insertAtTuple!2(t, "qwer");
auto s3 = insertAtTuple!3(t, "qwer");
assert(s0 == tuple("qwer",1,'a',3.14));
assert(s1 == tuple(1,"qwer",'a',3.14));
assert(s2 == tuple(1,'a',"qwer",3.14));
assert(s3 == tuple(1,'a',3.14, "qwer"));
}
/**
Creates a new tuple by extracting the components of tup and reassembling them
according to an indices array. [0,1,2] means 'take the first, second and third component' and so on.
The indices can be in any order ([1,2,0]), can be repeated or omitted([0,2,2,0]) and the array
can be as long as you wish.
See_Also: shred in dranges.range
Examples:
----
auto t = tuple(1, 'a', 3.14);
auto s1 = shredTuple!([0,2])(t);
assert(s1 == tuple(1, 3.14));
auto s2 = shredTuple!([2,0,0,1,1])(t);
assert(s2 == tuple(3.14,1,1,'a','a'));
auto s3 = shredTuple!([0])(t);
assert(s3 == tuple(1));
----
*/
ShredType!(array, R) shredTuple(alias array, R...)(Tuple!R tup) {
alias ExtractType!(array, R) ET;
Tuple!ET tup2;
foreach(i, Unused; ET) tup2.field[i] = tup.field[array[i]];
return tup2;
}
template ShredType(alias array, R...) {
alias Tuple!(ExtractType!(array, R)) ShredType;
}
unittest
{
auto t = tuple(1, 'a', 3.14);
auto s1 = shredTuple!([0,2])(t);
assert(s1 == tuple(1, 3.14));
auto s2 = shredTuple!([2,0,0,1,1])(t);
assert(s2 == tuple(3.14,1,1,'a','a'));
auto s3 = shredTuple!([0])(t);
assert(s3 == tuple(1));
}
/**
Takes a tuple and rotates its fields by n positions. If n>0, it rotates to the left (takes
the first n fields and put them at the end). If n<0, it rotate to the right (takes the last
n fields and put them at the beginning).
Example:
----
auto t = tuple(1, 'a', 3.14);
auto r1 = rotateTuple!1(t);
auto r5 = rotateTuple!5(t);
auto r0 = rotateTuple!0(t);
auto r_1 = rotateTuple!(-1)(t);
auto r_5 = rotateTuple!(-5)(t);
assert(r1 == tuple('a',3.14,1));
assert(r5 == tuple(3.14,1,'a'));
assert(r0 == t);
assert(r_1 == tuple(3.14,1,'a'));
assert(r_5 == tuple('a',3.14,1)); // equivalent to rotateTuple!(-2) and also to rotateTuple!1
assert(rotateTuple!1(tuple(1)) == tuple(1));
----
*/
Tuple!(RotateTypes!(n,R)) rotateTuple(int n, R...)(Tuple!R tup)
{
enum size_t nn = n>=0 ? n%R.length : R.length - (-n)%R.length;
RotateTypes!(n,R) vals;
static if (nn != R.length) vals[0..(R.length-nn)] = tup.expand[nn..$];
static if (nn != 0) vals[(R.length-nn)..$] = tup.expand[0..nn];
return tuple(vals);
}
unittest
{
auto t = tuple(1, 'a', 3.14);
auto r1 = rotateTuple!1(t);
auto r5 = rotateTuple!5(t);
auto r0 = rotateTuple!0(t);
auto r_1 = rotateTuple!(-1)(t);
auto r_5 = rotateTuple!(-5)(t);
assert(r1 == tuple('a',3.14,1));
assert(r5 == tuple(3.14,1,'a'));
assert(r0 == t);
assert(r_1 == tuple(3.14,1,'a'));
assert(r_5 == tuple('a',3.14,1)); // equivalent to rotateTuple!(-2) and also to rotateTuple!1
assert(rotateTuple!1(tuple(1)) == tuple(1));
}
/**
Takes a tuple and reverse its fields.
Example:
----
auto t = tuple(1,'a',3.14);
auto r = reverseTuple(t);
assert(r == tuple(3.14,'a',1));
----
*/
Tuple!(ReverseTypes!R) reverseTuple(R...)(Tuple!R tup)
{
ReverseTypes!R vals;
foreach(i, Unused; R) vals[R.length-1-i] = tup.field[i];
return tuple(vals);
}
unittest
{
auto t = tuple(1,'a',3.14);
auto r = reverseTuple(t);
assert(r == tuple(3.14,'a',1));
assert(reverseTuple(tuple(1)) == tuple(1));
}
/**
Stitches (glues) two tuples together, creating a larger one.
Example:
----
auto t1 = tuple(1, 'a', 3.14);
auto t2 = tuple("abc", true);
auto t3 = stitchTuples(t1,t2);
assert(is(typeof(t3) == Tuple!(int,char,double,string,bool)));
assert(t3 == tuple(1, 'a', 3.14, "abc", true));
----
*/
Tuple!(T1.Types, T2.Types) stitchTuples(T1, T2)(T1 tup1, T2 tup2) if (is(T1.Types) && is(T2.Types))
{
return tuple(tup1.expand, tup2.expand);
}
unittest
{
auto t1 = tuple(1, 'a', 3.14);
auto t2 = tuple("abc", true);
auto t3 = stitchTuples(t1,t2);
assert(is(typeof(t3) == Tuple!(int,char,double,string,bool)));
assert(t3 == tuple(1, 'a', 3.14, "abc", true));
}
/**
Returns: a new tuple with tup's fields and a new field at the end, containing 'element'.
Example:
----
auto t = tuple(1, 'a', 3.14);
auto t2 = append(t, "abc");
assert(is(typeof(t2) == Tuple!(int, char, double, string)));
assert(t2 == tuple(1, 'a', 3.14, "abc"));
----
*/
Tuple!(R, E) append(E, R...)(Tuple!R tup, E element)
{
return tuple(tup.expand, element);
}
unittest
{
auto t = tuple(1, 'a', 3.14);
auto t2 = append(t, "abc");
assert(is(typeof(t2) == Tuple!(int, char, double, string)));
assert(t2 == tuple(1, 'a', 3.14, "abc"));
}
/**
Returns: a new tuple with element as first field and then tup's fields.
Example:
----
auto t = tuple(1, 'a', 3.14);
auto t2 = prepend("abc", t);
assert(is(typeof(t2) == Tuple!(string, int, char, double)));
assert(t2 == tuple("abc", 1, 'a', 3.14));
----
*/
Tuple!(E, R) prepend(E, R...)(E element, Tuple!R tup)
{
return tuple(element, tup.expand);
}
unittest
{
auto t = tuple(1, 'a', 3.14);
auto t2 = prepend("abc", t);
assert(is(typeof(t2) == Tuple!(string, int, char, double)));
assert(t2 == tuple("abc", 1, 'a', 3.14));
}
/**
Swaps the fields at index i1 and index i2 in a tuple.
Example:
----
auto t = tuple(1, 'a', 3.14, "abc");
auto ts1 = swapTuple!(1,3)(t);
assert(ts1 == tuple(1, "abc", 3.14, 'a'));
auto ts2 = swapTuple!(3,1)(t);
assert(ts2 == tuple(1, "abc", 3.14, 'a'));
auto ts3 = swapTuple!(1,1)(t);
assert(ts3 == t);
----
*/
Tuple!(SwapTypes!(i1,i2,R)) swapTuple(size_t i1, size_t i2, R...)(Tuple!R tup)
{
Tuple!(SwapTypes!(i1,i2,R)) tup2;
foreach(i, Unused; R) {
static if (i == i1) {
tup2.field[i] = tup.field[i2];
}
else {
static if (i == i2) {
tup2.field[i] = tup.field[i1];
}
else {
tup2.field[i] = tup.field[i];
}
}
}
return tup2;
}
unittest
{
auto t = tuple(1, 'a', 3.14, "abc");
auto ts1 = swapTuple!(1,3)(t);
assert(ts1 == tuple(1, "abc", 3.14, 'a'));
auto ts2 = swapTuple!(3,1)(t);
assert(ts2 == tuple(1, "abc", 3.14, 'a'));
auto ts3 = swapTuple!(1,1)(t);
assert(ts3 == t);
}
template SumOfLength(alias zero, T)
{
enum size_t SumOfLength = zero + (FlattenTuple!T).length;
}
template SumOfLengths(T...)
{
alias StaticScan!(SumOfLength, 0, T) SumOfLengths;
}
/**
Flatten a tuple: any tuples inside the main tuple are opened and their content inserted, recursively.
Example:
----
auto t = tuple(1, tuple("a",3.14), tuple(tuple(0), 100));
auto f = flattenTuple(t);
assert(f == tuple(1, "a", 3.14, 0, 100));
----
*/
Tuple!(FlattenTuple!T) flattenTuple(T...)(T tup)
{
alias SumOfLengths!T lengths;
FlattenTuple!T flat;
foreach(i, Type; T)
static if (isTuple!(T[i]) && (T[i].Types.length > 0)) // Only if someone tries to flatten Tuple!() (aka Unit)
flat[lengths[i]..lengths[i+1]] = flattenTuple(tup[i].expand).expand;
else
flat[lengths[i]] = tup[i];
return tuple(flat);
}
unittest
{
auto t0 = tuple(0);
auto t1 = tuple("a", 3.14);
auto t2 = tuple(t0, 100);
auto t3 = tuple(1, t1, t2);
auto f0 = flattenTuple(t0);
assert(f0 == t0);
auto f1 = flattenTuple(t1);
assert(f1 == t1);
auto f2 = flattenTuple(t2);
assert(f2 == tuple(0, 100));
auto f3 = flattenTuple(t3);
assert(f3 == tuple(1, "a", 3.14, 0, 100));
}
template NoEmptyTuple(alias zero, T)
{
static if (isTuple!T && T.Types.length == 0)
enum NoEmptyTuple = zero;
else
enum NoEmptyTuple = zero + 1;
}
template NoEmptyList(T...)
{
alias StaticScan!(NoEmptyTuple, 0, T) NoEmptyList;
}
///
typeof(fun(ifLeaf(T[0].init))) tupleReduce(alias fun, alias ifLeaf, T...)(T tup) if (T.length)
{
// alias SumOfLengths!T lengths;
alias NoEmptyList!T noempty;
TypeNuple!(typeof(fun(ifLeaf(T[0].init))), noempty[$-1]) temp;
foreach(i, Type; T)
static if (__traits(compiles, T[i].Types)) {
static if (T[i].Types.length)
temp[noempty[i]] = tupleReduce!(fun,ifLeaf)(tup[i].expand);}
else
temp[noempty[i]] = ifLeaf(tup[i]); // = tup[i] ?
return fun(temp);
}
///
typeof(fun(CommonType!(Flatten!T).init)) tupleReduce0(alias fun, alias ifLeaf, T...)(T tup)
{
TypeNuple!(typeof(fun(ifLeaf(T[0].init))), T.length) temp;
foreach(i, Type; T)
static if (__traits(compiles, T[i].Types))
temp[i] = tupleReduce0!(fun,ifLeaf)(tup[i].expand);
else
temp[i] = ifLeaf(tup[i]);
return fun(temp);
}
private template Firsts(T...) if (allSatisfy!(isTuple, T))
{
alias Tuple!(StaticMap!(T1!"a.Types[0]", T)) Firsts;
}
private template NthTypes(size_t n, T...)if (allSatisfy!(isTuple, T))
{
alias StaticMap!(T1!("a.Types[" ~to!string(n) ~ "]"), T) NthTypes;
}
private template Rests(T...) if (allSatisfy!(isTuple, T))
{
alias Tuple!(StaticMap!(T1!"Tuple!(a.Types[1..$])", T)) Rests;
}
private template RetTypeTuples(alias fun, T...) if (allSatisfy!(isTuple, T))
{
static if(T[0].Types.length)
alias TypeTuple!(typeof(fun(Firsts!T.init.expand)), RetTypeTuples!(fun, (Rests!T).Types)) RetTypeTuples;
else
alias TypeTuple!() RetTypeTuples;
}
template TupleLength(T) if (isTuple!T)
{
enum size_t TupleLength = T.Types.length;
}
/// Maps on a tuple, using a polymorphic function. Produces another tuple.
Tuple!(StaticMap!(RT!fun, T)) mapTuple(alias fun, T...)(Tuple!T tup)
{
StaticMap!(RT!fun, T) res;
foreach(i, Type; T) res[i] = unaryFun!fun(tup.field[i]);
return tuple(res);
}
/// Maps n tuples in parallel, using a polymorphing n-args function.
Tuple!(RetTypeTuples!(fun, T))
mapTuples(alias fun, T...)(T tuples)
if (allSatisfy!(isTuple, T) && allEqual!(StaticMap!(TupleLength, T)))
{
alias RetTypeTuples!(fun, T) RTT;
RTT result;
foreach(i, Type; RTT)
{
alias NthTypes!(i, T) NF;
NF nf;
foreach(j, Type2; T)
{
nf[j] = tuples[j].field[i];
}
result[i] = naryFun!fun(nf);
}
return tuple(result);
}
/// Folds a tuple using a polymorphic function.
StaticScan!(RT2!fun, T)[$-1] reduceTuple(alias fun, T...)(Tuple!T ts)
{
alias StaticScan!(RT2!fun, T) RTS;
RTS result;
foreach(i, Type; RTS) {
static if (i == 0)
result[i] = ts.field[0];
else
result[i] = fun(result[i-1], ts.field[i]);
}
return result[$-1];
}
/// Scan on a tuple. See $(M dranges.algorithm.scan).
Tuple!(StaticScan!(RT2!fun, T)) scanTuple(alias fun, T...)(Tuple!T ts)
{
alias StaticScan!(RT2!fun, T) RTS;
RTS result;
foreach(i, Type; RTS) {
static if (i == 0)
result[i] = ts.field[0];
else
result[i] = binaryFun!fun(result[i-1], ts.field[i]);
}
return tuple(result);
}
/// Returns a tuple whith only 1 element in n.
Tuple!(StaticStride!(n,T)) strideTuple(size_t n, T...)(Tuple!T tup) if (n > 0)
{
return tuple(strideVariadic!n(tup.expand));
}
/// Returns a tuple which has for elements the successive application (times times) of fun on the seed value.
Tuple!(StaticIterate!(times, RT!fun, S)) iterateTuple(size_t times, alias fun, S)(S seed)
{
alias StaticIterate!(times, RT!fun, S) SuccessiveTypes;
SuccessiveTypes st;
foreach(i, Type; SuccessiveTypes) {
static if (i == 0)
st[i] = t;
else
st[i] = unaryFun!fun(st[i-1]);
}
return tuple(st);
}
template RTS2(alias fun)
{
template RTS2(T...)
{
alias typeof(fun((Tuple!T).init.expand)).Types RTS2;
}
}
template RTSState(alias fun)
{
template RTSState(T...)
{
alias TypeTuple!(Tuple!(typeof(fun((Tuple!T).init.expand)).Types[1..$]), typeof(fun((Tuple!T).init.expand)).Types[1..$]) RTSState;
}
}
///
Tuple!(StaticUnfold!(times, RTS2!fun, State)) unfoldTuple(size_t times, alias fun, State...)(State state)
{
alias StaticUnfold!(times, RTS2!fun, State) SuccessiveTypes;
alias StaticUnfold!(times, RTSState!fun, State) SuccessiveStates;
SuccessiveTypes st;
SuccessiveStates ss;
foreach(i, Type; SuccessiveTypes) {
static if (i == 0)
{
st[i] = fun(state).field[0];
ss[i] = tuple(fun(state).expand[1..$]);
}
else
{
st[i] = fun(ss[i-1].expand).field[0];
static if (i < SuccessiveStates.length-1) ss[i] = tuple(fun(ss[i-1].expand).expand[1..$]);
}
}
return tuple(st);
}
template FilterTupleTypes(alias pred, alias tup)
{
static if (tup.field.length)
{
static if (pred(tup.field[0]))
alias TypeTuple!(tup.Types[0], FilterTupleTypes!(pred, tuple(tup.expand[1..$]))) FilterTupleTypes;
else
alias FilterTupleTypes!(pred, tuple(tup.expand[1..$])) FilterTupleTypes;
}
else
{
alias TypeTuple!() FilterTupleTypes;
}
}
template FilterTupleIndices(alias pred, alias tup, size_t ind)
{
static if (tup.field.length)
{
static if (pred(tup.field[0]))
alias TypeTuple!(ind, FilterTupleIndices!(pred, tuple(tup.expand[1..$]), ind+1)) FilterTupleIndices;
else
alias /+TypeTuple!(+/FilterTupleIndices!(pred, tuple(tup.expand[1..$]), ind+1)/+)+/ FilterTupleIndices;
}
else
{
alias TypeTuple!() FilterTupleIndices;
}
}
/// Filter a tuple on its values.
Tuple!(FilterTupleTypes!(pred, tup)) filterTuple(alias pred, alias tup)()
{
FilterTupleTypes!(pred, tup) result;
alias FilterTupleIndices!(pred, tup, 0) indices;
foreach(i, ind; indices)
{
result[i] = tup.field[ind];
}
return tuple(result);
}
template TakeWhileTupleTypes(alias pred, alias tup)
{
static if (tup.field.length && pred(tup.field[0]))
alias TypeTuple!(tup.Types[0], TakeWhileTupleTypes!(pred, tuple(tup.expand[1..$]))) TakeWhileTupleTypes;
else
alias TypeTuple!() TakeWhileTupleTypes;
}
///
Tuple!(TakeWhileTupleTypes!(pred, tup)) takeWhileTuple(alias pred, alias tup)()
{
TakeWhileTupleTypes!(pred, tup) result;
foreach(i, Type; result) {result[i] = tup.field[i];}
return tuple(result);
}
template DropWhileTupleIndex(alias pred, alias tup, size_t ind)
{
static if (tup.field.length && pred(tup.field[0]))
enum size_t DropWhileTupleIndex = DropWhileTupleIndex!(pred, tuple(tup.expand[1..$]), ind+1);
else
enum size_t DropWhileTupleIndex = ind;
}
///
Tuple!(tup.Types[DropWhileTupleIndex!(pred, tup,0)..$]) dropWhileTuple(alias pred, alias tup)()
{
enum size_t n = DropWhileTupleIndex!(pred, tup,0);
tup.Types[n..$] result;
foreach(i, Type; result) {result[i] = tup.field[i+n];}
return tuple(result);
}
///
Tuple!(tup.Types[DropWhileTupleIndex!(pred, tup,0)..$], tup.Types[0..DropWhileTupleIndex!(pred,tup, 0)])
rotateWhileTuple(alias pred, alias tup)()
{
enum size_t n = DropWhileTupleIndex!(pred, tup,0);
TypeTuple!(tup.Types[n..$], tup.Types[0..n]) result;
foreach(i, Type; result)
{
static if (i < result.length-n)
result[i] = tup.field[i+n];
else
result[i] = tup.field[i+n-result.length];
}
return tuple(result);
}
///
bool contains(U, T...)(Tuple!T tup, U elem)
{
static if (staticIndexOf!(U,T) == -1)
return false;
else
{
foreach(i, Type; tup.Types)
{
static if (is(Type == U))
if (tup.field[i] == elem) return true;
}
return false;
}
}