]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/gdc.test/runnable/template2.d
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / runnable / template2.d
CommitLineData
b4c522fa
IB
1// original post to the D newsgroup:
2// http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=D&artnum=10554&header
3// Test to manipulate 3D vectors, in D!
4// by Sean L Palmer (seanpalmer@directvinternet.com)
5// This code is released without any warranty. Use at your own risk.
6import core.stdc.stdio;
5fee5ec3 7import core.math : sqrt;
b4c522fa
IB
8
9template VecTemplate(tfloat, int dim:3)
10{
11 struct Vector
12 {
5fee5ec3 13 tfloat[dim] d;
b4c522fa
IB
14
15 version(none)
16 {
17 // sets the vector to the value of the given array
18 void set(tfloat[dim] r) { d[] = r[]; }
19 // comparison (a == b, a != b)
20 bool opEquals(Vector b) { for (int i=0; i<dim; ++i) if (d[i] != b.d[i]) return
21false; return true; }
22 // negate (-a)
5fee5ec3 23 Vector opUnary(string op : "-")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = -d[i]; return
b4c522fa
IB
24r; }
25 // complement (~a)
5fee5ec3 26 Vector opUnary(string op : "~")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[(i+1)%dim];
b4c522fa
IB
27d[0] = -d[0]; return r; }
28 // add (a + b)
5fee5ec3 29 Vector opBinary(string op : "+")(Vector b) { Vector r; r.d[] = d[] + b.d[]; return r; }
b4c522fa 30 // addto (a += b)
5fee5ec3 31 Vector opOpAssign(string op : "+")(Vector b) { d[] += b.d[]; return r; }
b4c522fa 32 // subtract (a - b)
5fee5ec3 33 Vector opBinary(string op : "-")(Vector b) { Vector r; r.d[] = d[] - b.d[]; return r; }
b4c522fa 34 // multiply by scalar (a * 2.0)
5fee5ec3 35 Vector opBinary(string op : "*")(tfloat b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i]
b4c522fa
IB
36* b; return r; }
37 // divide by scalar (a / b)
5fee5ec3 38 Vector opBinary(string op : "/")(tfloat b) { return *this * (1/b); }
b4c522fa 39 // dot product (a * b)
5fee5ec3 40 tfloat opBinary(string op : "*")(Vector b) { tfloat r=0; for (int i=0; i<dim; ++i) r += d[i];
b4c522fa
IB
41return r; }
42 // outer product (a ^ b)
43 //Vector opXor(Vector b) { Vector r; for (int i=0; i<dim; ++i) r += d[i]; return r; }
44 }
45
46 void set(tfloat[dim] r) { for (int i=0; i<dim; ++i) d[i] = r[i]; }
47 // comparison (a == b, a != b)
48 const bool opEquals(ref const Vector b) { for (int i=0; i<dim; ++i) if (d[i] != b.d[i]) return
49false; return true; }
50 // negate (-a)
5fee5ec3 51 Vector opUnary(string op : "-")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = -d[i]; return
b4c522fa
IB
52r; }
53 // complement (~a)
5fee5ec3 54 Vector opUnary(string op : "~")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[(i+1)%dim];
b4c522fa
IB
55d[0] = -d[0]; return r; }
56 // add (a + b)
5fee5ec3 57 Vector opBinary(string op : "+")(Vector b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] +
b4c522fa
IB
58b.d[i]; return r; }
59 // addto (a += b)
5fee5ec3 60 Vector opOpAssign(string op : "+")(Vector b) { for (int i=0; i<dim; ++i) d[i] += b.d[i]; return
b4c522fa
IB
61this; }
62 // subtract (a - b)
5fee5ec3 63 Vector opBinary(string op : "-")(Vector b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] -
b4c522fa
IB
64b.d[i]; return r; }
65 // multiply by scalar (a * 2.0)
5fee5ec3 66 Vector opBinary(string op : "*")(tfloat b) { Vector r; for (int i=0; i<dim; ++i) r.d[i] = d[i] *
b4c522fa
IB
67b; return r; }
68 // divide by scalar (a / b)
5fee5ec3 69 Vector opBinary(string op : "/")(tfloat b) { return this * (1/b); }
b4c522fa 70 // dot product (a * b)
5fee5ec3 71 tfloat opBinary(string op : "*")(Vector b) { tfloat r=0; for (int i=0; i<dim; ++i) r += d[i];
b4c522fa
IB
72return r; }
73 // outer product (a ^ b)
74 //Vector opXor(Vector b) { Vector r; for (int i=0; i<dim; ++i) r += d[i]; return r; }
75
76 void print() { for (int i=0; i<dim; ++i) printf("%f ", d[i]);
77printf("\n"); }
78 }
79
80 tfloat abs(Vector v)
81 {
82 return sqrt(sqr(v));
83 }
84
85 tfloat sqr(Vector v)
86 {
87 return v * v;
88 }
89}
90
91alias VecTemplate!(float, 3) VcT;
92
93float[3] up = [ 0, 1, 0 ];
94
95int main(char[][] args)
96{
97 printf("running\n");
98 with (VcT) // try this, I dare ya!! crashes DMD 0.51
99 {
100 VcT.Vector a,b,c;
101 c.set(up);
102 b.set(up);
103 a = b + c;
104 a.print();
105 printf("b * c = %f\n",b * c);
106 printf("abs(a) = %f\n",VcT.abs(a));
107 printf("sqr(a) = %f\n",VcT.sqr(a));
108 }
109 printf("closing\n");
110 return 0;
111}