Skip to content

Instantly share code, notes, and snippets.

@sasq64
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save sasq64/11219714 to your computer and use it in GitHub Desktop.

Select an option

Save sasq64/11219714 to your computer and use it in GitHub Desktop.
template <class T, int SIZE> struct vec {
template <class... S> vec(const S& ... args) : data {args...} {}
bool operator==(const vec &v) const {
for(int i=0; i<SIZE; i++)
if(v[i] != data[i]) return false;
return true;
}
vec operator+(const vec &v) const {
vec r;
for(int i=0; i<SIZE; i++)
r[i] = data[i] + v[i];
return r;
}
const T& operator[](const int &i) const {
return data[i];
}
T& operator[](const int &i) {
return data[i];
}
T dot(const vec &v) const {
T r = 0;
for(int i=0; i<SIZE; i++)
r += (v[i] * data[i]);
return r;
}
T data[SIZE];
T& x = data[0];
T& y = data[1 % SIZE];
T& z = data[2 % SIZE];
T& w = data[3 % SIZE];
};
template <class T, int SIZE> struct mat {
mat() {
for(int i=0; i<SIZE*SIZE; i++)
data[i/SIZE][i%SIZE] = i%(SIZE+1) ? 0.0 : 1.0;
}
vec<T, SIZE> operator*(const vec<T, SIZE> &v) const {
vec<T, SIZE> r;
for(int i=0; i<SIZE; i++)
r[i] = data[i].dot(v);
return r;
}
std::string to_string() {
std::string s;
for(int i=0; i<SIZE; i++) {
for(int j=0; j<SIZE; j++) {
s += std::to_string(data[i][j]);
s += " ";
}
s += "\n";
}
return s;
}
vec<T, SIZE> data[SIZE];
};
template <class T, class... S> vec<T, sizeof...(S)+1> make_vec(const T& a0, const S& ... args) {
return vec<T, sizeof...(S)+1>(a0, args...);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment