diff options
Diffstat (limited to 'gcc/testsuite/gdc.test/runnable/template2.d')
-rw-r--r-- | gcc/testsuite/gdc.test/runnable/template2.d | 40 |
1 files changed, 18 insertions, 22 deletions
diff --git a/gcc/testsuite/gdc.test/runnable/template2.d b/gcc/testsuite/gdc.test/runnable/template2.d index 3adaeae61ae..fe08a6c0738 100644 --- a/gcc/testsuite/gdc.test/runnable/template2.d +++ b/gcc/testsuite/gdc.test/runnable/template2.d @@ -1,17 +1,16 @@ -// RUNNABLE_PHOBOS_TEST // original post to the D newsgroup: // http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=D&artnum=10554&header // Test to manipulate 3D vectors, in D! // by Sean L Palmer (seanpalmer@directvinternet.com) // This code is released without any warranty. Use at your own risk. import core.stdc.stdio; -import std.math : sqrt; +import core.math : sqrt; template VecTemplate(tfloat, int dim:3) { struct Vector { - tfloat d[dim]; + tfloat[dim] d; version(none) { @@ -21,24 +20,24 @@ template VecTemplate(tfloat, int dim:3) bool opEquals(Vector b) { for (int i=0; i<dim; ++i) if (d[i] != b.d[i]) return false; return true; } // negate (-a) - Vector opNeg() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = -d[i]; return + Vector opUnary(string op : "-")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = -d[i]; return r; } // complement (~a) - Vector opCom() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[(i+1)%dim]; + Vector opUnary(string op : "~")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[(i+1)%dim]; d[0] = -d[0]; return r; } // add (a + b) - Vector opAdd(Vector b) { Vector r; r.d[] = d[] + b.d[]; return r; } + Vector opBinary(string op : "+")(Vector b) { Vector r; r.d[] = d[] + b.d[]; return r; } // addto (a += b) - Vector opAddAssign(Vector b) { d[] += b.d[]; return r; } + Vector opOpAssign(string op : "+")(Vector b) { d[] += b.d[]; return r; } // subtract (a - b) - Vector opSub(Vector b) { Vector r; r.d[] = d[] - b.d[]; return r; } + Vector opBinary(string op : "-")(Vector b) { Vector r; r.d[] = d[] - b.d[]; return r; } // multiply by scalar (a * 2.0) - Vector opMul(tfloat b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] + Vector opBinary(string op : "*")(tfloat b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] * b; return r; } // divide by scalar (a / b) - Vector opDiv(tfloat b) { return *this * (1/b); } + Vector opBinary(string op : "/")(tfloat b) { return *this * (1/b); } // dot product (a * b) - tfloat opMul(Vector b) { tfloat r=0; for (int i=0; i<dim; ++i) r += d[i]; + tfloat opBinary(string op : "*")(Vector b) { tfloat r=0; for (int i=0; i<dim; ++i) r += d[i]; return r; } // outer product (a ^ b) //Vector opXor(Vector b) { Vector r; for (int i=0; i<dim; ++i) r += d[i]; return r; } @@ -49,27 +48,27 @@ return r; } const bool opEquals(ref const Vector b) { for (int i=0; i<dim; ++i) if (d[i] != b.d[i]) return false; return true; } // negate (-a) - Vector opNeg() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = -d[i]; return + Vector opUnary(string op : "-")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = -d[i]; return r; } // complement (~a) - Vector opCom() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[(i+1)%dim]; + Vector opUnary(string op : "~")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[(i+1)%dim]; d[0] = -d[0]; return r; } // add (a + b) - Vector opAdd(Vector b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] + + Vector opBinary(string op : "+")(Vector b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] + b.d[i]; return r; } // addto (a += b) - Vector opAddAssign(Vector b) { for (int i=0; i<dim; ++i) d[i] += b.d[i]; return + Vector opOpAssign(string op : "+")(Vector b) { for (int i=0; i<dim; ++i) d[i] += b.d[i]; return this; } // subtract (a - b) - Vector opSub(Vector b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] - + Vector opBinary(string op : "-")(Vector b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] - b.d[i]; return r; } // multiply by scalar (a * 2.0) - Vector opMul(tfloat b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] * + Vector opBinary(string op : "*")(tfloat b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] * b; return r; } // divide by scalar (a / b) - Vector opDiv(tfloat b) { return this * (1/b); } + Vector opBinary(string op : "/")(tfloat b) { return this * (1/b); } // dot product (a * b) - tfloat opMul(Vector b) { tfloat r=0; for (int i=0; i<dim; ++i) r += d[i]; + tfloat opBinary(string op : "*")(Vector b) { tfloat r=0; for (int i=0; i<dim; ++i) r += d[i]; return r; } // outer product (a ^ b) //Vector opXor(Vector b) { Vector r; for (int i=0; i<dim; ++i) r += d[i]; return r; } @@ -110,6 +109,3 @@ int main(char[][] args) printf("closing\n"); return 0; } - - - |