| 1 |
// std.array |
| 2 |
@property bool empty(T)(in T[] a) { return !a.length; } |
| 3 |
@property ref T front(T)(T[] a) { return a[0]; } |
| 4 |
void popFront(T)(ref T[] a) { a = a[1 .. $]; } |
| 5 |
|
| 6 |
// std.typecons |
| 7 |
struct Tuple(T...) |
| 8 |
{ |
| 9 |
T field; |
| 10 |
alias field this; |
| 11 |
} |
| 12 |
Tuple!T tuple(T...)(T args) { return typeof(return)(args); } |
| 13 |
|
| 14 |
// std.range |
| 15 |
template ElementType(R) |
| 16 |
{ |
| 17 |
static if (is(typeof(R.init.front.init) T)) |
| 18 |
alias T ElementType; |
| 19 |
else |
| 20 |
alias void ElementType; |
| 21 |
} |
| 22 |
|
| 23 |
struct Repeat(T) |
| 24 |
{ |
| 25 |
private T _value; |
| 26 |
|
| 27 |
enum bool empty = false; |
| 28 |
@property inout(T) front() inout { return _value; } |
| 29 |
void popFront() {} |
| 30 |
} |
| 31 |
Repeat!T repeat(T)(T value) { return Repeat!T(value); } |
| 32 |
|
| 33 |
struct Zip(R...) |
| 34 |
{ |
| 35 |
//alias Tuple!(staticMap!(.ElementType, R)) ElementType; |
| 36 |
static if (R.length == 3) |
| 37 |
alias Tuple!(int, int, int) ElementType; |
| 38 |
static if (R.length == 2) |
| 39 |
alias Tuple!(int, int) ElementType; |
| 40 |
|
| 41 |
R ranges; |
| 42 |
|
| 43 |
this(R rs) |
| 44 |
{ |
| 45 |
foreach (i, Unused; R) |
| 46 |
{ |
| 47 |
ranges[i] = rs[i]; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
@property bool empty() |
| 52 |
{ |
| 53 |
foreach (i, Unused; R) |
| 54 |
{ |
| 55 |
if (ranges[i].empty) |
| 56 |
return true; |
| 57 |
} |
| 58 |
return false; |
| 59 |
} |
| 60 |
@property ElementType front() |
| 61 |
{ |
| 62 |
ElementType result; |
| 63 |
return result; |
| 64 |
} |
| 65 |
void popFront() |
| 66 |
{ |
| 67 |
foreach (i, Unused; R) |
| 68 |
{ |
| 69 |
ranges[i].popFront(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
ElementType opIndex(size_t n) |
| 74 |
{ |
| 75 |
ElementType result; |
| 76 |
return result; |
| 77 |
} |
| 78 |
} |
| 79 |
auto zip(Rs...)(Rs ranges) { return Zip!Rs(ranges); } |
| 80 |
|
| 81 |
// std.algorithm |
| 82 |
template map(fun...) |
| 83 |
{ |
| 84 |
auto map(Range)(Range r) |
| 85 |
{ |
| 86 |
return MapResult!(fun, Range)(r); |
| 87 |
} |
| 88 |
} |
| 89 |
private struct MapResult(alias fun, R) |
| 90 |
{ |
| 91 |
R _input; |
| 92 |
|
| 93 |
this(R input) |
| 94 |
{ |
| 95 |
_input = input; |
| 96 |
} |
| 97 |
|
| 98 |
@property bool empty() { return _input.empty; } |
| 99 |
@property auto ref front() { return fun(_input.front); } |
| 100 |
void popFront() { _input.popFront(); } |
| 101 |
} |
| 102 |
|
| 103 |
auto cartesianProduct(R1, R2)(R1 range1, R2 range2) |
| 104 |
{ |
| 105 |
return range2.map!((ElementType!R2 a) => zip(range1, repeat(a))); |
| 106 |
} |
| 107 |
auto cartesianProduct(R1, R2, RR...)(R1 range1, R2 range2, RR otherRanges) |
| 108 |
{ |
| 109 |
return map!(a => tuple(a[0], a[1][0], a[1][1]))( |
| 110 |
cartesianProduct(range1, cartesianProduct(range2, otherRanges)) |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
// test |
| 115 |
void main() |
| 116 |
{ |
| 117 |
foreach (i, j, k; cartesianProduct([1], [1], [1])) {} |
| 118 |
} |