aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdrian Kummerlaender2018-03-21 21:18:38 +0100
committerAdrian Kummerlaender2018-03-21 21:18:38 +0100
commitc6c1f897b3750826f8ded1ea088195703676c4e1 (patch)
tree689d2ce5b562aa61fc223eece146f6016b89ac09 /src
parentd91bdb4f8d5e133060029516dca26d0971213696 (diff)
downloadboltzbub-c6c1f897b3750826f8ded1ea088195703676c4e1.tar
boltzbub-c6c1f897b3750826f8ded1ea088195703676c4e1.tar.gz
boltzbub-c6c1f897b3750826f8ded1ea088195703676c4e1.tar.bz2
boltzbub-c6c1f897b3750826f8ded1ea088195703676c4e1.tar.lz
boltzbub-c6c1f897b3750826f8ded1ea088195703676c4e1.tar.xz
boltzbub-c6c1f897b3750826f8ded1ea088195703676c4e1.tar.zst
boltzbub-c6c1f897b3750826f8ded1ea088195703676c4e1.zip
Reroll some loops and rely on the compiler to unroll them
Diffstat (limited to 'src')
-rw-r--r--src/vector.h45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/vector.h b/src/vector.h
new file mode 100644
index 0000000..55c7663
--- /dev/null
+++ b/src/vector.h
@@ -0,0 +1,45 @@
+#include <cmath>
+
+inline double sq(double x) noexcept {
+ return x * x;
+}
+
+struct Vector {
+ double data[2];
+
+ double comp(int x, int y) const {
+ return x*data[0] + y*data[1];
+ }
+
+ double norm() const {
+ return std::sqrt(sq(data[0]) + sq(data[1]));
+ }
+
+ double& operator[](std::size_t i) {
+ return data[i];
+ }
+
+ double operator[](std::size_t i) const {
+ return data[i];
+ }
+
+ Vector operator*(double scalar) const {
+ return Vector{
+ data[0] * scalar,
+ data[1] * scalar
+ };
+ }
+
+ Vector& operator+=(const Vector& rhs) {
+ data[0] += rhs[0];
+ data[1] += rhs[1];
+ return *this;
+ }
+};
+
+Vector operator*(double scalar, const Vector& vec) {
+ return Vector{
+ vec[0] * scalar,
+ vec[1] * scalar
+ };
+}