File: template2.d

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (111 lines) | stat: -rw-r--r-- 3,721 bytes parent folder | download | duplicates (5)
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
// original post to the D newsgroup:
//    https://www.digitalmars.com/d/archives/10510.html#N10554
// 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 core.math : sqrt;

template VecTemplate(tfloat, int dim:3)
{
 struct Vector
 {
  tfloat[dim] d;

  version(none)
  {
   // sets the vector to the value of the given array
   void set(tfloat[dim] r) { d[] = r[]; }
   // comparison (a == b, a != b)
   bool opEquals(Vector b) { for (int i=0; i<dim; ++i) if (d[i] != b.d[i]) return
false; return true; }
   // negate (-a)
   Vector opUnary(string op : "-")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = -d[i]; return
r;  }
   // complement (~a)
   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 opBinary(string op : "+")(Vector b) { Vector r; r.d[] = d[] + b.d[]; return r;  }
   // addto (a += b)
   Vector opOpAssign(string op : "+")(Vector b) { d[] += b.d[]; return r;  }
   // subtract (a - b)
   Vector opBinary(string op : "-")(Vector b) { Vector r; r.d[] = d[] - b.d[]; return r;  }
   // multiply by scalar (a * 2.0)
   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 opBinary(string op : "/")(tfloat b) { return *this * (1/b);  }
   // dot product (a * b)
   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;  }
  }

  void set(tfloat[dim] r) { for (int i=0; i<dim; ++i) d[i] = r[i]; }
  // comparison (a == b, a != b)
  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 opUnary(string op : "-")() { Vector r; for (int i=0; i<dim; ++i) r.d[i] = -d[i]; return
r;  }
  // complement (~a)
  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 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 opOpAssign(string op : "+")(Vector b) { for (int i=0; i<dim; ++i) d[i] += b.d[i]; return
this; }
  // subtract (a - b)
  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 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 opBinary(string op : "/")(tfloat b) { return this * (1/b);  }
  // dot product (a * b)
  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;  }

  void print() { for (int i=0; i<dim; ++i) printf("%f ", d[i]);
printf("\n"); }
 }

 tfloat abs(Vector v)
 {
  return sqrt(sqr(v));
 }

 tfloat sqr(Vector v)
 {
  return v * v;
 }
}

alias VecTemplate!(float, 3) VcT;

float[3] up = [ 0, 1, 0 ];

int main(char[][] args)
{
 printf("running\n");
 with (VcT)         // try this, I dare ya!!  crashes DMD 0.51
 {
  VcT.Vector a,b,c;
  c.set(up);
  b.set(up);
  a = b + c;
  a.print();
  printf("b * c = %f\n",b * c);
  printf("abs(a) = %f\n",VcT.abs(a));
  printf("sqr(a) = %f\n",VcT.sqr(a));
 }
 printf("closing\n");
 return 0;
}