| 1 |
alias T = MyStruct!float; |
| 2 |
|
| 3 |
struct MyStruct(U) |
| 4 |
{ |
| 5 |
U x; |
| 6 |
U y; |
| 7 |
|
| 8 |
this(U xx, U yy) |
| 9 |
{ |
| 10 |
x = xx; |
| 11 |
y = yy; |
| 12 |
} |
| 13 |
|
| 14 |
MyStruct!U opBinary(string op)(MyStruct!U z) const |
| 15 |
{ |
| 16 |
alias C = typeof(return); |
| 17 |
auto w = C(this.x, this.y); |
| 18 |
return w.opOpAssign!(op)(z); |
| 19 |
} |
| 20 |
|
| 21 |
MyStruct!U opBinaryRight(string op)(MyStruct!U z) const |
| 22 |
{ |
| 23 |
return opBinary!(op)(z); |
| 24 |
} |
| 25 |
|
| 26 |
ref MyStruct opOpAssign(string op, U)(const MyStruct!U z) |
| 27 |
{ |
| 28 |
mixin ("x "~op~"= z.x;"); |
| 29 |
mixin ("y "~op~"= z.y;"); |
| 30 |
return this; |
| 31 |
} |
| 32 |
|
| 33 |
MyStruct!U opBinary(string op, R)(R z) const |
| 34 |
if (is(R == int) || is(R == float)) |
| 35 |
{ |
| 36 |
alias C = typeof(return); |
| 37 |
auto w = C(this.x, this.y); |
| 38 |
return w.opOpAssign!(op)(z); |
| 39 |
} |
| 40 |
|
| 41 |
MyStruct!U opBinaryRight(string op, R)(R z) const |
| 42 |
if (is(R == int) || is(R == float)) |
| 43 |
{ |
| 44 |
return opBinary!(op)(z); |
| 45 |
} |
| 46 |
|
| 47 |
ref MyStruct opOpAssign(string op, R)(const R z) |
| 48 |
if (is(R == int) || is(R == float)) |
| 49 |
{ |
| 50 |
mixin ("x "~op~"= z;"); |
| 51 |
return this; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
void main() |
| 56 |
{ |
| 57 |
T c = MyStruct!float(1.0f, 1.0f); |
| 58 |
T[] arr = [T(1,1), T(2,2), T(3,3), T(4,4), T(5,5), T(6,6)]; |
| 59 |
T[] result = new T[arr.length]; |
| 60 |
|
| 61 |
// part 2 |
| 62 |
|
| 63 |
result[0] = c * c; |
| 64 |
assert(result[0] == T(1, 1)); |
| 65 |
|
| 66 |
result[0] = arr[1] * arr[2]; |
| 67 |
assert(result[0] == T(6, 6)); |
| 68 |
|
| 69 |
int[] intarr = [6, 5, 4, 3, 2, 1]; |
| 70 |
|
| 71 |
result[] = arr[] * arr[]; |
| 72 |
assert(result[] == [T(1, 1), T(4, 4), T(9, 9), T(16, 16), T(25, 25), T(36, 36)]); |
| 73 |
|
| 74 |
result[] = arr[] * 3; |
| 75 |
assert(result[] == [T(3, 1), T(6, 2), T(9, 3), T(12, 4), T(15, 5), T(18, 6)]); |
| 76 |
result[] = 3 * arr[]; |
| 77 |
assert(result[] == [T(3, 1), T(6, 2), T(9, 3), T(12, 4), T(15, 5), T(18, 6)]); |
| 78 |
|
| 79 |
result[] = arr[]; |
| 80 |
result[1..3] = arr[1..3] * 2.0f; |
| 81 |
assert(result[] == [T(1, 1), T(4, 2), T(6, 3), T(4, 4), T(5, 5), T(6, 6)]); |
| 82 |
result[1..3] = 2.0f * arr[1..3]; |
| 83 |
assert(result[] == [T(1, 1), T(4, 2), T(6, 3), T(4, 4), T(5, 5), T(6, 6)]); |
| 84 |
|
| 85 |
result[] = arr[]; |
| 86 |
result[1..$] = arr[1..$] * 2.0f; |
| 87 |
assert(result[] == [T(1, 1), T(4, 2), T(6, 3), T(8, 4), T(10, 5), T(12, 6)]); |
| 88 |
result[1..$] = 2.0f * arr[1..$]; |
| 89 |
assert(result[] == [T(1, 1), T(4, 2), T(6, 3), T(8, 4), T(10, 5), T(12, 6)]); |
| 90 |
|
| 91 |
result[] = intarr[] * arr[]; |
| 92 |
assert(result[] == [T(6, 1), T(10, 2), T(12, 3), T(12, 4), T(10, 5), T(6, 6)]); |
| 93 |
result[] = arr[] * intarr[]; |
| 94 |
assert(result[] == [T(6, 1), T(10, 2), T(12, 3), T(12, 4), T(10, 5), T(6, 6)]); |
| 95 |
|
| 96 |
result[] = intarr[] * T(2,3); |
| 97 |
assert(result[] == [T(12, 3), T(10, 3), T(8, 3), T(6, 3), T(4, 3), T(2, 3)]); |
| 98 |
result[] = T(2,3) * intarr[]; |
| 99 |
assert(result[] == [T(12, 3), T(10, 3), T(8, 3), T(6, 3), T(4, 3), T(2, 3)]); |
| 100 |
|
| 101 |
result[] = intarr[] * c; |
| 102 |
assert(result[] == [T(6, 1), T(5, 1), T(4, 1), T(3, 1), T(2, 1), T(1, 1)]); |
| 103 |
result[] = c * intarr[]; |
| 104 |
assert(result[] == [T(6, 1), T(5, 1), T(4, 1), T(3, 1), T(2, 1), T(1, 1)]); |
| 105 |
|
| 106 |
result[] = arr[]; |
| 107 |
result[1..3] = intarr[1..3] * c; |
| 108 |
assert(result[] == [T(1, 1), T(5, 1), T(4, 1), T(4, 4), T(5, 5), T(6, 6)]); |
| 109 |
result[1..3] = c * intarr[1..3]; |
| 110 |
assert(result[] == [T(1, 1), T(5, 1), T(4, 1), T(4, 4), T(5, 5), T(6, 6)]); |
| 111 |
|
| 112 |
result[1..$] = intarr[1..$] * c; |
| 113 |
assert(result[] == [T(1, 1), T(5, 1), T(4, 1), T(3, 1), T(2, 1), T(1, 1)]); |
| 114 |
result[1..$] = c * intarr[1..$]; |
| 115 |
assert(result[] == [T(1, 1), T(5, 1), T(4, 1), T(3, 1), T(2, 1), T(1, 1)]); |
| 116 |
|
| 117 |
result[] = arr[]; |
| 118 |
result[1..3] = intarr[1..3] * arr[1..3]; |
| 119 |
assert(result[] == [T(1, 1), T(10, 2), T(12, 3), T(4, 4), T(5, 5), T(6, 6)]); |
| 120 |
result[1..3] = arr[1..3] * intarr[1..3]; |
| 121 |
assert(result[] == [T(1, 1), T(10, 2), T(12, 3), T(4, 4), T(5, 5), T(6, 6)]); |
| 122 |
|
| 123 |
result[] = [1,2,3,4,5,6] * c; |
| 124 |
assert(result[] == [T(1, 1), T(2, 1), T(3, 1), T(4, 1), T(5, 1), T(6, 1)]); |
| 125 |
result[] = c * [1,2,3,4,5,6]; |
| 126 |
assert(result[] == [T(1, 1), T(2, 1), T(3, 1), T(4, 1), T(5, 1), T(6, 1)]); |
| 127 |
|
| 128 |
result[] = arr[] * [1,2,3,4,5,6]; |
| 129 |
assert(result[] == [T(1, 1), T(4, 2), T(9, 3), T(16, 4), T(25, 5), T(36, 6)]); |
| 130 |
result[] = [1,2,3,4,5,6] * arr[]; |
| 131 |
assert(result[] == [T(1, 1), T(4, 2), T(9, 3), T(16, 4), T(25, 5), T(36, 6)]); |
| 132 |
|
| 133 |
result[] = [c, 2 * c, 3 * c, 4 * c, 5 * c, 6 * c] * [c, 2 * c, 3 * c, 4 * c, 5 * c, 6 * c]; |
| 134 |
assert(result[] == [T(1, 1), T(4, 1), T(9, 1), T(16, 1), T(25, 1), T(36, 1)]); |
| 135 |
|
| 136 |
result[] = [c, 2 * c, 3 * c, 4 * c, 5 * c, 6 * c] * [1,2,3,4,5,6]; |
| 137 |
assert(result[] == [T(1, 1), T(4, 1), T(9, 1), T(16, 1), T(25, 1), T(36, 1)]); |
| 138 |
result[] = [1,2,3,4,5,6] * [c, 2 * c, 3 * c, 4 * c, 5 * c, 6 * c]; |
| 139 |
assert(result[] == [T(1, 1), T(4, 1), T(9, 1), T(16, 1), T(25, 1), T(36, 1)]); |
| 140 |
|
| 141 |
result[] = arr[] * c; |
| 142 |
assert(result[] == [T(1, 1), T(2, 2), T(3, 3), T(4, 4), T(5, 5), T(6, 6)]); |
| 143 |
result[] = c * arr[]; |
| 144 |
assert(result[] == [T(1, 1), T(2, 2), T(3, 3), T(4, 4), T(5, 5), T(6, 6)]); |
| 145 |
|
| 146 |
result[] = c * 3.0f * arr[]; |
| 147 |
assert(result[] == [T(3, 1), T(6, 2), T(9, 3), T(12, 4), T(15, 5), T(18, 6)]); |
| 148 |
result[] = 3.0f * c * arr[]; |
| 149 |
assert(result[] == [T(3, 1), T(6, 2), T(9, 3), T(12, 4), T(15, 5), T(18, 6)]); |
| 150 |
|
| 151 |
result[] = arr[] * 3.0f * c; |
| 152 |
assert(result[] == [T(3, 1), T(6, 2), T(9, 3), T(12, 4), T(15, 5), T(18, 6)]); |
| 153 |
// result[] = arr[] * c * 3.0f; //not ok |
| 154 |
// assert(result[] == [T(3, 1), T(6, 2), T(9, 3), T(12, 4), T(15, 5), T(18, 6)]); |
| 155 |
|
| 156 |
result[] = 3.0f * arr[] * c; |
| 157 |
assert(result[] == [T(3, 1), T(6, 2), T(9, 3), T(12, 4), T(15, 5), T(18, 6)]); |
| 158 |
// result[] = c * arr[] * 3.0f; //not ok |
| 159 |
// assert(result[] == [T(3, 1), T(6, 2), T(9, 3), T(12, 4), T(15, 5), T(18, 6)]); |
| 160 |
|
| 161 |
result[] = c * arr[] * c; |
| 162 |
assert(result[] == [T(1, 1), T(2, 2), T(3, 3), T(4, 4), T(5, 5), T(6, 6)]); |
| 163 |
} |