summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Santmyer <jon@jonsantmyer.com>2022-03-27 09:34:35 -0400
committerJon Santmyer <jon@jonsantmyer.com>2022-03-27 09:34:35 -0400
commit8e34a3b5abf194851c1e15a5f2e23e08d23f6ab4 (patch)
treea8333d638ab4dd7dd299cabfc8a70a2eebc43045
parent030526ce29ffcede35a6f641c72cdc0d25866736 (diff)
downloadvex-8e34a3b5abf194851c1e15a5f2e23e08d23f6ab4.tar.gz
vex-8e34a3b5abf194851c1e15a5f2e23e08d23f6ab4.tar.bz2
vex-8e34a3b5abf194851c1e15a5f2e23e08d23f6ab4.zip
Add const operator[]. Fix dot and magnitude
Constexpr everything
-rw-r--r--vex.hpp44
1 files changed, 22 insertions, 22 deletions
diff --git a/vex.hpp b/vex.hpp
index 3abfda8..9ef62dd 100644
--- a/vex.hpp
+++ b/vex.hpp
@@ -19,33 +19,33 @@ struct vec_dimd
{
std::array<T, D> v;
- constexpr vec_dimd() = default;
- template<vectype ...Args>
- constexpr vec_dimd(Args&& ...args) : v{args...} {}
- constexpr vec_dimd(T args[D]) : v(args) {}
- constexpr vec_dimd(T fill) { v.fill(fill); }
- constexpr vec_dimd(const vec_dimd<T, D-1> &vec, T val) { std::copy(vec.v.begin(), vec.v.end(), v); v[D-1] = val; }
+ explicit constexpr vec_dimd() = default;
+ template<typename ...Args>
+ explicit constexpr vec_dimd(Args&&... args) : v{args...} {}
+ explicit constexpr vec_dimd(T args[D]) : v(args) {}
+ explicit constexpr vec_dimd(T fill) { v.fill(fill); }
constexpr T operator[](std::size_t i) { return v[i]; }
+ constexpr T operator[](std::size_t i) const { return v[i]; }
- vec_dimd<T, D> operator+=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] += rhs.v[i]; return *this; }
- vec_dimd<T, D> operator-=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] -= rhs.v[i]; return *this; }
- vec_dimd<T, D> operator*=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] *= rhs.v[i]; return *this; }
- vec_dimd<T, D> operator/=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] /= rhs.v[i]; return *this; }
+ constexpr vec_dimd<T, D> operator+=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] += rhs.v[i]; return *this; }
+ constexpr vec_dimd<T, D> operator-=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] -= rhs.v[i]; return *this; }
+ constexpr vec_dimd<T, D> operator*=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] *= rhs.v[i]; return *this; }
+ constexpr vec_dimd<T, D> operator/=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] /= rhs.v[i]; return *this; }
- vec_dimd<T, D> operator*=(const T &rhs) { for(size_t i = 0; i < D; i++) v[i] *= rhs; return *this; }
- vec_dimd<T, D> operator/=(const T &rhs) { for(size_t i = 0; i < D; i++) v[i] *= rhs; return *this; }
+ constexpr vec_dimd<T, D> operator*=(const T &rhs) { for(size_t i = 0; i < D; i++) v[i] *= rhs; return *this; }
+ constexpr vec_dimd<T, D> operator/=(const T &rhs) { for(size_t i = 0; i < D; i++) v[i] /= rhs; return *this; }
- auto operator<=>(const vec_dimd<T,D> &rhs) const { return magnitude() <=> rhs.magnitude(); }
- bool operator==(const vec_dimd<T,D> &rhs) const = default;
+ constexpr auto operator<=>(const vec_dimd<T,D> &rhs) const { return magnitude() <=> rhs.magnitude(); }
+ constexpr bool operator==(const vec_dimd<T,D> &rhs) const { for(unsigned i = 0; i < D; i++) if(v[i] != rhs.v[i]) return false; return true; }
- friend vec_dimd<T, D> operator+(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs += rhs; return lhs; }
- friend vec_dimd<T, D> operator-(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs -= rhs; return lhs; }
- friend vec_dimd<T, D> operator*(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs *= rhs; return lhs; }
- friend vec_dimd<T, D> operator/(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs /= rhs; return lhs; }
+ constexpr friend vec_dimd<T, D> operator+(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs += rhs; return lhs; }
+ constexpr friend vec_dimd<T, D> operator-(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs -= rhs; return lhs; }
+ constexpr friend vec_dimd<T, D> operator*(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs *= rhs; return lhs; }
+ constexpr friend vec_dimd<T, D> operator/(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs /= rhs; return lhs; }
- friend vec_dimd<T, D> operator*(vec_dimd<T, D> lhs, const T &rhs) { lhs *= rhs; return lhs; }
- friend vec_dimd<T, D> operator/(vec_dimd<T, D> lhs, const T &rhs) { lhs /= rhs; return lhs; }
+ constexpr friend vec_dimd<T, D> operator*(vec_dimd<T, D> lhs, const T &rhs) { lhs *= rhs; return lhs; }
+ constexpr friend vec_dimd<T, D> operator/(vec_dimd<T, D> lhs, const T &rhs) { lhs /= rhs; return lhs; }
friend std::ostream &operator<<(std::ostream &os, const vec_dimd<T,D> obj) {
os << '{';
@@ -54,11 +54,11 @@ struct vec_dimd
}
/*Finds the distance from the origin to the ray cast in D dimension space using components of vec*/
- constexpr T magnitude() const { T t; for(size_t i = 0; i < D; i++) t += v[i] * v[i]; return std::pow(t, 1./D); }
+ constexpr T magnitude() const { T t{}; for(size_t i = 0; i < D; i++) t += v[i] * v[i]; return std::pow(t, 1./D); }
/*Finds the dot product of itself and another vec of same T and D*/
constexpr T dot(const vec_dimd<T, D> &b) const { T t; for(size_t i = 0; i < D; i++) t += (v[i] * b.v[i]); return t; }
- friend constexpr T dot(const vec_dimd<T,D> &lhs, const vec_dimd<T,D> &rhs) { return lhs.dot(rhs); }
+ constexpr friend T dot(const vec_dimd<T,D> &lhs, const vec_dimd<T,D> &rhs) { return lhs.dot(rhs); }
};
template<vectype T>