generic matrix template
This commit is contained in:
parent
a1a0bc1a2f
commit
223c486c92
|
@ -0,0 +1,64 @@
|
|||
template<class T, unsigned ... RestD> struct Matrix;
|
||||
|
||||
template<class T, unsigned FirstDim >
|
||||
struct Matrix<T, FirstDim> {
|
||||
typedef T GenericDimType[FirstDim];
|
||||
|
||||
GenericDimType data;
|
||||
inline T& operator[](unsigned i) { return data[i]; }
|
||||
};
|
||||
|
||||
template<class T, unsigned FirstDim, unsigned ... RestD >
|
||||
struct Matrix<T, FirstDim, RestD...> {
|
||||
typedef typename Matrix<T, RestD...>::GenericDimType FirstDimType;
|
||||
typedef FirstDimType GenericDimType[FirstDim];
|
||||
|
||||
GenericDimType data;
|
||||
inline FirstDimType& operator[](unsigned i) { return data[i]; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
Matrix<int, 2, 3, 4> m;
|
||||
|
||||
int cnt = 0;
|
||||
for (size_t i = 0; i < 2; ++i) {
|
||||
for (size_t j = 0; j < 3; ++j) {
|
||||
for (size_t h = 0; h < 4; ++h) {
|
||||
m[i][j][h] = cnt++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Data in matrix:\n";
|
||||
for (size_t i = 0; i < 2; ++i) {
|
||||
for (size_t j = 0; j < 3; ++j) {
|
||||
for (size_t h = 0; h < 4; ++h) {
|
||||
std::cout << m[i][j][h] << ' ';
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
std::cout << "ENDDIM\n";
|
||||
}
|
||||
|
||||
std::cout << "Addresses in matrix:\n";
|
||||
for (size_t i = 0; i < 2; ++i) {
|
||||
for (size_t j = 0; j < 3; ++j) {
|
||||
for (size_t h = 0; h < 4; ++h) {
|
||||
std::cout << &m[i][j][h] << ' ';
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
std::cout << "ENDDIM\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue