source: libcfa/src/vec/vec2.hfa@ aca6a54c

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since aca6a54c was 3376ec9, checked in by Dmitry Kobets <dkobets@…>, 6 years ago

make vector an interface, allowing for shared code between dimensions

  • Property mode set to 100644
File size: 2.3 KB
Line 
1#pragma once
2
3#include "vec.hfa"
4
5forall (otype T) {
6 struct vec2 {
7 T x, y;
8 };
9}
10
11
12forall (otype T) {
13 static inline {
14
15 void ?{}(vec2(T)& v, T x, T y) {
16 v.[x, y] = [x, y];
17 }
18
19 forall(| zero_assign(T))
20 void ?{}(vec2(T)& vec, zero_t) with (vec) {
21 x = y = 0;
22 }
23
24 void ?{}(vec2(T)& vec, T val) with (vec) {
25 x = y = val;
26 }
27
28 void ?{}(vec2(T)& vec, vec2(T) other) with (vec) {
29 [x,y] = other.[x,y];
30 }
31
32 // Assignment
33 void ?=?(vec2(T)& vec, vec2(T) other) with (vec) {
34 [x,y] = other.[x,y];
35 }
36 forall(| zero_assign(T))
37 void ?=?(vec2(T)& vec, zero_t) with (vec) {
38 x = y = 0;
39 }
40
41 // Primitive mathematical operations
42
43 // Subtraction
44
45 forall(| subtract(T)) {
46 vec2(T) ?-?(vec2(T) u, vec2(T) v) { // TODO( can't make this const ref )
47 return [u.x - v.x, u.y - v.y];
48 }
49 vec2(T)& ?-=?(vec2(T)& u, vec2(T) v) {
50 u = u - v;
51 return u;
52 }
53 }
54
55 forall(| negate(T)) {
56 vec2(T) -?(vec2(T)& v) with (v) {
57 return [-x, -y];
58 }
59 }
60
61 // Addition
62 forall(| add(T)) {
63 vec2(T) ?+?(vec2(T) u, vec2(T) v) { // TODO( can't make this const ref )
64 return [u.x + v.x, u.y + v.y];
65 }
66 vec2(T)& ?+=?(vec2(T)& u, vec2(T) v) {
67 u = u + v;
68 return u;
69 }
70 }
71
72 // Scalar Multiplication
73 forall(| multiply(T)) {
74 vec2(T) ?*?(vec2(T) v, T scalar) with (v) { // TODO (can't make this const ref)
75 return [x * scalar, y * scalar];
76 }
77 vec2(T) ?*?(T scalar, vec2(T) v) { // TODO (can't make this const ref)
78 return v * scalar;
79 }
80 vec2(T)& ?*=?(vec2(T)& v, T scalar) {
81 v = v * scalar;
82 return v;
83 }
84 }
85
86 // Scalar Division
87 forall(| divide(T)) {
88 vec2(T) ?/?(vec2(T) v, T scalar) with (v) {
89 return [x / scalar, y / scalar];
90 }
91 vec2(T)& ?/=?(vec2(T)& v, T scalar) with (v) {
92 v = v / scalar;
93 return v;
94 }
95 }
96
97 // Relational Operators
98 forall(| equality(T)) {
99 bool ?==?(vec2(T) u, vec2(T) v) with (u) {
100 return x == v.x && y == v.y;
101 }
102 bool ?!=?(vec2(T) u, vec2(T) v) {
103 return !(u == v);
104 }
105 }
106
107 // Geometric functions
108 forall(| add(T) | multiply(T))
109 T dot(vec2(T) u, vec2(T) v) {
110 return u.x * v.x + u.y * v.y;
111 }
112
113 } // static inline
114}
Note: See TracBrowser for help on using the repository browser.